package com.liang.controller;

import com.liang.common.JsonTool;
import com.liang.common.base.BaseResult;
import com.liang.common.enums.ProjectTypeEnnum;
import com.liang.dao.PrjAnnualbudgetDao;
import com.liang.entity.*;
import com.liang.service.PrjAnnualbudgetdetailService;
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.web.bind.annotation.*;

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

/**
 * 年度预算明细(PrjAnnualbudgetdetail)表控制层
 *
 * @author makejava
 * @since 2023-05-19 21:59:11
 */
@Controller
@RequestMapping("prjAnnualbudgetdetail")
public class PrjAnnualbudgetdetailController {
    /**
     * 服务对象
     */
    @Resource
    private PrjAnnualbudgetdetailService prjAnnualbudgetdetailService;

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

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

    /**
     * 新增数据
     *
     * @param prjAnnualbudgetdetail 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<PrjAnnualbudgetdetail> add(PrjAnnualbudgetdetail prjAnnualbudgetdetail) {
        return ResponseEntity.ok(this.prjAnnualbudgetdetailService.insert(prjAnnualbudgetdetail));
    }

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

    @Resource
    private PrjAnnualbudgetDao prjAnnualbudgetDao;

    /**
     * 获取年度预算列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{nys}", method = RequestMethod.POST)
    public Object list(@PathVariable Integer nys) {
        List<PrjAnnualbudgetdetail> list = prjAnnualbudgetdetailService.getBudgetList(nys);
        PrjAnnualbudget prjAnnualbudget = prjAnnualbudgetDao.queryById(nys);
        //国网项目不展示备用金
        if (ProjectTypeEnnum.GWGSKJXM.getCode().equals(prjAnnualbudget.getXmlx()) ||
                ProjectTypeEnnum.GWGSKJXM_2018.getCode().equals(prjAnnualbudget.getXmlx())) {
            list.forEach(item -> item.setByj(null));
        }
        String strData = JsonTool.toJsonString(list);
        return strData;
    }

    /**
     * 编辑数据（我的年度预算：年度预算）
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doUpdate", method = RequestMethod.POST)
    public BaseResult doUpdate(@RequestBody String data) {
        List<PrjAnnualbudgetdetail> budgetDetails = JsonTool.parseArray(data, PrjAnnualbudgetdetail.class);
        int num = prjAnnualbudgetdetailService.updateBatch(budgetDetails);
        if (num > 0) {
            return BaseResult.success("保存成功!");
        } else {
            return BaseResult.failure("保存失败!");
        }
    }

    /**
     * 获取年度预算列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/yslist/{nys}", method = RequestMethod.POST)
    public Object yslist(@PathVariable Integer nys) {
        List<PrjAnnualbudgetdetail> list = prjAnnualbudgetdetailService.getBudgetList(nys);

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

        return resultMap;
    }
}

