package com.repair.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jcraft.jsch.*;
import com.repair.api.RepairRecordControllerAPI;
import com.repair.common.utils.*;
import com.repair.config.WxOpenidConfig;
import com.repair.model.enumModel.*;
import com.repair.model.pojo.*;
import com.repair.model.request.*;
import com.repair.model.result.*;
import com.repair.model.vo.*;
import com.repair.services.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static com.repair.common.utils.StrUtils.getUUIDBits;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-07-24
 */
@RestController
public class RepairRecordController implements RepairRecordControllerAPI {

    @Autowired
    private RepairUserService repairUserService;
    @Autowired
    private RepairRecordService repairRecordService;
    @Autowired
    private RepairPayRecordService repairPayRecordService;
    @Autowired
    private RepairReceivingService repairReceivingService;
    @Autowired
    private RepairConsumablesService repairConsumablesService;
    @Autowired
    private RepairLeaveMessageService repairLeaveMessageService;
    @Autowired
    private RepairShiftSettingsService repairShiftSettingsService;
    @Autowired
    private RepairTransferRecordService repairTransferRecordService;
    @Autowired
    private RepairDispatchRecordService repairDispatchRecordService;
    @Autowired
    private RepairSystemMessagesService repairSystemMessagesService;
    @Autowired
    private RepairCollaborateRecordService repairCollaborateRecordService;
    @Autowired
    private RepairArticleTypeService repairArticleTypeService;
    @Autowired
    private RepairTrackRecordService repairTrackRecordService;
    @Autowired
    private RepairConsumeService repairConsumeService;
    @Autowired
    private RepairAdminService repairAdminService;
    @Autowired
    private RepairEvaluateService repairEvaluateService;
    @Autowired
    private RepairSchoolService repairSchoolService;
    @Autowired
    private WeiXiaoUtil weiXiaoUtil;
    @Autowired
    private WxOpenidConfig wxOpenidConfig;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult pay(WxPayRequest wpr, BindingResult bindingResult) throws Exception {
        System.out.println("进入微信支付====");
        System.out.println("微信支付参数====：" + JSON.toJSON(wpr));
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        RepairRecord rr = repairRecordService.getRepairById(wpr.getRecordId());
        if (rr == null) {
            return CommonResult.fail("维修单已失效，支付失败");
        }

        if (!wpr.getTotalPrice().equals(rr.getPrice())) {
            return CommonResult.fail("支付金额与订单金额不一致");
        }

        //查看该订单是否支付过
        RepairPayRecord oldRpr = repairPayRecordService.getRepairPayByRecordId(wpr.getRecordId());
        if (oldRpr != null && oldRpr.getIsSuccess().equals(ePayStatu.Paysuccess.getValue())) {
            return CommonResult.fail("该订单已支付，请勿重复操作");
        }

        try {
            //返回0表示等于0，返回1表示大于0，返回-1表示小于0
            if (wpr.getTotalPrice().compareTo(BigDecimal.ZERO) < 0 || wpr.getTotalPrice().compareTo(BigDecimal.ZERO) == 0) {
                if (oldRpr == null) {
                    RepairPayRecord rpr = new RepairPayRecord();
                    rpr.setRecordId(wpr.getRecordId());
                    rpr.setUserId(rr.getUserId());
                    rpr.setPayNo(getUUIDBits(15));
                    rpr.setPayPrice(BigDecimal.ZERO);
                    rpr.setIsSuccess(ePayStatu.Paysuccess.getValue());
                    rpr.setRemark("支付金额为0，不用走微信支付");
                    int insertRpr = repairPayRecordService.insertRepairPayRecord(rpr);
                    if (insertRpr <= 0) {
                        throw new Exception("支付失败！");
                    }
                } else {
                    oldRpr.setRemark(oldRpr.getRemark() + "后续支付：支付金额为0，不用走微信支付");
                    oldRpr.setPayPrice(BigDecimal.ZERO);
                    oldRpr.setIsSuccess(ePayStatu.Paysuccess.getValue());
                    int updateRpr = repairPayRecordService.updateRepairPayRecord(oldRpr);
                    if (updateRpr <= 0) {
                        throw new Exception("支付失败！");
                    }
                }

                rr.setMaintenanceState(eRecordStatu.Maintenance.getValue());

                int updateRecord = repairRecordService.updateRepairRecord(rr);
                if (updateRecord <= 0) {
                    throw new Exception("支付失败！");
                }
            }
        } catch (Exception e) {
            throw new Exception("支付失败！");
        }

        RepairUser ru = repairUserService.queryUserByOpenid(wpr.getOpenid());
        if (ru == null) {
            return CommonResult.fail("用户信息不合法，支付失败");
        }

        if (!ru.getId().equals(rr.getUserId())) {
            return CommonResult.fail("无法为其他订单支付");
        }

        //微信支付以分为单位
        int totalfee = (wpr.getTotalPrice().multiply(new BigDecimal(100))).intValue();
        Map<String, String> params = new HashMap<String, String>();
        params.put("attach", "报修系统");//附加数据，在查询API和支付通知中原样返回，可作为自定义参数使用。
        params.put("appid", wxOpenidConfig.getXappid());//微信分配的小程序ID
        params.put("mch_id", wxOpenidConfig.getShappid());//微信支付分配的商户号
        String nonceStr = WxUtil.getWxNonceStr();
        params.put("nonce_str", nonceStr);//随机字符串，长度要求在32位以内。推荐随机数生成算法
        params.put("body", "南交-报修系统");//商品简单描述，该字段请按照规范传递，具体请见参数规定
        params.put("out_trade_no", rr.getRecordNo());//商户系统内部订单号，要求32个字符内，只能是数字、大小写字母_-|*@ ，且在同一个商户号下唯一。详见商户订单号
        params.put("total_fee", String.valueOf(totalfee));//支付金额
        params.put("spbill_create_ip", wxOpenidConfig.getIpconfig());//支持IPV4和IPV6两种格式的IP地址。调用微信支付API的机器IP：服务器IP地址
        params.put("notify_url", "https://chtech.ncjti.edu.cn/campusMaintenance/repair-api/repair/api/repairRecord/returnPay");//异步接收微信支付结果通知的回调地址，通知url必须为外网可访问的url，不能携带参数。
        params.put("trade_type", "JSAPI");//取值如下：JSAPI，NATIVE，APP等，说明详见参数规定
        params.put("openid", wpr.getOpenid());//trade_type=JSAPI时（即公众号支付），此参数必传，此参数为微信用户在商户对应appid下的唯一标识
        String Sign = WxUtil.getSign(params, wxOpenidConfig.getShsecret());//参数 + 商户密钥
        params.put("sign", Sign);
        String retXml = WxUtil.getRequestXml(params);
        String msg = HttpUtils.post("https://api.mch.weixin.qq.com/pay/unifiedorder", retXml);
        System.out.println(msg);

        // 支付失败
        if (msg.indexOf("FAIL") > -1) {
            return CommonResult.fail("支付异常");
        }

        JaxbUtil requestBinder = new JaxbUtil(TongYiReturn.class, JaxbUtil.CollectionWrapper.class);
        TongYiReturn to = requestBinder.fromXml(msg);

        // 支付成功
        if (to.getReturn_code().equals("SUCCESS") && to.getResult_code().equals("SUCCESS")) {
            EndPay pay = new EndPay();
            pay.setAppId(wxOpenidConfig.getXappid());
            pay.setSignType("MD5");
            pay.setTimeStamp(System.currentTimeMillis() / 1000 + "");
            pay.setPrepay_id(to.getPrepay_id());
            pay.setNonceStr(nonceStr);
            Map<String, String> requestMap = new HashMap<String, String>();
            requestMap.put("appId", pay.getAppId());
            requestMap.put("timeStamp", pay.getTimeStamp());
            requestMap.put("nonceStr", pay.getNonceStr());
            requestMap.put("package", "prepay_id=" + pay.getPrepay_id());
            requestMap.put("signType", "MD5");
            pay.setPaySign(WxUtil.getSign(requestMap, wxOpenidConfig.getShsecret()));

            return CommonResult.ok(pay);
        }

        return CommonResult.ok();
    }

