package xiaoqu.home.open.controller;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import xiaoqu.home.open.controller.export.efficiency.*;
import xiaoqu.home.open.controller.input.BaseInput;
import xiaoqu.home.open.controller.input.task.*;
import xiaoqu.home.open.controller.output.task.EfficienctTaskInfoOutput;
import xiaoqu.home.open.controller.output.task.EfficiencyDetailOutput;
import xiaoqu.home.open.controller.output.task.EfficiencyRecordingOutput;
import xiaoqu.home.open.controller.output.task.EfficiencyStatisticsOutput;
import xiaoqu.home.open.model.db1.District;
import xiaoqu.home.open.model.db1.User;
import xiaoqu.home.open.model.db2.*;
import xiaoqu.home.open.publicBean.BaseOutput;
import xiaoqu.home.open.publicBean.PageOutput;
import xiaoqu.home.open.service.RedisService5;
import xiaoqu.home.open.service.db1.DistrictService;
import xiaoqu.home.open.service.db1.UserService;
import xiaoqu.home.open.service.db2.*;
import xiaoqu.home.open.utils.*;
import xiaoqu.home.open.utils.poi.ExcelUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.Object;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Api(value = "效率相关")
@Controller
@RequestMapping("/efficiency")
@Slf4j
public class EfficiencyController {

    @Autowired
    private EfficiencyRecordingService efficiencyRecordingService;
    @Autowired
    private EfficiencyDetailService efficiencyDetailService;
    @Autowired
    private TaskInfoService taskInfoService;
    @Autowired
    private TaskUserService taskUserService;
    @Autowired
    private TaskScenesService taskScenesService;
    @Autowired
    private WageService wageService;
    @Autowired
    private UserService userService;
    @Autowired
    private DistrictService districtService;
    @Autowired
    private FieldWebpageAuthorityService fieldWebpageAuthorityService;
    @Autowired
    private RedisService5 redisService5;

    @ApiOperation(value = "任务人效查询")
    @ResponseBody
    @RequestMapping(value = "/taskEfficiencyPageList", method = RequestMethod.POST)
    public BaseOutput<PageOutput<EfficienctTaskInfoOutput>> taskEfficiencyPageList(HttpServletRequest request, @RequestBody TaskInfoPageInput input) {
        User user = UserUtils.getUser(request);
        log.info("【taskEfficiencyPageList】" + user.getName() + JSONObject.toJSONString(input));

        PageOutput<EfficienctTaskInfoOutput> output = new PageOutput<EfficienctTaskInfoOutput>();
        Long districtId = input.getDistrictId();
        if (StringUtil.isNull(districtId)) {
            return ResultResponseBeanUtil.fail("请选择站点");
        }
        QueryWrapper<TaskInfo> wrapper = WrapperUtils.getWrapper(false);
        wrapper.ge("status", 1);// 条件限定
        wrapper.and(wrapperq -> wrapperq.notIn("task_accept_user", "").or().notIn("task_distributi_user", ""));
//        if (input.getTaskType() == null) {
//            wrapper.in("task_type", 0, 1, 2);
//        }
        if (StringUtil.isNotEmpty(input.getGridAndScenes())) {
            Set<Long> taskIds = getQueryCiteria(input);
            if (taskIds.size() == 0) {
                return ResultResponseBeanUtil.success(output);
            } else {
                wrapper.in("id", taskIds);
            }
            input.setGridAndScenes(null);
        }
        setQueryByStatus(input.getStatus(), wrapper);
        input.setStatus(null);
        wrapper = WrapperUtils.beanToWrapper(wrapper, input);

//		User user = UserUtils.getUser(request);
//		List<FieldWebpageAuthority> fieldList = fieldWebpageAuthorityService.getFieldList(input.getWebId(), user.getPropertyAuthorityId());
//		String[] columns = WrapperUtils.entityToColumns(fieldList);
//		wrapper.select(columns);

        Page<TaskInfo> pageList = new Page<TaskInfo>(input.getPage(), input.getRows());
        List<TaskInfo> entityList = taskInfoService.page(pageList, wrapper).getRecords();
        List<EfficienctTaskInfoOutput> outList = new ArrayList<EfficienctTaskInfoOutput>();
        if (entityList.size() > 0) {
            Map<Long, String> wageToName = new HashMap<Long, String>();
            QueryWrapper<Wage> wagWrapper = WrapperUtils.getWrapper(false);
            wagWrapper.eq("district_id", input.getDistrictId());
            List<Wage> wages = wageService.list(wagWrapper);
            if (wages.size() > 0) {
                for (Wage wage : wages) {
                    wageToName.put(wage.getId(), wage.getName());
                }
            }
            for (TaskInfo bean : entityList) {
                EfficienctTaskInfoOutput outputBean = new EfficienctTaskInfoOutput();
                BeanUtils.copyProperties(bean, outputBean);
                if (bean.getWageId() != 0 && wageToName.size() > 0) {
                    outputBean.setWageType(bean.getWageName());
                }
                switch (bean.getStatus()) {
                    case 2:
                        outputBean.setStatus(1);
                        break;
                    case 3:
                        outputBean.setStatus(1);
                        break;
                    case 4:
                        outputBean.setStatus(1);
                        break;
                    case 7:
                        outputBean.setStatus(9);
                        break;
                    case 8:
                        outputBean.setStatus(9);
                        break;
                    default:
                        break;
                }

                outList.add(outputBean);
            }
        }
        output.setPage(pageList.getCurrent());
        output.setRecords(pageList.getTotal());
        output.setTotal(pageList.getPages());
        output.setRows(outList);
        return ResultResponseBeanUtil.success(output);
    }

    @ApiOperation(value = "任务人效导出")
    @PostMapping("/taskEfficiencyExport")
    public void taskEfficiencyExport(HttpServletRequest request, HttpServletResponse response, @RequestBody TaskInfoPageInput input) throws IOException {
        User user = UserUtils.getUser(request);
        log.info("【taskEfficiencyExport】" + user.getName() + JSONObject.toJSONString(input));


        Long districtId = input.getDistrictId();
        if (districtId == null || districtId == 0) {
            return;
        }

        List<EfficiencyTaskExport> outList = new ArrayList<EfficiencyTaskExport>();
        QueryWrapper<TaskInfo> wrapper = WrapperUtils.getWrapper(false);
        wrapper.ge("status", 1);// 条件限定
        wrapper.and(wrapperq -> wrapperq.notIn("task_accept_user", "").or().notIn("task_distributi_user", ""));

        if (StringUtil.isNotEmpty(input.getGridAndScenes())) {
            Set<Long> taskIds = getQueryCiteria(input);
            if (taskIds.size() == 0) {
                log.info("【taskEfficiencyExport】未找到记录");
                return;
            } else {
                wrapper.in("id", taskIds);
            }
            input.setGridAndScenes(null);
        }
        setQueryByStatus(input.getStatus(), wrapper);
        input.setStatus(null);
        wrapper = WrapperUtils.beanToWrapper(wrapper, input);
        List<TaskInfo> entityList = taskInfoService.list(wrapper);
        if (entityList.size() > 0) {
            Map<Long, String> wageToName = new HashMap<Long, String>();
            QueryWrapper<Wage> wagWrapper = WrapperUtils.getWrapper(false);
            wagWrapper.eq("district_id", input.getDistrictId());
            List<Wage> wages = wageService.list(wagWrapper);
            if (wages.size() > 0) {
                for (Wage wage : wages) {
                    wageToName.put(wage.getId(), wage.getName());
                }
            }
            for (TaskInfo bean : entityList) {
                EfficiencyTaskExport outputBean = new EfficiencyTaskExport();
                BeanUtils.copyProperties(bean, outputBean);
//                if (bean.getWageId() != 0 && wageToName.size() > 0) {
//                    outputBean.setWageType(wageToName.get(bean.getWageId()));
//                }
                outList.add(outputBean);
            }
        }
        String formattedDate = DateToConverter.getFormattedDate(new Date(), 10);
        List<FieldWebpageAuthority> fieldList = fieldWebpageAuthorityService.getExportFieldList(input.getWebId(), user.getAuthorityId());
        String[] exportColumns = WrapperUtils.entityToColumns(fieldList);
        ExcelUtil<EfficiencyTaskExport> util = new ExcelUtil<EfficiencyTaskExport>(EfficiencyTaskExport.class);
        util.exportExcelByColumns(response, outList, "任务人效统计" + formattedDate, exportColumns);
    }


    private void setQueryByStatus(Integer status, QueryWrapper<TaskInfo> wrapper) {
        if (status != null) {
            switch (status) {
                case 1:
                    wrapper.in("status", 2, 3, 4);// 条件限定,待完成
                    break;
                case 5:
                    wrapper.eq("status", 5);
                    break;
                case 6:
                    wrapper.eq("status", 6);
                    break;
                case 9:
                    wrapper.in("status", 7, 8, 9,-4);
                    break;
            }
        }
    }

