package com.bonus.hn.bearing.capacity.backstage.newpro.controller;

import com.bonus.hn.bearing.capacity.backstage.manager.annotation.LogAnnotation;
import com.bonus.hn.bearing.capacity.backstage.manager.table.PageTableRequest;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.*;
import com.bonus.hn.bearing.capacity.backstage.newpro.entity.HistoryProBean;
import com.bonus.hn.bearing.capacity.backstage.newpro.entity.NewProBean;
import com.bonus.hn.bearing.capacity.backstage.newpro.entity.ReleaseBean;
import com.bonus.hn.bearing.capacity.backstage.newpro.entity.ReleaseViewBean;
import com.bonus.hn.bearing.capacity.backstage.newpro.service.NewProService;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xliu
 * @date 2023/11/6 14:46 新工程预安排
 */
@RestController
@RequestMapping("/newPro")
public class NewProController {

    @Resource
    private NewProService service;

    @LogAnnotation
    @PostMapping("getBasicDataList")
    @ApiOperation(value = "新工程预安排-基础列表查询")
    public R getBasicDataList(NewProBean o) {
        List<NewProBean> list = service.getBasicDataList(o);
        return list.size() > 0 ? R.okTable(list, list.size()) : R.failTable("暂无数据");
    }

    @LogAnnotation
    @PostMapping("getHistoryProList")
    @ApiOperation(value = "历史工程-列表查询")
    public R getHistoryProList(HistoryProBean o) {
        List<HistoryProBean> list = service.getHistoryProList(o);
        return list.size() > 0 ? R.okTable(list, list.size()) : R.failTable("暂无数据");
    }

    @LogAnnotation
    @PostMapping("getReleaseDataList")
    @ApiOperation(value = "预释放资源查询")
    public R getReleaseDataList(ReleaseBean o) {
        List<ReleaseBean> list = service.getReleaseDataList(o);
        return list.size() > 0 ? R.okTable(list, list.size()) : R.failTable("暂无数据");
    }

    @LogAnnotation
    @PostMapping("getTeamTable")
    @ApiOperation(value = "预释放资源查询-根据proId查询项目管理资源")
    public R getTeamTable(ReleaseViewBean o) {
        List<ReleaseViewBean> list = service.getTeamTable(o);
        return list.size() > 0 ? R.okTable(list, list.size()) : R.failTable("暂无数据");
    }

    @LogAnnotation
    @PostMapping("getSubTeamTable")
    @ApiOperation(value = "预释放资源查询-根据proId查询分包班组")
    public R getSubTeamTable(ReleaseViewBean o) {
        List<ReleaseViewBean> list = service.getSubTeamTable(o);
        return list.size() > 0 ? R.okTable(list, list.size()) : R.failTable("暂无数据");
    }

    @LogAnnotation
    @PostMapping("getOwnTeamTable")
    @ApiOperation(value = "预释放资源查询-根据proId查询自有班组")
    public R getOwnTeamTable(ReleaseViewBean o) {
        List<ReleaseViewBean> list = service.getOwnTeamTable(o);
        return list.size() > 0 ? R.okTable(list, list.size()) : R.failTable("暂无数据");
    }

