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.gis.entity.TbGisRunningbus;
import com.gci.boot.modules.gis.service.ITbGisRunningbusService;
import com.gci.boot.modules.msg.enums.RepairSenderEnum;
import com.gci.boot.modules.msg.enums.UpkeepBillSenderEnum;
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.SysRole;
import com.gci.boot.modules.system.entity.SysUser;
import com.gci.boot.modules.system.entity.SysUserRole;
import com.gci.boot.modules.system.service.ISysDictService;
import com.gci.boot.modules.system.service.ISysRoleService;
import com.gci.boot.modules.system.service.ISysUserRoleService;
import com.gci.boot.modules.system.service.ISysUserService;
import com.gci.boot.modules.system.vo.DictModel;
import com.gci.boot.modules.system.vo.LoginUser;
import com.gci.boot.modules.vehicles.entity.*;
import com.gci.boot.modules.vehicles.enums.*;
import com.gci.boot.modules.vehicles.mapper.TbUpkeepBillMapper;
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 TbUpkeepBillServiceImpl extends ServiceImpl<TbUpkeepBillMapper, TbUpkeepBill> implements ITbUpkeepBillService {

    @Autowired
    private IProjectsService projectsService;
    @Autowired
    private ITbUpkeepOfferService upkeepOfferService;
    @Autowired
    private ITbUpkeepAssignService upkeepAssignService;
    @Autowired
    private ITbUpkeepCheckService upkeepCheckService;
    @Autowired
    private TbUpkeepBillMapper upkeepBillMapper;
    @Autowired
    private ITbGisRunningbusService gisRunningbusService;

    @Autowired
    private CommonService commonService;
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private MsgPushService msgPushService;

    @Transactional
    @Override
    public String addDataEntry(UpkeepDataEntryVO vo, String isGuarantee) {
        //校验
        String ver = verifyOffer(vo.getUpkeepOffer(),isGuarantee);
        if(StrUtil.isNotBlank(ver)){
            return ver;
        }
        String ver2 = verifyBill(vo.getUpkeepBill());
        if(StrUtil.isNotBlank(ver2)){
            return ver2;
        }
        String upkeepBillId = StrUtil.uuid().replace("-","");
        TbUpkeepBill upkeepBill = vo.getUpkeepBill();
        TbUpkeepOffer upkeepOffer = vo.getUpkeepOffer();
        Projects projects = projectsService.getById(upkeepBill.getProjectId());
        if(Objects.nonNull(projects)){
            upkeepBill.setProjectName(projects.getProjectName());
        }
        upkeepBill.setId(upkeepBillId);
        //直接结束归档
        upkeepBill.setFlowStatus(8);
        upkeepBill.setFlowNode(4);
        //工单号
        String format = DateUtil.format(new Date(),"yyyyMMddHHmmssSSS");
        upkeepBill.setOrderNo(format);
        //数据录入
        upkeepBill.setAddType(1);
        //台账附件
        if(Objects.nonNull(upkeepBill.getAttachmentList())){
            upkeepBill.setAttachmentJson(JSONUtil.toJsonPrettyStr(upkeepBill.getAttachmentList()));
        }
        //指派信息
        TbUpkeepAssign upkeepAssign = vo.getUpkeepAssign();
        upkeepAssign.setUpkeepBillId(upkeepBillId);

        //报价
        upkeepOffer.setUpkeepBillId(upkeepBillId);
        upkeepOffer.setRepairShop(upkeepAssign.getRepairShop());
        if(Objects.nonNull(upkeepOffer.getRepairDetailList())){
            upkeepOffer.setUpkeepDetailJson(JSONUtil.toJsonPrettyStr(upkeepOffer.getRepairDetailList()));
        }
        //附件
        if(Objects.nonNull(upkeepOffer.getAttachmentUpkeepList())){
            upkeepOffer.setAttachmentJson(JSONUtil.toJsonPrettyStr(upkeepOffer.getAttachmentUpkeepList()));
        }

        this.save(upkeepBill);
        upkeepOfferService.save(upkeepOffer);
        upkeepAssignService.save(upkeepAssign);
        return null;
    }

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

        if("2".equals(authorityType)){
            vo.setRepairParam("维修厂");
            //维修厂派单不可见
        }
        List<UpkeepInfoListVO> list = new ArrayList<>();
        list = upkeepBillMapper.findUpkeepBillList(page,vo);
        //todo 做按钮权限处理
        handlerButtonAndType(list, authorityType);
        page.setRecords(list);

        return page;
    }

    @Override
    public List<UpkeepInfoListVO> getAllUpkeepBillList(UpkeepInfoListVO vo) {
        List<UpkeepInfoListVO> list = new ArrayList<>();
        list = upkeepBillMapper.findAllUpkeepBillList(vo);
        return list;
    }

    @Override
    public UpkeepObjectVO getUpkeepObjectById(String id) {
        UpkeepObjectVO vo = new UpkeepObjectVO();
        TbUpkeepBill tbUpkeepBill = this.getById(id);
        vo.setId(id);
        vo.setFlowStatus(tbUpkeepBill.getFlowStatus());
        if(StrUtil.isNotBlank(tbUpkeepBill.getAttachmentJson())){
            tbUpkeepBill.setAttachmentList(JSONUtil.toList(tbUpkeepBill.getAttachmentJson(), AttachmentDetailVO.class));
        }
        vo.setUpkeepBill(tbUpkeepBill);
        vo.setAddType(tbUpkeepBill.getAddType());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //暂只做单一”司机、维修厂、维保员工、领导” 身份的处理。取最高身份
        String authorityType = commonService.getAuthority(sysUser.getUsername());
        if("3".equals(authorityType)){
            authorityType = "2";
        }
        if("6".equals(authorityType)){
            authorityType = "5";
        }
        vo.setUserType(authorityType);

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

        LambdaQueryWrapper<TbUpkeepAssign> assignQueryWrapper = new LambdaQueryWrapper<>();
        assignQueryWrapper.eq(TbUpkeepAssign::getUpkeepBillId,id).last(" limit 1 ");
        TbUpkeepAssign tbUpkeepAssign = upkeepAssignService.getOne(assignQueryWrapper);
        vo.setUpkeepAssign(tbUpkeepAssign);

        LambdaQueryWrapper<TbUpkeepOffer> offerQueryWrapper = new LambdaQueryWrapper<>();
        offerQueryWrapper.eq(TbUpkeepOffer::getUpkeepBillId,id).last(" limit 1 ");
        TbUpkeepOffer tbUpkeepOffer = upkeepOfferService.getOne(offerQueryWrapper);
        if(Objects.nonNull(tbUpkeepOffer)){
            if("".equals(tbUpkeepOffer.getInUpkeepImgUrl())){
                tbUpkeepOffer.setInUpkeepImgUrl(null);
            }
            if("".equals(tbUpkeepOffer.getAfterUpkeepImgUrl())){
                tbUpkeepOffer.setAfterUpkeepImgUrl(null);
            }
            if("".equals(tbUpkeepOffer.getBeforeUpkeepImgUrl())){
                tbUpkeepOffer.setBeforeUpkeepImgUrl(null);
            }
        }
        if(Objects.nonNull(tbUpkeepOffer) && StrUtil.isNotBlank(tbUpkeepOffer.getUpkeepDetailJson())){
            tbUpkeepOffer.setRepairDetailList(JSONUtil.toList(tbUpkeepOffer.getUpkeepDetailJson(), RepairDetailVO.class));
        }
        if(Objects.nonNull(tbUpkeepOffer) && StrUtil.isNotBlank(tbUpkeepOffer.getAttachmentJson())){
            tbUpkeepOffer.setAttachmentUpkeepList(JSONUtil.toList(tbUpkeepOffer.getAttachmentJson(), AttachmentDetailVO.class));
        }
        vo.setUpkeepOffer(tbUpkeepOffer);

        LambdaQueryWrapper<TbUpkeepCheck> checkQueryWrapper = new LambdaQueryWrapper<>();
        checkQueryWrapper.eq(TbUpkeepCheck::getUpkeepBillId,id).orderByDesc(TbUpkeepCheck::getCheckTime);
        List<TbUpkeepCheck> tbRepairCheckList = upkeepCheckService.list(checkQueryWrapper);
        vo.setUpkeepCheckList(tbRepairCheckList);

        return vo;
    }

    @Transactional
    @Override
    public String submitUpkeepObject(UpkeepObjectVO vo, HttpServletRequest request) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        TbUpkeepBill upkeepBill = this.getById(vo.getId());
