package com.bonus.hn.bearing.capacity.backstage.ownteam.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.ownteam.entity.OwnTeamManageBean;
import com.bonus.hn.bearing.capacity.backstage.ownteam.service.OwnTeamManageService;
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:29 自有班组管理
 */
@Api(tags = "自有班组管理")
@RestController
@RequestMapping("/owmTeams")
public class OwnTeamManageController {

    @Resource
    private OwnTeamManageService service;

    @LogAnnotation
    @PostMapping("getOwnTeamsList")
    @ApiOperation(value = "自有班组管理-列表查询")
    public R getOwnTeamsList(OwnTeamManageBean o){
        List<OwnTeamManageBean> list = service.getOwnTeamsList(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 {
        OwnTeamManageBean b = new OwnTeamManageBean();
        b.setKeyWord(request.getParameter("keyword"));
        b.setStatus(request.getParameter("status"));
        b.setTeamTypeId(request.getParameter("teamTypeId"));
        List<OwnTeamManageBean> list = service.getOwnTeamsList(b);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] excelHeader = {"序号","班组名称","班组类别","标准班组类型","班组人数","状态"
                ,"分配分公司","所属项目","创建时间"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2};
        String fileName = "自有班组管理信息";
        if (list != null) {
            int i = 0;
            for (OwnTeamManageBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号",i);
                map.put("班组名称",bean.getTeamName());
                map.put("班组类别",bean.getTeamTypeName());
                map.put("标准班组类型",bean.getStandardTeamType());
                map.put("班组人数",bean.getTeamNum());
                String proStatus = "0".equals(bean.getStatus()) ? "闲置中" : "已投入";
                map.put("状态", proStatus);
                map.put("分配分公司",bean.getBranchName());
                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("getOwnTeamsFormLists")
    @ApiOperation(value = "自有班组管理-表单列表查询")
    public  List<OwnTeamManageBean> getProjectManagementTeamsFormList(OwnTeamManageBean o){
        List<OwnTeamManageBean> list = service.getProjectManagementTeamsFormList(o);
        return list;
    }

    @LogAnnotation
    @PostMapping("getOwnPersonList")
    @ApiOperation(value = "自有班组管理-获取人员表单")
    public R getOwnPersonList(OwnTeamManageBean o){
        List<OwnTeamManageBean> list = service.getOwnPersonList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    @LogAnnotation
    @PostMapping("getTeamsList")
    @ApiOperation(value = "自有班组管理-团队列表查询")
    public  List<OwnTeamManageBean> getTeamsList(OwnTeamManageBean o){
        List<OwnTeamManageBean> list = service.getTeamsList(o);
        return list;
    }

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

    @RequestMapping(value = "addOwnTeams", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('freeTeam:add')")
    @ApiOperation(value = "自有班组管理-新增")
    public AjaxRes addProManageTeam(@RequestBody OwnTeamManageBean 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 = "updateOwnTeams", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('freeTeam:update')")
    @ApiOperation(value = "自有班组管理-修改")
    public AjaxRes updateProManageTeam(@RequestBody OwnTeamManageBean 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("delOwnTeamById")
    @PreAuthorize("hasAuthority('freeTeam:del')")
    @ApiOperation(value = "自有班组管理-删除功能")
    public AjaxRes delOwnTeamById(OwnTeamManageBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.delOwnTeamById(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(OwnTeamManageBean bean) {
        AjaxRes ar = new AjaxRes();
        int result  = service.bindOrUnBindProTeam(bean);
        if(result==1){
            ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
        }else if(result==3){
            ar.setFailMsg("存在已绑定的工程");
        } 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(OwnTeamManageBean 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;
    }


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


}