    @RequestMapping(value = "addInfo", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('sysBasicData:add')")
    @ApiOperation(value = "新工程预安排-新增")
    public AjaxRes addInfo(@RequestBody NewProBean bean) {
        AjaxRes ar = new AjaxRes();
        System.out.println(bean);
        int cb = service.addInfo(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if (cb > 0) {
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3) {
                ar.setFailMsg("工程名称已存在");
            }
        }
        return ar;
    }

    @RequestMapping(value = "updateInfo", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('sysBasicData:update')")
    @ApiOperation(value = "新工程预安排-修改")
    public AjaxRes updateInfo(@RequestBody NewProBean bean) {
        AjaxRes ar = new AjaxRes();
        int cb = service.updateInfo(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if (cb > 0) {
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3) {
                ar.setFailMsg("工程名称已存在");
            }
        }
        return ar;
    }

    @RequestMapping(value = "getListById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新工程预安排-根据id查询详情")
    public AjaxRes getListById(NewProBean o) {
        AjaxRes ar = new AjaxRes();
        NewProBean newProBean = service.getListById(o);
        Map<String, Object> map = new HashMap<>(100);
        if (newProBean != null) {
            map.put("newProBean", newProBean);
        }
        ar.setSucceed(map, GlobalConst.DATA_SUCCEED);
        return ar;
    }

    @RequestMapping(value = "getHistoryListById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "历史工程-根据id查询详情")
    public AjaxRes getHistoryListById(NewProBean o) throws ParseException {
        AjaxRes ar = new AjaxRes();
        NewProBean newProBean = service.getHistoryListById(o);
        Map<String, Object> map = new HashMap<>(100);
        if (newProBean != null) {
            map.put("newProBean", newProBean);
        }
        ar.setSucceed(map, GlobalConst.DATA_SUCCEED);
        return ar;
    }

    @RequestMapping(value = "getReleaseDataListById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新工程预安排-根据id查询详情")
    public AjaxRes getReleaseDataListById(ReleaseBean o) {
        AjaxRes ar = new AjaxRes();
        List<ReleaseBean> list = service.getReleaseDataList(o);
        Map<String, Object> map = new HashMap<>(100);
        if (list != null && list.size() > 0) {
            map.put("info", list.get(0));
        }
        ar.setSucceed(map, GlobalConst.DATA_SUCCEED);
        return ar;
    }

    @LogAnnotation
    @PostMapping("delById")
    @PreAuthorize("hasAuthority('sysBasicData:del')")
    @ApiOperation(value = "新工程预安排-删除功能")
    public AjaxRes delById(NewProBean bean) {
        AjaxRes ar = new AjaxRes();
        int result = service.delById(bean);
        if (result == 1) {
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        } else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }


    @LogAnnotation
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    @ApiOperation(value = "新工程预安排-导出")
    public AjaxRes export(NewProBean bean, HttpServletResponse response, HttpServletRequest request) throws JsonProcessingException {
        AjaxRes ar = new AjaxRes();
        int result = service.export(bean, response, request);
        if (result == 1) {
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        } else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @LogAnnotation
    @RequestMapping(value = "/exportHistoryPro", method = RequestMethod.GET)
    @ApiOperation(value = "历史工程-导出")
    public AjaxRes exportHistoryPro(HistoryProBean bean, HttpServletResponse response, HttpServletRequest request) throws JsonProcessingException {
        AjaxRes ar = new AjaxRes();
        int result = service.exportHistoryPro(bean, response, request);
        if (result == 1) {
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        } else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @RequestMapping(value = "/exportPreReleaseResource", method = RequestMethod.GET)
    @ApiOperation(value = "预释放资源-导出")
    public void exportPreReleaseResource(HttpServletRequest request, HttpServletResponse response, PageTableRequest page) throws ParseException {
        ReleaseBean b = new ReleaseBean();
        b.setProName(request.getParameter("proName"));
        b.setReleaseData(request.getParameter("releaseData"));
        List<ReleaseBean> list = service.getReleaseDataList(b);
        List<Map<String, Object>> mapList = new ArrayList<>();
        String[] excelHeader = {"序号", "分公司", "工程名称", "计划完成时间", "管理团队数量", "分包班组数量", "基础班组数量", "组塔班组数量", "架线班组数量"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2};
        String fileName = "预释放工程清单" + DateTimeHelper.getNowDateToSecond();
        if (list != null) {
            int i = 0;
            for (ReleaseBean bean : list) {
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号", i);
                map.put("分公司", bean.getOrgName());
                map.put("工程名称", bean.getProName());
                map.put("计划完成时间", bean.getEndTime());
                map.put("管理团队数量", bean.getTeamNum());
                map.put("分包班组数量", bean.getSubTeamNum());
                map.put("基础班组数量", bean.getBasicNum());
                map.put("组塔班组数量", bean.getGroupTowerNum());
                map.put("架线班组数量", bean.getLineNum());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/exportProTeamPersonData", method = RequestMethod.GET)
    @ApiOperation(value = "预释放资源-项目管理资源人员-导出")
    public void exportProTeamPersonData(HttpServletRequest request, HttpServletResponse response, PageTableRequest page) throws ParseException {
        ReleaseViewBean b = new ReleaseViewBean();
        b.setProId(request.getParameter("proId"));
        List<ReleaseViewBean> list = service.getTeamTable(b);
        List<Map<String, Object>> mapList = new ArrayList<>();
        String[] excelHeader = {"序号", "分公司", "项目管理团队", "电压等级", "岗位", "姓名", "性别", "身份证", "执证情况","职称/技能等级"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2,2};
        String fileName = "项目管理资源人员清单" + DateTimeHelper.getNowDateToSecond();
        if (list != null) {
            int i = 0;
            for (ReleaseViewBean bean : list) {
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号", i);
                map.put("分公司", bean.getOrgName());
                map.put("项目管理团队", bean.getTeamName());
                map.put("电压等级", bean.getVoltageLevel());
                map.put("岗位", bean.getPostName());
                map.put("姓名", bean.getPersonName());
                map.put("性别", bean.getSex());
                map.put("身份证", bean.getIdCard());
                map.put("执证情况", bean.getCertification());
                map.put("职称/技能等级", bean.getSkillLevel());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/exportSubTeamPersonData", method = RequestMethod.GET)
    @ApiOperation(value = "预释放资源-分包班组人员-导出")
    public void exportSubTeamPersonData(HttpServletRequest request, HttpServletResponse response, PageTableRequest page) throws ParseException {
        ReleaseViewBean b = new ReleaseViewBean();
        b.setProId(request.getParameter("proId"));
        List<ReleaseViewBean> list = service.getSubTeamTable(b);
        List<Map<String, Object>> mapList = new ArrayList<>();
        String[] excelHeader = {"序号", "分包单位名称", "工序", "班组", "姓名", "岗位", "性别", "民族", "身份证","职称/技能等级"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2,2};
        String fileName = "分包班组人员清单" + DateTimeHelper.getNowDateToSecond();
        if (list != null) {
            int i = 0;
            for (ReleaseViewBean bean : list) {
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号", i);
                map.put("分包单位名称", bean.getSubName());
                map.put("工序", bean.getStructure());
                map.put("班组", bean.getTeamName());
                map.put("姓名", bean.getPersonName());
                map.put("岗位", bean.getPostName());
                map.put("性别", bean.getSex());
                map.put("民族", bean.getNation());
                map.put("身份证", bean.getIdCard());
                map.put("职称/技能等级", bean.getSkillLevel());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping(value = "/exportOwnTeamPersonData", method = RequestMethod.GET)
    @ApiOperation(value = "预释放资源-自有班组人员-导出")
    public void exportOwnTeamPersonData(HttpServletRequest request, HttpServletResponse response, PageTableRequest page) throws ParseException {
        ReleaseViewBean b = new ReleaseViewBean();
        b.setProId(request.getParameter("proId"));
        List<ReleaseViewBean> list = service.getOwnTeamTable(b);
        List<Map<String, Object>> mapList = new ArrayList<>();
        String[] excelHeader = {"序号", "工序", "班组", "岗位", "姓名", "性别", "民族", "身份证", "执证情况","职称/技能等级"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2,2};
        String fileName = "自有班组人员清单" + DateTimeHelper.getNowDateToSecond();
        if (list != null) {
            int i = 0;
            for (ReleaseViewBean bean : list) {
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号", i);
                map.put("工序", bean.getStructure());
                map.put("班组", bean.getTeamName());
                map.put("岗位", bean.getPostName());
                map.put("姓名", bean.getPersonName());
                map.put("性别", bean.getSex());
                map.put("民族", bean.getNation());
                map.put("身份证", bean.getIdCard());
                map.put("执证情况", bean.getCertification());
                map.put("职称/技能等级", bean.getSkillLevel());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
