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

import com.bonus.hn.bearing.capacity.backstage.companypro.entity.ProjectManagerBean;
import com.bonus.hn.bearing.capacity.backstage.companypro.entity.TreeBean;
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.AjaxRes;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.ExcelUtils;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.GlobalConst;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.R;
import com.bonus.hn.bearing.capacity.backstage.subteam.entity.SubTeamBean;
import com.bonus.hn.bearing.capacity.backstage.subteam.service.SubTeamService;
import io.swagger.annotations.Api;
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 15:25 分包班组管理
 */
@Api(tags = "分包班组管理")
@RestController
@RequestMapping("/subTeam")
public class SubTeamController {

    @Resource
    private SubTeamService service;

    @LogAnnotation
    @PostMapping("getSubTeamsList")
    @ApiOperation(value = "分包班组管理-列表查询")
    public R getSubTeamsList(SubTeamBean o){
        List<SubTeamBean> list = service.getSubTeamsList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }
    /**
     * 分包班组管理导出
     * @param request
     * @param response
     * @param page
     */
    @RequestMapping(value = "/exportPhysicalCheckResults", method = RequestMethod.GET)
    @ApiOperation(value = "分包班组管理-导出")
    public void exportPayrolls(HttpServletRequest request, HttpServletResponse response, PageTableRequest page) throws ParseException {
        SubTeamBean b = new SubTeamBean();
        b.setKeyWord(request.getParameter("keyword"));
        b.setStatus(request.getParameter("status"));
        b.setSubTeamTypeId(request.getParameter("subTeamTypeId"));
        List<SubTeamBean> list = service.getSubTeamsList(b);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] excelHeader = {"序号","分包商","工序","班组名称","班组骨干人数","状态"
                ,"所属项目","创建时间"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2};
        String fileName = "分包班组资源统计";
        if (list != null) {
            int i = 0;
            for (SubTeamBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号",i);
                map.put("分包商",bean.getBmSubcontractorName());
                map.put("工序",bean.getSubTeamType());
                map.put("班组名称",bean.getTeamName());
                map.put("班组骨干人数",bean.getTeamNum());
                String proStatus = "0".equals(bean.getStatus()) ? "闲置中" : "已投入";
                map.put("状态", proStatus);
                map.put("所属项目",bean.getProName());
                map.put("创建时间",bean.getCreateTime());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @LogAnnotation
    @PostMapping("getSubPersonList")
    @ApiOperation(value = "分包班组管理-获取人员表单")
    public R getSubPersonList(SubTeamBean o){
        List<SubTeamBean> list = service.getSubPersonList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    @LogAnnotation
    @PostMapping("getSubTeamsFormList")
    @ApiOperation(value = "分包班组管理-表单列表查询")
    public  List<SubTeamBean> getSubTeamsFormList(SubTeamBean o){
        List<SubTeamBean> list = service.getSubTeamsFormList(o);
        return list;
    }

    @RequestMapping(value = "getSubTeamNum", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分包班组管理-表单查询班组各数量")
    public AjaxRes getSubTeamNum(SubTeamBean o){
        AjaxRes ar = new AjaxRes();
        List<SubTeamBean> SubTeamBean = service.getSubTeamNum(o);
        Map<String, Object> map = new HashMap<>(100);
        if(SubTeamBean !=null){
            map.put("SubTeamBean",SubTeamBean);
        }
        ar.setSucceed(map, GlobalConst.DATA_SUCCEED);
        return ar;
    }

    @RequestMapping(value = "addSubTeam", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('Subcontract:add')")
    @ApiOperation(value = "分包班组管理-新增")
    public AjaxRes addSubTeam(@RequestBody SubTeamBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.addSubTeam(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3){
                ar.setFailMsg("班组名称已存在");
            }
        }
        return  ar;
    }

    @RequestMapping(value = "updateSubTeam", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('Subcontract:update')")
    @ApiOperation(value = "分包班组管理-修改")
    public AjaxRes updateSubTeam(@RequestBody SubTeamBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.updateSubTeam(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3){
                ar.setFailMsg("班组名称已存在");
            }
        }
        return  ar;
    }

    @LogAnnotation
    @PostMapping("updateSubTeamStatus")
    @ApiOperation(value = "分公司管理-启用/冻结班组")
    public AjaxRes updateSubTeamStatus(SubTeamBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.updateSubTeamStatus(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }


    @LogAnnotation
    @PostMapping("tree")
    @ApiOperation(value = "项目管理团队-公司-项目部树")
    public List<TreeBean> getCompanyProTree(TreeBean o) {
        return service.getSubProTree(o);
    }

    @LogAnnotation
    @PostMapping("getTowerProTree")
    @ApiOperation(value = "杆塔-公司-项目部树")
    public List<TreeBean> getTowerProTree(TreeBean o) {
        return service.getTowerProTree(o);
    }

    @LogAnnotation
    @PostMapping("getTeamsList")
    @ApiOperation(value = "分包班组管理-班组列表查询")
    public  List<SubTeamBean> getTeamsList(SubTeamBean o){
        List<SubTeamBean> list = service.getTeamsList(o);
        return list;
    }

    @LogAnnotation
    @PostMapping("bindOrUnBindProTeam")
    @ApiOperation(value = "分包班组管理-绑定或解绑功能")
    public AjaxRes bindOrUnBindProTeam(SubTeamBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.bindOrUnBindProTeam(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }
    @LogAnnotation
    @PostMapping("delSubTeamTeamById")
    @PreAuthorize("hasAuthority('Subcontract:del')")
    @ApiOperation(value = "分包班组管理-删除功能")
    public AjaxRes delSubTeamTeamById(SubTeamBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.delSubTeamTeamById(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }
    @LogAnnotation
    @PostMapping("delSubTeamPersonById")
    @ApiOperation(value = "分包班组管理-删除班组人员功能")
    public AjaxRes delSubTeamPersonById(SubTeamBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.delSubTeamPersonById(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    /******************************************************************分包基本信息****************************************************************/
    @LogAnnotation
    @PostMapping("getSubcontractaList")
    @ApiOperation(value = "分包基本信息-列表查询")
    public R getSubcontractaList(SubTeamBean o){
        List<SubTeamBean> list = service.getSubcontractaList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }
    /**
     * 分包基本信息导出
     * @param request
     * @param response
     * @param page
     */
    @RequestMapping(value = "/exportPhysicalCheckResult", method = RequestMethod.GET)
    @ApiOperation(value = "分包基本信息-导出")
    public void exportPayroll(HttpServletRequest request, HttpServletResponse response, PageTableRequest page) throws ParseException {
        SubTeamBean b = new SubTeamBean();
        b.setKeyWord(request.getParameter("keyword"));
        List<SubTeamBean> list = service.getSubcontractaList(b);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] excelHeader = {"序号","分包名称","法定代表人","营业地址"};
        int[] ds_format = {2, 2, 2, 2};
        String fileName = "分包商汇总清单";
        if (list != null) {
            int i = 0;
            for (SubTeamBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号",i);
                map.put("分包名称",bean.getName());
                map.put("法定代表人",bean.getLegalRepre());
                map.put("营业地址",bean.getBusinessAddress());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @LogAnnotation
    @PostMapping("delSubcontractById")
    @PreAuthorize("hasAuthority('Subcontract:del')")
    @ApiOperation(value = "分包班组管理-删除功能")
    public AjaxRes delSubcontractById(SubTeamBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.delSubcontractById(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @RequestMapping(value = "addSubcontract", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('Subcontract:add')")
    @ApiOperation(value = "分包班组管理-新增")
    public AjaxRes addSubcontract(@RequestBody SubTeamBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.addSubcontract(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3){
                ar.setFailMsg("分包商名称已存在");
            }
        }
        return  ar;
    }

    @RequestMapping(value = "upSubcontract", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('Subcontract:update')")
    @ApiOperation(value = "分包班组管理-修改")
    public AjaxRes upSubcontract(@RequestBody SubTeamBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.upSubcontract(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3){
                ar.setFailMsg("分包商名称已存在");
            }
        }
        return  ar;
    }


    @LogAnnotation
    @PostMapping("getTowerBindList")
    @ApiOperation(value = "班组工程绑定-班组列表查询")
    public  List<SubTeamBean> getTowerBindList(SubTeamBean o){
        List<SubTeamBean> list = service.getTowerBindList(o);
        return list;
    }

    @LogAnnotation
    @PostMapping("bindTowerBindTeam")
    @ApiOperation(value = "班组工程绑定-绑定或解绑功能")
    public AjaxRes bindTowerBindTeam(SubTeamBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.bindTowerBindTeam(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @LogAnnotation
    @RequestMapping(value = "getTowerSubcontract", method = RequestMethod.POST)
    @ApiOperation(value = "工程杆塔绑定-杆塔穿梭框数据查询")
    public AjaxRes getTowerSubcontract(SubTeamBean o) {
        AjaxRes ar = new AjaxRes();
        Map<String,Object> map = service.getTowerSubcontract(o);
        ar.setSucceed(map, GlobalConst.DATA_SUCCEED);
        return ar;
    }

    @LogAnnotation
    @PostMapping("bindTowerSubcon")
    @ApiOperation(value = "工程杆塔绑定-杆塔和分包商绑定功能")
    public AjaxRes bindTowerSubcon(SubTeamBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.bindTowerSubcon(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }


}