//        if(!upkeepBill.getFlowStatus().equals(vo.getFlowStatus())){
//            return "该保养台账单流程状态已被修改，请刷新后重试！";
//        }

        UpkeepStatusChangeEvent event = UpkeepStatusChangeEvent.getEnum(vo.getFlowStatus(),vo.getCheckFlag());
        if(Objects.isNull(event)){
            return "找不到配置流程!";
        }
        //            2         3         4        6        7          8        9      10
        //流程状态：   待委派     待报价    待审核     保养中    待确认      结束     作废     待复核
        //补充指派信息
        if(event.getFlowStatus() == 2){
            TbUpkeepAssign upkeepAssign = vo.getUpkeepAssign();
            if(StrUtil.isBlank(upkeepAssign.getRepairShop())){
                return "请选择维修厂！";
            }
            upkeepAssign.setUpkeepBillId(vo.getId());
            upkeepAssignService.save(upkeepAssign);
            //补充节点操作人
            saveCheck(sysUser, vo.getId(), "5");
        }
        //维修厂报价信息
        if(event.getFlowStatus() == 3){
            //补充指派维修厂
            LambdaQueryWrapper<TbUpkeepAssign> upkeepAssignQuery = new LambdaQueryWrapper<>();
            upkeepAssignQuery.eq(TbUpkeepAssign::getUpkeepBillId,vo.getId()).last("limit 1");
            TbUpkeepAssign tbUpkeepAssign = upkeepAssignService.getOne(upkeepAssignQuery);

            TbUpkeepOffer upkeepOffer = vo.getUpkeepOffer();
            //校验
            String ver = verifyOffer(vo.getUpkeepOffer(),"0");
            if(StrUtil.isNotBlank(ver)){
                return ver;
            }
            upkeepOffer.setUpkeepBillId(vo.getId());
            upkeepOffer.setRepairShop(tbUpkeepAssign.getRepairShop());
            if(Objects.nonNull(upkeepOffer.getRepairDetailList())){
                upkeepOffer.setUpkeepDetailJson(JSONUtil.toJsonPrettyStr(upkeepOffer.getRepairDetailList()));
            }
            if(Objects.nonNull(upkeepOffer.getAttachmentUpkeepList())){
                upkeepOffer.setAttachmentJson(JSONUtil.toJsonPrettyStr(upkeepOffer.getAttachmentUpkeepList()));
            }
            if("".equals(upkeepOffer.getInUpkeepImgUrl())){
                upkeepOffer.setInUpkeepImgUrl(null);
            }
            if("".equals(upkeepOffer.getAfterUpkeepImgUrl())){
                upkeepOffer.setAfterUpkeepImgUrl(null);
            }
            if("".equals(upkeepOffer.getBeforeUpkeepImgUrl())){
                upkeepOffer.setBeforeUpkeepImgUrl(null);
            }

            LambdaQueryWrapper<TbUpkeepOffer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TbUpkeepOffer::getUpkeepBillId,vo.getId());
            TbUpkeepOffer one = upkeepOfferService.getOne(queryWrapper);
            if(StrUtil.isBlank(upkeepOffer.getId()) && Objects.nonNull(one)){
                upkeepOffer.setId(one.getId());
                upkeepOfferService.updateById(upkeepOffer);
            }else{
                upkeepOfferService.save(upkeepOffer);
            }
            //补充节点操作人
            saveCheck(sysUser, vo.getId(), "6");

        }
        //员工领导维修报价审核
        if(event.getFlowStatus() == 4){
            TbUpkeepCheck upkeepCheck = vo.getCurrentUpkeepCheck();
            if("2".equals(upkeepCheck.getCheckResult())){
                if(StrUtil.isNotBlank(StringUtil.checkString(upkeepCheck.getCheckIdea(),200))){
                    return ("审核意见 " + StringUtil.checkString(upkeepCheck.getCheckIdea(),200));
                }
            }else{
                if(StrUtil.isNotBlank(StringUtil.checkString2(upkeepCheck.getCheckIdea(),200))){
                    return ("审核意见 " + StringUtil.checkString2(upkeepCheck.getCheckIdea(),200));
                }
            }
            upkeepCheck.setUpkeepBillId(vo.getId());
            String realName = "";
            if(Objects.nonNull(sysUser)){
                realName = sysUser.getRealname();
            }
            if(StrUtil.isBlank(upkeepCheck.getCheckPeople())){
                upkeepCheck.setCheckPeople(realName);
            }
            upkeepCheck.setCheckType("1");
            upkeepCheck.setCheckPeopleType("1");
            upkeepCheck.setCheckTime(new Date());
            upkeepCheckService.save(upkeepCheck);
        }
        //待复核
        if(event.getFlowStatus() == 10){
            TbUpkeepCheck upkeepCheck = vo.getCurrentUpkeepCheck();
            if("2".equals(upkeepCheck.getCheckResult())){
                if(StrUtil.isNotBlank(StringUtil.checkString(upkeepCheck.getCheckIdea(),200))){
                    return ("复核意见 " + StringUtil.checkString(upkeepCheck.getCheckIdea(),200));
                }
            }else{
                if(StrUtil.isNotBlank(StringUtil.checkString2(upkeepCheck.getCheckIdea(),200))){
                    return ("复核意见 " + StringUtil.checkString2(upkeepCheck.getCheckIdea(),200));
                }
            }
            upkeepCheck.setUpkeepBillId(vo.getId());
            String realName = "";
            if(Objects.nonNull(sysUser)){
                realName = sysUser.getRealname();
            }
            if(StrUtil.isBlank(upkeepCheck.getCheckPeople())){
                upkeepCheck.setCheckPeople(realName);
            }
            upkeepCheck.setCheckType("5");
            upkeepCheck.setCheckPeopleType("3");
            upkeepCheck.setCheckTime(new Date());
            upkeepCheckService.save(upkeepCheck);
            //更新实际保养开始时间 --- 审核通过才有
            if(1 == vo.getCheckFlag()){
                TbUpkeepBill tbUpkeepBill = this.getById(vo.getId());
                tbUpkeepBill.setStartTime(new Date());
                this.updateById(tbUpkeepBill);
            }
        }
        //维修厂报价信息补充
        if(event.getFlowStatus() == 6){
            TbUpkeepOffer upkeepOffer = vo.getUpkeepOffer();
            //校验
            String ver = verifyOffer(vo.getUpkeepOffer(),"0");
            if(StrUtil.isNotBlank(ver)){
                return ver;
            }
            upkeepOffer.setUpkeepBillId(vo.getId());
            if(Objects.nonNull(upkeepOffer.getAttachmentUpkeepList())){
                upkeepOffer.setAttachmentJson(JSONUtil.toJsonPrettyStr(upkeepOffer.getAttachmentUpkeepList()));
            }
            if(Objects.nonNull(upkeepOffer.getRepairDetailList())){
                upkeepOffer.setUpkeepDetailJson(JSONUtil.toJsonPrettyStr(upkeepOffer.getRepairDetailList()));
            }
            if("".equals(upkeepOffer.getInUpkeepImgUrl())){
                upkeepOffer.setInUpkeepImgUrl(null);
            }
            if("".equals(upkeepOffer.getAfterUpkeepImgUrl())){
                upkeepOffer.setAfterUpkeepImgUrl(null);
            }
            if("".equals(upkeepOffer.getBeforeUpkeepImgUrl())){
                upkeepOffer.setBeforeUpkeepImgUrl(null);
            }
            LambdaQueryWrapper<TbUpkeepOffer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TbUpkeepOffer::getUpkeepBillId,vo.getId());
            TbUpkeepOffer one = upkeepOfferService.getOne(queryWrapper);
            if(StrUtil.isBlank(upkeepOffer.getId()) && Objects.nonNull(one)){
                upkeepOffer.setId(one.getId());
            }
            upkeepOfferService.updateById(upkeepOffer);
            //补充节点操作人
            saveCheck(sysUser, vo.getId(), "8");
        }
        //维修结果
        if(event.getFlowStatus() == 7){
            TbUpkeepCheck upkeepCheck = vo.getCurrentUpkeepCheck();
            upkeepCheck.setUpkeepBillId(vo.getId());
            //校验特殊字符
            if("2".equals(upkeepCheck.getCheckResult())){
                if(StrUtil.isNotBlank(StringUtil.checkString(upkeepCheck.getCheckIdea(),200))){
                    return ("确认意见 " + StringUtil.checkString(upkeepCheck.getCheckIdea(),200));
                }
            }else{
                if(StrUtil.isNotBlank(StringUtil.checkString2(upkeepCheck.getCheckIdea(),200))){
                    return ("确认意见 " + StringUtil.checkString2(upkeepCheck.getCheckIdea(),200));
                }
            }
            String realName = "";
            if(Objects.nonNull(sysUser)){
                realName = sysUser.getRealname();
            }
            upkeepCheck.setUpkeepBillId(vo.getId());
            upkeepCheck.setCheckType("4");
            upkeepCheck.setCheckPeopleType("2");
            if("1".equals(upkeepCheck.getCheckResult())){
                upkeepCheck.setCheckResult("5");
                //todo 最终结果通过，更新计划表的状态为已归档--5
                upkeepBillMapper.updateFinshPlan(vo.getId(),5);
                //更新维修完成时间、维修实际里程
                TbUpkeepBill tbUpkeepBill = this.getById(vo.getId());
                tbUpkeepBill.setEndTime(new Date());
                LambdaQueryWrapper<TbGisRunningbus> busQuery = new LambdaQueryWrapper<>();
                busQuery.eq(TbGisRunningbus::getPlateNumber,tbUpkeepBill.getPlateNumber()).last(" limit 1 ");
                TbGisRunningbus runningbus = gisRunningbusService.getOne(busQuery);
                if(Objects.nonNull(runningbus)){
                    if(Objects.nonNull(runningbus.getTotalMileage())){
                        tbUpkeepBill.setRealMileage(runningbus.getTotalMileage().intValue());
                    }
                }
                this.updateById(tbUpkeepBill);
            }else{
                upkeepCheck.setCheckResult("6");
            }
            if(StrUtil.isBlank(upkeepCheck.getCheckPeople())){
                upkeepCheck.setCheckPeople(realName);
            }
            upkeepCheck.setCheckTime(new Date());
            upkeepCheckService.save(upkeepCheck);
        }
        //更新维修单流程状态、流程节点
        upkeepBill.setFlowStatus(event.getNextFlowStatus());
        upkeepBill.setFlowNode(event.getNextNode());
        this.updateById(upkeepBill);
        //待推送消息状态
        List<Integer> validFlowStatuses = Arrays.asList(2, 3, 4, 6, 7);
        if (validFlowStatuses.contains(upkeepBill.getFlowStatus())) {
            //推送微信消息
            ThreadUtil.execAsync(()->{
                Projects projects = projectsService.getById(upkeepBill.getProjectId());
                List<DictModel> flowStatusList = sysDictService.queryDictItemsByCode("upkeep_status");
                Map<String, String> flowStatusMap = flowStatusList.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText));
                msgPushService.pushMsgToRedis(new MsgRedisVO(
                        UpkeepBillSenderEnum.getEnumByFlowStatus(upkeepBill.getFlowStatus()).getSendType(), upkeepBill.getId(),
                        upkeepBill.getProjectId(), WorkOrderType.UPKEEP_BILL.getType(),flowStatusMap.getOrDefault(upkeepBill.getFlowStatus().toString(),""),
                        DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"),projects.getProjectName() + "工单更新，请及时查看"));
            });
        }
        return null;
    }



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

        if("6".equals(authorityType) || "5".equals(authorityType) || "4".equals(authorityType)){
            list.stream().forEach(item->{
                UpkeepStatusEnumEmployeeAndLeader statusEnum = UpkeepStatusEnumEmployeeAndLeader.getEnum(item.getFlowStatus());
                if(Objects.nonNull(statusEnum)){
                    item.setCheckButton(statusEnum.getCheckButton());
                    item.setDeleteButton(statusEnum.getDeleteButton());
                    if("6".equals(authorityType)){
                        //返回最高身份
                        item.setUserType("5");
                    }else{
                        item.setUserType(authorityType);
                    }
                }
            });
        }
    }

    //校验
    public String verifyBill(TbUpkeepBill upkeepBill){
        if(StrUtil.isBlank(upkeepBill.getProjectId())){
            return "所属项目不能为空!";
        }
        if(StrUtil.isBlank(upkeepBill.getPlateNumber())){
            return "车牌号不能为空!";
        }
        if(Objects.isNull(upkeepBill.getLastTime())){
            return "上次保养时间不能为空!";
        }
        if(Objects.isNull(upkeepBill.getLastMileage())){
            return "上次保养里程不能为空!";
        }else{
            if(upkeepBill.getLastMileage() <0 || upkeepBill.getLastMileage()>99999999){
                return "上次保养里程在[0,99999999]之间!";
            }
        }
        if(Objects.isNull(upkeepBill.getPredictTime())){
            return "预计保养时间不能为空!";
        }
        if(Objects.isNull(upkeepBill.getPredictMileage())){
            return "预计保养里程不能为空!";
        }else{
            if(upkeepBill.getPredictMileage() <0 || upkeepBill.getPredictMileage()>99999999){
                return "预计保养里程在[0,99999999]之间!";
            }
        }
        if(StrUtil.isBlank(upkeepBill.getContent())){
            return "保养内容不能为空!";
        }else{
            String str = StringUtil.checkString2(upkeepBill.getContent(),200);
            if(StrUtil.isNotBlank(str)){
                return "保养内容 " + str;
            }
        }
        return null;
    }

    public String verifyOffer(TbUpkeepOffer upkeepOffer,String isGuarantee){
        if(Objects.isNull(upkeepOffer.getTotaCost())){
            return "总计不能为空!";
        }else{
            if(upkeepOffer.getTotaCost() <0 || upkeepOffer.getTotaCost()>99999999){
                return "总计在[0,99999999]之间!";
            }
        }
        if(Objects.isNull(upkeepOffer.getTotalWorkHours())){
            return "总计工时不能为空!";
        }else{
            if(upkeepOffer.getTotalWorkHours() <0 || upkeepOffer.getTotalWorkHours()>99999999){
                return "总计工时在[0,99999999]之间!";
            }
        }
        if(Objects.isNull(upkeepOffer.getTotalWorkHoursCost())){
            return "工时费用总计不能为空!";
        }else{
            if(upkeepOffer.getTotalWorkHoursCost() <0 || upkeepOffer.getTotalWorkHoursCost()>99999999){
                return "工时费用总计在[0,99999999]之间!";
            }
        }
        if(Objects.isNull(upkeepOffer.getMaterialCost())){
            return "总计材料费不能为空!";
        }else{
            if(upkeepOffer.getMaterialCost() <0 || upkeepOffer.getMaterialCost()>99999999){
                return "总计材料费在[0,99999999]之间!";
            }
        }
        if("0".equals(isGuarantee)){
            if(Objects.isNull(upkeepOffer.getAttachmentUpkeepList()) || upkeepOffer.getAttachmentUpkeepList().size() == 0){
                return "单据附件不能为空!";
            }else{
                if(JSONUtil.toJsonPrettyStr(upkeepOffer.getAttachmentUpkeepList()).length() >= 5000){
                    return "单据附件长度不能超过5000!";
                }
            }
        }
        if(Objects.isNull(upkeepOffer.getRepairDetailList()) || upkeepOffer.getRepairDetailList().size() == 0){
            return "维修明细不能为空!";
        }else{
            for (RepairDetailVO vo : upkeepOffer.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;
                    }
                }else{
                    return "保养内容不能为空!";
                }
            }
        }
        if(StrUtil.isBlank(upkeepOffer.getOfferContent())){
            return "内容说明不能为空!";
        }else{
            String str = StringUtil.checkString2(upkeepOffer.getOfferContent(),100);
            if(StrUtil.isNotBlank(str)){
                return "内容说明 " + str;
            }else{
                return null;
            }
        }
    }

    public void saveCheck(LoginUser sysUser, String billId, String checkPeopleType){
        //补充节点操作人
        TbUpkeepCheck upkeepCheck = new TbUpkeepCheck();
        if(Objects.nonNull(sysUser)){
            upkeepCheck.setCheckPeople(sysUser.getRealname());
        }
        upkeepCheck.setUpkeepBillId(billId);
        upkeepCheck.setCheckPeopleType(checkPeopleType);
        upkeepCheck.setCheckTime(new Date());
        upkeepCheckService.save(upkeepCheck);
    }
}