    /**
     * 支付成功后回调,推送消息给商家
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void returnPay(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("进入支付回调====");
        try {
            BufferedReader reader = null;// BufferedReader 字符输入流
            reader = request.getReader();
            String line = "";
            String xmlString = null;
            StringBuilder inputString = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                inputString.append(line);
            }
            xmlString = inputString.toString();
            request.getReader().close();
            JaxbUtil requestBinder = new JaxbUtil(PayResult.class, PayResult.class);
            PayResult result = requestBinder.fromXml(xmlString);// Xml到Java，用fromXML()方法
            String recordNo = result.getOut_trade_no();
            System.out.println("支付成功回调，维修单号：" + recordNo);
            RepairRecord rr = repairRecordService.queryRepairByRecordno(recordNo);
            // 支付成功
            if (result.getResult_code().equals("SUCCESS") && rr != null) {
                RepairPayRecord rpr = repairPayRecordService.getRepairPayByRecordId(rr.getId());
                if (rpr == null) {
                    // 保存支付信息
                    rpr = new RepairPayRecord();
                    rpr.setRecordId(rr.getId());
                    rpr.setUserId(rr.getUserId());
                    rpr.setPayNo(result.getNonce_str());
                    rpr.setPayPrice(new BigDecimal(result.getTotal_fee() / 100));
                    rpr.setIsSuccess(ePayStatu.Paysuccess.getValue());
                    rpr.setRemark("微信支付支付成功");
                    int insertRpr = repairPayRecordService.insertRepairPayRecord(rpr);
                    if (insertRpr <= 0) {
                        String returnMsg = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[MESSAGE]]></return_msg></xml>";
                        response.getWriter().write(returnMsg);// 发送消息返回给微信
                        throw new Exception("支付回调失败！");
                    }
                } else if (rpr.getIsSuccess() != ePayStatu.Paysuccess.getValue()) {
                    rpr.setPayNo(result.getNonce_str());
                    rpr.setPayPrice(new BigDecimal(result.getTotal_fee() / 100));
                    rpr.setIsSuccess(ePayStatu.Paysuccess.getValue());
                    rpr.setRemark(rpr.getRemark() + "微信支付支付成功");
                    int updateRpr = repairPayRecordService.updateRepairPayRecord(rpr);
                    if (updateRpr <= 0) {
                        String returnMsg = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[MESSAGE]]></return_msg></xml>";
                        response.getWriter().write(returnMsg);// 发送消息返回给微信
                        throw new Exception("支付回调失败！");
                    }
                }

                rr.setMaintenanceState(eRecordStatu.Maintenance.getValue());

                int updateRecord = repairRecordService.updateRepairRecord(rr);
                if (updateRecord <= 0) {
                    String returnMsg = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[MESSAGE]]></return_msg></xml>";
                    response.getWriter().write(returnMsg);// 发送消息返回给微信
                    throw new Exception("支付回调失败！");
                }

                //添加跟踪记录
                RepairTrackRecord rtr = new RepairTrackRecord();
                rtr.setRecordId(rr.getId());
                rtr.setMaintenanceState(eRecordStatu.Maintenance.getValue());
                rtr.setContent("订单支付");
                int insertRtr = repairTrackRecordService.insertRepairTrackRecord(rtr);
                if (insertRtr <= 0) {
                    throw new Exception("支付回调失败！");
                }

                String returnMsg = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
                response.getWriter().write(returnMsg);// 发送成功消息返回给微信
            } else {
                String returnMsg = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[MESSAGE]]></return_msg></xml>";
                response.getWriter().write(returnMsg);// 发送消息返回给微信
                throw new Exception("支付回调失败！");
            }
        } catch (Exception e) {
            String returnMsg = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[MESSAGE]]></return_msg></xml>";
            response.getWriter().write(returnMsg);// 发送消息返回给微信
            throw new Exception("支付回调失败！");
        }
    }


    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult InsertRepairRecord(repairRecordRequest rrr, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        //循环班次数据
        Integer isDuty = 0;
        try {
            String dateNow = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            String dateHour = new SimpleDateFormat("HH:mm").format(new Date());
            Date nowDate = new Date();
            RepairRecord rr = new RepairRecord();
            rr.setAreaId(rrr.getAreaId());
            rr.setArticleId(rrr.getArticleId());
            rr.setSchoolId(rrr.getSchoolId());
            rr.setRecordNo(getUUIDBits(15));
            rr.setAddress(rrr.getAddress());
            rr.setDescription(rrr.getDescription());
            rr.setImage(StringUtils.join(rrr.getImages(), ','));
            rr.setVoice(rrr.getVoice());
            rr.setVoiceLength(rrr.getVoiceLength());
            rr.setUserId(rrr.getUserId());
            rr.setUserName(rrr.getUserName());
            rr.setUserPhone(rrr.getUserPhone());
            rr.setReportTime(new Date());
            rr.setMaintenanceState(eRecordStatu.Dispatch.getValue());
            rr.setBuildId(rrr.getBuildId());
            rr.setIsCollaborate(0);
            rr.setVersion(0);

            //派单操作
            //根据类型和校区找到对应的维修师傅
            //获取到了多位师傅的话 单子随机派给满足条件的一位师傅
            //照接单数从小到大排 最小的优先接单（前提是满足条件）
            //值班或下班或周末有派给指定的维修师傅的单子就是值班单 并且不考核
            //其他无人接的单子，第二天（工作日）的时候自动派
            //派单表中需要记录接单时间  接单表中需要记录维修完成时间
            //楼栋条件 员工必填楼栋 满足楼栋条件才能派
            List<AutoDispatchUserVo> users = repairUserService.autoDispatchUser(rrr.getSchoolId(), dateNow, rrr.getArticleId(), rrr.getBuildId());

            String shiftStr = StringUtils.join(users.stream().map(AutoDispatchUserVo::getArticleId).collect(Collectors.toList()), ",");
            List<String> shifts = Arrays.asList(shiftStr.split(",")).stream().distinct().collect(Collectors.toList());
            RepairDispatchRecord rdr = null;
            //只获取不是值班 或下班后的排班数据
            List<RepairShiftSettings> shiftDatas = repairShiftSettingsService.getRepairsShiftIdByHour(shifts, dateHour);
            for (AutoDispatchUserVo user : users) {
                List<RepairShiftSettings> datas = shiftDatas.stream().filter(e -> ("," + user.getShiftId() + ",").contains(("," + e.getId() + ","))).collect(Collectors.toList());
                if (datas.size() <= 0) {
                    continue;
                }

                int acceptanceAssessTime = user.getAcceptanceTime();
                try {

                    //下班之后或者值班接的单不考核 将报修工单中的is_duty改为1
                    Date workTime = TimeExchange.StringToDate(dateNow + " " + datas.get(0).getEndTime() + ":00", "yyyy-MM-dd HH:mm:ss");
                    if (workTime.before(nowDate)) {
                        isDuty = 1;
                    }

                    if (isDuty == 0) {
                        //在工作时间才去派单
                        List<timeVo> tvs = DelayedUtils.timeVos(datas);

                        //工作的时间是否够修完 临下班接的单不做延长
                        for (int i = 0; i < tvs.size(); i++) {
                            Date startWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i).getStart() + ":00", "yyyy-MM-dd HH:mm:ss");
                            Date endWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i).getEnd() + ":00", "yyyy-MM-dd HH:mm:ss");

                            //小于工作时间段的开始时间
                            if (nowDate.before(startWorkTime) || nowDate.equals(startWorkTime) && i == 0) {
                                //早上开始上班之前的单子
                                int minute = TimeExchange.getOffsetMinutes(nowDate, startWorkTime);
                                int workMinte = TimeExchange.getOffsetMinutes(nowDate, endWorkTime);
                                acceptanceAssessTime = acceptanceAssessTime + minute;
                                acceptanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, acceptanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                                break;
                            } else if ((startWorkTime.before(nowDate) || startWorkTime.equals(nowDate)) && (nowDate.before(endWorkTime) || nowDate.equals(endWorkTime))) {
                                //工作时间段内的单子
                                int workMinte = TimeExchange.getOffsetMinutes(nowDate, endWorkTime);
                                acceptanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, acceptanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                                break;

                            } else {
                                //非工作时间段的单子
                                Date nextStartWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i + 1).getStart() + ":00", "yyyy-MM-dd HH:mm:ss");
                                if ((endWorkTime.before(nowDate) || endWorkTime.equals(nowDate)) && (nowDate.before(nextStartWorkTime) || nowDate.equals(nextStartWorkTime))) {
                                    Date nextEndWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i + 1).getEnd() + ":00", "yyyy-MM-dd HH:mm:ss");
                                    int minute = TimeExchange.getOffsetMinutes(nowDate, nextStartWorkTime);
                                    int workMinte = TimeExchange.getOffsetMinutes(nowDate, nextEndWorkTime);
                                    acceptanceAssessTime = acceptanceAssessTime + minute;
                                    acceptanceAssessTime = DelayedUtils.addMinuteStart(tvs, nextEndWorkTime, acceptanceAssessTime, nowDate, dateNow, workMinte, i, 2);
                                    break;
                                }
                            }
                        }

                        rdr = new RepairDispatchRecord();
                        //创建派单记录
                        rdr.setAssignedTime(new Date());
                        rdr.setAcceptanceAssessTime(acceptanceAssessTime);
                        rdr.setOrderType(0);
                        rdr.setIsLoseEfficacy(0);
                        rdr.setUsersId(user.getId());
                        break;
                    }

                } catch (Exception e) {
                    throw new Exception("报修单提交失败！");
                }
            }

            rr.setIsDuty(isDuty);
            int result = repairRecordService.insertRepairRecord(rr);

            if (result <= 0) {
                throw new Exception("报修单提交失败！");
            }

            //两种情况：
            //1、工单已提交（没找到被派单师傅）
            //工单已生成，将尽快为您派单。
            //2、工单待接单（找到了被派单）
            //工单已交给系统，系统将催促师傅师尽快接单。
            if (rdr != null) {
                rdr.setRecordId(result);

                int dispatch = repairDispatchRecordService.insertRepairDispatchRecord(rdr);
                if (dispatch <= 0) {
                    throw new Exception("报修单提交失败！");
                }
                List<RepairSystemMessages> rsms = new ArrayList<>();
                RepairSystemMessages rsm = new RepairSystemMessages();
                rsm.setIsRead(0);
                rsm.setRecordId(result);
                rsm.setRecipientId(rdr.getUsersId());
                rsm.setContent("用户提交工单，请尽快处理！");
                rsms.add(rsm);
                RepairSystemMessages rsm1 = new RepairSystemMessages();
                rsm1.setIsRead(0);
                rsm1.setRecordId(result);
                rsm1.setRecipientId(rrr.getUserId());
                rsm1.setContent("工单已交给系统，系统将催促师傅师尽快接单！");
                rsms.add(rsm1);
                boolean insertRsm = repairSystemMessagesService.inserBatchSystemMessage(rsms);
                if (!insertRsm) {
                    throw new Exception("报修单提交失败！");
                }
            } else {
                RepairSystemMessages rsm = new RepairSystemMessages();
                rsm.setIsRead(0);
                rsm.setRecordId(result);
                rsm.setRecipientId(rrr.getUserId());
                rsm.setContent("工单已生成，将尽快为您派单！");
                int insertRsm = repairSystemMessagesService.insertRepairSystemMessages(rsm);
                if (insertRsm <= 0) {
                    throw new Exception("报修单提交失败！");
                }
            }

            //添加跟踪记录
            RepairTrackRecord rtr = new RepairTrackRecord();
            rtr.setRecordId(result);
            rtr.setMaintenanceState(eRecordStatu.Dispatch.getValue());
            rtr.setContent("提交订单");
            int insertRtr = repairTrackRecordService.insertRepairTrackRecord(rtr);
            if (insertRtr <= 0) {
                throw new Exception("报修单提交失败！");
            }

        } catch (Exception e) {
            throw new Exception("报修单提交失败！");
        }

        return CommonResult.ok("报修单提交成功");
    }

    @Override
    public CommonResult updateRepairRecordById(RepairRecord rr, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = repairRecordService.updateRepairRecord(rr);

        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    @Override
    public CommonResult queryPageRepairRecords(int currentPage, int pageCount, String name) {
        PageUtils<RepairRecord> result = repairRecordService.queryPageRepairRecords(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }

    @Override
    public CommonResult deleteRepairRecordById(int id) {
        RepairRecord data = repairRecordService.getRepairById(id);
        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = repairRecordService.deleteRepairRecordById(id);

        return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
    }

    @Override
    public CommonResult uploadFile(MultipartFile file) throws Exception {

        UploadFileVo result = new UploadFileVo();

        String name = StrUtils.getRandomName(file.getOriginalFilename());

        String user = "root"; //SFTP服务器用户名
        String password = "Best@123!@#"; // SFTP服务器密码
        String host = "172.16.20.92";  //  SFTP服务器地址
        int port = 22; // SFTP 服务器端口
        String remoteDirPath = "/usr/local/nginx/image"; // 远程目录路径
        //从MultipartFile对象中获取流
        InputStream inputStream = null;
        inputStream = file.getInputStream();
//        String downpath="/book/《Python爬虫开发与项目实战》.pdf";
//        String spath="C:/Users/F1339769/Desktop";
        JSch jsch = new JSch();
        Session session = null;
        ChannelSftp channelSftp = null;
        try {
            session = jsch.getSession(user, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect(30000);
            System.out.println("连接成功");
            channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();
            // 创建远程目录
            String[] dirArr = remoteDirPath.split("/");
            String path = "";
            for (int i = 0; i < dirArr.length; i++) {
                path = path + "/" + dirArr[i];
                try {
                    channelSftp.cd(path);
                } catch (SftpException e) {
                    channelSftp.mkdir(path);
                    channelSftp.cd(path);
                }
            }
            //  文件上传到远程服务器上
            channelSftp.put(inputStream, name);
            //  从远程服务器上下载文件
            //channelSftp.get(downpath, spath);
        } catch (JSchException e) {
            e.printStackTrace();
        } finally {
            if (channelSftp != null) {
                channelSftp.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }
            result.setResultUrl("https://chtech.ncjti.edu.cn/campusMaintenance/image/" + name);
        }
        return CommonResult.ok(result);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult revokeRepairRecordById(int id) throws Exception {
        RepairRecord data = repairRecordService.getRepairById(id);
        if (data == null) {
            return CommonResult.fail("当前数据不存在，撤销失败！");
        }

        if (data.getMaintenanceState() != eRecordStatu.Dispatch.getValue() || data.getMaintenanceState() != eRecordStatu.ToConfirmed.getValue()) {
            return CommonResult.fail("待接单或待确认才能进行撤销操作！");
        }

        try {
            List<Integer> userIds = new ArrayList<>();
            //获取该维修单的派单记录数据
            List<RepairDispatchRecord> rdrs = repairDispatchRecordService.queryDispatchByRecordId(data.getId());
            if (rdrs.size() > 0) {
                userIds = rdrs.stream().map(RepairDispatchRecord::getUsersId).collect(Collectors.toList());
                //将该维修单的派单记录和接单记录都置为无效
                RepairDispatchRecord rdr = new RepairDispatchRecord();
                int updateD = repairDispatchRecordService.updateDispatch(data.getId(), rdr.setIsLoseEfficacy(eDispatchResult.Expired.getValue()));
                if (updateD <= 0) {
                    throw new Exception("撤销失败！");
                }
                if (rdrs.get(0).getReceivingTime() != null) {
                    RepairReceiving rr = new RepairReceiving();
                    int updateR = repairReceivingService.updateReceiving(data.getId(), rr.setIsLoseEfficacy(eDispatchResult.Expired.getValue()));
                    if (updateR <= 0) {
                        throw new Exception("撤销失败！");
                    }
                }
            }

            if (userIds.size() > 0) {
                List<RepairSystemMessages> rsms = new ArrayList<>();
                for (int i = 0; i < userIds.size(); i++) {
                    RepairSystemMessages rsm = new RepairSystemMessages();
                    rsm.setIsRead(0);
                    rsm.setRecordId(data.getId());
                    rsm.setRecipientId(userIds.get(i));
                    rsm.setContent("工单已被用户取消！");
                    rsms.add(rsm);
                }
                boolean insertRsm = repairSystemMessagesService.inserBatchSystemMessage(rsms);
                if (!insertRsm) {
                    throw new Exception("撤销失败！");
                }
            }

            data.setMaintenanceState(eRecordStatu.Canceled.getValue());
            int result = repairRecordService.updateRepairRecord(data);
            if (result <= 0) {
                throw new Exception("撤销失败！");
            }

            //添加跟踪记录
            RepairTrackRecord rtr = new RepairTrackRecord();
            rtr.setRecordId(id);
            rtr.setMaintenanceState(eRecordStatu.Canceled.getValue());
            rtr.setContent("用户撤销");
            int insertRtr = repairTrackRecordService.insertRepairTrackRecord(rtr);
            if (insertRtr <= 0) {
                throw new Exception("报修单提交失败！");
            }
        } catch (Exception e) {
            throw new Exception("撤销失败！");
        }


        return CommonResult.ok("撤销成功");
    }

    @Override//还要处理一下订单超时状态
    public CommonResult getRepairRecordsByUserId(int currentPage, int pageCount, int userId, int state) {
        PageUtils<MyRepairRecordVo> result = repairRecordService.queryMyRepairRecordList(currentPage, pageCount, state, userId);

        List<Integer> recordIds = result.getList().stream().map(MyRepairRecordVo::getId).collect(Collectors.toList());

        List<RepairConsumables> rcs = repairConsumablesService.getRepairsByRecordIds(recordIds);

        List<RepairEvaluate> evaluates = repairEvaluateService.getRepairByRecordIds(recordIds);

        for (MyRepairRecordVo r : result.getList()) {
            long count = evaluates.stream().filter(e -> e.getRecordId().equals(r.getId())).count();
            r.setState(eRecordStatu.stringOf(r.getStateId()));
            r.setImages(Arrays.asList(r.getImage().split(",")));
            List<RepairConsumables> datas = rcs.stream().filter(e -> e.getRecordId() == r.getId()).collect(Collectors.toList());
            r.setConsumables(datas);
            r.setEvaluate((count <= 0 && r.getStateId() == eRecordStatu.Finished.getValue()) ? 1 : 0);
        }
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult getRepairRecordCountByUserId(int userId) {
        RepairRecordCountVo result = repairRecordService.getRepairRecordCount(userId);
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult getTechnicianCountByUserId(int userId) {
        String dateNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        MaintenanceTechnicianVo result = repairRecordService.queryRepairTechnicianCount(userId, dateNow);
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult getTechniciansByUserId(int currentPage, int pageCount, int userId, int state) {
        PageUtils<RepairTechnicianVo> result = null;
        String dateNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        switch (state) {
            case 1://待接单
                result = repairRecordService.queryPendingOrderList(currentPage, pageCount, userId, dateNow);
                break;
            case 2://维修中
                result = repairRecordService.queryRepairOrderList(currentPage, pageCount, userId, dateNow);
                break;
            case 3://已超时
                result = repairRecordService.queryTimeoutOrderList(currentPage, pageCount, userId, dateNow);
                for (RepairTechnicianVo re : result.getList()) {
                    re.setState(eRecordStatu.stringOf(re.getMaintenanceState()));
                }
                break;
            case 4://待审核
                result = repairRecordService.queryExamineOrderList(currentPage, pageCount, userId);
                break;
            default:
                break;
        }

        return CommonResult.ok(result);
    }

    @Override
    public CommonResult repairRecordReminder(int recordId) {
        // 根据报修ID查找是否有派单数据 并且是有效的
        RepairRecord record = repairRecordService.getRepairById(recordId);
        if (record == null) {
            return CommonResult.fail("报修工单数据无效，催单失败");
        }

        if (record.getMaintenanceState() >= eRecordStatu.Finished.getValue()) {
            return CommonResult.fail("当前报修工单状态不支持催单操作");
        }

        List<String> cardnumber = new ArrayList<>();
        List<Integer> userIds = new ArrayList<>();

        // 有的话就发给被派单的维修师傅
        // 否则就发给郭师傅
        List<RepairCardNumberVo> cardNumbers = repairDispatchRecordService.queryRepairCardNumber(recordId);
        if (cardNumbers.size() <= 0) {
            //获取郭师傅的卡号把消息发给郭师傅
            List<Integer> uz = new ArrayList<>();
            uz.add(eUserZZ.Monitor.getValue());
            List<RepairUser> users = repairUserService.getRepairByUserZZ(uz, record.getSchoolId());
            cardnumber = users.stream().map(RepairUser::getCardNumber).collect(Collectors.toList());
            userIds = users.stream().map(RepairUser::getId).collect(Collectors.toList());
        } else {
            cardnumber = cardNumbers.stream().map(RepairCardNumberVo::getCardNumber).collect(Collectors.toList());
            userIds = cardNumbers.stream().map(RepairCardNumberVo::getUserId).collect(Collectors.toList());
        }

        if (cardnumber.size() <= 0) {
            return CommonResult.fail("当前工单无接收人，催单失败");
        }

        List<RepairSystemMessages> rsms = new ArrayList<>();
        for (int i = 0; i < userIds.size(); i++) {
            RepairSystemMessages rsm = new RepairSystemMessages();
            rsm.setIsRead(0);
            rsm.setRecordId(recordId);
            rsm.setRecipientId(userIds.get(i));
            rsm.setContent("您有工单用户催促，请尽快处理！");
            rsms.add(rsm);
        }
        boolean insertS = repairSystemMessagesService.inserBatchSystemMessage(rsms);
        if (!insertS) {
            return CommonResult.fail("催单失败");
        }

        String content = MessageFormat.format("【报修系统】维修师傅，你好，工单{0}用户催单，请及时处理，谢谢！", record.getRecordNo());
        CommonResult result = weiXiaoUtil.sendNotice(cardnumber, "报修工单催单", "催单", content, null);
        return result;
    }

    @Override
    public CommonResult queryManageCount(Integer schoolId, @RequestHeader("user_head") String userhead) {
//        String userID = AesUtils.decrypt(userhead);
//        RepairAdmin operateData = repairAdminService.getRepairById(userID);
//        if (operateData == null) {
//            return CommonResult.fail("操作人信息不合法，无法获取工单数据！");
//        }
//        if (operateData.getIsSuper().equals(eIsSuper.Super.getValue())) {
//            schoolId = null;
//        }
        RepairManageCountVo result = repairRecordService.queryManageCount(schoolId);
        return CommonResult.ok(result);
    }


    /**
     * 全部    0
     * 待接单  1
     * 维修中  2
     * 待处理  3
     * 待审核  4
     * 已完成  5
     * 已取消  6
     * 已关单  7
     * 已超时  8
     */
    @Override
    public CommonResult queryManagePage(int currentPage, int pageCount, @RequestHeader("user_head") String userhead, int state, String keyWord, String startTime, String endTime, Integer schoolId) {

        if (startTime != null && endTime != null) {
            try {
                startTime = TimeExchange.getStartOfDayStr(TimeExchange.StringToDate(startTime, "yyyy-MM-dd"));
                endTime = TimeExchange.getEndOfDayStr(TimeExchange.StringToDate(endTime, "yyyy-MM-dd"));
            } catch (Exception e) {
            }
        }

//        String userID = AesUtils.decrypt(userhead);
//        RepairAdmin operateData = repairAdminService.getRepairById(userID);
//        if (operateData == null) {
//            return CommonResult.fail("操作人信息不合法，无法获取工单数据！");
//        }
//        if (operateData.getIsSuper().equals(eIsSuper.Super.getValue())) {
//            schoolId = null;
//        }

        PageUtils<RepairRecordPageVo> result = null;
        /**
         * 出现延时按钮
         * 待接单 已完成 已取消 已关单 不需要出现延时按钮
         * 0 2 3 4 8 需要
         */
        String stateStr = null;
        switch (state) {
            case 0:
                result = repairRecordService.queryNotTimeout(currentPage, pageCount, stateStr, keyWord, startTime, endTime, schoolId);
                for (RepairRecordPageVo r : result.getList()) {
                    if (r.getMaintenanceState() == 1) {
                        r.setState("待接单");
                    } else if (r.getMaintenanceState() == 2) {
                        r.setState("维修中");
                    } else if (r.getMaintenanceState() == 3) {
                        r.setState("待处理");
                    } else if (r.getMaintenanceState() == 4) {
                        r.setState("待确认");
                    } else if (r.getMaintenanceState() == 5 || r.getMaintenanceState() == 6) {
                        r.setState(r.getMaintenanceState() == 5 ? "转单待审核" : "协作待审核");
                    } else if (r.getMaintenanceState() == 7) {
                        r.setState("已完成");
                    } else if (r.getMaintenanceState() == 8) {
                        r.setState("已取消");
                    } else if (r.getMaintenanceState() == 9) {
                        r.setState("已关单");
                    }
                    r.setDelayed(1);
                }
                break;
            case 1:
                stateStr = "=1)";
                result = repairRecordService.queryNotTimeout(currentPage, pageCount, stateStr, keyWord, startTime, endTime, schoolId);
                for (RepairRecordPageVo r : result.getList()) {
                    //待接单才能关单
                    r.setState("待接单");
                    r.setClose(1);
                }
                break;
            case 2:
                stateStr = "=2)";
                result = repairRecordService.queryNotTimeout(currentPage, pageCount, stateStr, keyWord, startTime, endTime, schoolId);
                for (RepairRecordPageVo r : result.getList()) {
                    r.setState("维修中");
                    r.setDelayed(1);
                }
                break;
            case 3:
                stateStr = "=3)";
                result = repairRecordService.queryNotTimeout(currentPage, pageCount, stateStr, keyWord, startTime, endTime, schoolId);
                for (RepairRecordPageVo r : result.getList()) {
                    r.setState("待处理");
                    r.setDelayed(1);
                }
                break;
            case 4:
                stateStr = "=5 or rr.maintenance_state = 6)";
                result = repairRecordService.queryNotTimeout(currentPage, pageCount, stateStr, keyWord, startTime, endTime, schoolId);
                for (RepairRecordPageVo r : result.getList()) {
                    r.setState(r.getMaintenanceState() == 5 ? "转单待审核" : "协作待审核");
                    r.setDelayed(1);
                }
                break;
            case 5:
                stateStr = "=7)";
                result = repairRecordService.queryNotTimeout(currentPage, pageCount, stateStr, keyWord, startTime, endTime, schoolId);
                for (RepairRecordPageVo r : result.getList()) {
                    r.setState("已完成");
                }
                break;
            case 6:
                stateStr = "=8)";
                result = repairRecordService.queryNotTimeout(currentPage, pageCount, stateStr, keyWord, startTime, endTime, schoolId);
                for (RepairRecordPageVo r : result.getList()) {
                    r.setState("已取消");
                    r.setDelayed(1);
                }
                break;
            case 7:
                stateStr = "=9)";
                result = repairRecordService.queryNotTimeout(currentPage, pageCount, stateStr, keyWord, startTime, endTime, schoolId);
                for (RepairRecordPageVo r : result.getList()) {
                    r.setState("已关单");
                }
                break;
            case 8:
                result = repairRecordService.queryTimeout(currentPage, pageCount, keyWord, startTime, endTime, schoolId);
                for (RepairRecordPageVo r : result.getList()) {
                    String[] Str = r.getMaintenancerName().split("-");
                    r.setMaintenancerName(Str.length >= 2 ? Str[0] : "");
                    r.setState(Str.length >= 2 ? Str[1] : "");
                }
                break;
            default:
                break;
        }

        if (result.getList().size() > 0) {
            List<Integer> recordIds = result.getList().stream().map(RepairRecordPageVo::getId).collect(Collectors.toList());
            /**
             * 全部 待接单 待处理
             * 出现派单按钮
             * 没有派单记录的数据出现派单按钮
             */
            if (state == 0 || state == 1 || state == 3) {

                List<RepairDispatchRecord> disps = repairDispatchRecordService.queryRepairDispatch(recordIds);
                for (RepairRecordPageVo r : result.getList()) {
                    if (r.getMaintenanceState() == eRecordStatu.Dispatch.getValue()) {
                        long count = disps.stream().filter(e -> e.getRecordId().equals(r.getId())).count();
                        r.setDispatch(count > 0 ? 0 : 1);
                    }
                }
            }

            /**
             * 全部 待审核
             * 出现协作审核按钮
             * 出现转单审核按钮
             */
            if (state == 0 || state == 4) {
                List<CollaborateRecordVo> collaborates = repairCollaborateRecordService.queryRepairCollaborate(recordIds);
                for (RepairRecordPageVo r : result.getList()) {
                    if (r.getMaintenanceState() == eRecordStatu.Collaborate.getValue()) {
                        long collaborate = collaborates.stream().filter(e -> e.getRecordId() == r.getId() && e.getState().equals("协作")).count();
                        r.setCollaborate(collaborate > 0 ? 1 : 0);
                    } else if (r.getMaintenanceState() == eRecordStatu.Transfer.getValue()) {
                        long transfer = collaborates.stream().filter(e -> e.getRecordId() == r.getId() && e.getState().equals("转单")).count();
                        r.setTransfer(transfer > 0 ? 1 : 0);
                    }
                }
            }

            /**
             * 全部 待处理 待审核 维修中
             * 有耗材记录就显示改价按钮
             * 出现改价按钮
             */
            if (state == 0 || state == 2 || state == 3 || state == 4) {
                List<RepairConsumables> consumables = repairConsumablesService.getRepairsByRecordIds(recordIds);
                for (RepairRecordPageVo r : result.getList()) {
                    if (r.getMaintenanceState() == eRecordStatu.ToLogistics.getValue()
                            || r.getMaintenanceState() == eRecordStatu.Maintenance.getValue()
                            || r.getMaintenanceState() == eRecordStatu.Transfer.getValue()
                            || r.getMaintenanceState() == eRecordStatu.Collaborate.getValue()) {
                        long consumable = consumables.stream().filter(e -> e.getRecordId() == r.getId()).count();
                        r.setChangePrice(consumable > 0 ? 1 : 0);
                    }
                }
            }

            /**
             * 出现回复按钮
             * 有留言 且用户回复消息数大于后勤人员回复的
             */
            List<RepairLeaveMessage> messages = repairLeaveMessageService.getRepairsByRecordIds(recordIds);
            for (RepairRecordPageVo r : result.getList()) {
                List<RepairLeaveMessage> alls = messages.stream().filter(e -> e.getRecordId() == r.getId()).collect(Collectors.toList());
                List<RepairLeaveMessage> owns = alls.stream().filter(e -> e.getRecordId() == r.getId() && e.getSenderId().equals(r.getUserId())).collect(Collectors.toList());
                r.setReply(owns.size() > alls.size() / 2 ? 1 : 0);
            }

        }
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult queryPendingOrder(int currentPage, int pageCount) {
        PageUtils<pendingOrderVo> result = repairRecordService.queryPendingOrder(currentPage, pageCount);
        return CommonResult.ok(result);
    }

    /**
     * 待处理池接单操作
     *
     * @param recordId 维修单ID
     * @param userId   操作人ID
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult receiveOrders(int recordId, int userId) throws Exception {
        try {
            Date nowDate = new Date();

            //判断是否存在派单数据
            int dispatchCount = repairDispatchRecordService.queryDispatchCount(recordId);
            if (dispatchCount > 0) {
                return CommonResult.fail("该工单存在待处理人，抢单失败！");
            }

            //获取订单
            RepairRecord recordData = repairRecordService.getRepairById(recordId);
            if (recordData == null) {
                return CommonResult.fail("订单不存在，抢单失败！");
            }

            if (!recordData.getMaintenanceState().equals(eRecordStatu.Dispatch.getValue())) {
                return CommonResult.fail("订单状态错误，抢单失败！");
            }

            //获取维修师傅的用户信息 并获取当天的排班数据
            String dateNow = TimeExchange.DateToString(nowDate, "yyyy-MM-dd");
            UserDataVo result = repairUserService.queryUserData(userId, dateNow);

            if (result == null) {
                return CommonResult.fail("非法身份，抢单失败！");
            }

            if (result.getStartDate() == null || result.getShiftId() == null) {
                return CommonResult.fail("当天未排班，无法接单！");
            }

            long count = Arrays.asList(result.getArticleId().split(",")).stream().filter(e -> e.equals(recordData.getArticleId().toString())).count();
            if (count <= 0) {
                return CommonResult.fail("无法维修该种类物品！");
            }

            List<String> shifts = Arrays.asList(result.getShiftId().split(","));
            List<RepairShiftSettings> shiftDatas = repairShiftSettingsService.getRepairsByShiftIds(shifts);
            if (shiftDatas.size() <= 0) {
                return CommonResult.fail("当天未排班，无法接单！");
            }

            //师傅自己抢单的时候不去限制抢单师傅的楼栋

            Integer isDuty = 0;
            int acceptanceAssessTime = result.getAcceptanceAssessTime();
            int maintenanceAssessTime = result.getMaintenanceAssessTime();
            try {

                //下班之后或者值班接的单不考核 将报修工单中的is_duty改为1
                Date workTime = TimeExchange.StringToDate(dateNow + " " + shiftDatas.get(0).getEndTime() + ":00", "yyyy-MM-dd HH:mm:ss");
                if (workTime.before(nowDate)) {
                    isDuty = 1;
                }

                if (isDuty == 0) {
                    List<timeVo> tvs = DelayedUtils.timeVos(shiftDatas);

                    //工作的时间是否够修完 临下班接的单不做延长
                    for (int i = 0; i < tvs.size(); i++) {
                        Date startWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i).getStart() + ":00", "yyyy-MM-dd HH:mm:ss");
                        Date endWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i).getEnd() + ":00", "yyyy-MM-dd HH:mm:ss");

                        //小于工作时间段的开始时间
                        if (nowDate.before(startWorkTime) || nowDate.equals(startWorkTime) && i == 0) {
                            //早上开始上班之前的单子
                            int minute = TimeExchange.getOffsetMinutes(nowDate, startWorkTime);
                            int workMinte = TimeExchange.getOffsetMinutes(nowDate, endWorkTime);
                            acceptanceAssessTime = acceptanceAssessTime + minute;
                            acceptanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, acceptanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                            maintenanceAssessTime = maintenanceAssessTime + minute;
                            maintenanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, maintenanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                            break;
                        } else if ((startWorkTime.before(nowDate) || startWorkTime.equals(nowDate)) && (nowDate.before(endWorkTime) || nowDate.equals(endWorkTime))) {
                            //工作时间段内的单子
                            int workMinte = TimeExchange.getOffsetMinutes(nowDate, endWorkTime);
                            acceptanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, acceptanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                            maintenanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, maintenanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                            break;

                        } else {
                            //非工作时间段的单子
                            Date nextStartWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i + 1).getStart() + ":00", "yyyy-MM-dd HH:mm:ss");
                            if ((endWorkTime.before(nowDate) || endWorkTime.equals(nowDate)) && (nowDate.before(nextStartWorkTime) || nowDate.equals(nextStartWorkTime))) {
                                Date nextEndWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i + 1).getEnd() + ":00", "yyyy-MM-dd HH:mm:ss");
                                int minute = TimeExchange.getOffsetMinutes(nowDate, nextStartWorkTime);
                                int workMinte = TimeExchange.getOffsetMinutes(nowDate, nextEndWorkTime);
                                acceptanceAssessTime = acceptanceAssessTime + minute;
                                acceptanceAssessTime = DelayedUtils.addMinuteStart(tvs, nextEndWorkTime, acceptanceAssessTime, nowDate, dateNow, workMinte, i, 2);
                                maintenanceAssessTime = maintenanceAssessTime + minute;
                                maintenanceAssessTime = DelayedUtils.addMinuteStart(tvs, nextEndWorkTime, maintenanceAssessTime, nowDate, dateNow, workMinte, i, 2);
                                break;
                            }
                        }
                    }
                }
            } catch (Exception e) {
                throw new Exception("延时异常，抢单失败！");
            }

            //将工单状态更新为维修中
            recordData.setMaintenanceState(eRecordStatu.Maintenance.getValue());
            recordData.setMaintenancerId(String.valueOf(userId));
            recordData.setMaintenancerName(result.getUserName());
            recordData.setMaintenancerPhone(result.getUserPhone());
            recordData.setIsDuty(isDuty);
            int updateRecord = repairRecordService.updateRepairRecord(recordData);
            if (updateRecord <= 0) {
                throw new Exception("抢单失败！");
            }

            //创建派单记录
            RepairDispatchRecord rdr = new RepairDispatchRecord();
            rdr.setRecordId(recordId);
            rdr.setUsersId(userId);
            rdr.setAssignedTime(nowDate);
            rdr.setAcceptanceAssessTime(acceptanceAssessTime);
            rdr.setReceivingTime(nowDate);
            rdr.setOrderType(0);
            rdr.setIsLoseEfficacy(0);
            int dispatch = repairDispatchRecordService.insertRepairDispatchRecord(rdr);
            if (dispatch <= 0) {
                throw new Exception("抢单失败！");
            }

            //创建接单记录
            RepairReceiving rr = new RepairReceiving();
            rr.setRecordId(recordId);
            rr.setDispatchId(rdr.getId());
            rr.setUserId(userId);
            rr.setReceivingTime(nowDate);
            rr.setMaintenanceAssessTime(maintenanceAssessTime);
            rr.setIsLoseEfficacy(0);
            int receive = repairReceivingService.insertRepairReceiving(rr);
            if (receive <= 0) {
                throw new Exception("抢单失败！");
            }

            //给用户发消息
            RepairSystemMessages rsm = new RepairSystemMessages();
            rsm.setIsRead(0);
            rsm.setRecordId(recordId);
            rsm.setRecipientId(recordData.getUserId());
            rsm.setContent("工单已被" + recordData.getMaintenancerName() + "接单，电话" + recordData.getMaintenancerPhone() + "，师傅会尽快上门维修。");
            int insertRsm = repairSystemMessagesService.insertRepairSystemMessages(rsm);
            if (insertRsm <= 0) {
                throw new Exception("抢单失败！");
            }

            //添加跟踪记录
            RepairTrackRecord rtr = new RepairTrackRecord();
            rtr.setRecordId(recordId);
            rtr.setMaintenanceState(eRecordStatu.Maintenance.getValue());
            rtr.setContent("已接单");
            int insertRtr = repairTrackRecordService.insertRepairTrackRecord(rtr);
            if (insertRtr <= 0) {
                throw new Exception("抢单失败！");
            }

        } catch (Exception e) {
            throw new Exception("抢单失败！");
        }

        return CommonResult.ok("工单已被您接单，请和用户确认上门维修时间");
    }

    /**
     * 维修师傅接单
     *
     * @param recordId 维修单记录ID
     * @param userId   操作用户ID
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult receiveOwnOrders(int recordId, int userId) throws Exception {
        try {
            Date nowDate = new Date();
            //获取订单
            RepairRecord recordData = repairRecordService.getRepairById(recordId);
            if (recordData == null) {
                return CommonResult.fail("订单不存在，接单失败！");
            }

            //协作单才能多个人接单 其他情况下只能一个人接单
            if (!recordData.getMaintenanceState().equals(eRecordStatu.Dispatch.getValue()) && recordData.getIsCollaborate() != 1) {
                return CommonResult.fail("订单状态错误，接单失败！");
            }

            //获取操作人的派单数据
            RepairDispatchRecord dispatch = repairDispatchRecordService.queryOwnRepairDispatch(recordId, userId);
            if (dispatch == null) {
                return CommonResult.fail("派单数据异常，接单失败");
            }

            String dateNow = TimeExchange.DateToString(nowDate, "yyyy-MM-dd");
            UserDataVo result = repairUserService.queryUserData(userId, dateNow);

            if (result == null) {
                return CommonResult.fail("非法身份，接单失败！");
            }

            if (result.getStartDate() == null || result.getShiftId() == null) {
                return CommonResult.fail("当天未排班，无法接单！");
            }

            long count = Arrays.asList(result.getArticleId().split(",")).stream().filter(e -> e.equals(recordData.getArticleId().toString())).count();
            if (count <= 0) {
                return CommonResult.fail("无法维修该种类物品！");
            }

            List<String> shifts = Arrays.asList(result.getShiftId().split(","));
            List<RepairShiftSettings> shiftDatas = repairShiftSettingsService.getRepairsByShiftIds(shifts);
            if (shiftDatas.size() <= 0) {
                return CommonResult.fail("当天未排班，无法接单！");
            }

            Integer isDuty = 0;
            int maintenanceAssessTime = result.getMaintenanceAssessTime();
            if (recordData.getIsDuty() != 1) {
                //派单的时候是值班单就按值班单算
                //如果不是值班时间派的 那这个单子就不是值班单
                //例如，A师傅上午09:00-12:00工作，下午13:00-16:00值班
                //有个单子10:00派给A师傅，下午14:00接单的时候这个单子依旧要进行考核；
                //即：最终是否是值班单以派单时机为准
                //最终如果是值班单就不用考虑维修考核时间延时的问题
                Date workTime = TimeExchange.StringToDate(dateNow + " " + shiftDatas.get(0).getEndTime() + ":00", "yyyy-MM-dd HH:mm:ss");
                if (workTime.before(nowDate)) {
                    maintenanceAssessTime = maintenanceAssessTime;
                }

                if (nowDate.before(workTime)) {
                    List<timeVo> tvs = DelayedUtils.timeVos(shiftDatas);

                    //工作的时间是否够修完 临下班接的单不做延长
                    for (int i = 0; i < tvs.size(); i++) {
                        Date startWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i).getStart() + ":00", "yyyy-MM-dd HH:mm:ss");
                        Date endWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i).getEnd() + ":00", "yyyy-MM-dd HH:mm:ss");

                        //小于工作时间段的开始时间
                        if (nowDate.before(startWorkTime) || nowDate.equals(startWorkTime) && i == 0) {
                            //早上开始上班之前的单子
                            int minute = TimeExchange.getOffsetMinutes(nowDate, startWorkTime);
                            int workMinte = TimeExchange.getOffsetMinutes(nowDate, endWorkTime);
                            maintenanceAssessTime = maintenanceAssessTime + minute;
                            maintenanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, maintenanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                            break;
                        } else if ((startWorkTime.before(nowDate) || startWorkTime.equals(nowDate)) && (nowDate.before(endWorkTime) || nowDate.equals(endWorkTime))) {
                            //工作时间段内的单子
                            int workMinte = TimeExchange.getOffsetMinutes(nowDate, endWorkTime);
                            maintenanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, maintenanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                            break;

                        } else {
                            //非工作时间段的单子
                            Date nextStartWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i + 1).getStart() + ":00", "yyyy-MM-dd HH:mm:ss");
                            if ((endWorkTime.before(nowDate) || endWorkTime.equals(nowDate)) && (nowDate.before(nextStartWorkTime) || nowDate.equals(nextStartWorkTime))) {
                                Date nextEndWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i + 1).getEnd() + ":00", "yyyy-MM-dd HH:mm:ss");
                                int minute = TimeExchange.getOffsetMinutes(nowDate, nextStartWorkTime);
                                int workMinte = TimeExchange.getOffsetMinutes(nowDate, nextEndWorkTime);
                                maintenanceAssessTime = maintenanceAssessTime + minute;
                                maintenanceAssessTime = DelayedUtils.addMinuteStart(tvs, nextEndWorkTime, maintenanceAssessTime, nowDate, dateNow, workMinte, i, 2);
                                break;
                            }
                        }
                    }
                }
            }

            //将工单状态更新为维修中
            recordData.setMaintenanceState(eRecordStatu.Maintenance.getValue());
            String mi = dispatch.getOrderType() == eDispatchType.Cooperation.getValue() ? recordData.getMaintenancerId() == null ? String.valueOf(userId) : recordData.getMaintenancerId() + "," + String.valueOf(userId) : String.valueOf(userId);
            recordData.setMaintenancerId(mi);
            recordData.setMaintenancerName(result.getUserName());
            recordData.setMaintenancerPhone(result.getUserPhone());
            recordData.setIsDuty(isDuty);
            int updateRecord = repairRecordService.updateRepairRecord(recordData);
            if (updateRecord <= 0) {
                throw new Exception("接单失败！");
            }

            //更新派单记录
            dispatch.setReceivingTime(nowDate);
            int updateDispatch = repairDispatchRecordService.insertRepairDispatchRecord(dispatch);
            if (updateDispatch <= 0) {
                throw new Exception("接单失败！");
            }

            //创建接单记录
            RepairReceiving rr = new RepairReceiving();
            rr.setRecordId(recordId);
            rr.setDispatchId(dispatch.getId());
            rr.setUserId(userId);
            rr.setReceivingTime(new Date());
            rr.setMaintenanceAssessTime(maintenanceAssessTime);
            rr.setIsLoseEfficacy(0);
            int receive = repairReceivingService.insertRepairReceiving(rr);
            if (receive <= 0) {
                throw new Exception("接单失败！");
            }

            RepairSystemMessages rsm = new RepairSystemMessages();
            rsm.setIsRead(0);
            rsm.setRecordId(recordId);
            rsm.setRecipientId(userId);
            rsm.setContent("工单已被" + result.getUserName() + "接单，电话" + result.getUserPhone() + "，师傅会尽快上门维修。");
            int insertRsm = repairSystemMessagesService.insertRepairSystemMessages(rsm);
            if (insertRsm <= 0) {
                throw new Exception("报修单提交失败！");
            }

            //添加跟踪记录
            RepairTrackRecord rtr = new RepairTrackRecord();
            rtr.setRecordId(recordId);
            rtr.setMaintenanceState(eRecordStatu.Maintenance.getValue());
            rtr.setContent("已接单");
            int insertRtr = repairTrackRecordService.insertRepairTrackRecord(rtr);
            if (insertRtr <= 0) {
                throw new Exception("接单失败");
            }
        } catch (Exception e) {
            throw new Exception("接单失败！");
        }

        return CommonResult.ok("工单已被您接单，请和用户确认上门维修时间");
    }

    /**
     * 派单
     *
     * @param rsor
     * @param bindingResult
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult receiveSendOrders(ReceiveSendOrdersRequest rsor, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        RepairRecord rr = repairRecordService.getById(rsor.getRecordId());
        if (rr == null) {
            return CommonResult.fail("维修单数据失效，派单失败");
        }

        if (rr.getMaintenanceState() != eRecordStatu.Dispatch.getValue()) {
            return CommonResult.fail("维修单状态异常，派单失败");
        }

        Integer isDuty = 0;
        Date nowDate = new Date();
        String dateHour = new SimpleDateFormat("HH:mm").format(new Date());
        String dateNow = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

        try {
            List<String> shifts = Arrays.asList(rsor.getShiftId().split(",")).stream().distinct().collect(Collectors.toList());
            RepairDispatchRecord rdr = null;
            //只获取不是值班 或下班后的排班数据
            List<RepairShiftSettings> shiftDatas = repairShiftSettingsService.getRepairsShiftIdByHour(shifts, dateHour);
            List<RepairShiftSettings> datas = shiftDatas.stream().filter(e -> ("," + rsor.getShiftId() + ",").contains(("," + e.getId() + ","))).collect(Collectors.toList());
            if (datas.size() > 0) {
                int acceptanceAssessTime = rsor.getAcceptanceTime();
                try {

                    //下班之后或者值班接的单不考核 将报修工单中的is_duty改为1
                    Date workTime = TimeExchange.StringToDate(dateNow + " " + datas.get(0).getEndTime() + ":00", "yyyy-MM-dd HH:mm:ss");
                    if (workTime.before(nowDate)) {
                        isDuty = 1;
                    }

                    if (isDuty == 0) {
                        //在工作时间才去派单
                        List<timeVo> tvs = DelayedUtils.timeVos(datas);

                        //工作的时间是否够修完 临下班接的单不做延长
                        for (int i = 0; i < tvs.size(); i++) {
                            Date startWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i).getStart() + ":00", "yyyy-MM-dd HH:mm:ss");
                            Date endWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i).getEnd() + ":00", "yyyy-MM-dd HH:mm:ss");

                            //小于工作时间段的开始时间
                            if (nowDate.before(startWorkTime) || nowDate.equals(startWorkTime) && i == 0) {
                                //早上开始上班之前的单子
                                int minute = TimeExchange.getOffsetMinutes(nowDate, startWorkTime);
                                int workMinte = TimeExchange.getOffsetMinutes(nowDate, endWorkTime);
                                acceptanceAssessTime = acceptanceAssessTime + minute;
                                acceptanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, acceptanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                                break;
                            } else if ((startWorkTime.before(nowDate) || startWorkTime.equals(nowDate)) && (nowDate.before(endWorkTime) || nowDate.equals(endWorkTime))) {
                                //工作时间段内的单子
                                int workMinte = TimeExchange.getOffsetMinutes(nowDate, endWorkTime);
                                acceptanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, acceptanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                                break;

                            } else {
                                //非工作时间段的单子
                                Date nextStartWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i + 1).getStart() + ":00", "yyyy-MM-dd HH:mm:ss");
                                if ((endWorkTime.before(nowDate) || endWorkTime.equals(nowDate)) && (nowDate.before(nextStartWorkTime) || nowDate.equals(nextStartWorkTime))) {
                                    Date nextEndWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i + 1).getEnd() + ":00", "yyyy-MM-dd HH:mm:ss");
                                    int minute = TimeExchange.getOffsetMinutes(nowDate, nextStartWorkTime);
                                    int workMinte = TimeExchange.getOffsetMinutes(nowDate, nextEndWorkTime);
                                    acceptanceAssessTime = acceptanceAssessTime + minute;
                                    acceptanceAssessTime = DelayedUtils.addMinuteStart(tvs, nextEndWorkTime, acceptanceAssessTime, nowDate, dateNow, workMinte, i, 2);
                                    break;
                                }
                            }
                        }

                        rdr = new RepairDispatchRecord();
                        //创建派单记录
                        rdr.setAssignedTime(new Date());
                        rdr.setAcceptanceAssessTime(acceptanceAssessTime);
                        rdr.setOrderType(eDispatchType.Send.getValue());
                        rdr.setIsLoseEfficacy(eDispatchResult.NotExpired.getValue());
                    }

                } catch (Exception e) {
                    throw new Exception("派单失败！");
                }
            }

            rr.setIsDuty(isDuty);
            int result = repairRecordService.updateRepairRecord(rr);

            if (result <= 0) {
                throw new Exception("派单失败！");
            }

            if (rdr != null) {
                rdr.setRecordId(rr.getId());
                rdr.setUsersId(rsor.getId());
                int dispatch = repairDispatchRecordService.insertRepairDispatchRecord(rdr);
                if (dispatch <= 0) {
                    throw new Exception("派单失败！");
                }
            }

            //添加跟踪记录
            RepairTrackRecord rtr = new RepairTrackRecord();
            rtr.setRecordId(rsor.getRecordId());
            rtr.setMaintenanceState(eRecordStatu.Dispatch.getValue());
            rtr.setContent("已派单");
            int insertRtr = repairTrackRecordService.insertRepairTrackRecord(rtr);
            if (insertRtr <= 0) {
                throw new Exception("派单失败");
            }
        } catch (Exception e) {
            throw new Exception("派单失败");
        }
        return CommonResult.ok("派单成功");
    }

    /**
     * 协作下拉框
     *
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult dropDown(Integer recordId) throws Exception {

        RepairRecord rr = repairRecordService.getRepairById(recordId);

        if (rr == null) {
            return CommonResult.fail("维修单无效，获取维修师傅数据失败");
        }

        Integer buildId = rr.getBuildId();
        Integer articleId = rr.getArticleId();
        Integer schoolId = rr.getSchoolId();

        List<AutoDispatchUserVo> autoDispatchUserVos = repairUserService.dropDown(buildId, articleId, schoolId);

////        将可以接单的师傅显示到协作下拉框中（员工状态 大量接单：1）
//        LambdaQueryWrapper<RepairUser> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(RepairUser::getState, eUserStatu.Received.getValue())
//                .like(RepairUser::getArticle_id,articleId);
//
//        List<RepairUser> repairUsers = repairUserService.list(wrapper);

        return CommonResult.ok(autoDispatchUserVos);
    }

    /**
     * 转单审核
     *
     * @param tr
     * @param bindingResult
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult transfer(transferRequest tr, @RequestHeader("user_head") String userhead, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        if (eApprovalStatu.Agree.getValue() == tr.getApproverStatu() && tr.getUserId() == null) {
            return CommonResult.fail("被派单师傅不能为空");
        }

        //获取转单审核数据
        RepairTransferRecord transferRecord = repairTransferRecordService.getById(tr.getId());
        if (transferRecord == null) {
            return CommonResult.fail("转单审核数据为空，审核失败");
        }

        RepairRecord rr = repairRecordService.getRepairById(transferRecord.getRecordId());
        if (rr == null) {
            return CommonResult.fail("维修单数据不存在，审核失败");
        }

        if (rr.getMaintenanceState() != eRecordStatu.Transfer.getValue()) {
            return CommonResult.fail("维修单状态异常，审核失败");
        }

        String userID = AesUtils.decrypt(userhead);
        RepairAdmin operateData = repairAdminService.getRepairById(userID);
        if (operateData == null) {
            return CommonResult.fail("操作人信息不合法，无法进行回复！");
        }

        //发送人
        Integer senderId = 0;//超级管理员用ID为0
        if (!operateData.getIsSuper().equals(eIsSuper.Super.getValue())) {
            RepairUser user = repairUserService.getRepairByCardNumber(operateData.getCardNumber());
            if (user == null) {
                return CommonResult.fail("管理端账号未与移动端账号绑定，无法进行审核！");
            }
            senderId = user.getId();
        }

        try {
            //转单表中的审批操作人和审批状态
            transferRecord.setApproverId(senderId);
            transferRecord.setApprovalStatu(tr.getApproverStatu());
            int insertTransfer = repairTransferRecordService.insertRepairTransferRecord(transferRecord);
            if (insertTransfer <= 0) {
                throw new Exception("转单审核失败！");
            }

            if (eApprovalStatu.Agree.getValue() == tr.getApproverStatu()) {
                //被派单用户
                RepairUser repairUser = repairUserService.getById(tr.getUserId());
                if (repairUser == null) {
                    return CommonResult.fail("被派单师傅数据异常，审核失败");
                }

                //同意
                //添加派单记录
                RepairDispatchRecord repairDispatchRecord = new RepairDispatchRecord();
                repairDispatchRecord.setRecordId(rr.getId());
                repairDispatchRecord.setUsersId(tr.getUserId());
                repairDispatchRecord.setAssignedTime(new Date());
                repairDispatchRecord.setAcceptanceAssessTime(repairUser.getAcceptanceAssessTime());
                repairDispatchRecord.setOrderType(eDispatchType.Transfer.getValue());
                repairDispatchRecord.setIsLoseEfficacy(eDispatchResult.NotExpired.getValue());
                int insertDispatch = repairDispatchRecordService.insertRepairDispatchRecord(repairDispatchRecord);
                if (insertDispatch <= 0) {
                    throw new Exception("转单审核失败！");
                }

            }

            //同意的时候将维修表的状态改成派单中
            //拒绝的时候将维修表的状态改成转单前的
            rr.setMaintenanceState(eRecordStatu.Dispatch.getValue());
            int updateRecord = repairRecordService.updateRepairRecord(rr);
            if (updateRecord <= 0) {
                throw new Exception("转单审核失败！");
            }

            RepairSystemMessages rsm = new RepairSystemMessages();
            rsm.setIsRead(0);
            rsm.setRecordId(transferRecord.getRecordId());
            rsm.setRecipientId(transferRecord.getApproverId());
            rsm.setContent(eApprovalStatu.Agree.getValue() == tr.getApproverStatu() ? "您的审核工单已通过！" : "您的审核工单被驳回");
            int insertRsm = repairSystemMessagesService.insertRepairSystemMessages(rsm);
            if (insertRsm <= 0) {
                throw new Exception("转单审核失败！");
            }

            //添加跟踪记录
            RepairTrackRecord repairTrackRecord = new RepairTrackRecord();
            repairTrackRecord.setRecordId(rr.getId());
            repairTrackRecord.setContent("转单审核");
            repairTrackRecord.setMaintenanceState(rr.getMaintenanceState());
            int insertTrack = repairTrackRecordService.insertRepairTrackRecord(repairTrackRecord);
            if (insertTrack <= 0) {
                throw new Exception("转单审核失败！");
            }

        } catch (Exception e) {
            throw new Exception("转单审核失败！");
        }

        return CommonResult.ok("转单审核成功");
    }

    @Override
    public CommonResult cooperationDetail(int recordId) {
        RepairRecord rr = repairRecordService.getRepairById(recordId);
        if (rr == null) {
            return CommonResult.fail("维修单数据不存在，获取协作审核失败");
        }

        if (rr.getMaintenanceState() != eRecordStatu.Collaborate.getValue()) {
            return CommonResult.fail("维修单状态异常，获取协作审核失败");
        }
        CollaborateDetailVo result = repairCollaborateRecordService.queryCollaborateDetail(recordId);
        if (result == null) {
            return CommonResult.fail("协作审核数据不存在，无法进行协作审核操作");
        }
        String[] cns = result.getCollaboratorName().split(",");
        List<CollaborateDataVo> cdvs = new ArrayList<>();
        for (int i = 0; i < cns.length; i++) {
            String[] cn = cns[i].split("-");
            CollaborateDataVo cdv = new CollaborateDataVo();
            cdv.setId(Integer.valueOf(cn[0]));
            cdv.setName(cn[1]);
            cdvs.add(cdv);
        }
        result.setCollaboratorDatas(cdvs);
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult transferDetail(int recordId) {
        RepairRecord rr = repairRecordService.getRepairById(recordId);
        if (rr == null) {
            return CommonResult.fail("维修单数据不存在，获取转单审核失败");
        }

        if (rr.getMaintenanceState() != eRecordStatu.Transfer.getValue()) {
            return CommonResult.fail("维修单状态异常，获取转单审核失败");
        }

        TransferDetailVo result = repairTransferRecordService.queryTransferDetail(recordId);
        if (result == null) {
            return CommonResult.fail("转单审核数据不存在，无法进行转单审核操作");
        }

        return CommonResult.ok(result);
    }

    /**
     * 协作审核
     *
     * @param cor
     * @param bindingResult
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult cooperationOrders(cooperationOrdersRequest cor, @RequestHeader("user_head") String userhead, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        //获取协作审核数据
        RepairCollaborateRecord collaborateRecord = repairCollaborateRecordService.getById(cor.getId());
        if (collaborateRecord == null) {
            return CommonResult.fail("协作审核数据为空，审核失败");
        }

        RepairRecord rr = repairRecordService.getRepairById(collaborateRecord.getRecordId());
        if (rr == null) {
            return CommonResult.fail("维修单数据不存在，审核失败");
        }

        if (rr.getMaintenanceState() != eRecordStatu.Collaborate.getValue()) {
            return CommonResult.fail("维修单状态异常，审核失败");
        }

        //批量获取人用户信息
        List<RepairUser> repairUsers = repairUserService.getRepairs(cor.getCollaborator());

        String userID = AesUtils.decrypt(userhead);
        RepairAdmin operateData = repairAdminService.getRepairById(userID);
        if (operateData == null) {
            return CommonResult.fail("操作人信息不合法，无法进行回复！");
        }

        //发送人
        Integer senderId = 0;//超级管理员用ID为0
        if (!operateData.getIsSuper().equals(eIsSuper.Super.getValue())) {
            RepairUser user = repairUserService.getRepairByCardNumber(operateData.getCardNumber());
            if (user == null) {
                return CommonResult.fail("管理端账号未与移动端账号绑定，无法进行审核！");
            }
            senderId = user.getId();
        }

        try {
            //转单表中的审批操作人和审批状态
            collaborateRecord.setApproverId(senderId);
            collaborateRecord.setApprovalStatu(cor.getApproverStatu());
            int insertCollaborate = repairCollaborateRecordService.insertRepairCollaborateRecord(collaborateRecord);
            if (insertCollaborate <= 0) {
                throw new Exception("协作审核失败！");
            }
            //同意 批量添加派单数据
            if (eApprovalStatu.Agree.getValue() == cor.getApproverStatu()) {
                List<RepairDispatchRecord> rdrs = new ArrayList<>();
                //获取维修数据
                for (RepairUser ru : repairUsers) {
                    RepairDispatchRecord rdr = new RepairDispatchRecord();
                    rdr.setRecordId(rr.getId());
                    rdr.setUsersId(ru.getId());
                    rdr.setAssignedTime(new Date());
                    rdr.setAcceptanceAssessTime(ru.getAcceptanceAssessTime());
                    rdr.setOrderType(eDispatchType.Cooperation.getValue());
                    rdr.setIsLoseEfficacy(eDispatchResult.NotExpired.getValue());
                    rdrs.add(rdr);
                }

                if (rdrs.size() > 0) {
                    boolean insertDispatch = repairDispatchRecordService.insertDispatchBatch(rdrs);
                    if (!insertDispatch) {
                        throw new Exception("协作审核失败！");
                    }
                }
            }

            //同意的时候将维修表的状态改成派单中
            //拒绝的时候将维修表的状态改成转单前的状态
            if (rr.getMaintenancerId() != null) {
                //说明有人接单了 那就把状态改为维修中
                rr.setMaintenanceState(eRecordStatu.Maintenance.getValue());
            } else {
                rr.setMaintenanceState(eRecordStatu.Dispatch.getValue());
            }

            int updateRecord = repairRecordService.updateRepairRecord(rr);
            if (updateRecord <= 0) {
                throw new Exception("协作审核失败！");
            }

            RepairSystemMessages rsm = new RepairSystemMessages();
            rsm.setIsRead(0);
            rsm.setRecordId(collaborateRecord.getRecordId());
            rsm.setRecipientId(collaborateRecord.getApproverId());
            rsm.setContent(eApprovalStatu.Agree.getValue() == cor.getApproverStatu() ? "您的审核工单已通过！" : "您的审核工单被驳回");
            int insertRsm = repairSystemMessagesService.insertRepairSystemMessages(rsm);
            if (insertRsm <= 0) {
                throw new Exception("协作审核失败！");
            }

            //添加跟踪记录
            RepairTrackRecord rtr = new RepairTrackRecord();
            rtr.setRecordId(collaborateRecord.getRecordId());
            rtr.setMaintenanceState(rr.getMaintenanceState());
            rtr.setContent("协作审批");
            int insertRtr = repairTrackRecordService.insertRepairTrackRecord(rtr);
            if (insertRtr <= 0) {
                throw new Exception("协作审核失败！");
            }
        } catch (Exception e) {
            throw new Exception("协作审核失败！");
        }

        return CommonResult.ok("协作审核成功");
    }

    /**
     * 详情展示
     *
     * @param recordId
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult details(int recordId) throws Exception {
        //工单信息数据
        DetailsVo rr = repairRecordService.queryDetailData(recordId);
        if (rr == null) {
            return CommonResult.fail("工单信息失效，获取详情数据失败");
        }

        List<TrackDetailsVo> tracks = repairTrackRecordService.queryDetailData(recordId);

        if (tracks.size() > 0) {
            rr.setTracks(tracks);
        }

        return CommonResult.ok(rr);
    }


    /**
     * 改价显示
     *
     * @param recordId
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult alterPriceShow(int recordId) throws Exception {
        if (ObjectUtils.isEmpty(recordId)) {
            return CommonResult.fail("显示失败");
        }
        LambdaQueryWrapper<RepairConsumables> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RepairConsumables::getRecordId, recordId);

        List<RepairConsumables> repairConsumablesList = repairConsumablesService.list(wrapper);

        if (ObjectUtils.isEmpty(repairConsumablesList) || repairConsumablesList.size() <= 0) {
            return CommonResult.fail("显示失败");
        }

        List<AlterPriceShowVo> vos = new ArrayList<>();

        repairConsumablesList.forEach(rc -> {
            Integer articleId = rc.getArticleId();
            RepairArticleType articleType = repairArticleTypeService.getById(articleId);
            AlterPriceShowVo alterPriceShowVo = new AlterPriceShowVo();
            if (ObjectUtils.isNotEmpty(articleType)) {
                alterPriceShowVo.setArticleName(articleType.getName());
            }
            alterPriceShowVo.setRepairConsumables(rc);
            vos.add(alterPriceShowVo);
        });

        return CommonResult.ok(vos);
    }

    /**
     * 物品种类下拉框
     *
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult alterPriceGoodsType(int schoolId) throws Exception {
        if (ObjectUtils.isEmpty(schoolId)) {
            return CommonResult.fail("显示失败");
        }

        LambdaQueryWrapper<RepairArticleType> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(RepairArticleType::getId, RepairArticleType::getName)
                .eq(RepairArticleType::getSchoolId, schoolId)
                .eq(RepairArticleType::getParentId, 0);

        List<RepairArticleType> list = repairArticleTypeService.list(wrapper);
        return CommonResult.ok(list);

    }

    /**
     * 耗材名称下拉
     *
     * @param articleId
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult alterPriceGoodsName(int schoolId, int articleId) throws Exception {
        if (ObjectUtils.isEmpty(schoolId) || ObjectUtils.isEmpty(articleId)) {
            return CommonResult.fail("显示失败");
        }


        LambdaQueryWrapper<RepairArticleType> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(RepairArticleType::getId)
                .eq(RepairArticleType::getParentId, articleId);
        List<RepairArticleType> list = repairArticleTypeService.list(wrapper);
        if (ObjectUtils.isEmpty(list) || list.size() <= 0) {
            return CommonResult.fail("显示失败");
        }

        ArrayList<String> strings = new ArrayList<>();
        strings.add(articleId + "");

        list.forEach(repairArticleType -> {
            String id = repairArticleType.getId() + "";
            strings.add(id);
        });

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < strings.size(); i++) {
            sb.append("FIND_IN_SET(" + strings.get(i) + ",article_id)");
            if (i + 1 != strings.size()) {
                sb.append(" or ");
            }
        }

        String sss = sb.toString();

        List<RepairArticleConsumeVo> consumeResult = repairConsumeService.queryArticleConsumesName(sss, schoolId);


        return CommonResult.ok(consumeResult);
    }

    /**
     * 改价
     *
     * @param apr
     * @param bindingResult
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult alterPrice(AlterPriceRequest apr, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        try {


            Integer recordId = apr.getRecordId();

            List<Integer> ids = apr.getIds();

            List<RepairConsumables> repairConsumables = apr.getRepairConsumablesList();
//        当前报修记录
            LambdaQueryWrapper<RepairConsumables> wrapper = new LambdaQueryWrapper<>();
            wrapper.select(RepairConsumables::getId)
                    .eq(RepairConsumables::getRecordId, recordId);
            List<RepairConsumables> list = repairConsumablesService.list(wrapper);
            if (ObjectUtils.isEmpty(list) || list.size() <= 0) {
                return CommonResult.fail("改价失败");
            }

            ArrayList<Integer> integers = new ArrayList<>();
            list.forEach(rc -> {
                Integer id = rc.getId();
                integers.add(id);
            });

//        当为空时，维修是不要耗材的
            if (ObjectUtils.isEmpty(repairConsumables)) {
                boolean remove = repairConsumablesService.removeByIds(integers);
                if (!remove) {
                    throw new Exception("改价失败");
                }
            } else {
//            将参数有主键的则修改，没有的则添加
                for (RepairConsumables repairConsumable : repairConsumables) {
                    Integer id = repairConsumable.getId();
                    if (ObjectUtils.isEmpty(id)) {
                        boolean save = repairConsumablesService.save(repairConsumable);
                        if (!save) {
                            throw new Exception("改价失败");
                        }
                    } else {
                        boolean update = repairConsumablesService.updateById(repairConsumable);
                        if (!update) {
                            throw new Exception("改价失败");
                        }
                    }
                }
                for (Integer integer : integers) {
                    if (!(ids.contains(integer))) {
                        boolean b = repairConsumablesService.removeById(integer);
                        if (!b) {
                            throw new Exception("改价失败");
                        }
                    }
                }
            }
//        修改维修表总价
            RepairRecord repairRecord = repairRecordService.getById(recordId);
            if (ObjectUtils.isEmpty(repairRecord)) {
                return CommonResult.fail("改价失败");
            }

            repairRecord.setPrice(apr.getTotalPrice());
            boolean update = repairRecordService.updateById(repairRecord);

            //        追踪记录表
            RepairTrackRecord repairTrackRecord = new RepairTrackRecord();
            repairTrackRecord.setRecordId(recordId);
            repairTrackRecord.setMaintenanceState(eRecordStatu.ToConfirmed.getValue());
            repairTrackRecord.setContent("改价");
            boolean save = repairTrackRecordService.save(repairTrackRecord);

            if (!(update && save)) {
                throw new Exception("改价失败");
            }

        } catch (Exception e) {
            throw new Exception("改价失败");
        }
        return CommonResult.ok("改价成功");
    }

    /**
     * 延时
     *
     * @param dr
     * @param bindingResult
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult delayed(DelayedRequest dr, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        try {

            //接单考核时间大于0 去修改派单记录表的数据
            if (dr.getReceiving() > 0) {
                List<RepairDispatchRecord> list = repairDispatchRecordService.queryDispatchByRecordId(dr.getRecordId());
                if (list.size() > 0) {
                    for (RepairDispatchRecord data : list) {
                        data.setAcceptanceAssessTime(data.getAcceptanceAssessTime() + dr.getReceiving());
                    }

                    boolean updateBatch = repairDispatchRecordService.updateDispatchBatch(list);
                    if (!updateBatch) {
                        throw new Exception("延时失败");
                    }
                }
            }

            //接单记录
            if (dr.getMaintain() > 0) {
                List<RepairReceiving> list = repairReceivingService.queryReceivingByRecordId(dr.getRecordId());
                if (list.size() > 0) {

                    for (RepairReceiving data : list) {
                        data.setMaintenanceAssessTime(data.getMaintenanceAssessTime() + dr.getMaintain());
                    }

                    boolean updateBatch = repairReceivingService.updateReceivingBatch(list);
                    if (!updateBatch) {
                        throw new Exception("延时失败");
                    }
                }

            }

            //追踪记录表
            RepairTrackRecord repairTrackRecord = new RepairTrackRecord();
            repairTrackRecord.setRecordId(dr.getRecordId());
            repairTrackRecord.setContent("延时");
            boolean save = repairTrackRecordService.save(repairTrackRecord);

            if (!save) {
                throw new Exception("延时失败");
            }
        } catch (Exception e) {
            throw new Exception("延时失败");
        }

        return CommonResult.ok("延时成功");
    }

    /**
     * 回复
     * 在订单是待确认状态下 用户能发送信息给后勤 此时订单状态改为待处理
     * 后勤回复信息给用户   此时订单状态为待确认
     * 如果是管理端回复的则记录
     *
     * @param rr
     * @param bindingResult
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult revert(@RequestBody RevertRequest rr, @RequestHeader("user_head") String userhead, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        try {
            String userID = AesUtils.decrypt(userhead);
            RepairAdmin operateData = repairAdminService.getRepairById(userID);
            if (operateData == null) {
                return CommonResult.fail("操作人信息不合法，无法进行回复！");
            }

            //发送人
            Integer senderId = 0;//超级管理员用ID为0
            if (!operateData.getIsSuper().equals(eIsSuper.Super.getValue())) {
                RepairUser user = repairUserService.getRepairByCardNumber(operateData.getCardNumber());
                if (user == null) {
                    return CommonResult.fail("管理端账号未与移动端账号绑定，无法进行回复！");
                }
                senderId = user.getId();
            }

            RepairRecord data = repairRecordService.getRepairById(rr.getRecordId());
            if (data == null) {
                return CommonResult.fail("报修单已失效，无法进行回复");
            }

            if (data.getMaintenanceState() != eRecordStatu.ToLogistics.getValue()) {
                return CommonResult.fail("待处理维修单才能进行回复操作");
            }

            //接收人
            Integer recipientId = data.getUserId();

            //将工单数据状态改为待确认
            data.setMaintenanceState(eRecordStatu.ToConfirmed.getValue());
            int result = repairRecordService.updateRepairRecord(data);
            if (result <= 0) {
                throw new Exception("回复失败！");
            }

            //内容
            String content = rr.getContent();

            RepairLeaveMessage repairLeaveMessage = new RepairLeaveMessage();
            repairLeaveMessage.setRecordId(data.getId());
            repairLeaveMessage.setRecipientId(recipientId);
            repairLeaveMessage.setSenderId(senderId);
            repairLeaveMessage.setContent(content);

            boolean mSave = repairLeaveMessageService.save(repairLeaveMessage);

            if (!mSave) {
                throw new Exception("回复失败！");
            }

            //        追踪记录表
            RepairTrackRecord repairTrackRecord = new RepairTrackRecord();
            repairTrackRecord.setRecordId(data.getId());
            repairTrackRecord.setContent("回复");
            boolean save = repairTrackRecordService.save(repairTrackRecord);

            if (!save) {
                throw new Exception("回复失败！");
            }

        } catch (Exception e) {
            throw new Exception("回复失败！");
        }

        return CommonResult.ok("回复成功");
    }

    @Override
    public void reportExport(HttpServletResponse response, String keyWord, String startTime, String endTime, Integer schoolId, Integer state, String userhead) throws Exception {

        if (startTime != null && endTime != null) {
            try {
                startTime = TimeExchange.getStartOfDayStr(TimeExchange.StringToDate(startTime, "yyyy-MM-dd"));
                endTime = TimeExchange.getEndOfDayStr(TimeExchange.StringToDate(endTime, "yyyy-MM-dd"));
            } catch (Exception e) {
            }
        }

//        状态为0是查全部，为1查派单中。为2查维修中，3查转后勤，4是查转单待审核，协作待审核，5是查已完成，6是查已取消，7是查已关单，8是查维修中值班单


//        String userID = AesUtils.decrypt(userhead);
//        RepairAdmin operateData = repairAdminService.getRepairById(userID);
//        if (operateData == null) {
////            return CommonResult.fail("操作人信息不合法，无法获取工单数据！");
//            return;
//        }
//
//        if (operateData.getIsSuper().equals(eIsSuper.Super.getValue())) {
//            schoolId = null;
//        }


        List<ReportExportVo> result = null;


        String stateStr = null;
        switch (state) {
            case 0:
                result = repairRecordService.queryNotTimeoutReportExport(stateStr, keyWord, startTime, endTime, schoolId);
                for (ReportExportVo r : result) {
                    if (r.getMaintenanceState() == 1) {
                        r.setState("待接单");
                    } else if (r.getMaintenanceState() == 2) {
                        r.setState("维修中");
                    } else if (r.getMaintenanceState() == 3) {
                        r.setState("待处理");
                    } else if (r.getMaintenanceState() == 5 || r.getMaintenanceState() == 6) {
                        r.setState("待审核");
                    } else if (r.getMaintenanceState() == 7) {
                        r.setState("已完成");
                    } else if (r.getMaintenanceState() == 8) {
                        r.setState("已取消");
                    } else if (r.getMaintenanceState() == 9) {
                        r.setState("已关单");
                    }
                }
                break;
            case 1:
                stateStr = "=1)";
                result = repairRecordService.queryNotTimeoutReportExport(stateStr, keyWord, startTime, endTime, schoolId);
                for (ReportExportVo r : result) {
                    //待接单才能关单
                    r.setState("待接单");
                }
                break;
            case 2:
                stateStr = "=2)";
                result = repairRecordService.queryNotTimeoutReportExport(stateStr, keyWord, startTime, endTime, schoolId);
                for (ReportExportVo r : result) {
                    r.setState("维修中");

                }
                break;
            case 3:
                stateStr = "=3)";
                result = repairRecordService.queryNotTimeoutReportExport(stateStr, keyWord, startTime, endTime, schoolId);
                for (ReportExportVo r : result) {
                    r.setState("待处理");

                }
                break;
            case 4:
                stateStr = "=5 or rr.maintenance_state = 6)";
                result = repairRecordService.queryNotTimeoutReportExport(stateStr, keyWord, startTime, endTime, schoolId);
                for (ReportExportVo r : result) {
                    r.setState("待审核");

                }
                break;
            case 5:
                stateStr = "=7)";
                result = repairRecordService.queryNotTimeoutReportExport(stateStr, keyWord, startTime, endTime, schoolId);
                for (ReportExportVo r : result) {
                    r.setState("已完成");
                }
                break;
            case 6:
                stateStr = "=8)";
                result = repairRecordService.queryNotTimeoutReportExport(stateStr, keyWord, startTime, endTime, schoolId);
                for (ReportExportVo r : result) {
                    r.setState("已取消");

                }
                break;
            case 7:
                stateStr = "=9)";
                result = repairRecordService.queryNotTimeoutReportExport(stateStr, keyWord, startTime, endTime, schoolId);
                for (ReportExportVo r : result) {
                    r.setState("已关单");
                }
                break;
            case 8:
                result = repairRecordService.queryTimeoutReportExport(keyWord, startTime, endTime, schoolId);
                for (ReportExportVo r : result) {
                    r.setState("已超时");
                }
                break;
            default:
                break;
        }

//        导出

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("报修记录表");
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("工单编号");
        headerRow.createCell(1).setCellValue("报修区域");
        headerRow.createCell(2).setCellValue("姓名");
        headerRow.createCell(3).setCellValue("电话");
        headerRow.createCell(4).setCellValue("报修物品");
        headerRow.createCell(5).setCellValue("故障描述");
        headerRow.createCell(6).setCellValue("状态");
        headerRow.createCell(7).setCellValue("报修时间");
        headerRow.createCell(8).setCellValue("技术员");

        for (int i = 0; i < result.size(); i++) {
            ReportExportVo reportExportVo = result.get(i);
            Row dataRow1 = sheet.createRow(i + 1);
            dataRow1.createCell(0).setCellValue(reportExportVo.getRecordNo());
            dataRow1.createCell(1).setCellValue(reportExportVo.getAreaName());
            dataRow1.createCell(2).setCellValue(reportExportVo.getUserName());
            dataRow1.createCell(3).setCellValue(reportExportVo.getUserPhone());
            dataRow1.createCell(4).setCellValue(reportExportVo.getArticleName());
            dataRow1.createCell(5).setCellValue(reportExportVo.getDescription());
            dataRow1.createCell(6).setCellValue(reportExportVo.getState());
            dataRow1.createCell(7).setCellValue(reportExportVo.getReportTime());
            dataRow1.createCell(8).setCellValue(reportExportVo.getMaintenancerName());

        }

        // 将工作簿写入文件
        ExcelUtils.excelDownload(workbook, "工单管理表.xlsx", response);

//        try (OutputStream outputStream = new FileOutputStream("报修记录表.xlsx")) {
//            EasyExcel.write(outputStream, ReportExportVo.class).sheet("报修记录表").doWrite(result);
//        }catch (Exception e){
//            e.printStackTrace();
//        }


    }

    @Override
    public CommonResult queryMasterHomeCount(int userId) {
        MasterHomepageVo result = repairRecordService.queryMasterHomeCount(userId);
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult queryLogisticHomeCount(int schoolId) {
        MasterHomepageVo result = repairRecordService.queryLogisticHomeCount(schoolId);
        return CommonResult.ok(result);
    }

    /**
     * 报修校区统计
     *
     * @param state
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult repairCampus(int state) throws Exception {


//    全部
        if (state == 0) {
            List<RepairCampusVo> campusVoList = repairRecordService.repairCampusTotal();
            return CommonResult.ok(campusVoList);
        }

        String startTime = DateUtils.startTime(state);
        String endTime = DateUtils.endTime();

        if (ObjectUtils.isEmpty(startTime)) {
            return CommonResult.fail("参数有误");
        }


        List<RepairCampusVo> campusVoList = repairRecordService.repairCampus(startTime, endTime);


        return CommonResult.ok(campusVoList);
    }

    /**
     * 报修区域
     *
     * @param state
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult repairArea(int state, int currentPage, int pageCount) throws Exception {
        //    全部
        if (state == 0) {
            IPage<RepairAreaVo> repairAreaTotal = repairRecordService.repairAreaTotal(currentPage, pageCount);
            return CommonResult.ok(repairAreaTotal);
        }

        String startTime = DateUtils.startTime(state);
        String endTime = DateUtils.endTime();

        if (ObjectUtils.isEmpty(startTime)) {
            return CommonResult.fail("参数有误");
        }

        IPage<RepairAreaVo> repairAreaTotal = repairRecordService.repairArea(startTime, endTime, currentPage, pageCount);

        return CommonResult.ok(repairAreaTotal);
    }

    /**
     * 报修区域导出
     *
     * @param state
     * @throws Exception
     */
    @Override
    public void repairAreaExport(HttpServletResponse response, int state) throws Exception {
        String startTime = DateUtils.startTime(state);
        String endTime = DateUtils.endTime();
        List<RepairAreaVo> vos = repairRecordService.repairAreaExport(startTime, endTime);

//        导出
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("报修区域统计");
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("序号");
        headerRow.createCell(1).setCellValue("学校名称");
        headerRow.createCell(2).setCellValue("报修区域");
        headerRow.createCell(3).setCellValue("工单总数");
        headerRow.createCell(4).setCellValue("有偿");
        headerRow.createCell(5).setCellValue("无偿");
        headerRow.createCell(6).setCellValue("耗材总费用");


        for (int i = 0; i < vos.size(); i++) {
            RepairAreaVo vo = vos.get(i);
            Row dataRow1 = sheet.createRow(i + 1);
            dataRow1.createCell(0).setCellValue(vo.getId());
            dataRow1.createCell(1).setCellValue(vo.getSchoolName());
            String areaName = vo.getAreaName();
            areaName = areaName.substring(5);
            dataRow1.createCell(2).setCellValue(areaName);
            dataRow1.createCell(3).setCellValue(vo.getOrdersTotal());
            dataRow1.createCell(4).setCellValue(vo.getOrdersCompensation());
            dataRow1.createCell(5).setCellValue(vo.getOrdersGratis());
            BigDecimal totalPrice = vo.getTotalPrice();
            if (ObjectUtils.isEmpty(totalPrice)) {
                totalPrice = new BigDecimal(0);
            }
            dataRow1.createCell(6).setCellValue(totalPrice.toString());

        }

        // 将工作簿写入文件
        ExcelUtils.excelDownload(workbook, "报修区域.xlsx", response);

    }

    /**
     * 维修物品
     *
     * @param state
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult maintenanceGoods(int state, int currentPage, int pageCount) throws Exception {
        //    全部
        if (state == 0) {
            IPage<MaintenanceGoodsVo> maintenanceGoodsVoList = repairRecordService.maintenanceGoodsTotal(currentPage, pageCount);
            return CommonResult.ok(maintenanceGoodsVoList);
        }

        String startTime = DateUtils.startTime(state);
        String endTime = DateUtils.endTime();

        if (ObjectUtils.isEmpty(startTime)) {
            return CommonResult.fail("参数有误");
        }

        IPage<MaintenanceGoodsVo> maintenanceGoodsVoList = repairRecordService.maintenanceGoods(startTime, endTime, currentPage, pageCount);
        return CommonResult.ok(maintenanceGoodsVoList);
    }

    @Override
    public void maintenanceGoodsExport(HttpServletResponse response, int state) throws Exception {
        String startTime = DateUtils.startTime(state);
        String endTime = DateUtils.endTime();

        List<MaintenanceGoodsVo> vos = repairRecordService.maintenanceGoodsExport(startTime, endTime);

        //        导出
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("维修物品统计");
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("序号");
        headerRow.createCell(1).setCellValue("名称");
        headerRow.createCell(2).setCellValue("工单总数");
        headerRow.createCell(3).setCellValue("有偿");
        headerRow.createCell(4).setCellValue("无偿");
        headerRow.createCell(5).setCellValue("耗材总费用");

        for (int i = 0; i < vos.size(); i++) {
            MaintenanceGoodsVo vo = vos.get(i);
            Row dataRow1 = sheet.createRow(i + 1);
            dataRow1.createCell(0).setCellValue(vo.getId());
            dataRow1.createCell(1).setCellValue(vo.getArticleName());
            dataRow1.createCell(2).setCellValue(vo.getOrdersTotal());
            dataRow1.createCell(3).setCellValue(vo.getOrdersCompensation());
            dataRow1.createCell(4).setCellValue(vo.getOrdersGratis());

            BigDecimal totalPrice = vo.getTotalPrice();
            if (ObjectUtils.isEmpty(totalPrice)) {
                totalPrice = new BigDecimal(0);
            }
            dataRow1.createCell(5).setCellValue(totalPrice.toString());

        }
        // 将工作簿写入文件
        ExcelUtils.excelDownload(workbook, "维修物品.xlsx", response);

    }

    /**
     * 维修师傅
     *
     * @param state
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult maintenanceWorker(int state, int currentPage, int pageCount) throws Exception {

        String startTime = DateUtils.startTime(state);
        String endTime = DateUtils.endTime();

        LambdaQueryWrapper<RepairReceiving> wrapperReceiving = new LambdaQueryWrapper<>();
        wrapperReceiving.between(ObjectUtils.isNotEmpty(startTime), RepairReceiving::getReceivingTime, startTime, endTime);
        wrapperReceiving.isNotNull(RepairReceiving::getFinishTime);
        wrapperReceiving.groupBy(RepairReceiving::getUserId);
        IPage<RepairReceiving> repairReceivingListPage = repairReceivingService.page(new Page<RepairReceiving>(currentPage,pageCount),wrapperReceiving);

        IPage<MaintenanceWorkerVo> page = new Page();
        BeanUtils.copyProperties(repairReceivingListPage,page);

        LambdaQueryWrapper<RepairReceiving> wrapperReceiving2 = new LambdaQueryWrapper<>();
        wrapperReceiving2.between(ObjectUtils.isNotEmpty(startTime), RepairReceiving::getReceivingTime, startTime, endTime);
        wrapperReceiving2.isNotNull(RepairReceiving::getFinishTime);
        List<RepairReceiving> repairReceivingList = repairReceivingService.list(wrapperReceiving2);
        if (ObjectUtils.isEmpty(repairReceivingList)&&repairReceivingList.size()<=0) {
            return CommonResult.ok(page);
        }

        ArrayList<Integer> userList = new ArrayList<>();

        ArrayList<Integer> recordList = new ArrayList<>();

//        接单总次数
        HashMap<Integer, List<Integer>> receivingCountMap = new HashMap<>();

//        维修超时次数
        Map<Integer, Integer> maintainTimeOutMap = new HashMap<>();

//        维修总时长
        Map<Integer, Long> maintainMap = new HashMap<>();


        for (RepairReceiving repairReceiving : repairReceivingList) {
            Date finishTime = repairReceiving.getFinishTime();

            if (ObjectUtils.isEmpty(finishTime)) {
                break;
            }

            Integer userId = repairReceiving.getUserId();
            userList.add(userId);

            Integer recordId = repairReceiving.getRecordId();
            recordList.add(recordId);

            if (receivingCountMap.containsKey(userId)) {
//                  总次数
                List<Integer> list = receivingCountMap.get(userId);
                list.add(recordId);
                receivingCountMap.put(userId, list);
            } else {
                ArrayList<Integer> list = new ArrayList<>();
                list.add(recordId);
                receivingCountMap.put(userId, list);
            }


//            接单时间
            long receivingTime = repairReceiving.getReceivingTime().getTime();
//            考核时间(分)
            Integer maintenanceAssessTime = repairReceiving.getMaintenanceAssessTime() * 1000 * 60;

            long time = receivingTime + maintenanceAssessTime;

//            完成时间
            long finishTimeTime = finishTime.getTime();

//              实际完成时间大于接单时间加考核时间为超时
            if (finishTimeTime > time) {
                if (maintainTimeOutMap.containsKey(userId)) {
//                  次数
                    Integer count = maintainTimeOutMap.get(userId);
                    count = count + 1;
                    maintainTimeOutMap.put(userId, count);
                } else {
                    maintainTimeOutMap.put(userId, 1);
                }
            }

//            维修时长=完成时间-接单时间(分)
            long maintainTime = finishTimeTime - receivingTime;
            maintainTime = maintainTime / 1000 / 60;

            if (maintainMap.containsKey(userId)) {
//                  维修时长
                Long maintainTotalTime = maintainMap.get(userId);
                maintainTotalTime = maintainTotalTime + maintainTime;
                maintainMap.put(userId, maintainTotalTime);
            } else {
                maintainMap.put(userId, maintainTime);
            }


        }


        //        接单总次数
        HashMap<Integer, List<Integer>> sendCountMap = new HashMap<>();

//        接单超时次数
        Map<Integer, Integer> sendTimeOutMap = new HashMap<>();

//        接单总时长
        Map<Integer, Long> sendMap = new HashMap<>();


        LambdaQueryWrapper<RepairDispatchRecord> rdrsWrapper = new LambdaQueryWrapper<>();
        rdrsWrapper.in(RepairDispatchRecord::getRecordId, recordList)
                .isNotNull(RepairDispatchRecord::getReceivingTime);


        List<RepairDispatchRecord> repairDispatchRecords = repairDispatchRecordService.list(rdrsWrapper);
        for (RepairDispatchRecord repairDispatchRecord : repairDispatchRecords) {
//                派单时间
            long sendTime = repairDispatchRecord.getAssignedTime().getTime();
//                接单考核时间
            Integer acceptanceAssessTime = repairDispatchRecord.getAcceptanceAssessTime() * 1000 * 60;

            long time = sendTime + acceptanceAssessTime;

//                接单时间
            long receivingTime = repairDispatchRecord.getReceivingTime().getTime();
//            接单人
            Integer usersId = repairDispatchRecord.getUsersId();
            Integer recordId = repairDispatchRecord.getRecordId();

            if (sendCountMap.containsKey(usersId)) {
                List<Integer> integers = sendCountMap.get(usersId);
                integers.add(recordId);
                sendCountMap.put(usersId, integers);
            } else {
                ArrayList<Integer> integers = new ArrayList<>();
                sendCountMap.put(usersId, integers);
            }


//              实际完成时间大于接单时间加考核时间为超时
            if (receivingTime > time) {
                if (sendTimeOutMap.containsKey(usersId)) {
//                  次数
                    Integer count = sendTimeOutMap.get(usersId);
                    count = count + 1;
                    sendTimeOutMap.put(usersId, count);
                } else {
                    sendTimeOutMap.put(usersId, 1);
                }
            }

//            接单时长=接单时间-派单时间(分)
            Long orderReceivingTime = receivingTime - sendTime;
            orderReceivingTime = orderReceivingTime / 1000 / 60;

            if (sendMap.containsKey(usersId)) {
//                  接单总时长
                Long sendTotalTime = sendMap.get(usersId);
                sendTotalTime = sendTotalTime + orderReceivingTime;
                sendMap.put(usersId, sendTotalTime);
            } else {
                sendMap.put(usersId, orderReceivingTime);
            }


        }


        LambdaQueryWrapper<RepairUser> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.in(RepairUser::getId, userList);

        List<RepairUser> users = repairUserService.list(userWrapper);


        Wrapper<RepairSchool> schoolWrapper = new LambdaQueryWrapper<>();
        List<RepairSchool> schoolList = repairSchoolService.list(schoolWrapper);

        ArrayList<MaintenanceWorkerVo> vos = new ArrayList<>();

        users.forEach(repairUser -> {
            MaintenanceWorkerVo maintenanceWorkerVo = new MaintenanceWorkerVo();
//            名字
            maintenanceWorkerVo.setName(repairUser.getUserName());

//            学校
            Integer schoolId = repairUser.getSchoolId();
            for (RepairSchool repairSchool : schoolList) {
                Integer id = repairSchool.getId();
                if (id.equals(schoolId)) {
                    maintenanceWorkerVo.setSchoolName(repairSchool.getName());
                }
            }

            Integer id = repairUser.getId();
//            工单集合
            List<Integer> recordsList = receivingCountMap.get(id);
            int size = recordsList.size();
            BigDecimal b = new BigDecimal(size);

//            工单总数
            maintenanceWorkerVo.setWorkOrderTotal(size);

//            超时率
            if (maintainTimeOutMap.containsKey(id)) {
                Integer integer = maintainTimeOutMap.get(id);
                BigDecimal a = new BigDecimal(integer);
                BigDecimal c = b;
//                接单
                if (sendTimeOutMap.containsKey(id)) {
                    Integer integer1 = sendTimeOutMap.get(id);
                    BigDecimal bigDecimal = new BigDecimal(integer1);
                    a = a.add(bigDecimal);
                    List<Integer> integers = sendCountMap.get(id);
                    c = c.add(new BigDecimal(integers.size()));
                }

                BigDecimal bigDecimal = a.divide(c, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal multiply = bigDecimal.multiply(new BigDecimal(100));
                maintenanceWorkerVo.setTimeOut(multiply + "%");
            }

//            平均维修时间
            Long aLong = maintainMap.get(id);
            BigDecimal bigDecimal = new BigDecimal(aLong);
            BigDecimal maintain = bigDecimal.divide(b, 2, BigDecimal.ROUND_HALF_UP);
            maintenanceWorkerVo.setMaintain(maintain);

            //        平均响应时长
            if (sendMap.containsKey(id)) {
                Long aLong1 = sendMap.get(id);
                BigDecimal bigDecimal1 = new BigDecimal(aLong1);
                List<Integer> integers = sendCountMap.get(id);
                BigDecimal bigDecimal2 = new BigDecimal(integers.size());
                BigDecimal send = bigDecimal1.divide(bigDecimal2, 2, BigDecimal.ROUND_HALF_UP);
                maintenanceWorkerVo.setAnswer(send);
            }


//            总费用
            LambdaQueryWrapper<RepairRecord> recordWrapper = new LambdaQueryWrapper<>();
            recordWrapper.in(RepairRecord::getId, recordsList);

            List<RepairRecord> list = repairRecordService.list(recordWrapper);
            BigDecimal totalPrice = new BigDecimal(0);
            for (RepairRecord repairRecord : list) {
                BigDecimal price = repairRecord.getPrice();
                if (ObjectUtils.isNotEmpty(price)) {
                    totalPrice = totalPrice.add(price);
                }
            }
            maintenanceWorkerVo.setTotalPrice(totalPrice);

//            好评率
            LambdaQueryWrapper<RepairEvaluate> evaluateWrapper = new LambdaQueryWrapper<>();
            evaluateWrapper.in(RepairEvaluate::getRecordId, recordsList);
//      根据工单id来查询对应的评价
            List<RepairEvaluate> repairEvaluates = repairEvaluateService.list(evaluateWrapper);
            BigDecimal evaluatesDecimal = new BigDecimal(repairEvaluates.size());
            BigDecimal satisfaction = new BigDecimal(0);
            for (RepairEvaluate repairEvaluate : repairEvaluates) {
                Integer star = repairEvaluate.getStar();
                if (star > 3) {
                    satisfaction = satisfaction.add(new BigDecimal(1));
                }
            }
            BigDecimal degreePraise = satisfaction.divide(evaluatesDecimal, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
            maintenanceWorkerVo.setDegreePraise(degreePraise + "%");


            vos.add(maintenanceWorkerVo);

        });


        page.setRecords(vos);

        return CommonResult.ok(page);
    }

    /**
     * 维修师傅导出
     *
     * @param response
     * @param state
     * @throws Exception
     */
    @Override
    public void maintenanceWorkerExport(HttpServletResponse response, int state) throws Exception {
        String startTime = DateUtils.startTime(state);
        String endTime = DateUtils.endTime();

        ArrayList<MaintenanceWorkerVo> vos = new ArrayList<>();

        //        导出
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("维修师傅效率统计");
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("序号");
        headerRow.createCell(1).setCellValue("校区");
        headerRow.createCell(2).setCellValue("姓名");
        headerRow.createCell(3).setCellValue("工单总数");
        headerRow.createCell(4).setCellValue("好评率");
        headerRow.createCell(5).setCellValue("超时率");
        headerRow.createCell(6).setCellValue("平均响应时长(分)");
        headerRow.createCell(7).setCellValue("平均维修时长(分)");
        headerRow.createCell(8).setCellValue("耗材总费用(元)");


        LambdaQueryWrapper<RepairReceiving> wrapperReceiving = new LambdaQueryWrapper<>();
        wrapperReceiving.between(ObjectUtils.isNotEmpty(startTime), RepairReceiving::getReceivingTime, startTime, endTime);
        wrapperReceiving.isNotNull(RepairReceiving::getFinishTime);
        List<RepairReceiving> repairReceivingList = repairReceivingService.list(wrapperReceiving);

        if (ObjectUtils.isNotEmpty(repairReceivingList)&&repairReceivingList.size()>0) {


            ArrayList<Integer> userList = new ArrayList<>();

            ArrayList<Integer> recordList = new ArrayList<>();

//        接单总次数
            HashMap<Integer, List<Integer>> receivingCountMap = new HashMap<>();

//        维修超时次数
            Map<Integer, Integer> maintainTimeOutMap = new HashMap<>();

//        维修总时长
            Map<Integer, Long> maintainMap = new HashMap<>();


            for (RepairReceiving repairReceiving : repairReceivingList) {
                Date finishTime = repairReceiving.getFinishTime();

                if (ObjectUtils.isEmpty(finishTime)) {
                    break;
                }

                Integer userId = repairReceiving.getUserId();
                userList.add(userId);

                Integer recordId = repairReceiving.getRecordId();
                recordList.add(recordId);

                if (receivingCountMap.containsKey(userId)) {
//                  总次数
                    List<Integer> list = receivingCountMap.get(userId);
                    list.add(recordId);
                    receivingCountMap.put(userId, list);
                } else {
                    ArrayList<Integer> list = new ArrayList<>();
                    list.add(recordId);
                    receivingCountMap.put(userId, list);
                }


//            接单时间
                long receivingTime = repairReceiving.getReceivingTime().getTime();
//            考核时间(分)
                Integer maintenanceAssessTime = repairReceiving.getMaintenanceAssessTime() * 1000 * 60;

                long time = receivingTime + maintenanceAssessTime;

//            完成时间
                long finishTimeTime = finishTime.getTime();

//              实际完成时间大于接单时间加考核时间为超时
                if (finishTimeTime > time) {
                    if (maintainTimeOutMap.containsKey(userId)) {
//                  次数
                        Integer count = maintainTimeOutMap.get(userId);
                        count = count + 1;
                        maintainTimeOutMap.put(userId, count);
                    } else {
                        maintainTimeOutMap.put(userId, 1);
                    }
                }

//            维修时长=完成时间-接单时间(分)
                long maintainTime = finishTimeTime - receivingTime;
                maintainTime = maintainTime / 1000 / 60;

                if (maintainMap.containsKey(userId)) {
//                  维修时长
                    Long maintainTotalTime = maintainMap.get(userId);
                    maintainTotalTime = maintainTotalTime + maintainTime;
                    maintainMap.put(userId, maintainTotalTime);
                } else {
                    maintainMap.put(userId, maintainTime);
                }

            }


            //        接单总次数
            HashMap<Integer, List<Integer>> sendCountMap = new HashMap<>();

//        接单超时次数
            Map<Integer, Integer> sendTimeOutMap = new HashMap<>();

//        接单总时长
            Map<Integer, Long> sendMap = new HashMap<>();


            LambdaQueryWrapper<RepairDispatchRecord> rdrsWrapper = new LambdaQueryWrapper<>();
            rdrsWrapper.in(RepairDispatchRecord::getRecordId, recordList)
                    .isNotNull(RepairDispatchRecord::getReceivingTime);


            List<RepairDispatchRecord> repairDispatchRecords = repairDispatchRecordService.list(rdrsWrapper);
            for (RepairDispatchRecord repairDispatchRecord : repairDispatchRecords) {
//                派单时间
                long sendTime = repairDispatchRecord.getAssignedTime().getTime();
//                接单考核时间
                Integer acceptanceAssessTime = repairDispatchRecord.getAcceptanceAssessTime() * 1000 * 60;

                long time = sendTime + acceptanceAssessTime;

//                接单时间
                long receivingTime = repairDispatchRecord.getReceivingTime().getTime();
//            接单人
                Integer usersId = repairDispatchRecord.getUsersId();
                Integer recordId = repairDispatchRecord.getRecordId();

                if (sendCountMap.containsKey(usersId)) {
                    List<Integer> integers = sendCountMap.get(usersId);
                    integers.add(recordId);
                    sendCountMap.put(usersId, integers);
                } else {
                    ArrayList<Integer> integers = new ArrayList<>();
                    sendCountMap.put(usersId, integers);
                }


//              实际完成时间大于接单时间加考核时间为超时
                if (receivingTime > time) {
                    if (sendTimeOutMap.containsKey(usersId)) {
//                  次数
                        Integer count = sendTimeOutMap.get(usersId);
                        count = count + 1;
                        sendTimeOutMap.put(usersId, count);
                    } else {
                        sendTimeOutMap.put(usersId, 1);
                    }
                }

//            接单时长=接单时间-派单时间(分)
                Long orderReceivingTime = receivingTime - sendTime;
                orderReceivingTime = orderReceivingTime / 1000 / 60;

                if (sendMap.containsKey(usersId)) {
//                  接单总时长
                    Long sendTotalTime = sendMap.get(usersId);
                    sendTotalTime = sendTotalTime + orderReceivingTime;
                    sendMap.put(usersId, sendTotalTime);
                } else {
                    sendMap.put(usersId, orderReceivingTime);
                }


            }


            LambdaQueryWrapper<RepairUser> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.in(RepairUser::getId, userList);

            List<RepairUser> users = repairUserService.list(userWrapper);


            Wrapper<RepairSchool> schoolWrapper = new LambdaQueryWrapper<>();
            List<RepairSchool> schoolList = repairSchoolService.list(schoolWrapper);


            users.forEach(repairUser -> {
                MaintenanceWorkerVo maintenanceWorkerVo = new MaintenanceWorkerVo();
//            名字
                maintenanceWorkerVo.setName(repairUser.getUserName());

//            学校
                Integer schoolId = repairUser.getSchoolId();
                for (RepairSchool repairSchool : schoolList) {
                    Integer id = repairSchool.getId();
                    if (id.equals(schoolId)) {
                        maintenanceWorkerVo.setSchoolName(repairSchool.getName());
                    }
                }

                Integer id = repairUser.getId();
//            工单集合
                List<Integer> recordsList = receivingCountMap.get(id);
                int size = recordsList.size();
                BigDecimal b = new BigDecimal(size);

//            工单总数
                maintenanceWorkerVo.setWorkOrderTotal(size);

//            超时率
                if (maintainTimeOutMap.containsKey(id)) {
                    Integer integer = maintainTimeOutMap.get(id);
                    BigDecimal a = new BigDecimal(integer);
                    BigDecimal c = b;
//                接单
                    if (sendTimeOutMap.containsKey(id)) {
                        Integer integer1 = sendTimeOutMap.get(id);
                        BigDecimal bigDecimal = new BigDecimal(integer1);
                        a = a.add(bigDecimal);
                        List<Integer> integers = sendCountMap.get(id);
                        c = c.add(new BigDecimal(integers.size()));
                    }

                    BigDecimal bigDecimal = a.divide(c, 2, BigDecimal.ROUND_HALF_UP);
                    BigDecimal multiply = bigDecimal.multiply(new BigDecimal(100));
                    maintenanceWorkerVo.setTimeOut(multiply + "%");
                }

//            平均维修时间
                Long aLong = maintainMap.get(id);
                BigDecimal bigDecimal = new BigDecimal(aLong);
                BigDecimal maintain = bigDecimal.divide(b, 2, BigDecimal.ROUND_HALF_UP);
                maintenanceWorkerVo.setMaintain(maintain);

                //        平均响应时长
                if (sendMap.containsKey(id)) {
                    Long aLong1 = sendMap.get(id);
                    BigDecimal bigDecimal1 = new BigDecimal(aLong1);
                    List<Integer> integers = sendCountMap.get(id);
                    BigDecimal bigDecimal2 = new BigDecimal(integers.size());
                    BigDecimal send = bigDecimal1.divide(bigDecimal2, 2, BigDecimal.ROUND_HALF_UP);
                    maintenanceWorkerVo.setAnswer(send);
                }


//            总费用
                LambdaQueryWrapper<RepairRecord> recordWrapper = new LambdaQueryWrapper<>();
                recordWrapper.in(RepairRecord::getId, recordsList);

                List<RepairRecord> list = repairRecordService.list(recordWrapper);
                BigDecimal totalPrice = new BigDecimal(0);
                for (RepairRecord repairRecord : list) {
                    BigDecimal price = repairRecord.getPrice();
                    if (ObjectUtils.isNotEmpty(price)) {
                        totalPrice = totalPrice.add(price);
                    }
                }
                maintenanceWorkerVo.setTotalPrice(totalPrice);

//            好评率
                LambdaQueryWrapper<RepairEvaluate> evaluateWrapper = new LambdaQueryWrapper<>();
                evaluateWrapper.in(RepairEvaluate::getRecordId, recordsList);
//      根据工单id来查询对应的评价
                List<RepairEvaluate> repairEvaluates = repairEvaluateService.list(evaluateWrapper);
                BigDecimal evaluatesDecimal = new BigDecimal(repairEvaluates.size());
                BigDecimal satisfaction = new BigDecimal(0);
                for (RepairEvaluate repairEvaluate : repairEvaluates) {
                    Integer star = repairEvaluate.getStar();
                    if (star > 3) {
                        satisfaction = satisfaction.add(new BigDecimal(1));
                    }
                }
                BigDecimal degreePraise = satisfaction.divide(evaluatesDecimal, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
                maintenanceWorkerVo.setDegreePraise(degreePraise + "%");


                vos.add(maintenanceWorkerVo);
            });

            for (int i = 0; i < vos.size(); i++) {
                MaintenanceWorkerVo vo = vos.get(i);
                Row dataRow1 = sheet.createRow(i + 1);
                dataRow1.createCell(0).setCellValue(i+1);
                dataRow1.createCell(1).setCellValue(vo.getSchoolName());
                dataRow1.createCell(2).setCellValue(vo.getName());
                dataRow1.createCell(3).setCellValue(vo.getWorkOrderTotal());
                dataRow1.createCell(4).setCellValue( vo.getDegreePraise());
                dataRow1.createCell(5).setCellValue( vo.getTimeOut());
                BigDecimal answer = vo.getAnswer();
                if (ObjectUtils.isEmpty(answer)) {
                    answer=new BigDecimal(0);
                }
                dataRow1.createCell(6).setCellValue(answer.toString());
                BigDecimal maintain = vo.getMaintain();
                if (ObjectUtils.isEmpty(maintain)) {
                    maintain=new BigDecimal(0);
                }

                dataRow1.createCell(7).setCellValue(maintain.toString());

                BigDecimal totalPrice = vo.getTotalPrice();
                if (ObjectUtils.isEmpty(totalPrice)) {
                    totalPrice=new BigDecimal(0);
                }
                dataRow1.createCell(8).setCellValue( totalPrice.toString());

            }
        }
        // 将工作簿写入文件
        ExcelUtils.excelDownload(workbook, "维修师傅.xlsx", response);


    }

    /**
     * 报修分析
     *
     * @param state
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult repairAnalysis(int state) throws Exception {
        String startTime = DateUtils.startTime(state);
        String endTime = DateUtils.endTime();

        RepairAnalysisVo vo=repairRecordService.repairAnalysis(startTime,endTime);

        Integer timeOutCount=repairRecordService.queryTimeOut(startTime,endTime);

        vo.setTimeOutCount(timeOutCount);

        return CommonResult.ok(vo);
    }

    /**
     * 报修趋势
     *
     * @param state
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult repairTrend(int state) throws Exception {

        DateTimeFormatter dateTimeFormatter= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

//        本次时间
        String startTime = DateUtils.trendStartTime(state);
        LocalDateTime sTime = LocalDateTime.parse(startTime, dateTimeFormatter);

        int monthValue = sTime.getMonthValue();

//        对比时间
        String doubleStartTime = DateUtils.trendComparisonStartTime(state);
        LocalDateTime dsTime = LocalDateTime.parse(doubleStartTime, dateTimeFormatter);

        ArrayList<RepairTrendVo> vos = new ArrayList<>();
//      本年和去年
        if (state==1) {

            for (int i = 1; i <= 12; i++) {
                RepairTrendVo repairTrendVo = new RepairTrendVo();

                LocalDateTime dateTime = sTime.plusMonths(i - 1);
                LocalDateTime endTime = sTime.plusMonths(i);
                LambdaQueryWrapper<RepairRecord> wrapper=new LambdaQueryWrapper<>();
                wrapper.between(RepairRecord::getReportTime,dateTime,endTime);
                int count = repairRecordService.count(wrapper);
                repairTrendVo.setTrendCount(count);

                LocalDateTime dDateTime = dsTime.plusMonths(i - 1);
                LocalDateTime doubleEndTime = dsTime.plusMonths(i);
                LambdaQueryWrapper<RepairRecord> dWrapper=new LambdaQueryWrapper<>();
                dWrapper.between(RepairRecord::getReportTime,dDateTime,doubleEndTime);
                int dCount = repairRecordService.count(dWrapper);
                repairTrendVo.setTrendComparisonCount(dCount);

                repairTrendVo.setTrendName(dateTime.getMonthValue()+"月");
                repairTrendVo.setTrendComparisonName(dDateTime.getMonthValue()+"月");
                vos.add(repairTrendVo);
            }

        }else if (state==2){
            for (int i = 1; i <= 31; i++) {
                RepairTrendVo repairTrendVo = new RepairTrendVo();

                LocalDateTime dateTime = sTime.plusDays(i - 1);
                LocalDateTime endTime = sTime.plusDays(i);

                LocalDateTime dDateTime = dsTime.plusDays(i - 1);
                LocalDateTime doubleEndTime = dsTime.plusDays(i);

                int value = dateTime.getMonthValue();
                int value2 = dDateTime.getMonthValue();

//                  当前时间月份相同就可以添加
                if (value==monthValue) {
                    LambdaQueryWrapper<RepairRecord> wrapper=new LambdaQueryWrapper<>();
                    wrapper.between(RepairRecord::getReportTime,dateTime,endTime);
                    int count = repairRecordService.count(wrapper);
                    repairTrendVo.setTrendCount(count);
                    repairTrendVo.setTrendName(value+"月"+dateTime.getDayOfMonth()+"号");
                }
//              对比的月份不相同就可以添加
                if (value2!=monthValue) {
                    LambdaQueryWrapper<RepairRecord> dWrapper=new LambdaQueryWrapper<>();
                    dWrapper.between(RepairRecord::getReportTime,dDateTime,doubleEndTime);
                    int dCount = repairRecordService.count(dWrapper);
                    repairTrendVo.setTrendComparisonCount(dCount);
                    repairTrendVo.setTrendComparisonName(value2+"月"+dDateTime.getDayOfMonth()+"号");
                }

                vos.add(repairTrendVo);
            }

        }else if (state==3){
            for (int i = 1; i <= 7; i++) {
                RepairTrendVo repairTrendVo = new RepairTrendVo();

                LocalDateTime dateTime = sTime.plusDays(i - 1);
                LocalDateTime endTime = sTime.plusDays(i);
                LambdaQueryWrapper<RepairRecord> wrapper=new LambdaQueryWrapper<>();
                wrapper.between(RepairRecord::getReportTime,dateTime,endTime);
                int count = repairRecordService.count(wrapper);
                repairTrendVo.setTrendCount(count);

                LocalDateTime dDateTime = dsTime.plusDays(i - 1);
                LocalDateTime doubleEndTime = dsTime.plusDays(i);
                LambdaQueryWrapper<RepairRecord> dWrapper=new LambdaQueryWrapper<>();
                dWrapper.between(RepairRecord::getReportTime,dDateTime,doubleEndTime);
                int dCount = repairRecordService.count(dWrapper);
                repairTrendVo.setTrendComparisonCount(dCount);

                repairTrendVo.setTrendName(DateUtils.weekName(dateTime.getDayOfWeek().getValue()));
                repairTrendVo.setTrendComparisonName(DateUtils.weekName(dDateTime.getDayOfWeek().getValue()));
                vos.add(repairTrendVo);
            }
        }else if (state==4){
            for (int i = 1; i <= 24; i++) {
                RepairTrendVo repairTrendVo = new RepairTrendVo();

                LocalDateTime dateTime = sTime.plusHours(i - 1);
                LocalDateTime endTime = sTime.plusHours(i);
                LambdaQueryWrapper<RepairRecord> wrapper=new LambdaQueryWrapper<>();
                wrapper.between(RepairRecord::getReportTime,dateTime,endTime);
                int count = repairRecordService.count(wrapper);
                repairTrendVo.setTrendCount(count);

                LocalDateTime dDateTime = dsTime.plusHours(i - 1);
                LocalDateTime doubleEndTime = dsTime.plusHours(i);
                LambdaQueryWrapper<RepairRecord> dWrapper=new LambdaQueryWrapper<>();
                dWrapper.between(RepairRecord::getReportTime,dDateTime,doubleEndTime);
                int dCount = repairRecordService.count(dWrapper);
                repairTrendVo.setTrendComparisonCount(dCount);

                repairTrendVo.setTrendName(dateTime.getHour()+"点");
                repairTrendVo.setTrendComparisonName(dDateTime.getHour()+"点");
                vos.add(repairTrendVo);
            }
        }


        return CommonResult.ok(vos);
    }

    /**
     * 总数据汇总
     * @return
     * @throws Exception
     */
    @Override
    public CommonResult repairSummarizing() throws Exception {

        List<RepairSummarizingVo> vos = new ArrayList<>();
        for (int i = 0; i < 5 ; i++) {
            String startTime = DateUtils.startTime(i);
            String endTime = DateUtils.endTime();
            RepairSummarizingVo vo=repairRecordService.repairSummarizing(startTime,endTime);
            if (i==0) {
                vo.setDateName("全部");
            }else if (i==1){
                vo.setDateName("本年");
            }else if (i==2){
                vo.setDateName("本月");
            }else if (i==3){
                vo.setDateName("本周");
            }else {
                vo.setDateName("今天");
            }
            vos.add(vo);
        }


        return CommonResult.ok(vos);
    }

    @Override
    public CommonResult mobileReply(MobileRevertRequest mrr, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        try {
            RepairRecord data = repairRecordService.getRepairById(mrr.getRecordId());
            if (data == null) {
                return CommonResult.fail("报修单已失效，无法进行回复");
            }

            if (data.getMaintenanceState() != eRecordStatu.ToLogistics.getValue()) {
                return CommonResult.fail("待处理维修单才能进行回复操作");
            }

            //接收人
            Integer recipientId = data.getUserId();

            //将工单数据状态改为待确认
            data.setMaintenanceState(eRecordStatu.ToConfirmed.getValue());
            int result = repairRecordService.updateRepairRecord(data);
            if (result <= 0) {
                throw new Exception("回复失败！");
            }


            RepairLeaveMessage repairLeaveMessage = new RepairLeaveMessage();
            repairLeaveMessage.setRecordId(data.getId());
            repairLeaveMessage.setRecipientId(recipientId);
            repairLeaveMessage.setSenderId(mrr.getUserId());
            repairLeaveMessage.setContent(mrr.getContent());

            boolean mSave = repairLeaveMessageService.save(repairLeaveMessage);

            if (!mSave) {
                throw new Exception("回复失败！");
            }

            //        追踪记录表
            RepairTrackRecord repairTrackRecord = new RepairTrackRecord();
            repairTrackRecord.setRecordId(data.getId());
            repairTrackRecord.setContent("回复");
            boolean save = repairTrackRecordService.save(repairTrackRecord);

            if (!save) {
                throw new Exception("回复失败！");
            }

            RepairSystemMessages rsm = new RepairSystemMessages();
            rsm.setIsRead(0);
            rsm.setRecordId(mrr.getRecordId());
            rsm.setRecipientId(recipientId);
            rsm.setContent("用户已回复，请尽快处理！");
            int insertRsm = repairSystemMessagesService.insertRepairSystemMessages(rsm);
            if (insertRsm <= 0) {
                throw new Exception("回复失败！");
            }

        } catch (Exception e) {
            throw new Exception("回复失败！");
        }

        return CommonResult.ok("回复成功");
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult closeOrders(int recordId, String userId, String reason) throws Exception {

        //获取订单
        RepairRecord recordData = repairRecordService.getRepairById(recordId);
        if (recordData == null) {
            return CommonResult.fail("订单不存在，关单失败！");
        }

        //待接单才能关单
        if (recordData.getMaintenanceState() != eRecordStatu.Dispatch.getValue()) {
            return CommonResult.fail("待接单状态的工单才能关闭");
        }

        try {
            //将工单状态更新为维修中
            recordData.setCustomsOfficerId(userId);
            recordData.setRemark(reason);
            recordData.setMaintenanceState(eRecordStatu.Closed.getValue());
            int updateRecord = repairRecordService.updateRepairRecord(recordData);
            if (updateRecord <= 0) {
                return CommonResult.fail("关单失败！");
            }

            RepairSystemMessages rsm = new RepairSystemMessages();
            rsm.setIsRead(0);
            rsm.setRecordId(recordId);
            rsm.setRecipientId(recordData.getUserId());
            rsm.setContent("工单已被师傅关闭，关单原因为：" + reason + "！");
            int insertRsm = repairSystemMessagesService.insertRepairSystemMessages(rsm);
            if (insertRsm <= 0) {
                throw new Exception("关单失败！");
            }

            //添加跟踪记录
            RepairTrackRecord rtr = new RepairTrackRecord();
            rtr.setRecordId(recordId);
            rtr.setMaintenanceState(eRecordStatu.Closed.getValue());
            rtr.setContent("订单关闭");
            int insertRtr = repairTrackRecordService.insertRepairTrackRecord(rtr);
            if (insertRtr <= 0) {
                throw new Exception("关单失败");
            }
        } catch (Exception e) {
            throw new Exception("关单失败！");
        }

        return CommonResult.ok("关单成功");
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult finishOrder(finishOrderRequest data, BindingResult bindingResult) throws Exception {
        try {
            if (bindingResult.hasErrors()) {
                String st = paramUtils.getParamError(bindingResult);
                return CommonResult.fail(st);
            }
            List<RepairConsumables> rcDatas = new ArrayList<>();
            //需要记录后续添加的耗材数据 但是不把钱记录到订单总金额中
            if (data.getConsumes().size() > 0) {
                for (insertConsumablesRequest rc : data.getConsumes()) {
                    if (rc.getConsumeId() == null) {
                        return CommonResult.fail("耗材ID不能为空");
                    }
                    if (rc.getConsumeName() == null) {
                        return CommonResult.fail("耗材名称不能为空");
                    }
                    if (rc.getNumber() == null || rc.getNumber() <= 0) {
                        return CommonResult.fail("耗材数量不能为空");
                    }
                    if (rc.getPrice() == null) {
                        return CommonResult.fail("耗材单价不能为空");
                    }
                    RepairConsumables rcData = new RepairConsumables();
                    rcData.setRecordId(data.getRecordId());
                    rcData.setConsumeId(rc.getConsumeId());
                    rcData.setConsumeName(rc.getConsumeName());
                    rcData.setNumber(rc.getNumber());
                    rcData.setPrice(rc.getPrice());
                    rcData.setTotalPrice(new BigDecimal(0));
                    rcDatas.add(rcData);
                }
            }

            //获取订单
            RepairRecord recordData = repairRecordService.getRepairById(data.getRecordId());
            if (recordData == null) {
                return CommonResult.fail("工单不存在，提交失败！");
            }

            //待接单才能关单
            if (recordData.getMaintenanceState() == eRecordStatu.Finished.getValue()) {
                return CommonResult.fail("该工单已被提交，请勿重复操作");
            }

            if (recordData.getMaintenanceState() != eRecordStatu.Maintenance.getValue()) {
                return CommonResult.fail("维修中的工单才能进行提交");
            }

            if (rcDatas.size() > 0) {
                boolean result = repairConsumablesService.insertConsumableBatch(rcDatas);

                if (!result) {
                    throw new Exception("提交失败！");
                }
            }

            //将工单状态更新为已完成
            recordData.setFinishVoice(data.getVoice());
            recordData.setFinishVoiceLength(data.getVoiceLength());
            recordData.setContent(data.getContnet());
            recordData.setMaintenancer(data.getImages().size() > 0 ? StringUtils.join(data.getImages(), ',') : null);
            recordData.setMaintenanceState(eRecordStatu.Finished.getValue());
            int updateRecord = repairRecordService.updateRepairRecord(recordData);
            if (updateRecord <= 0) {
                throw new Exception("提交失败！");
            }

            //更新接单记录数据的维修完成时间
            RepairReceiving rr = new RepairReceiving();
            rr.setFinishTime(new Date());
            int updateReceiving = repairReceivingService.updateRepairByRecordId(data.getRecordId(), rr);
            if (updateReceiving <= 0) {
                throw new Exception("提交失败！");
            }

            RepairSystemMessages rsm = new RepairSystemMessages();
            rsm.setIsRead(0);
            rsm.setRecordId(data.getRecordId());
            rsm.setRecipientId(recordData.getUserId());
            rsm.setContent("服务已经完成，期待再次为您服务，请为我们的服务点评~");
            int insertRsm = repairSystemMessagesService.insertRepairSystemMessages(rsm);
            if (insertRsm <= 0) {
                throw new Exception("提交失败！");
            }

            //添加跟踪记录
            RepairTrackRecord rtr = new RepairTrackRecord();
            rtr.setRecordId(data.getRecordId());
            rtr.setMaintenanceState(eRecordStatu.Finished.getValue());
            rtr.setContent("已完成");
            int insertRtr = repairTrackRecordService.insertRepairTrackRecord(rtr);
            if (insertRtr <= 0) {
                throw new Exception("提交失败");
            }
        } catch (Exception e) {
            throw new Exception("提交失败！");
        }

        return CommonResult.ok("提交成功！");
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult transferApply(transferApplyRequest data, BindingResult bindingResult) throws Exception {
        try {
            if (bindingResult.hasErrors()) {
                String st = paramUtils.getParamError(bindingResult);
                return CommonResult.fail(st);
            }

            //已关单 已取消 已完成的单子不能提交转单申请
            //获取订单 判断当前订单是否是待接单状态
            RepairRecord recordData = repairRecordService.getRepairById(data.getRecordId());
            if (recordData == null) {
                return CommonResult.fail("订单不存在，申请失败");
            }

            if (recordData.getMaintenanceState() == eRecordStatu.Transfer.getValue()) {
                return CommonResult.fail("该订单已发起转单审核，请勿重复操作");
            }

            if (recordData.getMaintenanceState() != eRecordStatu.Dispatch.getValue()) {
                return CommonResult.fail("待接单的订单才能进行转单申请");
            }

            RepairTransferRecord rtr = new RepairTransferRecord();
            rtr.setRecordId(data.getRecordId());
            rtr.setVoice(data.getVoice());
            rtr.setVoiceLength(data.getVoiceLength());
            rtr.setRemark(data.getRemark());
            rtr.setUserId(data.getUserId());
            rtr.setApprovalStatu(2);

            int result = repairTransferRecordService.insertRepairTransferRecord(rtr);
            if (result <= 0) {
                throw new Exception("申请失败！");
            }

            recordData.setMaintenanceState(eRecordStatu.Transfer.getValue());
            int updateRecord = repairRecordService.updateRepairRecord(recordData);
            if (updateRecord <= 0) {
                throw new Exception("申请失败！");
            }

            //给郭班长和后勤发消息
            List<Integer> userZZ = new ArrayList<>();
            userZZ.add(2);
            userZZ.add(3);
            List<RepairUser> users = repairUserService.getRepairByUserZZ(userZZ, recordData.getSchoolId());
            List<RepairSystemMessages> rsms = new ArrayList<>();
            for (RepairUser user : users) {
                RepairSystemMessages rsm = new RepairSystemMessages();
                rsm.setIsRead(0);
                rsm.setRecordId(data.getRecordId());
                rsm.setRecipientId(user.getId());
                rsm.setContent("您有一个审核工单，请尽快处理!");
                rsms.add(rsm);
            }

            boolean insertRsm = repairSystemMessagesService.inserBatchSystemMessage(rsms);
            if (!insertRsm) {
                throw new Exception("转单审核发起失败！");
            }

            //添加跟踪记录
            RepairTrackRecord rtrR = new RepairTrackRecord();
            rtrR.setRecordId(data.getRecordId());
            rtrR.setMaintenanceState(eRecordStatu.Transfer.getValue());
            rtrR.setContent("转单审核申请");
            int insertRtr = repairTrackRecordService.insertRepairTrackRecord(rtrR);
            if (insertRtr <= 0) {
                throw new Exception("转单审核发起失败！");
            }


        } catch (Exception e) {
            throw new Exception("转单审核发起失败！");
        }

        return CommonResult.fail("转单审核发起成功");
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CommonResult collaborateApply(collaborateApplyRequest data, BindingResult bindingResult) throws Exception {
        try {
            if (bindingResult.hasErrors()) {
                String st = paramUtils.getParamError(bindingResult);
                return CommonResult.fail(st);
            }

            //已关单 已取消 已完成的单子不能提交转单申请
            //获取订单 判断当前订单是否是待接单状态
            RepairRecord recordData = repairRecordService.getRepairById(data.getRecordId());
            if (recordData == null) {
                return CommonResult.fail("订单不存在，申请失败");
            }

            if (recordData.getMaintenanceState() == eRecordStatu.Collaborate.getValue()) {
                return CommonResult.fail("该订单已发起协作审核，请勿重复操作");
            }

            if (recordData.getMaintenanceState() != eRecordStatu.Dispatch.getValue()) {
                return CommonResult.fail("待接单的订单才能进行协作申请");
            }

            //协作人中不能包含申请人
            long count = data.getCollaborator().stream().filter(e -> e.equals(data.getUserId())).count();
            if (count > 0) {
                return CommonResult.fail("协作人中不能包含当前申请人");
            }

            RepairCollaborateRecord rcr = new RepairCollaborateRecord();
            rcr.setRecordId(data.getRecordId());
            rcr.setVoice(data.getVoice());
            rcr.setVoiceLength(data.getVoiceLength());
            rcr.setRemark(data.getRemark());
            rcr.setUserId(data.getUserId());
            rcr.setApprovalStatu(2);
            rcr.setCollaborator(StringUtils.join(data.getCollaborator(), ','));

            int result = repairCollaborateRecordService.insertRepairCollaborateRecord(rcr);
            if (result <= 0) {
                throw new Exception("申请失败！");
            }

            recordData.setMaintenanceState(eRecordStatu.Collaborate.getValue());
            int updateRecord = repairRecordService.updateRepairRecord(recordData);
            if (updateRecord <= 0) {
                throw new Exception("申请失败！");
            }

            //给郭班长和后勤发消息
            List<Integer> userZZ = new ArrayList<>();
            userZZ.add(2);
            userZZ.add(3);
            List<RepairUser> users = repairUserService.getRepairByUserZZ(userZZ, recordData.getSchoolId());
            List<RepairSystemMessages> rsms = new ArrayList<>();
            for (RepairUser user : users) {
                RepairSystemMessages rsm = new RepairSystemMessages();
                rsm.setIsRead(0);
                rsm.setRecordId(data.getRecordId());
                rsm.setRecipientId(user.getId());
                rsm.setContent("您有一个审核工单，请尽快处理!");
                rsms.add(rsm);
            }

            boolean insertRsm = repairSystemMessagesService.inserBatchSystemMessage(rsms);
            if (!insertRsm) {
                throw new Exception("转单审核发起失败！");
            }

            //添加跟踪记录
            RepairTrackRecord rtr = new RepairTrackRecord();
            rtr.setRecordId(data.getRecordId());
            rtr.setMaintenanceState(eRecordStatu.Collaborate.getValue());
            rtr.setContent("协作审核申请");
            int insertRtr = repairTrackRecordService.insertRepairTrackRecord(rtr);
            if (insertRtr <= 0) {
                throw new Exception("协作审核发起失败");
            }
        } catch (Exception e) {
            throw new Exception("协作审核发起失败！");
        }

        return CommonResult.ok("协作审核发起成功");
    }

    @Override
    public CommonResult queryTeamWorkCount(int schoolId) {
        String dateNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        TeamWorkCountVo result = repairRecordService.queryTeamWorkCount(schoolId, dateNow);
        return CommonResult.ok(result);
    }

    /**
     * 待审核  1
     * 待接单  2
     * 维修中  3
     * 已完成  4
     * 待处理  5
     * 已超时  6
     * 已关单  7
     */
    @Override
    public CommonResult queryTeamWorkPage(int currentPage, int pageCount, int state, int schoolId) {
        PageUtils<RepairTechnicianVo> result = null;
        String dateNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        switch (state) {
            case 1:
                result = repairRecordService.queryTeamWorkPending(currentPage, pageCount, schoolId, dateNow);
                break;
            case 2:
                result = repairRecordService.queryTeamWorkMaintenance(currentPage, pageCount, schoolId, dateNow);
                break;
            case 3:
                result = repairRecordService.queryTeamWorkTimeout(currentPage, pageCount, schoolId, dateNow);
                break;
            case 4:
                result = repairRecordService.queryTeamWorkExamine(currentPage, pageCount, schoolId);
                break;
            case 5:
                result = repairRecordService.queryTeamWorkFinished(currentPage, pageCount, schoolId);
                break;
            case 6:
                result = repairRecordService.queryTeamWorkLogistics(currentPage, pageCount, schoolId);
                break;
            case 7:
                result = repairRecordService.queryTeamWorkClosed(currentPage, pageCount, schoolId);
                break;
            default:
                break;
        }

        return CommonResult.ok(result);
    }
}

