package com.liang.controller;

import com.liang.common.JsonTool;
import com.liang.common.base.BaseResult;
import com.liang.entity.*;
import com.liang.service.BaseProjectbudgetService;
import com.liang.service.BaseProjecttemplateService;
import com.liang.service.PrjProjectService;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模板项目预算(BaseProjectbudget)表控制层
 *
 * @author makejava
 * @since 2023-03-14 21:25:05
 */
@Controller
@RequestMapping("baseProjectbudget")
public class BaseProjectbudgetController {
    /**
     * 服务对象
     */
    @Resource
    private BaseProjectbudgetService baseProjectbudgetService;
    @Resource
    private BaseProjecttemplateService projecttemplateService;
    @Resource
    private PrjProjectService projectService;

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

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

    /**
     * 获取项目预算列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{xm}", method = RequestMethod.GET)
    public Object list(@PathVariable Integer xm) {
        List<BaseProjectbudget> list = baseProjectbudgetService.getBudgetList(xm);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("count", list.size());
        resultMap.put("data", list);

        return resultMap;
    }

    /**
     * 获取项目预算列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/yslist/{xm}", method = RequestMethod.POST)
    public Object yslist(@PathVariable Integer xm) {
        List<BaseProjectbudget> list = baseProjectbudgetService.getPrjBudgetList(xm);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("count", list.size());
        resultMap.put("data", list);

        return resultMap;
    }

    /**
     * 新建数据
     *
     * @return
     */
    @RequestMapping(value = "/forAdd1/{mbId}", method = RequestMethod.GET)
    public String forAdd1(Model model, @PathVariable Integer mbId) {
        BaseProjecttemplate template = projecttemplateService.queryById(mbId);
        BaseProjectbudget budget = new BaseProjectbudget();
        budget.setXm(template.getId());
        budget.setXmmc(template.getZwmc());

        model.addAttribute("budget", budget);
        return "baseprojectbudget/add";
    }

    @RequestMapping(value = "/forAdd2/{ysId}", method = RequestMethod.GET)
    public String forAdd2(Model model, @PathVariable Integer ysId) {
        BaseProjectbudget budget = baseProjectbudgetService.queryById(ysId);
        model.addAttribute("budget", budget);
        return "baseprojectbudget/add";
    }

    /**
     * 保存数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    public BaseResult doAdd(BaseProjectbudget projectbudget) {
        int num = baseProjectbudgetService.insert(projectbudget);
        if (num > 0) {
            return BaseResult.success("保存成功!");
        } else if (num == -1) {
            return BaseResult.failure("该编码已存在！");
        } else {
            return BaseResult.failure("保存失败!");
        }
    }

    /**
     * 编辑数据
     *
     * @return
     */
    @RequestMapping(value = "/forUpdate/{id}", method = RequestMethod.GET)
    public String forUpdate(Model model, @PathVariable Integer id) {
        BaseProjectbudget budget = baseProjectbudgetService.queryById(id);
        model.addAttribute("budget", budget);
        return "baseprojectbudget/update";
    }

    /**
     * 更新数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doUpdate", method = RequestMethod.POST)
    public BaseResult doUpdate(BaseProjectbudget projectbudget) {
        int num = baseProjectbudgetService.update(projectbudget);
        if (num > 0) {
            return BaseResult.success("保存成功!");
        } else if (num == -1) {
            return BaseResult.failure("该编码已存在！");
        } else {
            return BaseResult.failure("保存失败!");
        }
    }

    /**
     * 浏览数据
     *
     * @return
     */
    @RequestMapping(value = "/forView/{id}", method = RequestMethod.GET)
    public String forView(Model model, @PathVariable Integer id) {
        BaseProjectbudget budget = baseProjectbudgetService.queryById(id);
        model.addAttribute("budget", budget);
        return "baseprojectbudget/view";
    }

    /**
     * 删除数据
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doDelete", method = RequestMethod.POST)
    public BaseResult doDelete(@RequestBody String strData) {
        BaseProjectbudget projectbudget = JsonTool.parseObject(strData, BaseProjectbudget.class);
        int num = baseProjectbudgetService.delete(projectbudget);
        if (num > 0) {
            return BaseResult.success("删除成功!");
        } else if (num == -1) {
            return BaseResult.failure("该节点下存在子节点，不能直接删除!");
        } else {
            return BaseResult.failure("删除失败!");
        }
    }
}

