package com.liang.controller;

import com.liang.common.JsonTool;
import com.liang.entity.PrjBudget;
import com.liang.service.PrjBudgetService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目预算(PrjBudget)表控制层
 *
 * @author makejava
 * @since 2023-03-10 07:49:56
 */
@Controller
@RequestMapping("prjBudget")
public class PrjBudgetController {
    /**
     * 服务对象
     */
    @Resource
    private PrjBudgetService prjBudgetService;

    /**
     * 分页查询
     *
     * @param prjBudget   筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<PrjBudget>> queryByPage(PrjBudget prjBudget, PageRequest pageRequest) {
        return ResponseEntity.ok(this.prjBudgetService.queryByPage(prjBudget, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<PrjBudget> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.prjBudgetService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @RequestMapping(value = "/forAdd/{xm}", method = RequestMethod.GET)
    public String forAdd(Model model, @PathVariable Integer xm) {
        model.addAttribute("xm", xm);
        return "prjBudget/add";
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.prjBudgetService.deleteById(id));
    }

    /**
     * 获取项目预算列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{xm}", method = RequestMethod.POST)
    public Object list(@PathVariable Integer xm) {
        List<PrjBudget> list = prjBudgetService.getBudgetList(xm);
        String strData = JsonTool.toJsonString(list);
        return strData;
    }
}

