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.baobiao.UtilEntity;
import com.liang.service.PrjAnnualbudgetService;
import com.liang.service.StaStatisticalSummaryService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 外委合同支付(staOutPayController)表控制层
 */
@Controller
@RequestMapping("/staStatisticalSummary")
public class StaStatisticalSummaryController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private StaStatisticalSummaryService staStatisticalSummaryService;
    @Resource
    private PrjAnnualbudgetService prjAnnualbudgetService;

    //项目报表（外委汇总表）页面
    @RequestMapping(value = "/outPay", method = RequestMethod.GET)
    public String outPay(Model model) {
        List<String> yearList = prjAnnualbudgetService.getYearList();
        model.addAttribute("yearList", yearList);
        return "statistical/outPay";
    }

    //外委汇总表全部及条件查询
    @ResponseBody
    @RequestMapping(value = "/outPayData", method = RequestMethod.POST)
    public BaseResult outPayData(
            @RequestParam("page")Integer pageNum,
            @RequestParam("limit") Integer pageSize,
            @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 = "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("qsrq",qsrq);
        map.put("jsrq",jsrq);
        map.put("erpbh",erpbh);
        map.put("nf",nf);
        map.put("bh",fysqbh);
        List<HashMap<String, Object>> staFeasibilityreport = staStatisticalSummaryService.outPaySummary(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);
    }


    //项目报表（项目支出汇总表）页面
    @RequestMapping("/projectPay")
    public String projectPay(Model model) {
        List<String> yearList = prjAnnualbudgetService.getYearList();
        model.addAttribute("yearList", yearList);

        return "statistical/projectPay";
    }

    //项目支出汇总表全部及条件查询
    @ResponseBody
    @RequestMapping(value = "/projectPayData", method = RequestMethod.POST)
    public BaseResult projectPayData(
            @RequestParam("page") Integer pageNum,
            @RequestParam("limit") Integer pageSize,
            @RequestParam(required = false, defaultValue = "", value = "XMMC") String xmmc,
            @RequestParam(required = false, defaultValue = "", value = "ERPBH") String erpbh,
            @RequestParam(required = false, defaultValue = "", value = "FYSQBH") String fysqbh,
            @RequestParam(required = false, defaultValue = "", value = "NF")String nf,
            @RequestParam(required = false, defaultValue = "", value = "QSRQ")String qsrq,
            @RequestParam(required = false, defaultValue = "", value = "JSRQ") String jsrq
    ) {
        Map<String, String> map = new HashMap<>();
        map.put("xmmc",xmmc);
        map.put("erpbh",erpbh);
        map.put("nf",nf);
        map.put("qsrq",qsrq);
        map.put("jsrq",jsrq);
        PageHelper.startPage(pageNum, pageSize);
        List<HashMap<String, Object>> staFeasibilityreport = staStatisticalSummaryService.projectPay(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/{formDate}",method = RequestMethod.POST)
    public BaseResult getAll(@PathVariable(required = false)String formDate)throws Exception{

        Map<String, String> map = new HashMap<>();

        ObjectMapper objectMapper = new ObjectMapper();
        UtilEntity util = objectMapper.readValue(formDate, UtilEntity.class);
        map.put("xmmc",util.getXmmc());
        map.put("erpbh",util.getErpbh());
        map.put("bh",util.getFysqbh());
        map.put("nf",util.getNf());
        map.put("qsrq",util.getQsrq());
        map.put("jsrq",util.getJsrq());

        List<HashMap<String, Object>> staFeasibilityreport = staStatisticalSummaryService.projectPay(map);
        return BaseResult.success(staFeasibilityreport);
    }

}