    private Set<Long> getQueryCiteria(TaskInfoPageInput input) {
        Set<Long> taskIds = new HashSet<Long>();
        List<GridAndSceneInput> gridAndScenes = input.getGridAndScenes();
        for (GridAndSceneInput gridAndSceneInput : gridAndScenes) {
            QueryWrapper<TaskScenes> wrapperTaskScenes = WrapperUtils.getWrapper(false);
            wrapperTaskScenes.eq("district_id", input.getDistrictId());
//				@ApiModelProperty("网格或场景级别")
            Integer level = gridAndSceneInput.getLevel();
//				@ApiModelProperty(value = "网格ID")
            Long gridId = gridAndSceneInput.getGridId();
//				@ApiModelProperty(value = "场景ID")
            Long sceneId = gridAndSceneInput.getSceneId();
//				@ApiModelProperty(value = "是否场景，0:否;1:是")
            Integer isscene = gridAndSceneInput.getIsscene();
            if (isscene == 0) {
//				    @TableField("building_id")
//				    @TableField("floor_id")
//				    @TableField("multistage_grid_id")
                switch (level) {
                    case 1:
                        wrapperTaskScenes.eq("building_id", gridId);
                        break;
                    case 2:
                        wrapperTaskScenes.eq("floor_id", gridId);
                        break;
                    default:
                        wrapperTaskScenes.eq("multistage_grid_id", gridId);
                        break;
                }
                wrapperTaskScenes.eq("grid_level", level);
            } else {
                switch (level) {
                    case 1:
                        wrapperTaskScenes.eq("scenes_id", sceneId);
                        break;
                    case 2:
                        wrapperTaskScenes.eq("scenes_id", sceneId);
                        break;
                }
                wrapperTaskScenes.eq("scenes_level", level);
            }
//			@ApiModelProperty(value = "任务生成时间")
//			private String createTimeStart;
            if (StringUtil.isNotEmpty(input.getCreateTimeStart())) {
                wrapperTaskScenes.ge("create_time", input.getCreateTimeStart());
                wrapperTaskScenes.le("create_time", input.getCreateTimeEnd());
            }
            List<TaskScenes> taskScenes = taskScenesService.list(wrapperTaskScenes);
            taskIds.addAll(taskScenes.stream().map(taskScen -> taskScen.getTaskId()).collect(Collectors.toSet()));
        }
        return taskIds;
    }


    private Set<Long> getQueryCiteria(TaskEfficiencyInput input) {
        Set<Long> taskIds = new HashSet<Long>();
        List<GridAndSceneInput> gridAndScenes = input.getGridAndScenes();
        for (GridAndSceneInput gridAndSceneInput : gridAndScenes) {
            QueryWrapper<TaskScenes> wrapperTaskScenes = WrapperUtils.getWrapper(false);
            wrapperTaskScenes.eq("district_id", input.getDistrictId());
//				@ApiModelProperty("网格或场景级别")
            Integer level = gridAndSceneInput.getLevel();
//				@ApiModelProperty(value = "网格ID")
            Long gridId = gridAndSceneInput.getGridId();
//				@ApiModelProperty(value = "场景ID")
            Long sceneId = gridAndSceneInput.getSceneId();
//				@ApiModelProperty(value = "是否场景，0:否;1:是")
            Integer isscene = gridAndSceneInput.getIsscene();
            if (isscene == 0) {
//				    @TableField("building_id")
//				    @TableField("floor_id")
//				    @TableField("multistage_grid_id")
                switch (level) {
                    case 1:
                        wrapperTaskScenes.eq("building_id", gridId);
                        break;
                    case 2:
                        wrapperTaskScenes.eq("floor_id", gridId);
                        break;
                    default:
                        wrapperTaskScenes.eq("multistage_grid_id", gridId);
                        break;
                }
                wrapperTaskScenes.eq("grid_level", level);
            } else {
                switch (level) {
                    case 1:
                        wrapperTaskScenes.eq("scenes_id", sceneId);
                        break;
                    case 2:
                        wrapperTaskScenes.eq("scenes_id", sceneId);
                        break;
                }
                wrapperTaskScenes.eq("scenes_level", level);
            }
//			@ApiModelProperty(value = "任务生成时间")
//			private String createTimeStart;
            if (StringUtil.isNotEmpty(input.getCreateTimeStart())) {
                wrapperTaskScenes.ge("create_time", input.getCreateTimeStart());
                wrapperTaskScenes.le("create_time", input.getCreateTimeEnd());
            }
            List<TaskScenes> taskScenes = taskScenesService.list(wrapperTaskScenes);
            taskIds.addAll(taskScenes.stream().map(taskScen -> taskScen.getTaskId()).collect(Collectors.toSet()));
        }
        return taskIds;
    }


