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

import com.bonus.hn.bearing.capacity.backstage.companypro.entity.OwmTeamQueryBean;
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.OwmTeamQueryService;
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 io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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:16  自有班组
 */
@RestController
@RequestMapping("/owmTeamQuery")
public class OwmTeamQueryController {

    @Resource
    private OwmTeamQueryService service;

    @LogAnnotation
    @PostMapping("getowmTeamQueryTeamsList")
    @ApiOperation(value = "自有班组查询-列表查询")
    public R getProjectManagementTeamsList(OwmTeamQueryBean o){
        List<OwmTeamQueryBean> list = service.getProjectManagementTeamsList(o);
        return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
    }

    @LogAnnotation
    @PostMapping("getowmTeamQueryTeamsFormList")
    @ApiOperation(value = "自有班组查询-人员查询")
    public  List<OwmTeamQueryBean> getowmTeamQueryTeamsFormList(OwmTeamQueryBean o){
        List<OwmTeamQueryBean> list = service.getowmTeamQueryTeamsFormList(o);
        return list;
    }

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

    @LogAnnotation
    @PostMapping("tree")
    @ApiOperation(value = "班组工程绑定-公司-项目部树")
    public List<TreeBean> getCompanyProTree(TreeBean o) {
        return service.getCompanyProTree(o);
    }

    @LogAnnotation
    @PostMapping("bindOrUnBindProTeam")
    @ApiOperation(value = "班组工程绑定-绑定或解绑功能")
    public AjaxRes bindOrUnBindProTeam(OwmTeamQueryBean 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);
//    }
    /**
     * 线路工程导出
     * @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 {
        OwmTeamQueryBean b = new OwmTeamQueryBean();
        b.setKeyWord(request.getParameter("keyword"));
        b.setStatus(request.getParameter("status"));
        b.setBranchId(request.getParameter("branchId"));
        List<OwmTeamQueryBean> list = service.getProjectManagementTeamsList(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 (OwmTeamQueryBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>(100);
                map.put("序号",i);
                map.put("分公司",bean.getOrgName());
                map.put("班组名称",bean.getOwnOrgName());
                switch (bean.getOwnTeamType()){
                    case "1":
                        map.put("工序","土建");
                        break;
                    case "2":
                        map.put("工序","安装");
                        break;
                    case "3":
                        map.put("工序","调试");
                        break;
                    default:
                        map.put("工序"," ");
                        break;
                }
                map.put("团队人数",bean.getOwnTeamNum());
                String proStatus = "0".equals(bean.getOwnTeamStatus()) ? "闲置中" : "已投入";
                map.put("状态", proStatus);
                map.put("所属项目",bean.getProName());
                map.put("分配人",bean.getBindPerson());
                map.put("分配时间",bean.getBindTime());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
