package com.gci.boot.modules.vehicles.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gci.boot.common.util.StringUtil;
import com.gci.boot.constant.CommonConstant;
import com.gci.boot.modules.msg.enums.RepairSenderEnum;
import com.gci.boot.modules.msg.enums.SenderTypeEnum;
import com.gci.boot.modules.msg.enums.WorkOrderType;
import com.gci.boot.modules.msg.service.MsgPushService;
import com.gci.boot.modules.msg.vo.MsgRedisVO;
import com.gci.boot.modules.projects.entity.Projects;
import com.gci.boot.modules.projects.service.IProjectsService;
import com.gci.boot.modules.system.entity.*;
import com.gci.boot.modules.system.service.*;
import com.gci.boot.modules.system.vo.DictModel;
import com.gci.boot.modules.system.vo.LoginUser;
import com.gci.boot.modules.vehicles.entity.TbRepairAssign;
import com.gci.boot.modules.vehicles.entity.TbRepairCheck;
import com.gci.boot.modules.vehicles.entity.TbRepairInfo;
import com.gci.boot.modules.vehicles.entity.TbRepairOffer;
import com.gci.boot.modules.vehicles.enums.*;
import com.gci.boot.modules.vehicles.mapper.TbRepairInfoMapper;
import com.gci.boot.modules.vehicles.service.*;
import com.gci.boot.modules.vehicles.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TbRepairInfoServiceImpl extends ServiceImpl<TbRepairInfoMapper, TbRepairInfo> implements ITbRepairInfoService {
    @Autowired
    private TbRepairInfoMapper repairInfoMapper;
    @Autowired
    private ITbRepairAssignService repairAssignService;
    @Autowired
    private ITbRepairCheckService repairCheckService;
    @Autowired
    private ITbRepairOfferService repairOfferService;
    @Autowired
    private IProjectsService projectsService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private MsgPushService msgPushService;

    @Override
    public Page getRepairInfoList(Page page, RepairInfoListVO vo) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        if(Objects.isNull(sysUser)){
//            return page;
//        }
        //1-司机，2-维修厂，3-司机&维修厂，4-维保管理员，5-维保领导，6-维保管理员&维保领导
        String authorityType = commonService.getAuthority(sysUser.getUsername());

        if("2".equals(authorityType)){
            vo.setRepairParam("维修厂");
            //维修厂派单不可见
        }

        List<RepairInfoListVO> list = new ArrayList<>();
        list = repairInfoMapper.findRepairInfoList(page,vo);
        //todo   做按钮权限处理
        handlerButtonAndType(list, authorityType);
        page.setRecords(list);
        return page;
    }

    @Override
    public RepairObjectVO getRepairObjectById(String id) {
        RepairObjectVO vo = new RepairObjectVO();
        //todo
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        if(Objects.isNull(sysUser)){
//            return vo;
//        }

        TbRepairInfo tbRepairInfo = this.getById(id);
        vo.setId(id);
        vo.setFlowStatus(tbRepairInfo.getFlowStatus());
        vo.setRepairInfo(tbRepairInfo);
        vo.setAddType(tbRepairInfo.getAddType());

        //补充作废信息
        if(9 == tbRepairInfo.getFlowStatus()){
            CancelObjectVO cancelObjectVO = new CancelObjectVO();
            cancelObjectVO.setCancelIdea(tbRepairInfo.getCancelIdea());
            cancelObjectVO.setCancelPeople(tbRepairInfo.getCancelPeople());
            cancelObjectVO.setCancelTime(tbRepairInfo.getCancelTime());
            vo.setCancelObject(cancelObjectVO);
        }

        LambdaQueryWrapper<TbRepairAssign> assignQueryWrapper = new LambdaQueryWrapper<>();
        assignQueryWrapper.eq(TbRepairAssign::getRepairId,id).last(" limit 1 ");
        TbRepairAssign tbRepairAssign = repairAssignService.getOne(assignQueryWrapper);
        vo.setRepairAssign(tbRepairAssign);

        //暂只做单一”司机、维修厂、维保员工、领导” 身份的处理。取最高身份
        String authorityType = commonService.getAuthority(sysUser.getUsername());
        if("3".equals(authorityType)){
            authorityType = "2";
        }
        if("6".equals(authorityType)){
            authorityType = "5";
        }
        vo.setUserType(authorityType);
        //todo 把报价放进去
        if(! "1".equals(authorityType)){
            LambdaQueryWrapper<TbRepairOffer> offerQueryWrapper = new LambdaQueryWrapper<>();
            offerQueryWrapper.eq(TbRepairOffer::getRepairId,id).last(" limit 1 ");
            TbRepairOffer tbRepairOffer = repairOfferService.getOne(offerQueryWrapper);
            if(Objects.nonNull(tbRepairOffer)){
                if("".equals(tbRepairOffer.getInRepairImgUrl())){
                    tbRepairOffer.setInRepairImgUrl(null);
                }
            }
            if(Objects.nonNull(tbRepairOffer) && StrUtil.isNotBlank(tbRepairOffer.getRepairDetailJson())){
                tbRepairOffer.setRepairDetailList(JSONUtil.toList(tbRepairOffer.getRepairDetailJson(), RepairDetailVO.class));
            }
            if(Objects.nonNull(tbRepairOffer) && StrUtil.isNotBlank(tbRepairOffer.getAttachmentJson())){
                tbRepairOffer.setAttachmentList(JSONUtil.toList(tbRepairOffer.getAttachmentJson(), AttachmentDetailVO.class));
            }
            vo.setRepairOffer(tbRepairOffer);
        }

        LambdaQueryWrapper<TbRepairCheck> checkQueryWrapper = new LambdaQueryWrapper<>();
        checkQueryWrapper.eq(TbRepairCheck::getRepairId,id).orderByDesc(TbRepairCheck::getCheckTime);
        List<TbRepairCheck> tbRepairCheckList = repairCheckService.list(checkQueryWrapper);
        vo.setRepairCheckList(tbRepairCheckList);

        return vo;
    }

    @Transactional
    @Override
    public String addDataEntry(RepairDataEntryVO vo, String isGuarantee) {
        String repairId = StrUtil.uuid().replace("-","");
        TbRepairInfo repairInfo = vo.getRepairInfo();
        TbRepairOffer repairOffer = vo.getRepairOffer();
        //校验报价
        String ver= verifyOffer(repairOffer,isGuarantee);
        if(StrUtil.isNotBlank(ver)){
            return ver;
        }
        TbRepairAssign repairAssign = vo.getRepairAssign();
        Projects projects = projectsService.getById(repairInfo.getProjectId());
        if(Objects.nonNull(projects)){
            repairInfo.setProjectName(projects.getProjectName());
        }
        repairInfo.setId(repairId);
        //报修时间
        if(Objects.isNull(repairInfo.getRepairTime())){
            repairInfo.setRepairTime(new Date());
        }
        //直接结束归档
        repairInfo.setFlowStatus(8);
        repairInfo.setFlowNode(4);
        repairInfo.setAddType(1);
        //工单号
        String format = DateUtil.format(new Date(),"yyyyMMddHHmmssSSS");
        repairInfo.setOrderNo(format);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String username = "";
        if(Objects.nonNull(sysUser)){
            username = sysUser.getUsername();
        }
        repairInfo.setCreateBy(username);
        //报价
        repairOffer.setRepairId(repairId);
        repairOffer.setRepairShop(repairAssign.getRepairShop());
        if(Objects.nonNull(repairOffer.getRepairDetailList())){
            repairOffer.setRepairDetailJson(JSONUtil.toJsonPrettyStr(repairOffer.getRepairDetailList()));
        }
        if(Objects.nonNull(repairOffer.getAttachmentList())){
            repairOffer.setAttachmentJson(JSONUtil.toJsonPrettyStr(repairOffer.getAttachmentList()));
        }
        //指派
        repairAssign.setRepairId(repairId);

        this.save(repairInfo);
        repairOfferService.save(repairOffer);
        repairAssignService.save(repairAssign);

        return null;
    }

    @Transactional
    @Override
    public String submitRepairObject(RepairObjectVO vo, HttpServletRequest request) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        TbRepairInfo repairInfo = this.getById(vo.getId());
//        if(!repairInfo.getFlowStatus().equals(vo.getFlowStatus())){
//            return "该报修单流程状态已被修改，请刷新后重试！";
//        }
        //判断是否超过2K到领导层级
        if(vo.getFlowStatus()==10 && vo.getCheckFlag()==1){
                LambdaQueryWrapper<TbRepairOffer> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(TbRepairOffer::getRepairId,vo.getId());
                TbRepairOffer one = repairOfferService.getOne(queryWrapper);
                if(Objects.nonNull(one) && one.getRepairCost() > 2000){
                    vo.setCheckFlag(2000);
                }
        }
        //判断是否质保期内的
        if(vo.getFlowStatus()==2 && vo.getCheckFlag()==1){
            TbRepairAssign repairAssign = vo.getRepairAssign();
            if(Objects.nonNull(repairAssign) && "1".equals(repairAssign.getIsGuarantee())){
                vo.setCheckFlag(-1);
            }
        }
        RepairStatusChangeEvent event = RepairStatusChangeEvent.getEnum(vo.getFlowStatus(),vo.getCheckFlag());
        if(Objects.isNull(event)){
            return "流程配置为空";
        }
        //补充指派信息
        if(event.getFlowStatus() == 2){
            TbRepairAssign repairAssign = vo.getRepairAssign();
            if(StrUtil.isBlank(repairAssign.getRepairShop())){
                return "请选择维修厂！";
            }
            if("1".equals(repairAssign.getIsGuarantee())){
                //质保期内的,直接到维修中,默认创建一条 质保维修的明细记录
                TbRepairOffer  tbRepairOffer = buildRepairOffer(vo.getId(), repairAssign.getRepairShop());
                repairOfferService.save(tbRepairOffer);
            }
            repairAssign.setRepairId(vo.getId());
            repairAssignService.save(repairAssign);
            //补充节点操作人
            saveCheck(sysUser, vo.getId(), "5");
        }
        //维修厂报价信息
        if(event.getFlowStatus() == 3){
            //补充指派维修厂
            LambdaQueryWrapper<TbRepairAssign> repairAssignQuery = new LambdaQueryWrapper<>();
            repairAssignQuery.eq(TbRepairAssign::getRepairId,vo.getId()).last("limit 1");
            TbRepairAssign tbRepairAssign = repairAssignService.getOne(repairAssignQuery);
            TbRepairOffer repairOffer = vo.getRepairOffer();
            String ver= verifyOffer(repairOffer,"0");
            if(StrUtil.isNotBlank(ver)){
                return ver;
            }
            repairOffer.setRepairId(vo.getId());
            repairOffer.setRepairShop(tbRepairAssign.getRepairShop());
            if(Objects.nonNull(repairOffer.getRepairDetailList())){
                repairOffer.setRepairDetailJson(JSONUtil.toJsonPrettyStr(repairOffer.getRepairDetailList()));
            }
            if(Objects.nonNull(repairOffer.getAttachmentList())){
                repairOffer.setAttachmentJson(JSONUtil.toJsonPrettyStr(repairOffer.getAttachmentList()));
            }
            if("".equals(repairOffer.getInRepairImgUrl())){
                repairOffer.setInRepairImgUrl(null);
            }
            LambdaQueryWrapper<TbRepairOffer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TbRepairOffer::getRepairId,vo.getId());
            TbRepairOffer one = repairOfferService.getOne(queryWrapper);
            if(StrUtil.isBlank(repairOffer.getId()) && Objects.nonNull(one)){
                repairOffer.setId(one.getId());
                repairOfferService.updateById(repairOffer);
            }else{
                repairOfferService.save(repairOffer);
            }
            //补充节点操作人
            saveCheck(sysUser, vo.getId(), "6");
        }
        //员工维修报价审核
        if(event.getFlowStatus() == 4){
            TbRepairCheck repairCheck = vo.getCurrentRepairCheck();
            if("2".equals(repairCheck.getCheckResult())){
                if(StrUtil.isNotBlank(StringUtil.checkString(repairCheck.getCheckIdea(),200))){
                    return ("审核意见 " + StringUtil.checkString(repairCheck.getCheckIdea(),200));
                }
            }else{
                if(StrUtil.isNotBlank(StringUtil.checkString2(repairCheck.getCheckIdea(),200))){
                    return ("审核意见 " + StringUtil.checkString2(repairCheck.getCheckIdea(),200));
                }
            }
            repairCheck.setRepairId(vo.getId());
            repairCheck.setCheckType("1");
            updateRepairCheck(repairCheck,request);
        }
        //审核审批--待复核
        if(event.getFlowStatus() == 10){
            TbRepairCheck repairCheck = vo.getCurrentRepairCheck();
            if("2".equals(repairCheck.getCheckResult())){
                if(StrUtil.isNotBlank(StringUtil.checkString(repairCheck.getCheckIdea(),200))){
                    return ("复核意见 " + StringUtil.checkString(repairCheck.getCheckIdea(),200));
                }
            }else{
                if(StrUtil.isNotBlank(StringUtil.checkString2(repairCheck.getCheckIdea(),200))){
                    return ("复核意见 " + StringUtil.checkString2(repairCheck.getCheckIdea(),200));
                }
            }
            repairCheck.setRepairId(vo.getId());
            repairCheck.setCheckType("5");
            updateRepairCheck(repairCheck,request);
        }
        //领导维修报价审核
        if(event.getFlowStatus() == 5){
            TbRepairCheck repairCheck = vo.getCurrentRepairCheck();
            if("2".equals(repairCheck.getCheckResult())){
                if(StrUtil.isNotBlank(StringUtil.checkString(repairCheck.getCheckIdea(),200))){
                    return ("审核意见 " + StringUtil.checkString(repairCheck.getCheckIdea(),200));
                }
            }else{
                if(StrUtil.isNotBlank(StringUtil.checkString2(repairCheck.getCheckIdea(),200))){
                    return ("审核意见 " + StringUtil.checkString2(repairCheck.getCheckIdea(),200));
                }
            }
            repairCheck.setRepairId(vo.getId());
            repairCheck.setCheckType("2");
            updateRepairCheck(repairCheck,request);
        }
        //维修厂报价信息补充
        if(event.getFlowStatus() == 6){
            //判断是否维修质保--维修单据附件做不做判断处理
            LambdaQueryWrapper<TbRepairAssign> repairAssignQuery = new LambdaQueryWrapper<>();
            repairAssignQuery.eq(TbRepairAssign::getRepairId,vo.getId()).last("limit 1");
            TbRepairAssign tbRepairAssign = repairAssignService.getOne(repairAssignQuery);
            String guarantee = tbRepairAssign.getIsGuarantee();

            TbRepairOffer repairOffer = vo.getRepairOffer();
            String ver= verifyOffer(repairOffer,guarantee);
            if(StrUtil.isNotBlank(ver)){
                return ver;
            }
            repairOffer.setRepairId(vo.getId());
            if(Objects.nonNull(repairOffer.getAttachmentList())){
                repairOffer.setAttachmentJson(JSONUtil.toJsonPrettyStr(repairOffer.getAttachmentList()));
            }
            if(Objects.nonNull(repairOffer.getRepairDetailList())){
                repairOffer.setRepairDetailJson(JSONUtil.toJsonPrettyStr(repairOffer.getRepairDetailList()));
            }
            if("".equals(repairOffer.getInRepairImgUrl())){
                repairOffer.setInRepairImgUrl(null);
            }
            LambdaQueryWrapper<TbRepairOffer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TbRepairOffer::getRepairId,vo.getId());
            TbRepairOffer one = repairOfferService.getOne(queryWrapper);
            if(StrUtil.isBlank(repairOffer.getId()) && Objects.nonNull(one)){
                repairOffer.setId(one.getId());
            }
            repairOfferService.updateById(repairOffer);
            //补充节点操作人
            saveCheck(sysUser, vo.getId(), "7");
        }
        //维修结果
        if(event.getFlowStatus() == 7){
            TbRepairCheck repairCheck = vo.getCurrentRepairCheck();
            repairCheck.setRepairId(vo.getId());
            repairCheck.setCheckType("3");
            if(StrUtil.isNotBlank(StringUtil.checkString2(repairCheck.getCheckIdea(),200))){
                return ("维修确认意见 " + StringUtil.checkString2(repairCheck.getCheckIdea(),200));
            }
            updateRepairCheck(repairCheck,request);
        }
        //更新维修单流程状态、流程节点
        repairInfo.setFlowStatus(event.getNextFlowStatus());
        repairInfo.setFlowNode(event.getNextNode());
        this.updateById(repairInfo);
        //待推送消息状态
        List<Integer> validFlowStatuses = Arrays.asList(2, 3, 4, 5, 6, 7);
        if (validFlowStatuses.contains(repairInfo.getFlowStatus())) {
            //推送微信消息
            ThreadUtil.execAsync(()->{
                Projects projects = projectsService.getById(repairInfo.getProjectId());
                List<DictModel> flowStatusList = sysDictService.queryDictItemsByCode("repair_status");
                Map<String, String> flowStatusMap = flowStatusList.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText));
                msgPushService.pushMsgToRedis(new MsgRedisVO(
                        RepairSenderEnum.getEnumByFlowStatus(repairInfo.getFlowStatus()).getSendType(),repairInfo.getId(),
                        repairInfo.getProjectId(), WorkOrderType.REPAIR.getType(),flowStatusMap.getOrDefault(repairInfo.getFlowStatus().toString(),""),
                        DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"),projects.getProjectName() + "工单更新，请及时查看"));
            });
        }
        return null;
    }

    public void updateRepairCheck(TbRepairCheck repairCheck,HttpServletRequest request){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String realName = "";
        if(Objects.nonNull(sysUser)){
            realName = sysUser.getRealname();
        }

        if("3".equals(repairCheck.getCheckType())){
            if("1".equals(repairCheck.getCheckResult())){
                repairCheck.setCheckResult("3");
            }else{
                repairCheck.setCheckResult("4");
            }
        }else{
            repairCheck.setCheckResult(repairCheck.getCheckResult());
        }
        if("2".equals(repairCheck.getCheckType())){
            repairCheck.setCheckPeopleType("2");
        }else if("5".equals(repairCheck.getCheckType())){
            repairCheck.setCheckPeopleType("3");
        }else{
            repairCheck.setCheckPeopleType("1");
        }
        if(StrUtil.isBlank(repairCheck.getCheckPeople())){
            repairCheck.setCheckPeople(realName);
        }
        repairCheck.setCheckTime(new Date());

        repairCheckService.save(repairCheck);

    }

    //补充按钮权限
    public void handlerButtonAndType(List<RepairInfoListVO> list, String authorityType){
        //1-司机，2-维修厂，3-司机&维修厂，4-维保管理员，5-维保领导，6-维保管理员&维保领导
        if("1".equals(authorityType)){
            list.stream().forEach(item->{
                RepairStatusEnumDriver statusEnum = RepairStatusEnumDriver.getEnum(item.getFlowStatus());
                if(Objects.nonNull(statusEnum)){
                    item.setCheckButton(statusEnum.getCheckButton());
                    item.setDeleteButton(statusEnum.getDeleteButton());
                    item.setUserType(authorityType);
                }
            });
        }
        if("2".equals(authorityType)){
            list.stream().forEach(item->{
                RepairStatusEnumRepair statusEnum = RepairStatusEnumRepair.getEnum(item.getFlowStatus());
                if(Objects.nonNull(statusEnum)){
                    item.setCheckButton(statusEnum.getCheckButton());
                    item.setDeleteButton(statusEnum.getDeleteButton());
                    item.setUserType(authorityType);
                }
            });
        }
        if("3".equals(authorityType)){
            list.stream().forEach(item->{
                RepairStatusEnumRepairAndDriver statusEnum = RepairStatusEnumRepairAndDriver.getEnum(item.getFlowStatus());
                if(Objects.nonNull(statusEnum)){
                    item.setCheckButton(statusEnum.getCheckButton());
                    item.setDeleteButton(statusEnum.getDeleteButton());
                    item.setUserType("2");
                }
            });
        }
        if("4".equals(authorityType)){
            list.stream().forEach(item->{
                RepairStatusEnumEmployee statusEnum = RepairStatusEnumEmployee.getEnum(item.getFlowStatus());
                if(Objects.nonNull(statusEnum)){
                    item.setCheckButton(statusEnum.getCheckButton());
                    item.setDeleteButton(statusEnum.getDeleteButton());
                    item.setUserType(authorityType);
                }
            });
        }
        if("5".equals(authorityType)){
            list.stream().forEach(item->{
                RepairStatusEnumLeader statusEnum = RepairStatusEnumLeader.getEnum(item.getFlowStatus());
                if(Objects.nonNull(statusEnum)){
                    item.setCheckButton(statusEnum.getCheckButton());
                    item.setDeleteButton(statusEnum.getDeleteButton());
                    item.setUserType(authorityType);
                }
            });
        }
        if("6".equals(authorityType)){
            list.stream().forEach(item->{
                RepairStatusEnumEmployeeAndLeader statusEnum = RepairStatusEnumEmployeeAndLeader.getEnum(item.getFlowStatus());
                if(Objects.nonNull(statusEnum)){
                    item.setCheckButton(statusEnum.getCheckButton());
                    item.setDeleteButton(statusEnum.getDeleteButton());
                    item.setUserType("5");
                }
            });
        }
    }

    public String verifyOffer(TbRepairOffer repairOffer, String isGuarantee){
        if(Objects.isNull(repairOffer.getRepairCost())){
            return "联维修报价不能为空!";
        }else{
            if(repairOffer.getRepairCost() <0 || repairOffer.getRepairCost()>99999999){
                return "联维修报价在[0,99999999]之间!";
            }
        }
        if(Objects.isNull(repairOffer.getTotalWorkHours())){
            return "总计工时不能为空!";
        }else{
            if(repairOffer.getTotalWorkHours() <0 || repairOffer.getTotalWorkHours()>99999999){
                return "总计工时在[0,99999999]之间!";
            }
        }
        if(Objects.isNull(repairOffer.getTotalWorkHoursCost())){
            return "工时费用总计不能为空!";
        }else{
            if(repairOffer.getTotalWorkHoursCost() <0 || repairOffer.getTotalWorkHoursCost()>99999999){
                return "工时费用总计在[0,99999999]之间!";
            }
        }
        if(Objects.isNull(repairOffer.getMaterialCost())){
            return "总计材料费不能为空!";
        }else{
            if(repairOffer.getMaterialCost() <0 || repairOffer.getMaterialCost()>99999999){
                return "总计材料费在[0,99999999]之间!";
            }
        }
        if("0".equals(isGuarantee)){
            if(Objects.isNull(repairOffer.getAttachmentList()) || repairOffer.getAttachmentList().size() == 0){
                return "单据附件不能为空!";
            }else{
                if(JSONUtil.toJsonPrettyStr(repairOffer.getAttachmentList()).length() >= 5000){
                    return "单据附件长度不能超过5000!";
                }
            }
        }
        if(Objects.isNull(repairOffer.getRepairDetailList()) || repairOffer.getRepairDetailList().size() == 0){
            return "维修明细不能为空!";
        }else{
            for (RepairDetailVO vo : repairOffer.getRepairDetailList()){
                if(vo.getQuantity() <0 || vo.getQuantity()>99999999){
                    return "数量在[0,99999999]之间!";
                }
                if(vo.getTotalCost() <0 || vo.getTotalCost()>99999999){
                    return "费用在[0,99999999]之间!";
                }
                if(vo.getUnitPrice() <0 || vo.getUnitPrice()>99999999){
                    return "材料在[0,99999999]之间!";
                }
                if(vo.getWorkHours() <0 || vo.getWorkHours()>99999999){
                    return "工时在[0,99999999]之间!";
                }
                if(vo.getWorkHoursCost() <0 || vo.getWorkHoursCost()>99999999){
                    return "工时费在[0,99999999]之间!";
                }
                if(StrUtil.isNotBlank(vo.getRepairContent())){
                    String str = StringUtil.checkString2(vo.getRepairContent(),100);
                    if(StrUtil.isNotBlank(str)){
                        return "维修内容" + str;
                    }
                }
            }
        }
        if(Objects.nonNull(repairOffer.getContent())){
            String str = StringUtil.checkString2(repairOffer.getContent(),100);
            if(StrUtil.isNotBlank(str)){
                return "内容说明" + str;
            }
        }
        return null;
    }

    public TbRepairOffer buildRepairOffer(String repairId, String repairShop){
        TbRepairOffer repairOffer = new TbRepairOffer();
        repairOffer.setRepairId(repairId);
        repairOffer.setRepairShop(repairShop);
        repairOffer.setContent("质保维修");
        repairOffer.setTotalWorkHours(0.0);
        repairOffer.setTotalWorkHoursCost(0.0);
        repairOffer.setRepairCost(0.0);
        repairOffer.setMaterialCost(0.0);
        RepairDetailVO repairDetailVO = new RepairDetailVO();
        repairDetailVO.setRepairClass("6");
        repairDetailVO.setRepairContent("质保维修");
        repairDetailVO.setQuantity(0);
        repairDetailVO.setTotalCost(0.0);
        repairDetailVO.setUnitPrice(0.0);
        repairDetailVO.setWorkHoursCost(0.0);
        repairDetailVO.setWorkHours(0.0);
        List<RepairDetailVO> repairDetailList = new ArrayList<>();
        repairDetailList.add(repairDetailVO);
        repairOffer.setRepairDetailJson(JSONUtil.toJsonPrettyStr(repairDetailList));
        return repairOffer;
    }

    public void saveCheck(LoginUser sysUser, String repairId, String checkPeopleType){
        TbRepairCheck repairCheck = new TbRepairCheck();
        repairCheck.setRepairId(repairId);
        if(Objects.nonNull(sysUser)){
            repairCheck.setCheckPeople(sysUser.getRealname());
        }
        repairCheck.setCheckTime(new Date());
        repairCheck.setCheckPeopleType(checkPeopleType);
        repairCheckService.save(repairCheck);
    }
}