    /**
     * @param request
     * @param input
     * @return
     */
    @ApiOperation(value = "人效统计")
    @ResponseBody
    @RequestMapping(value = "/taskEfficiencyStatistics", method = RequestMethod.POST)
    public BaseOutput<EfficiencyStatisticsOutput> taskEfficiencyStatistics(HttpServletRequest request, @RequestBody TaskEfficiencyInput input) {
        User user = UserUtils.getUser(request);
        log.info("【taskEfficiencyStatistics】" + user.getName() + JSONObject.toJSONString(input));
        EfficiencyStatisticsOutput efficiencyStatisticsOutput = new EfficiencyStatisticsOutput();
        Long districtId = input.getDistrictId();
        if (StringUtil.isNull(districtId)) {
            return ResultResponseBeanUtil.fail("请选择站点");
        }

        Set<Long> conditionUserIds = new HashSet<>();

        QueryWrapper<TaskInfo> wrapper = WrapperUtils.getWrapper(false);
        wrapper.eq("district_id", input.getDistrictId());
        wrapper.ge("status", 1);// 条件限定
//        wrapper.and(wrapperq -> wrapperq.notIn("task_accept_user", "").or().notIn("task_distributi_user", ""));

        setQueryByStatus(input.getStatus(), wrapper);

        if (input.getCreateTimeStart() != null) {
            wrapper.ge("create_time", input.getCreateTimeStart());
        }
        if (input.getCreateTimeEnd() != null) {
            wrapper.le("create_time", input.getCreateTimeEnd());
        }

        Integer unAppointCount=0;
        {
            QueryWrapper<TaskInfo> wrapper2 = WrapperUtils.getWrapper(false);
            wrapper2.eq("district_id", input.getDistrictId());
            wrapper2.eq("status", 10);// 条件限定
//        wrapper.and(wrapperq -> wrapperq.notIn("task_accept_user", "").or().notIn("task_distributi_user", ""));

//            setQueryByStatus(input.getStatus(), wrapper);

            if (input.getCreateTimeStart() != null) {
                wrapper2.ge("create_time", input.getCreateTimeStart());
            }
            if (input.getCreateTimeEnd() != null) {
                wrapper2.le("create_time", input.getCreateTimeEnd());
            }
            unAppointCount = taskInfoService.count(wrapper2);

        }

        Double personTotalPoint = 0.0;
        Double personTotalSalary = 0.0;
        Double personTotalWageHour = 0.0;
        //任务
        if (input.getType() == 0) {
            if (input.getNo() != null) {
                wrapper.like("no", input.getNo());
            }
            if (input.getName() != null) {
                wrapper.like("name", input.getName());
            }

            if (StringUtil.isNotEmpty(input.getGridAndScenes())) {
                Set<Long> taskIds = getQueryCiteria(input);
                if (taskIds.size() == 0) {
                    return ResultResponseBeanUtil.success(efficiencyStatisticsOutput);
                } else {
                    wrapper.in("id", taskIds);
                }
                input.setGridAndScenes(null);
            }
            if (input.getTaskType() != null) {
                wrapper.eq("task_type", input.getTaskType());
            }
            if (input.getManualAllocation() != null) {
                wrapper.eq("manual_allocation", input.getManualAllocation());
            }
            if (input.getTaskAcceptUser() != null) {
                wrapper.like("task_accept_user", input.getTaskAcceptUser());
            }
//            if (input.getStatus() != null) {
//
//                switch (input.getStatus()) {
//                    case 1:
//                        wrapper.in("status", 2,3, 4);
//                        break;
//                    case 5:
//                        wrapper.in("status", 5);
//                        break;
//                    case 6:
//                        wrapper.in("status", 6);
//                        break;
//                    case 9:
//                        wrapper.ge("status", 7);
//                        break;
//                }
//            }

            //人员
        } else if (input.getType() == 1) {
            QueryWrapper<User> userwrapper = WrapperUtils.getWrapper(false);
            Boolean userFlag = false;

            if (input.getUserName() != null && !input.getUserName().trim().equals("")) {
                userwrapper.like("name", input.getUserName());
                userFlag = true;
            }
            if (input.getMobile() != null && !input.getMobile().trim().equals("")) {
                userwrapper.like("mobile", input.getMobile());
                userFlag = true;

            }
            if (input.getAppRole() != null) {
                userwrapper.eq("approle", input.getAppRole());
                userFlag = true;

            }
            Map<String, User> userNameMap = new HashMap();
//            if (userFlag) {
//                List<User> userlist = userService.list(userwrapper);
//                if (userlist.isEmpty()) {
//                    return ResultResponseBeanUtil.success(efficiencyStatisticsOutput);
//                }
//                for (User curuser : userlist) {
//                    userNameMap.put(curuser.getName(), curuser);
//                    conditionUserIds.add(curuser.getId());
//                }
//            }

            QueryWrapper<TaskUser> wrapperTU = WrapperUtils.getWrapper(true);
//            QueryWrapper<TaskUser> wrapperTU = WrapperUtils.getWrapper(false);
            wrapperTU.eq("district_id", input.getDistrictId());
            wrapperTU.gt("user_id", 0);


            if (input.getStatus() != null) {
                switch (input.getStatus()) {
                    case 1:
                        wrapperTU.in("status", 2, 3, 4);// 条件限定,待完成
                        break;
                    case 5:
                        wrapperTU.eq("status", 5);
                        break;
                    case 6:
                        wrapperTU.eq("status", 6);
                        break;
                    case 9:
                        wrapperTU.in("status", 7, 8, 9,-4);
                        break;
                }
            }

//            wrapperTU.groupBy("task_id");
//            if (userFlag && !conditionUserIds.isEmpty()) {
//                wrapperTU.in("user_id", conditionUserIds);
//            }
            if (input.getCreateTimeStart() != null) {
                wrapperTU.ge("create_time", input.getCreateTimeStart());
            }
            if (input.getCreateTimeEnd() != null) {
                wrapperTU.le("create_time", input.getCreateTimeEnd());
            }
            List<TaskUser> taskUserList = taskUserService.list(wrapperTU);
            Set<Long> taskIds = new HashSet<>();
            Set<Long> taskUserIds = new HashSet<>();



            for (TaskUser taskUser : taskUserList) {
                taskUserIds.add(taskUser.getUserId());
                taskIds.add(taskUser.getTaskId());
            }
            if (taskUserIds.isEmpty()) {
                return ResultResponseBeanUtil.success(efficiencyStatisticsOutput);
            }
//            if (userFlag) {
            userwrapper.in("id", taskUserIds);
            List<User> userlist = userService.list(userwrapper);
            if (userlist.isEmpty()) {
                return ResultResponseBeanUtil.success(efficiencyStatisticsOutput);
            }
            for (User curuser : userlist) {
                userNameMap.put(curuser.getName(), curuser);
                conditionUserIds.add(curuser.getId());
            }
//            }


            if (!taskIds.isEmpty()) {
                wrapper.in("id", taskIds);
            } else {

            }
        }


//        wrapper.and(wrapperq -> wrapperq.like("task_accept_user", taskUser.getUserName()).or().eq("task_distributi_user", taskUser.getUserName()));

        StringBuilder sql = getStringBuilder(null);

        wrapper.select(sql.toString());
        TaskInfo curtask = taskInfoService.getOne(wrapper);
        Integer totalCount = 0;
        if (input.getType() == 0) {
            totalCount = curtask.getPoolOrder();
            efficiencyStatisticsOutput.setTaskCount(totalCount);
//            efficiencyStatisticsOutput.setSalaryTotal(curtask.getSalary());
//            efficiencyStatisticsOutput.setPointTotal(curtask.getPoint());
//            efficiencyStatisticsOutput.setWageHourTotal(curtask.getWageHour());
        } else if (input.getType() == 1) {
            totalCount = conditionUserIds.size();
            efficiencyStatisticsOutput.setUserCount(totalCount);
            efficiencyStatisticsOutput.setTaskCount(curtask.getPoolOrder());
//
//            efficiencyStatisticsOutput.setSalaryTotal(personTotalSalary);
//            efficiencyStatisticsOutput.setPointTotal(personTotalPoint);
//            efficiencyStatisticsOutput.setWageHourTotal(personTotalWageHour);
        }
        efficiencyStatisticsOutput.setSalaryTotal(curtask.getSalary());
        efficiencyStatisticsOutput.setPointTotal(curtask.getPoint());
        efficiencyStatisticsOutput.setWageHourTotal(curtask.getWageHour());
        efficiencyStatisticsOutput.setTaskAcceptCount(curtask.getGroupOrder());
        efficiencyStatisticsOutput.setTaskDistributiCount(curtask.getTaskOrder());
        efficiencyStatisticsOutput.setConfirmTimeingTotal(curtask.getConfirmTiming().doubleValue());
        efficiencyStatisticsOutput.setTransferTimingTotal(curtask.getTransferTiming().doubleValue());
        efficiencyStatisticsOutput.setTimingTotal(curtask.getTiming().doubleValue());
        efficiencyStatisticsOutput.setOnTimeCount(curtask.getShouldInspect());
        efficiencyStatisticsOutput.setTimeOutCount(curtask.getActualInspect());


        if (totalCount != null && totalCount > 0) {

            BigDecimal totalCountBD = new BigDecimal(totalCount);
//        BigDecimal accpetPercent = new BigDecimal(efficiencyStatisticsOutput.getTaskAcceptCount()).divide(new BigDecimal(efficiencyDetail.getDistributiCount()), 4, BigDecimal.ROUND_HALF_UP);
//        efficiencyStatisticsOutput.setAccpetPercentStr(accpetPercent.doubleValue() * 100 + "%");

            BigDecimal comfirmAve = new BigDecimal(efficiencyStatisticsOutput.getConfirmTimeingTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
            BigDecimal transferAve = new BigDecimal(efficiencyStatisticsOutput.getTransferTimingTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
            BigDecimal torkTimingAve = new BigDecimal(efficiencyStatisticsOutput.getTimingTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
            BigDecimal wageHourAve = new BigDecimal(efficiencyStatisticsOutput.getWageHourTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
            BigDecimal pointAve = new BigDecimal(efficiencyStatisticsOutput.getPointTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
            BigDecimal salaryAve = new BigDecimal(efficiencyStatisticsOutput.getSalaryTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
            efficiencyStatisticsOutput.setConfirmTimeAve(comfirmAve.doubleValue());
            efficiencyStatisticsOutput.setTransferTimingAve(transferAve.doubleValue());
            efficiencyStatisticsOutput.setTimingAve(torkTimingAve.doubleValue());
            efficiencyStatisticsOutput.setWageHourAve(wageHourAve.doubleValue());
            efficiencyStatisticsOutput.setPointAve(pointAve.doubleValue());
            efficiencyStatisticsOutput.setSalaryAve(salaryAve.doubleValue());

            if (input.getType() == 0) {
                efficiencyStatisticsOutput.setUnfinishCount(curtask.getNotInspect() + curtask.getTaskType() + curtask.getTaskCreateType() + curtask.getUserType());
            } else if (input.getType() == 1) {
                efficiencyStatisticsOutput.setUnfinishCount(curtask.getTaskCreateType());
                efficiencyStatisticsOutput.setCancleCount(curtask.getNotInspect() + curtask.getTaskType());
                efficiencyStatisticsOutput.setAppointFailCount(unAppointCount);
                efficiencyStatisticsOutput.setGrabCount(curtask.getUserStatus());
            }


            Integer totalcount = efficiencyStatisticsOutput.getTaskCount();
            if (totalcount > 0) {
                BigDecimal finishPercent = new BigDecimal(100 * (efficiencyStatisticsOutput.getOnTimeCount() + efficiencyStatisticsOutput.getTimeOutCount())).divide(BigDecimal.valueOf(totalcount), 2, BigDecimal.ROUND_HALF_UP);
                efficiencyStatisticsOutput.setFinishPercent(finishPercent.doubleValue());
            } else {
                efficiencyStatisticsOutput.setFinishPercent(0.0);
            }

        }
        return ResultResponseBeanUtil.success(efficiencyStatisticsOutput);
    }


    @ApiOperation(value = "人员人效查询")
    @ResponseBody
    @RequestMapping(value = "/userEfficiencyPageList", method = RequestMethod.POST)
    public BaseOutput<PageOutput<EfficiencyDetailOutput>> userEfficiencyPageList(HttpServletRequest request, @RequestBody TaskEfficiencyInput input) {

        User curuser = UserUtils.getUser(request);
        log.info("【userEfficiencyPageList】" + curuser.getName() + JSONObject.toJSONString(input));

        PageOutput<EfficiencyDetailOutput> output = new PageOutput<EfficiencyDetailOutput>();
        Page<TaskInfo> pageList = new Page<TaskInfo>(input.getPage(), input.getRows());
        Long districtId = input.getDistrictId();
        if (StringUtil.isNull(districtId)) {
            return ResultResponseBeanUtil.fail("请选择站点");
        }
        //人员查询
        QueryWrapper<User> userwrapper = WrapperUtils.getWrapper(false);
        Boolean userFlag = false;

        if (input.getUserName() != null && !input.getUserName().trim().equals("")) {
            userwrapper.like("name", input.getUserName());
            userFlag = true;
        }
        if (input.getMobile() != null && !input.getMobile().trim().equals("")) {
            userwrapper.like("mobile", input.getMobile());
            userFlag = true;

        }
        if (input.getAppRole() != null) {
            userwrapper.eq("approle", input.getAppRole());
            userFlag = true;

        }
        Map<String, User> userNameMap = new HashMap();
        Set<Long> conditionUserIds = new HashSet<>();
        if (userFlag) {
            List<User> userlist = userService.list(userwrapper);
            if (userlist.isEmpty()) {
                return ResultResponseBeanUtil.success(output);
            }
            for (User user : userlist) {
                userNameMap.put(user.getName(), user);
                conditionUserIds.add(user.getId());
            }
        }

//2221024
        QueryWrapper<TaskUser> wrapperTU = WrapperUtils.getWrapper(true);
        wrapperTU.eq("district_id", input.getDistrictId());
        wrapperTU.gt("user_id", 0);
        wrapperTU.groupBy("user_id");
        if (userFlag && !conditionUserIds.isEmpty()) {
            wrapperTU.in("user_id", conditionUserIds);
        }
        if (input.getCreateTimeStart() != null) {
            wrapperTU.ge("create_time", input.getCreateTimeStart());
        }
        if (input.getCreateTimeEnd() != null) {
            wrapperTU.le("create_time", input.getCreateTimeEnd());
        }
        wrapperTU.select("user_id,user_name,mobile,sum(point) point,sum(salary) salary");
        Page<TaskUser> userpageList = new Page<TaskUser>(input.getPage(), input.getRows());
        List<TaskUser> userCountlist = taskUserService.page(userpageList, wrapperTU).getRecords();
        List<EfficiencyDetailOutput> outList = new ArrayList<>();
        String districtName = redisService5.get("name:District:" + input.getDistrictId());

        for (TaskUser taskUser : userCountlist) {
            EfficiencyDetailOutput efficiencyDetail = new EfficiencyDetailOutput();
            efficiencyDetail.setUserId(taskUser.getUserId());
            efficiencyDetail.setUserName(taskUser.getUserName());
            efficiencyDetail.setMobile(taskUser.getMobile());
            efficiencyDetail.setDistrictId(input.getDistrictId());
            efficiencyDetail.setDistrictName(districtName);

            if (userNameMap.containsKey(taskUser.getUserName())) {
                User user = userNameMap.get(taskUser.getUserName());
                efficiencyDetail.setAppRole(user.getApprole());
            } else {
                User curUser = userService.getById(taskUser.getUserId());
                if (curUser != null) {
                    efficiencyDetail.setAppRole(curUser.getApprole());

                }
            }


            QueryWrapper<TaskInfo> wrapper = WrapperUtils.getWrapper(false);
            QueryWrapper<TaskInfo> wrapper2 = WrapperUtils.getWrapper(false);
            if (input.getCreateTimeStart() != null) {
                wrapper.ge("create_time", input.getCreateTimeStart());
                wrapper2.ge("create_time", input.getCreateTimeStart());
            }
            if (input.getCreateTimeEnd() != null) {
                wrapper.le("create_time", input.getCreateTimeEnd());
                wrapper2.le("create_time", input.getCreateTimeEnd());
            }
            setQueryByStatus(input.getStatus(), wrapper);
            setQueryByStatus(input.getStatus(), wrapper2);

            wrapper.eq("district_id", input.getDistrictId());
            wrapper2.eq("district_id", input.getDistrictId());
//            BeanUtils.copyProperties(wrapper,wrapper2);
            wrapper2.and(wrapperq -> wrapperq.like("task_accept_user", taskUser.getUserName()).or().like("task_distributi_user", taskUser.getUserName()));
            wrapper.like("task_accept_user", taskUser.getUserName());

            StringBuilder sql = getStringBuilder(null);
            wrapper.select(sql.toString());
            wrapper2.select(sql.toString());
            //接单数据
            TaskInfo curtask = taskInfoService.getOne(wrapper);
            //派单数据
            TaskInfo curtask2 = taskInfoService.getOne(wrapper2);
            Integer totalCount = curtask.getPoolOrder() != null ? curtask.getPoolOrder() : 0;
            efficiencyDetail.setAcceptCount(curtask.getPoolOrder() != null ? curtask.getPoolOrder() : 0);
            efficiencyDetail.setDistributiCount(curtask2.getPoolOrder() != null ? curtask2.getPoolOrder() : 0);


            efficiencyDetail.setConfirmTimingTotal(curtask.getConfirmTiming().doubleValue());
            efficiencyDetail.setTransferTimingTotal(curtask.getTransferTiming().doubleValue());
            efficiencyDetail.setWorkTimingTotal(curtask.getTiming().doubleValue());
            efficiencyDetail.setWageHourTotal(curtask.getWageHour());
            efficiencyDetail.setPointTotal(taskUser.getPoint());
            efficiencyDetail.setOnTimeCount(curtask.getShouldInspect());
            efficiencyDetail.setTimeOutCount(curtask.getActualInspect());
            efficiencyDetail.setSalaryTotal(taskUser.getSalary());

            BigDecimal totalCountBD = new BigDecimal(totalCount);
            if (efficiencyDetail.getDistributiCount() > 0) {
                BigDecimal accpetPercent = new BigDecimal(efficiencyDetail.getAcceptCount() * 100).divide(new BigDecimal(efficiencyDetail.getDistributiCount()), 2, BigDecimal.ROUND_HALF_UP);
                efficiencyDetail.setAccpetPercentStr(accpetPercent.doubleValue() + "%");

            } else {
                efficiencyDetail.setAccpetPercentStr("0%");

            }
            if (totalCount != null && totalCount > 0) {

                BigDecimal comfirmAve = new BigDecimal(efficiencyDetail.getConfirmTimingTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal transferAve = new BigDecimal(efficiencyDetail.getTransferTimingTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal torkTimingAve = new BigDecimal(efficiencyDetail.getWorkTimingTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal wageHourAve = new BigDecimal(efficiencyDetail.getWageHourTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
                efficiencyDetail.setConfirmTimingAve(comfirmAve.doubleValue());
                efficiencyDetail.setTransferTimingAve(transferAve.doubleValue());
                efficiencyDetail.setWorkTimingAve(torkTimingAve.doubleValue());
                efficiencyDetail.setWageHourAve(wageHourAve.doubleValue());
                efficiencyDetail.setUnfinishCount(curtask.getNotInspect() + curtask.getTaskType() + curtask.getTaskCreateType());
                if (efficiencyDetail.getAcceptCount() > 0) {
                    BigDecimal finishPercent = new BigDecimal((efficiencyDetail.getOnTimeCount() + efficiencyDetail.getTimeOutCount()) * 100).divide(BigDecimal.valueOf(efficiencyDetail.getAcceptCount()), 2, BigDecimal.ROUND_HALF_UP);
                    efficiencyDetail.setFinishPercentStr(finishPercent.doubleValue() + "%");
                } else {
                    efficiencyDetail.setFinishPercentStr("0%");

                }

            }
            outList.add(efficiencyDetail);
        }
        output.setPage(userpageList.getCurrent());
        output.setRecords(userpageList.getTotal());
        output.setTotal(userpageList.getPages());
        output.setRows(outList);

        return ResultResponseBeanUtil.success(output);
    }


    @ApiOperation(value = "人员人效导出")
    @PostMapping("/userEfficiencyExport")
    public void userEfficiencyExport(HttpServletRequest request, HttpServletResponse response, @RequestBody TaskEfficiencyInput input) throws IOException {
        User user = UserUtils.getUser(request);
        log.info("【userEfficiencyExport】" + user.getName() + JSONObject.toJSONString(input));


        Long districtId = input.getDistrictId();
        if (districtId == null || districtId == 0) {
            return;
        }

        List<EfficiencyUserExport> outList = new ArrayList<EfficiencyUserExport>();
        QueryWrapper<User> userwrapper = WrapperUtils.getWrapper(false);
        Boolean userFlag = false;

        if (input.getUserName() != null && !input.getUserName().trim().equals("")) {
            userwrapper.like("name", input.getUserName());
            userFlag = true;
        }
        if (input.getMobile() != null && !input.getMobile().trim().equals("")) {
            userwrapper.like("mobile", input.getMobile());
            userFlag = true;

        }
        if (input.getAppRole() != null) {
            userwrapper.eq("approle", input.getAppRole());
            userFlag = true;

        }
        Map<String, User> userNameMap = new HashMap();
        Set<Long> conditionUserIds = new HashSet<>();
        if (userFlag) {
            List<User> userlist = userService.list(userwrapper);
            if (userlist.isEmpty()) {
                return;
            }
            for (User curuser : userlist) {
                userNameMap.put(curuser.getName(), curuser);
                conditionUserIds.add(curuser.getId());
            }
        }


        QueryWrapper<TaskUser> wrapperTU = WrapperUtils.getWrapper(true);
        wrapperTU.eq("district_id", input.getDistrictId());
        wrapperTU.gt("user_id", 0);

        wrapperTU.groupBy("user_id");
        if (userFlag && !conditionUserIds.isEmpty()) {
            wrapperTU.in("user_id", conditionUserIds);
        }
        if (input.getCreateTimeStart() != null) {
            wrapperTU.ge("create_time", input.getCreateTimeStart());
        }
        if (input.getCreateTimeEnd() != null) {
            wrapperTU.le("create_time", input.getCreateTimeEnd());
        }
        Page<TaskUser> userpageList = new Page<TaskUser>(input.getPage(), input.getRows());
        List<TaskUser> userCountlist = taskUserService.page(userpageList, wrapperTU).getRecords();
        String districtName = redisService5.get("name:District:" + input.getDistrictId());

        for (TaskUser taskUser : userCountlist) {
            EfficiencyUserExport efficiencyDetail = new EfficiencyUserExport();
//            efficiencyDetail.setUserId(taskUser.getUserId());
            efficiencyDetail.setUserName(taskUser.getUserName());
            efficiencyDetail.setMobile(taskUser.getMobile());

            if (userNameMap.containsKey(taskUser.getUserName())) {
                User curuser = userNameMap.get(taskUser.getUserName());
                efficiencyDetail.setAppRole(curuser.getApprole());
            } else {
                User curUser = userService.getById(taskUser.getUserId());
                if (curUser != null) {

                    efficiencyDetail.setAppRole(curUser.getApprole());
                }
            }
//            efficiencyDetail.setDistrictId(taskUser.getDistrictId());
            efficiencyDetail.setDistrictName(districtName);

            QueryWrapper<TaskInfo> wrapper = WrapperUtils.getWrapper(false);
            QueryWrapper<TaskInfo> wrapper2 = WrapperUtils.getWrapper(false);
            if (input.getCreateTimeStart() != null) {
                wrapper.ge("create_time", input.getCreateTimeStart());
                wrapper2.ge("create_time", input.getCreateTimeStart());
            }
            if (input.getCreateTimeEnd() != null) {
                wrapper.le("create_time", input.getCreateTimeEnd());
                wrapper2.le("create_time", input.getCreateTimeEnd());
            }
            setQueryByStatus(input.getStatus(), wrapper);
            setQueryByStatus(input.getStatus(), wrapper2);
            wrapper.eq("district_id", input.getDistrictId());
            wrapper2.eq("district_id", input.getDistrictId());
//            BeanUtils.copyProperties(wrapper,wrapper2);
            wrapper2.and(wrapperq -> wrapperq.like("task_accept_user", taskUser.getUserName()).or().like("task_distributi_user", taskUser.getUserName()));
            wrapper.like("task_accept_user", taskUser.getUserName());

            StringBuilder sql = getStringBuilder(null);
            wrapper.select(sql.toString());
            wrapper2.select(sql.toString());
            TaskInfo curtask = taskInfoService.getOne(wrapper);
            TaskInfo curtask2 = taskInfoService.getOne(wrapper2);
            Integer totalCount = curtask.getPoolOrder() != null ? curtask.getPoolOrder() : 0;
            efficiencyDetail.setAcceptCount(curtask.getPoolOrder() != null ? curtask.getPoolOrder() : 0);
            efficiencyDetail.setDistributiCount(curtask2.getPoolOrder() != null ? curtask2.getPoolOrder() : 0);
            efficiencyDetail.setConfirmTimingTotal(curtask.getConfirmTiming().doubleValue());
            efficiencyDetail.setTransferTimingTotal(curtask.getTransferTiming().doubleValue());
            efficiencyDetail.setWorkTimingTotal(curtask.getTiming().doubleValue());
            efficiencyDetail.setWageHourTotal(curtask.getWageHour());
            efficiencyDetail.setPointTotal(curtask.getPoint());
            efficiencyDetail.setOnTimeCount(curtask.getShouldInspect());
            efficiencyDetail.setTimeOutCount(curtask.getActualInspect());
            efficiencyDetail.setSalaryTotal(curtask.getSalary());

            if (totalCount != null && totalCount > 0) {
                BigDecimal totalCountBD = new BigDecimal(totalCount);
                if (efficiencyDetail.getDistributiCount() > 0) {
                    BigDecimal accpetPercent = new BigDecimal(efficiencyDetail.getAcceptCount() * 100).divide(new BigDecimal(efficiencyDetail.getDistributiCount()), 2, BigDecimal.ROUND_HALF_UP);
                    efficiencyDetail.setAcceptPercent(accpetPercent.doubleValue());
                } else {
                    efficiencyDetail.setAcceptPercent(0.0);

                }


                BigDecimal comfirmAve = new BigDecimal(efficiencyDetail.getConfirmTimingTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal transferAve = new BigDecimal(efficiencyDetail.getTransferTimingTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal torkTimingAve = new BigDecimal(efficiencyDetail.getWorkTimingTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal wageHourAve = new BigDecimal(efficiencyDetail.getWageHourTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
                efficiencyDetail.setConfirmTimingAve(comfirmAve.doubleValue());
                efficiencyDetail.setTransferTimingAve(transferAve.doubleValue());
                efficiencyDetail.setWorkTimingAve(torkTimingAve.doubleValue());
                efficiencyDetail.setWageHourAve(wageHourAve.doubleValue());
                efficiencyDetail.setUnfinishCount(curtask.getNotInspect() + curtask.getTaskType() + curtask.getTaskCreateType());
                if (efficiencyDetail.getAcceptCount() > 0) {
                    BigDecimal finishPercent = new BigDecimal(100 * (efficiencyDetail.getOnTimeCount() + efficiencyDetail.getTimeOutCount())).divide(BigDecimal.valueOf(efficiencyDetail.getAcceptCount()), 2, BigDecimal.ROUND_HALF_UP);
                    efficiencyDetail.setFinishPercent(finishPercent.doubleValue());
                } else {
                    efficiencyDetail.setFinishPercent(0.0);
                }
            }

            outList.add(efficiencyDetail);
        }
        String formattedDate = DateToConverter.getFormattedDate(new Date(), 10);
//        List<FieldWebpageAuthority> fieldList = fieldWebpageAuthorityService.getExportFieldList(input.getWebId(), user.getAuthorityId());
//        String[] exportColumns = WrapperUtils.entityToColumns(fieldList);
        ExcelUtil<EfficiencyUserExport> util = new ExcelUtil<EfficiencyUserExport>(EfficiencyUserExport.class);
        util.exportExcel(response, outList, "任务人效统计" + formattedDate);
    }

    /**
     * softDelete
     * taskType				系统取消
     * taskCreateType			未完成/过期
     * status
     * confirmTiming			confirmTiming
     * timing					timing
     * overTiming
     * pointStatus
     * userType				派发失败过期
     * userStatus				待抢单
     * expireType
     * transferTiming			transferTiming
     * poolOrder				total
     * groupOrder				task_accept_user
     * taskOrder				task_distributi_user
     * manualAllocation
     * shouldInspect			按时
     * actualInspect			超时
     * notInspect				人工取消
     *
     * @param userName
     * @return
     */
    private StringBuilder getStringBuilder(String userName) {
        StringBuilder sql = new StringBuilder();


        sql.append("IFNULL(COUNT(*),0) pool_order,")

//                .append("COUNT(task_accept_user LIKE '%" + userName + "%' or null) group_order,")
//                .append("COUNT(task_distributi_user LIKE '%" + userName + "%' or null) task_order,")
                .append("IFNULL(COUNT(`status` = 5 or null),0) should_inspect,")
                .append("IFNULL(COUNT(`status` = 6 or null),0) actual_inspect,")
                .append("IFNULL(COUNT(`status` = 7 or null),0) not_inspect,")
                .append("IFNULL(COUNT(`status` = 8 or null),0) task_type,")
                .append("IFNULL(COUNT(`status` = 9 or null),0) task_create_type,")
                .append("IFNULL(COUNT(`status` = 10 or null),0) user_type,")
                .append("IFNULL(COUNT(`status` = 2 or null),0) user_status,")
                .append("IFNULL(SUM(confirm_timing),0) confirm_timing,")
                .append("IFNULL(SUM(transfer_timing),0) transfer_timing,")
                .append("IFNULL(SUM(timing),0) timing,")
                .append("IFNULL(sum(if(`status`=5||`status`=6,wage_hour,0)),0) wage_hour,")
                .append("IFNULL(SUM(if(`status`=5||`status`=6,point,0)),0) point,")
                .append("IFNULL(SUM(if(`status`=5||`status`=6,salary,0)),0) salary");
        if (userName != null) {
            if (userName.equals("")) {
                sql.append(",COUNT(task_accept_user !='' or null) group_order,")
                        .append("COUNT(task_distributi_user !=''  or null) task_order");
            } else {
                sql.append(",COUNT(task_accept_user LIKE '%" + userName + "%' or null) group_order,")
                        .append("COUNT(task_distributi_user LIKE '%" + userName + "%' or null) task_order");
            }
        }

        return sql;
    }


    @ApiOperation(value = "生成人效报表")
    @ResponseBody
    @RequestMapping(value = "/setUserEfficiencyReport", method = RequestMethod.POST)
    public BaseOutput<String> setUserEfficiencyReport(HttpServletRequest request, @RequestBody TaskEfficiencyInput input) {
        User user = UserUtils.getUser(request);
        log.info("【setUserEfficiencyReport】" + user.getName() + JSONObject.toJSONString(input));

        Long districtId = input.getDistrictId();
        if (StringUtil.isNull(districtId)) {
            return ResultResponseBeanUtil.fail("请选择站点");
        }
        {
            QueryWrapper<EfficiencyRecording> wrapperER = WrapperUtils.getWrapper(false);
            if (input.getCreateTimeStart() == null) {
                return ResultResponseBeanUtil.fail("设置开始时间");
            }
            if (input.getCreateTimeEnd() == null) {
                return ResultResponseBeanUtil.fail("设置结束时间");
            }
            wrapperER.eq("statistics_end_time", input.getCreateTimeStart());
            wrapperER.eq("statistics_start_time", input.getCreateTimeEnd());
            wrapperER.eq("district_id", input.getDistrictId());
            int checkcount = efficiencyRecordingService.count(wrapperER);
            if (checkcount > 0) {
                return ResultResponseBeanUtil.fail("报表不能重复生成，请在人效报表中查看");
            }
        }

        Date date = new Date();
        //人员查询
        QueryWrapper<User> userwrapper = WrapperUtils.getWrapper(false);
        Boolean userFlag = false;

        if (input.getUserName() != null && !input.getUserName().trim().equals("")) {
            userwrapper.like("name", input.getUserName());
            userFlag = true;
        }
        if (input.getMobile() != null && !input.getMobile().trim().equals("")) {
            userwrapper.like("mobile", input.getMobile());
            userFlag = true;

        }
        if (input.getAppRole() != null) {
            userwrapper.eq("approle", input.getAppRole());
            userFlag = true;

        }
        Map<String, User> userNameMap = new HashMap();
        Set<Long> conditionUserIds = new HashSet<>();
        if (userFlag) {
            List<User> userlist = userService.list(userwrapper);
            if (userlist.isEmpty()) {
                return ResultResponseBeanUtil.fail("未查询到人员");
            }
            for (User curuser : userlist) {
                userNameMap.put(curuser.getName(), curuser);
                conditionUserIds.add(curuser.getId());
            }
        }

        EfficiencyRecording efficiencyRecording = new EfficiencyRecording();

        QueryWrapper<TaskInfo> wrapperTask = WrapperUtils.getWrapper(true);
        wrapperTask.eq("district_id", input.getDistrictId());
        QueryWrapper<TaskUser> wrapperTU = WrapperUtils.getWrapper(true);
        wrapperTU.gt("user_id", 0);
        wrapperTU.eq("district_id", input.getDistrictId());
        wrapperTU.groupBy("user_id");
        wrapperTU.select("user_id,user_name,mobile,sum(point) point,sum(salary) salary");

        if (userFlag && !conditionUserIds.isEmpty()) {
            wrapperTU.in("user_id", conditionUserIds);
        }
        if (input.getCreateTimeStart() != null) {
            wrapperTU.ge("create_time", input.getCreateTimeStart());
            wrapperTask.ge("create_time", input.getCreateTimeStart());
        }
        if (input.getCreateTimeEnd() != null) {
            wrapperTU.le("create_time", input.getCreateTimeEnd());
            wrapperTask.le("create_time", input.getCreateTimeEnd());
        }
        setQueryByStatus(input.getStatus(), wrapperTask);
        wrapperTask.and(wrapperq -> wrapperq.notIn("task_accept_user", "").or().notIn("task_distributi_user", ""));

        int taskTotalCount = taskInfoService.count(wrapperTask);

        District district = districtService.getById(input.getDistrictId());
        if (district == null) {
            return ResultResponseBeanUtil.fail("站点异常");
        }
        {
            efficiencyRecording.setAgentId(district.getAgentId());
            efficiencyRecording.setPropertyId(district.getPropertyId());
            efficiencyRecording.setDistrictId(district.getId());
            efficiencyRecording.setDistrictName(district.getDistrictName());
            efficiencyRecording.setCreateId(user.getId());
            efficiencyRecording.setCreateName(user.getCreateName());
            efficiencyRecording.setCreateTime(date);
            efficiencyRecording.setStatisticsStartTime(DateToConverter.parseDate(input.getCreateTimeStart(), 5));
            efficiencyRecording.setStatisticsEndTime(DateToConverter.parseDate(input.getCreateTimeEnd(), 5));
//            efficiencyRecording.set
        }

        List<TaskUser> userCountlist = taskUserService.list(wrapperTU);
        List<EfficiencyDetail> outList = new ArrayList<>();
        String districtName = redisService5.get("name:District:" + input.getDistrictId());
        efficiencyRecording.setUserTotal(new BigDecimal(userCountlist.size()).doubleValue());
        efficiencyRecordingService.save(efficiencyRecording);
        Double recordingPoint = 0.0;
        Double recordingwageHour = 0.0;
        Double recordingSalary = 0.0;


        for (TaskUser taskUser : userCountlist) {
            EfficiencyDetail efficiencyDetail = new EfficiencyDetail();
            efficiencyDetail.setRecordingId(efficiencyRecording.getId());
            efficiencyDetail.setUserId(taskUser.getUserId());
            efficiencyDetail.setUserName(taskUser.getUserName());
            efficiencyDetail.setMobile(taskUser.getMobile());
            User curUser = userService.getById(taskUser.getUserId());
            if (curUser != null) {
                efficiencyDetail.setAppRole(curUser.getApprole());

            }

            efficiencyDetail.setDistrictId(taskUser.getDistrictId());
            efficiencyDetail.setDistrictName(districtName);

            QueryWrapper<TaskInfo> wrapper = WrapperUtils.getWrapper(false);
            QueryWrapper<TaskInfo> wrapper2 = WrapperUtils.getWrapper(false);
            if (input.getCreateTimeStart() != null) {
                wrapper.ge("create_time", input.getCreateTimeStart());
                wrapper2.ge("create_time", input.getCreateTimeStart());
            }
            if (input.getCreateTimeEnd() != null) {
                wrapper.le("create_time", input.getCreateTimeEnd());
                wrapper2.le("create_time", input.getCreateTimeEnd());
            }
            wrapper.eq("district_id", input.getDistrictId());
            wrapper2.eq("district_id", input.getDistrictId());
            setQueryByStatus(input.getStatus(), wrapper);
            setQueryByStatus(input.getStatus(), wrapper2);
            wrapper2.and(wrapperq -> wrapperq.like("task_accept_user", taskUser.getUserName()).or().like("task_distributi_user", taskUser.getUserName()));
            wrapper.like("task_accept_user", taskUser.getUserName());

            StringBuilder sql = getStringBuilder(null);
            wrapper.select(sql.toString());
            wrapper2.select(sql.toString());
            TaskInfo curtask = taskInfoService.getOne(wrapper);
            TaskInfo curtask2 = taskInfoService.getOne(wrapper2);
            Integer totalCount = curtask.getPoolOrder() != null ? curtask.getPoolOrder() : 0;
            efficiencyDetail.setAcceptCount(curtask.getPoolOrder() != null ? curtask.getPoolOrder() : 0);

            efficiencyDetail.setDistributiCount(curtask2.getPoolOrder() != null ? curtask2.getPoolOrder() : 0);
            efficiencyDetail.setConfirmTimingTotal(curtask.getConfirmTiming().doubleValue());
            efficiencyDetail.setTransferTimingTotal(curtask.getTransferTiming().doubleValue());
            efficiencyDetail.setWorkTimingTotal(curtask.getTiming().doubleValue());
            efficiencyDetail.setWageHourTotal(curtask.getWageHour());
            efficiencyDetail.setPointTotal(taskUser.getPoint() != null ? taskUser.getPoint() : 0.0);
            efficiencyDetail.setOnTimeCount(curtask.getShouldInspect());
            efficiencyDetail.setTimeOutCount(curtask.getActualInspect());
            efficiencyDetail.setSalaryTotal(taskUser.getSalary() != null ? taskUser.getSalary() : 0.0);
            efficiencyDetail.setStatisticsEndTime(efficiencyRecording.getStatisticsEndTime());
            efficiencyDetail.setStatisticsStartTime(efficiencyRecording.getStatisticsStartTime());
            BigDecimal totalCountBD = new BigDecimal(totalCount);


            if (efficiencyDetail.getDistributiCount() > 0) {
                BigDecimal accpetPercent = new BigDecimal(efficiencyDetail.getAcceptCount() * 100).divide(new BigDecimal(efficiencyDetail.getDistributiCount()), 2, BigDecimal.ROUND_HALF_UP);
                efficiencyDetail.setAcceptPercent(accpetPercent.doubleValue());
            } else {
                efficiencyDetail.setAcceptPercent(0.0);

            }

            if (totalCount != null && totalCount > 0) {


                BigDecimal comfirmAve = new BigDecimal(efficiencyDetail.getConfirmTimingTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal transferAve = new BigDecimal(efficiencyDetail.getTransferTimingTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal torkTimingAve = new BigDecimal(efficiencyDetail.getWorkTimingTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal wageHourAve = new BigDecimal(efficiencyDetail.getWageHourTotal()).divide(totalCountBD, 2, BigDecimal.ROUND_HALF_UP);
                efficiencyDetail.setConfirmTimingAve(comfirmAve.doubleValue());
                efficiencyDetail.setTransferTimingAve(transferAve.doubleValue());
                efficiencyDetail.setWorkTimingAve(torkTimingAve.doubleValue());
                efficiencyDetail.setWageHourAve(wageHourAve.doubleValue());
                efficiencyDetail.setUnfinishCount(curtask.getNotInspect() + curtask.getTaskType() + curtask.getTaskCreateType());

                if (efficiencyDetail.getAcceptCount() > 0) {
                    BigDecimal finishPercent = new BigDecimal(100 * (efficiencyDetail.getOnTimeCount() + efficiencyDetail.getTimeOutCount())).divide(BigDecimal.valueOf(efficiencyDetail.getAcceptCount()), 2, BigDecimal.ROUND_HALF_UP);
                    efficiencyDetail.setFinishPercent(finishPercent.doubleValue());
                } else {
                    efficiencyDetail.setFinishPercent(0.0);
                }

            }
            recordingPoint += efficiencyDetail.getPointTotal();
            recordingwageHour += efficiencyDetail.getWageHourTotal();
            recordingSalary += efficiencyDetail.getSalaryTotal();
            outList.add(efficiencyDetail);
        }

        efficiencyDetailService.saveBatch(outList);
        efficiencyRecording.setPointTotal(recordingPoint);
        efficiencyRecording.setWageHourTotal(recordingwageHour);
        efficiencyRecording.setSalaryTotal(recordingSalary);
        efficiencyRecordingService.updateById(efficiencyRecording);
//        output.setPage(userpageList.getCurrent());
//        output.setRecords(userpageList.getTotal());
//        output.setTotal(userpageList.getPages());
//        output.setRows(outList);

        return ResultResponseBeanUtil.success("操作成功");
    }


    @ApiOperation(value = "站点人效查询")
    @ResponseBody
    @RequestMapping(value = "/districtEfficiencyList", method = RequestMethod.POST)
    public BaseOutput<List<EfficiencyStatisticsOutput>> districtEfficiencyList(HttpServletRequest request, @RequestBody EfficiencyDistrictInput input) {
        User user = UserUtils.getUser(request);
        log.info("【districtEfficiencyList】" + user.getName() + JSONObject.toJSONString(input));


        Long districtId = input.getDistrictId();
        if (StringUtil.isNull(districtId)) {
            return ResultResponseBeanUtil.fail("请选择站点");
        }
        QueryWrapper<TaskInfo> wrapperTask = WrapperUtils.getWrapper(true);
        wrapperTask.eq("district_id", input.getDistrictId());
        if (input.getCreateTimeStart() != null) {
            wrapperTask.ge("create_time", input.getCreateTimeStart());
        }
        if (input.getCreateTimeEnd() != null) {
            wrapperTask.le("create_time", input.getCreateTimeEnd());
        }
        wrapperTask.groupBy("district_id");
        setQueryByStatus(input.getStatus(), wrapperTask);
        wrapperTask.and(wrapperq -> wrapperq.notIn("task_accept_user", "").or().notIn("task_distributi_user", ""));

        StringBuilder sql = getStringBuilder("");

        wrapperTask.select(sql.toString());
        List<TaskInfo> list = taskInfoService.list(wrapperTask);
        List<EfficiencyStatisticsOutput> outList = new ArrayList<>();
        for (TaskInfo taskInfo : list) {
            EfficiencyStatisticsOutput efficiencyStatisticsOutput = new EfficiencyStatisticsOutput();
            efficiencyStatisticsOutput.setDistrictId(input.getDistrictId());
            String districtName = redisService5.get("name:District:" + input.getDistrictId());
            efficiencyStatisticsOutput.setDistrictName(districtName);
            efficiencyStatisticsOutput.setTaskCount(taskInfo.getPoolOrder());
            efficiencyStatisticsOutput.setTaskAcceptCount(taskInfo.getGroupOrder());
            efficiencyStatisticsOutput.setTaskDistributiCount(taskInfo.getTaskOrder());
            if (efficiencyStatisticsOutput.getTaskDistributiCount() > 0) {
                BigDecimal accpetPercent = new BigDecimal(efficiencyStatisticsOutput.getTaskAcceptCount() * 100).divide(new BigDecimal(efficiencyStatisticsOutput.getTaskDistributiCount()), 2, BigDecimal.ROUND_HALF_UP);
                efficiencyStatisticsOutput.setAcceptPercent(accpetPercent.doubleValue());
            } else {
                efficiencyStatisticsOutput.setAcceptPercent(0.0);

            }

            efficiencyStatisticsOutput.setOnTimeCount(taskInfo.getShouldInspect());
            efficiencyStatisticsOutput.setTimeOutCount(taskInfo.getActualInspect());
            efficiencyStatisticsOutput.setUnfinishCount(taskInfo.getNotInspect() + taskInfo.getTaskType() + taskInfo.getTaskCreateType());
            Integer total = efficiencyStatisticsOutput.getTaskCount();

            if (total > 0) {
                BigDecimal finishPercent = new BigDecimal(100 * (efficiencyStatisticsOutput.getOnTimeCount() + efficiencyStatisticsOutput.getTimeOutCount())).divide(BigDecimal.valueOf(total), 2, BigDecimal.ROUND_HALF_UP);
                efficiencyStatisticsOutput.setFinishPercent(finishPercent.doubleValue());
            } else {
                efficiencyStatisticsOutput.setFinishPercent(0.0);
            }


            efficiencyStatisticsOutput.setConfirmTimeingTotal(taskInfo.getConfirmTiming().doubleValue());
            efficiencyStatisticsOutput.setTransferTimingTotal(taskInfo.getTransferTiming().doubleValue());
            efficiencyStatisticsOutput.setTimingTotal(taskInfo.getTiming().doubleValue());
            efficiencyStatisticsOutput.setWageHourTotal(taskInfo.getWageHour());
            efficiencyStatisticsOutput.setPointTotal(taskInfo.getPoint());
            efficiencyStatisticsOutput.setSalaryTotal(taskInfo.getSalary());
            if (taskInfo.getPoolOrder() != null && taskInfo.getPoolOrder() > 0) {

                BigDecimal taskTotal = new BigDecimal(taskInfo.getPoolOrder());

                BigDecimal comfirmAve = new BigDecimal(efficiencyStatisticsOutput.getConfirmTimeingTotal()).divide(taskTotal, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal transferAve = new BigDecimal(efficiencyStatisticsOutput.getTransferTimingTotal()).divide(taskTotal, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal torkTimingAve = new BigDecimal(efficiencyStatisticsOutput.getTimingTotal()).divide(taskTotal, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal wageHourAve = new BigDecimal(efficiencyStatisticsOutput.getWageHourTotal()).divide(taskTotal, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal pointAve = new BigDecimal(efficiencyStatisticsOutput.getPointTotal()).divide(taskTotal, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal salaryAve = new BigDecimal(efficiencyStatisticsOutput.getSalaryTotal()).divide(taskTotal, 2, BigDecimal.ROUND_HALF_UP);
                efficiencyStatisticsOutput.setConfirmTimeAve(comfirmAve.doubleValue());
                efficiencyStatisticsOutput.setTransferTimingAve(transferAve.doubleValue());
                efficiencyStatisticsOutput.setTimingAve(torkTimingAve.doubleValue());
                efficiencyStatisticsOutput.setWageHourAve(wageHourAve.doubleValue());
                efficiencyStatisticsOutput.setPointAve(pointAve.doubleValue());
                efficiencyStatisticsOutput.setSalaryAve(salaryAve.doubleValue());
            }

            QueryWrapper<TaskUser> wrapperUser = WrapperUtils.getWrapper(true);
            wrapperUser.eq("district_id", input.getDistrictId());
            if (input.getCreateTimeStart() != null) {
                wrapperUser.ge("create_time", input.getCreateTimeStart());
            }
            if (input.getCreateTimeEnd() != null) {
                wrapperUser.le("create_time", input.getCreateTimeEnd());
            }
            wrapperUser.gt("user_id", 0);
            wrapperUser.groupBy("user_id");
            List<TaskUser> userlist = taskUserService.list(wrapperUser);
            efficiencyStatisticsOutput.setUserCount(userlist.size());
            outList.add(efficiencyStatisticsOutput);
        }


//        PageOutput<EfficienctTaskInfoOutput> output = new PageOutput<EfficienctTaskInfoOutput>();
//        output.setPage(1L);
//        output.setRecords(1L);
//        output.setTotal(1L);
//        output.setRows(outList);
        return ResultResponseBeanUtil.success(outList);
    }


    @ApiOperation(value = "站点人效导出")
    @PostMapping("/districtEfficiencyExport")
    public void districtEfficiencyExport(HttpServletRequest request, HttpServletResponse response, @RequestBody EfficiencyDistrictInput input) throws IOException {


        User user = UserUtils.getUser(request);
        log.info("【districtEfficiencyExport】" + user.getName() + JSONObject.toJSONString(input));


        Long districtId = input.getDistrictId();

        QueryWrapper<TaskInfo> wrapperTask = WrapperUtils.getWrapper(true);
        wrapperTask.eq("district_id", input.getDistrictId());
        if (input.getCreateTimeStart() != null) {
            wrapperTask.ge("create_time", input.getCreateTimeStart());
        }
        if (input.getCreateTimeEnd() != null) {
            wrapperTask.le("create_time", input.getCreateTimeEnd());
        }
        setQueryByStatus(input.getStatus(), wrapperTask);
        wrapperTask.and(wrapperq -> wrapperq.notIn("task_accept_user", "").or().notIn("task_distributi_user", ""));

        wrapperTask.groupBy("district_id");
        StringBuilder sql = getStringBuilder("");

        wrapperTask.select(sql.toString());
        List<TaskInfo> list = taskInfoService.list(wrapperTask);
        List<EfficiencyDistrictTaskExport> outList = new ArrayList<>();
        for (TaskInfo taskInfo : list) {
            EfficiencyDistrictTaskExport efficiencyStatisticsOutput = new EfficiencyDistrictTaskExport();
//            efficiencyStatisticsOutput.setDistrictId(input.getDistrictId());
            String districtName = redisService5.get("name:District:" + input.getDistrictId());
            efficiencyStatisticsOutput.setDistrictName(districtName);
            efficiencyStatisticsOutput.setTaskCount(taskInfo.getPoolOrder());
            efficiencyStatisticsOutput.setTaskAcceptCount(taskInfo.getGroupOrder());
            efficiencyStatisticsOutput.setTaskDistributiCount(taskInfo.getTaskOrder());
            if (efficiencyStatisticsOutput.getTaskDistributiCount() > 0) {
                BigDecimal accpetPercent = new BigDecimal(efficiencyStatisticsOutput.getTaskAcceptCount() * 100).divide(new BigDecimal(efficiencyStatisticsOutput.getTaskDistributiCount()), 2, BigDecimal.ROUND_HALF_UP);
                efficiencyStatisticsOutput.setAcceptPercent(accpetPercent.doubleValue());
            } else {
                efficiencyStatisticsOutput.setAcceptPercent(0.0);

            }

            efficiencyStatisticsOutput.setOnTimeCount(taskInfo.getShouldInspect());
            efficiencyStatisticsOutput.setTimeOutCount(taskInfo.getActualInspect());
            efficiencyStatisticsOutput.setUnfinishCount(taskInfo.getNotInspect() + taskInfo.getTaskType() + taskInfo.getTaskCreateType());
            if (efficiencyStatisticsOutput.getTaskCount() > 0) {
                BigDecimal finishPercent = new BigDecimal(100 * (efficiencyStatisticsOutput.getOnTimeCount() + efficiencyStatisticsOutput.getTimeOutCount())).divide(BigDecimal.valueOf(efficiencyStatisticsOutput.getTaskCount()), 2, BigDecimal.ROUND_HALF_UP);
                efficiencyStatisticsOutput.setFinishPercent(finishPercent.doubleValue());
            } else {
                efficiencyStatisticsOutput.setFinishPercent(0.0);

            }


            efficiencyStatisticsOutput.setConfirmTimeingTotal(taskInfo.getConfirmTiming().doubleValue());
            efficiencyStatisticsOutput.setTransferTimingTotal(taskInfo.getTransferTiming().doubleValue());
            efficiencyStatisticsOutput.setTimingTotal(taskInfo.getTiming().doubleValue());
            efficiencyStatisticsOutput.setWageHourTotal(taskInfo.getWageHour());
            efficiencyStatisticsOutput.setPointTotal(taskInfo.getPoint());
            efficiencyStatisticsOutput.setSalaryTotal(taskInfo.getSalary());
            if (taskInfo.getPoolOrder() != null && taskInfo.getPoolOrder() > 0) {

                BigDecimal taskTotal = new BigDecimal(taskInfo.getPoolOrder());

                BigDecimal comfirmAve = new BigDecimal(efficiencyStatisticsOutput.getConfirmTimeingTotal()).divide(taskTotal, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal transferAve = new BigDecimal(efficiencyStatisticsOutput.getTransferTimingTotal()).divide(taskTotal, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal torkTimingAve = new BigDecimal(efficiencyStatisticsOutput.getTimingTotal()).divide(taskTotal, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal wageHourAve = new BigDecimal(efficiencyStatisticsOutput.getWageHourTotal()).divide(taskTotal, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal pointAve = new BigDecimal(efficiencyStatisticsOutput.getPointTotal()).divide(taskTotal, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal salaryAve = new BigDecimal(efficiencyStatisticsOutput.getSalaryTotal()).divide(taskTotal, 2, BigDecimal.ROUND_HALF_UP);
                efficiencyStatisticsOutput.setConfirmTimeAve(comfirmAve.doubleValue());
                efficiencyStatisticsOutput.setTransferTimingAve(transferAve.doubleValue());
                efficiencyStatisticsOutput.setTimingAve(torkTimingAve.doubleValue());
                efficiencyStatisticsOutput.setWageHourAve(wageHourAve.doubleValue());
                efficiencyStatisticsOutput.setPointAve(pointAve.doubleValue());
                efficiencyStatisticsOutput.setSalaryAve(salaryAve.doubleValue());
            }

            QueryWrapper<TaskUser> wrapperUser = WrapperUtils.getWrapper(true);
            wrapperUser.eq("district_id", input.getDistrictId());
            if (input.getCreateTimeStart() != null) {
                wrapperUser.ge("create_time", input.getCreateTimeStart());
            }
            if (input.getCreateTimeEnd() != null) {
                wrapperUser.le("create_time", input.getCreateTimeEnd());
            }
            wrapperUser.notIn("user_id", 0);
            wrapperUser.groupBy("user_id");
            List<TaskUser> userlist = taskUserService.list(wrapperUser);
            efficiencyStatisticsOutput.setUserCount(userlist.size());
            outList.add(efficiencyStatisticsOutput);
        }


        String formattedDate = DateToConverter.getFormattedDate(new Date(), 10);
//        List<FieldWebpageAuthority> fieldList = fieldWebpageAuthorityService.getExportFieldList(input.getWebId(), user.getAuthorityId());
//        String[] exportColumns = WrapperUtils.entityToColumns(fieldList);
        ExcelUtil<EfficiencyDistrictTaskExport> util = new ExcelUtil<EfficiencyDistrictTaskExport>(EfficiencyDistrictTaskExport.class);
        util.exportExcel(response, outList, "任务人效统计" + formattedDate);
    }


    @ApiOperation(value = "人效报表查询")
    @ResponseBody
    @RequestMapping(value = "/efficiencyRecordingPageList", method = RequestMethod.POST)
    public BaseOutput<PageOutput<EfficiencyRecordingOutput>> efficiencyRecordingPageList(HttpServletRequest request, @RequestBody TaskInfoPageInput input) {
        User user = UserUtils.getUser(request);
        log.info("【efficiencyRecordingPageList】" + user.getName() + JSONObject.toJSONString(input));


        PageOutput<EfficiencyRecordingOutput> output = new PageOutput<EfficiencyRecordingOutput>();
        Long districtId = input.getDistrictId();
        if (StringUtil.isNull(districtId)) {
            return ResultResponseBeanUtil.fail("请选择站点");
        }
        Page<EfficiencyRecording> pageList = new Page<EfficiencyRecording>(input.getPage(), input.getRows());
        QueryWrapper<EfficiencyRecording> wrapper = WrapperUtils.getWrapper(false);
        wrapper.eq("district_id", input.getDistrictId());

        if (input.getCreateTimeStart() != null) {
            wrapper.ge("create_time", input.getCreateTimeStart());
        }
        if (input.getCreateTimeEnd() != null) {
            wrapper.le("create_time", input.getCreateTimeEnd());
        }
        wrapper.orderByDesc("id");
        List<EfficiencyRecording> list = efficiencyRecordingService.page(pageList, wrapper).getRecords();

        List<EfficiencyRecordingOutput> efficiencyRecordingOutputs = new ArrayList<>();
        for (EfficiencyRecording efficiencyRecording : list) {
            EfficiencyRecordingOutput efficiencyRecordingOutput = new EfficiencyRecordingOutput();
            BeanUtils.copyProperties(efficiencyRecording, efficiencyRecordingOutput);
            efficiencyRecordingOutput.setCreateDateStr(DateToConverter.getDateString(efficiencyRecording.getStatisticsStartTime()) + "~" + DateToConverter.getDateString(efficiencyRecording.getStatisticsEndTime()));
            efficiencyRecordingOutputs.add(efficiencyRecordingOutput);
        }


        output.setPage(pageList.getCurrent());
        output.setRecords(pageList.getTotal());
        output.setTotal(pageList.getPages());
        output.setRows(efficiencyRecordingOutputs);
        return ResultResponseBeanUtil.success(output);
    }


    @ApiOperation(value = "人效报表导出")
    @PostMapping("/efficiencyRecordingExport")
    public void efficiencyRecordingExport(HttpServletRequest request, HttpServletResponse response, @RequestBody BaseInput input) throws IOException {
        User user = UserUtils.getUser(request);
        log.info("【efficiencyRecordingExport】" + user.getName() + JSONObject.toJSONString(input));


        Long districtId = input.getDistrictId();
        if (districtId == null || districtId == 0) {
            return;
        }

        QueryWrapper<EfficiencyRecording> wrapper = WrapperUtils.getWrapper(false);
        wrapper.eq("district_id", input.getDistrictId());
        if (input.getCreateTimeStart() != null) {
            wrapper.ge("create_time", input.getCreateTimeStart());
        }
        if (input.getCreateTimeEnd() != null) {
            wrapper.le("create_time", input.getCreateTimeEnd());
        }
        List<EfficiencyRecording> list = efficiencyRecordingService.list(wrapper);

        List<EfficiencyRecordingExport> efficiencyRecordingOutputs = new ArrayList<>();
        for (EfficiencyRecording efficiencyRecording : list) {
            EfficiencyRecordingExport efficiencyRecordingOutput = new EfficiencyRecordingExport();
            BeanUtils.copyProperties(efficiencyRecording, efficiencyRecordingOutput);
            efficiencyRecordingOutput.setCreateDateStr(DateToConverter.getDateString(efficiencyRecording.getStatisticsStartTime()) + "-" + DateToConverter.getDateString(efficiencyRecording.getStatisticsEndTime()));
            efficiencyRecordingOutputs.add(efficiencyRecordingOutput);
        }
        String formattedDate = DateToConverter.getFormattedDate(new Date(), 10);
//        List<FieldWebpageAuthority> fieldList = fieldWebpageAuthorityService.getExportFieldList(input.getWebId(), user.getAuthorityId());
//        String[] exportColumns = WrapperUtils.entityToColumns(fieldList);
        ExcelUtil<EfficiencyRecordingExport> util = new ExcelUtil<EfficiencyRecordingExport>(EfficiencyRecordingExport.class);
        util.exportExcel(response, efficiencyRecordingOutputs, "人效报表" + formattedDate);
    }


    @ApiOperation(value = "人效报表详情查询")
    @ResponseBody
    @RequestMapping(value = "/efficiencyDetailList", method = RequestMethod.POST)
    public BaseOutput<List<EfficiencyDetail>> efficiencyDetailList(HttpServletRequest request, @RequestBody EfficiencyDetailInput input) {
        User user = UserUtils.getUser(request);
        log.info("【efficiencyDetailList】" + user.getName() + JSONObject.toJSONString(input));


        Long recordingId = input.getRecordingId();
        if (StringUtil.isNull(recordingId)) {
            return ResultResponseBeanUtil.fail("请选择报表记录");
        }
        QueryWrapper<EfficiencyDetail> wrapper = WrapperUtils.getWrapper(false);
        wrapper = WrapperUtils.beanToWrapper(wrapper, input);
        List<EfficiencyDetail> list = efficiencyDetailService.list(wrapper);

        List<EfficiencyDetailOutput> efficiencyRecordingOutputs = new ArrayList<>();
        for (EfficiencyDetail efficiencyDetail : list) {
            EfficiencyDetailOutput efficiencyRecordingOutput = new EfficiencyDetailOutput();
            BeanUtils.copyProperties(efficiencyDetail, efficiencyRecordingOutput);
            efficiencyRecordingOutput.setCreateDateStr(DateToConverter.getDateString(efficiencyDetail.getStatisticsStartTime()) + "-" + DateToConverter.getDateString(efficiencyDetail.getStatisticsEndTime()));
            efficiencyRecordingOutputs.add(efficiencyRecordingOutput);
        }

        return ResultResponseBeanUtil.success(list);
    }


    @ApiOperation(value = "人效报表详情导出")
    @PostMapping("/efficiencyDetailExport")
    public void efficiencyDetailExport(HttpServletRequest request, HttpServletResponse response, @RequestBody EfficiencyDetailInput input) throws IOException {
        User user = UserUtils.getUser(request);
        log.info("【efficiencyDetailExport】" + user.getName() + JSONObject.toJSONString(input));


        Long recordingId = input.getRecordingId();
        if (recordingId == null || recordingId == 0) {
            return;
        }
        QueryWrapper<EfficiencyDetail> wrapper = WrapperUtils.getWrapper(false);

        wrapper = WrapperUtils.beanToWrapper(wrapper, input);
        List<EfficiencyDetail> list = efficiencyDetailService.list(wrapper);

        List<EfficiencyDetailExport> efficiencyDetailOutputs = new ArrayList<>();
        for (EfficiencyDetail efficiencyDetail : list) {
            EfficiencyDetailExport efficiencyDetailOutput = new EfficiencyDetailExport();
            BeanUtils.copyProperties(efficiencyDetail, efficiencyDetailOutput);
            efficiencyDetailOutput.setCreateDateStr(DateToConverter.getDateString(efficiencyDetail.getStatisticsStartTime()) + "-" + DateToConverter.getDateString(efficiencyDetail.getStatisticsEndTime()));
            efficiencyDetailOutputs.add(efficiencyDetailOutput);
        }
        String formattedDate = DateToConverter.getFormattedDate(new Date(), 10);
//        List<FieldWebpageAuthority> fieldList = fieldWebpageAuthorityService.getExportFieldList(input.getWebId(), user.getAuthorityId());
//        String[] exportColumns = WrapperUtils.entityToColumns(fieldList);
        ExcelUtil<EfficiencyDetailExport> util = new ExcelUtil<EfficiencyDetailExport>(EfficiencyDetailExport.class);
        util.exportExcel(response, efficiencyDetailOutputs, "人效报表详情" + formattedDate);
    }

    @ApiOperation(value = "积分设置")
    @ResponseBody
    @RequestMapping(value = "/setEfficiencyDetailPoint", method = RequestMethod.POST)
    public BaseOutput<String> setEfficiencyDetailPoint(HttpServletRequest request, @RequestBody EfficiencyDetailPointInput input) {
        User user = UserUtils.getUser(request);
        log.info("【setEfficiencyDetailPoint】" + user.getName() + JSONObject.toJSONString(input));
        List<Long> efficiencyDetailIds = input.getEfficiencyDetailIds();
        if (StringUtil.isNull(efficiencyDetailIds)) {
            return ResultResponseBeanUtil.fail("请选择报表详情记录");
        }
        if (input.getType() == null) {
            return ResultResponseBeanUtil.fail("请选择核对方式");
        }
        if (input.getPoint() == null || input.getPoint() < 0) {
            return ResultResponseBeanUtil.fail("请输入积分");
        }
        QueryWrapper<EfficiencyDetail> wrapper = WrapperUtils.getWrapper(false);
        wrapper.in("id", efficiencyDetailIds);
        List<EfficiencyDetail> list = efficiencyDetailService.list(wrapper);
        for (EfficiencyDetail efficiencyDetail : list) {
            if (input.getType() == 0) {
                efficiencyDetail.setResultPoint(NumberUtil.add(efficiencyDetail.getPointTotal(), input.getPoint()));
                efficiencyDetail.setCheckPoint("+" + input.getPoint());

            } else if (input.getType() == 1) {
                efficiencyDetail.setResultPoint(NumberUtil.sub(efficiencyDetail.getPointTotal(), input.getPoint()));
                efficiencyDetail.setCheckPoint("-" + input.getPoint());
            }
            efficiencyDetailService.updateById(efficiencyDetail);
        }
        return ResultResponseBeanUtil.success("操作成功");
    }
}
