package com.bonus.hn.bearing.capacity.backstage.companypro.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.companypro.service.ProjectManagerService;
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.project.entity.LineProjectBean;
import com.bonus.hn.bearing.capacity.backstage.subteam.entity.SubTeamBean;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.AjaxRes;
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.manager.utils.UserUtil;
import com.bonus.hn.bearing.capacity.backstage.subteam.entity.SubTeamBean;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.AjaxRes;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.GlobalConst;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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:07  项目团队
 */
@Api(tags = "项目团队")
@RestController
@RequestMapping("/proManage")
public class ProjectManagerController {
    @Resource
    private ProjectManagerService service;

    @LogAnnotation
    @PostMapping("getProjectManagementTeamsList")
    @ApiOperation(value = "项目管理团队-列表查询")
    public R getProjectManagementTeamsList(ProjectManagerBean o){
        List<ProjectManagerBean> list = service.getProjectManagementTeamsList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }
    @LogAnnotation
    @PostMapping("getProjectManagementTeamsFormList")
    @ApiOperation(value = "项目管理团队-表单列表查询")
    public  List<ProjectManagerBean> getProjectManagementTeamsFormList(ProjectManagerBean o){
        List<ProjectManagerBean> list = service.getProjectManagementTeamsFormList(o);
        return list;
    }
    @LogAnnotation
    @PostMapping("getProPersonList")
    @ApiOperation(value = "项目管理团队-获取人员表单")
    public R getProPersonList(ProjectManagerBean o){
        List<ProjectManagerBean> list = service.getProPersonList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }
    @LogAnnotation
    @PostMapping("getTeamsList")
    @ApiOperation(value = "项目管理团队-团队列表查询")
    public  List<ProjectManagerBean> getTeamsList(ProjectManagerBean o){
        List<ProjectManagerBean> list = service.getTeamsList(o);
        return list;
    }

    @RequestMapping(value = "getProManageTeamNum", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "项目管理团队-表单查询团队各数量")
    public AjaxRes getProManageTeamNum(ProjectManagerBean o){
        AjaxRes ar = new AjaxRes();
        List<ProjectManagerBean> projectManagerBean = service.getProManageTeamNum(o);
        Map<String, Object> map = new HashMap<>(100);
        if(projectManagerBean !=null){
            map.put("projectManagerBean",projectManagerBean);
        }
        ar.setSucceed(map, GlobalConst.DATA_SUCCEED);
        return ar;
    }

    @RequestMapping(value = "addProManageTeam", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "项目管理团队-新增")
    public AjaxRes addProManageTeam(@RequestBody ProjectManagerBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.addProManageTeam(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3){
                ar.setFailMsg("团队名称已存在");
            }
        }
        return  ar;
    }

    @RequestMapping(value = "updateProManageTeam", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "项目管理团队-修改")
    public AjaxRes updateProManageTeam(@RequestBody ProjectManagerBean bean){
        AjaxRes ar = new AjaxRes();
        int cb = service.updateProManageTeam(bean);
        ar.setFailMsg(GlobalConst.DATA_FAIL);
        if(cb>0){
            ar.setFailMsg(GlobalConst.INIT_SUCCEED);
            if (cb == 3){
                ar.setFailMsg("团队名称已存在");
            }
        }
        return  ar;
    }

    @LogAnnotation
    @PostMapping("delProManageTeamById")
    @ApiOperation(value = "项目管理团队-删除功能")
    public AjaxRes delProManageTeamById(ProjectManagerBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.delProManageTeamById(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }
    @LogAnnotation
    @PostMapping("bindOrUnBindProTeam")
    @ApiOperation(value = "项目管理团队-绑定或解绑功能")
    public AjaxRes bindOrUnBindProTeam(ProjectManagerBean 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("tree")
    @ApiOperation(value = "项目管理团队-公司-项目部树")
    public List<TreeBean> getCompanyProTree(TreeBean o) {
        return service.getCompanyProTree(o);
    }
    @LogAnnotation
    @PostMapping("delProTeamPersonById")
    @ApiOperation(value = "项目管理团队-删除班组人员功能")
    public AjaxRes delProTeamPersonById(ProjectManagerBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.delProTeamPersonById(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    /**
     * 线路工程导出
     * @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 {
        ProjectManagerBean b = new ProjectManagerBean();
        b.setKeyWord(request.getParameter("keyword"));
        b.setStatus(request.getParameter("status"));
        b.setBranchId(request.getParameter("branchId"));
        List<ProjectManagerBean> list = service.getProjectManagementTeamsList(b);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] excelHeader = {"序号","分公司","团队名称","团队类型","电压等级(KV)","团队人数"
                ,"状态","所属项目","备注","创建时间"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
        String fileName = "项目团队信息";
        if (list != null) {
            int i = 0;
            for (ProjectManagerBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号",i);
                map.put("分公司",bean.getBranchName());
                map.put("团队名称",bean.getTeamName());
                map.put("团队类型",bean.getTeamTypeName());
                map.put("电压等级(KV)",bean.getVoltageLevel());
                map.put("团队人数",bean.getTeamNum());
                String proStatus = "0".equals(bean.getStatus()) ? "闲置中" : "已投入";
                map.put("状态", proStatus);
                map.put("所属项目",bean.getProName());
                map.put("备注",bean.getRemark());
                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();
        }
    }
}
