package com.liang.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.common.JsonTool;
import com.liang.common.base.BaseController;
import com.liang.common.base.BaseResult;
import com.liang.entity.StaMaterial;
import com.liang.entity.baobiao.UtilEntity;
import com.liang.service.PrjAnnualbudgetService;
import com.liang.service.StaMaterialService;
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;

/**
 * 材料费(StaMaterial)表控制层
 */
@Controller
@RequestMapping("staMaterial")
public class StaMaterialController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private StaMaterialService staMaterialService;

    @Resource
    private PrjAnnualbudgetService prjAnnualbudgetService;

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

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

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

    /**
     * 初始化材料费列表
     *
     * @param zb
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{zb}", method = RequestMethod.POST)
    public BaseResult list(@PathVariable Integer zb) {
        List<StaMaterial> list = staMaterialService.getList(zb);

        PageInfo<StaMaterial> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<StaMaterial> rows = pageinfo.getList();
        int total = (int) pageinfo.getTotal();
        Map<String, Object> result = new HashMap<>();
        result.put(RESULT_ROWS, rows);
        result.put(RESULT_TOTAL, total);

        return BaseResult.success(result);
    }

    /**
     * 新建数据
     *
     * @return
     */
    @RequestMapping(value = "/forAdd", method = RequestMethod.GET)
    public String forAdd(Model model) {
        return "stamaterial/add";
    }

    /**
     * 批量保存材料费
     *
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchInsert/{zb}", method = RequestMethod.POST)
    public BaseResult batchInsert(@RequestBody String data, @PathVariable Integer zb) {
        //删除材料费
        staMaterialService.deleteByZb(zb);

        List<StaMaterial> expenseList = JsonTool.parseArray(data, StaMaterial.class);
        for (int i = 0; i < expenseList.size(); i++) {
            expenseList.get(i).setZb(zb);
        }
        if (expenseList.size() > 0) {
            int num = staMaterialService.insertBatch(expenseList);
            if (num == expenseList.size()) {
                return BaseResult.success("材料费保存成功！");
            } else {
                return BaseResult.failure("材料费保存失败！");
            }
        } else {
            return BaseResult.success("没有要保存的信息");
        }
    }

    /**
     * 编辑数据
     *
     * @return
     */
    @RequestMapping(value = "/forUpdate", method = RequestMethod.GET)
    public String forUpdate() {
        return "stamaterial/update";
    }

    /**
     * 查看数据
     *
     * @return
     */
    @RequestMapping(value = "/forView", method = RequestMethod.GET)
    public String forView() {
        return "stamaterial/view";
    }

    //项目报表（材料费用）页面
    @RequestMapping("/search")
    public String search(Model model) {
        List<String> yearList = prjAnnualbudgetService.getYearList();
        model.addAttribute("yearList", yearList);
        return "statistical/material";
    }

    //材料费用全部及条件查询
    @ResponseBody
    @RequestMapping(value = "/searchData", method = RequestMethod.POST)
    public BaseResult searchData(
            @RequestParam("limit")Integer pageSize,
            @RequestParam("page")Integer pageNum,
            @RequestParam(required = false, defaultValue = "", value = "XMMC") String xmmc,
                                 @RequestParam(required = false, defaultValue = "", value = "CLMC") String clmc,
                                 @RequestParam(required = false, defaultValue = "", value = "WLBM") String wlbm,
                                 @RequestParam(required = false, defaultValue = "", value = "BZ") String bz,
                                 @RequestParam(required = false, defaultValue = "", value = "QSRQ") String qsrq,
                                 @RequestParam(required = false, defaultValue = "", value = "JSRQ") String jsrq,
            @RequestParam(required = false,defaultValue = "",value = "ERPBH") String erpbh,
            @RequestParam(required = false,defaultValue = "",value = "FYSQBH")String fysqbh,
            @RequestParam(required = false,defaultValue = "",value = "NF")String nf
                                 ) {
        PageHelper.startPage(pageNum,pageSize);
        Map<String, String> map = new HashMap<>();
        map.put("xmmc",xmmc);
        map.put("clmc",clmc);
        map.put("wlbm",wlbm);
        map.put("bz",bz);
        map.put("qsrq",qsrq);
        map.put("jsrq",jsrq);
        map.put("erpbh",erpbh);
        map.put("fysqbh",fysqbh);
        map.put("nf",nf);
        List<HashMap<String, Object>> staFeasibilityreport = staMaterialService.searchMaterialCost(map);
        PageInfo<HashMap<String, Object>> pageinfo = new PageInfo<>(staFeasibilityreport);
        List<HashMap<String, Object>> rows = pageinfo.getList();
        int total = (int) pageinfo.getTotal();
        Map<String, Object> result = new HashMap<>();
        result.put(RESULT_ROWS, rows);
        result.put(RESULT_TOTAL, total);
        return BaseResult.success(result);
    }

    @ResponseBody
    @RequestMapping(value = "/getAll/{fromdata}",method = RequestMethod.POST)
    public BaseResult getAll(@PathVariable(required = false) String fromdata) throws  Exception{
        Map<String, String> map = new HashMap<>();
        if (fromdata == null){
            List<HashMap<String, Object>> staFeasibilityreport = staMaterialService.searchMaterialCost(map);
            return BaseResult.success("导出成功",staFeasibilityreport);
        }
        ObjectMapper objectMapper = new ObjectMapper();
        UtilEntity formDatas = objectMapper.readValue(fromdata, UtilEntity.class);
        map.put("xmmc",formDatas.getXmmc());
        map.put("clmc",formDatas.getClmc());
        map.put("wlbm",formDatas.getWlbm());
        map.put("bz",formDatas.getBz());
        map.put("qsrq",formDatas.getQsrq());
        map.put("jsrq",formDatas.getJsrq());
        map.put("erpbh",formDatas.getErpbh());
        map.put("fysqbh",formDatas.getFysqbh());
        map.put("nf",formDatas.getNf());
        List<HashMap<String, Object>> staFeasibilityreport = staMaterialService.searchMaterialCost(map);

        return BaseResult.success("导出成功",staFeasibilityreport);

    }
}
