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.StaIndirectCosts;
import com.liang.entity.baobiao.UtilEntity;
import com.liang.service.PrjAnnualbudgetService;
import com.liang.service.StaIndirectCostsService;
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;

/**
 * 间接费(StaIndirectCosts)表控制层
 */
@Controller
@RequestMapping("staIndirectCosts")
public class StaIndirectCostsController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private StaIndirectCostsService staIndirectCostsService;

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

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

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

    /**
     * 初始化间接费列表
     *
     * @param zb
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{zb}", method = RequestMethod.POST)
    public BaseResult list(@PathVariable Integer zb) {
        List<StaIndirectCosts> list = staIndirectCostsService.getJjfList(zb);

        PageInfo<StaIndirectCosts> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<StaIndirectCosts> 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 "staindirectcosts/add";
    }

    /**
     * 批量保存间接费
     *
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchInsert/{zb}", method = RequestMethod.POST)
    public BaseResult batchInsert(@RequestBody String data, @PathVariable Integer zb) {
        List<StaIndirectCosts> expenseList = JsonTool.parseArray(data, StaIndirectCosts.class);
        for (int i = 0; i < expenseList.size(); i++) {
            expenseList.get(i).setZb(zb);
        }
        if (expenseList.size() > 0) {
            //删除间接费
            staIndirectCostsService.deleteByZb(zb);
            int num = staIndirectCostsService.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 "staindirectcosts/update";
    }

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

    @Resource
    private PrjAnnualbudgetService prjAnnualbudgetService;
    //项目报表（间接费用）页面
    @RequestMapping("/search")
    public String search(Model model) {
        List<String> yearList = prjAnnualbudgetService.getYearList();
        model.addAttribute("yearList", yearList);

        return "statistical/indirectCosts";
    }

    //间接费用全部及条件查询
    @ResponseBody
    @RequestMapping(value = "/searchData", method = RequestMethod.POST)
    public BaseResult searchData(
            @RequestParam(required = false, defaultValue = "", value = "XMMC") String xmmc,
            @RequestParam(required = false, defaultValue = "", value = "QSRQ") String qsrq,
            @RequestParam(required = false, defaultValue = "", value = "JSRQ") String jsrq,
            @RequestParam(required = false, defaultValue = "", value = "FYSQBH") String fysqbh,
            @RequestParam(required = false, defaultValue = "", value = "NF")String nf,
            @RequestParam(required = false, defaultValue = "", value = "ERPBH")String erpbh,
            @RequestParam("page")Integer pageNum,
            @RequestParam("limit")Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Map<String, String> map = new HashMap<>();
        map.put("xmmc",xmmc);
        map.put("qsrq",qsrq);
        map.put("jsrq",jsrq);
        map.put("fysqbh",fysqbh);
        map.put("nf",nf);
        map.put("erpbh",erpbh);
        List<HashMap<String, Object>> staFeasibilityreport = staIndirectCostsService.searchIndirectCost(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/{fromDate}",method = RequestMethod.POST)
    public BaseResult getAll(@PathVariable(required = false) String fromDate)throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
       UtilEntity util =  objectMapper.readValue(fromDate, UtilEntity.class);

        Map<String, String> map = new HashMap<>();
        map.put("xmmc",util.getXmmc());
        map.put("qsrq",util.getQsrq());
        map.put("jsrq",util.getJsrq());
        map.put("fysqbh",util.getFysqbh());
        map.put("nf",util.getNf());
        map.put("erpbh",util.getErpbh());
        List<HashMap<String, Object>> staFeasibilityreport = staIndirectCostsService.searchIndirectCost(map);
       return BaseResult.success(staFeasibilityreport);
    }

}
