package com.bonus.material.lossAssessment.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.bonus.common.biz.constant.GlobalConstants;
import com.bonus.common.biz.domain.BmFileInfo;
import com.bonus.common.biz.enums.*;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.back.domain.BackApplyInfo;
import com.bonus.material.back.domain.BackCheckDetails;
import com.bonus.material.back.mapper.BackApplyInfoMapper;
import com.bonus.material.back.mapper.BackCheckDetailsMapper;
import com.bonus.material.basic.mapper.BmFileInfoMapper;
import com.bonus.material.lossAssessment.mapper.LossAssessmentMapper;
import com.bonus.material.lossAssessment.service.LossAssessmentService;
import com.bonus.material.ma.domain.PartType;
import com.bonus.material.ma.mapper.MachineMapper;
import com.bonus.material.repair.domain.*;
import com.bonus.material.repair.domain.vo.RepairDeviceSummaryVo;
import com.bonus.material.repair.domain.vo.RepairDeviceVO;
import com.bonus.material.settlement.mapper.SltAgreementInfoMapper;
import com.bonus.material.task.domain.TmTask;
import com.bonus.system.api.model.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.internal.util.StringHelper;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hay
 */
@Service("LossAssessmentService")
@Validated
@Slf4j
public class LossAssessmentServiceImpl implements LossAssessmentService {

    @Resource
    private LossAssessmentMapper mapper;
    @Resource
    private BmFileInfoMapper bmFileInfoMapper;
    @Resource
    private BackApplyInfoMapper backApplyInfoMapper;
    @Resource
    private SltAgreementInfoMapper sltAgreementInfoMapper;
    @Resource
    private BackCheckDetailsMapper backCheckDetailsMapper;
    @Resource
    private MachineMapper machineMapper;


    @Override
    public List<RepairTask> getLossAssessmentList(RepairTask bean) {
        try {
            Long userId = SecurityUtils.getLoginUser().getUserid();
            // 首先根据用户名去ma_type_manage表查询是否存在绑定物资信息
            /*List<Long> typeIdList = backApplyInfoMapper.selectTypeIdList(userId);
            if (CollectionUtils.isEmpty(typeIdList)) {
                bean.setUserId(userId == 0 ? null : userId);
            }*/
            Set<String> userRoles = SecurityUtils.getLoginUser().getRoles();
            // 检查用户是否具有特殊角色
            boolean hasSpecialRole = hasSpecialRole(userRoles);
            if (!hasSpecialRole) {
                bean.setUserId(userId == 0 ? null : userId);
            }
            List<RepairTask> list = mapper.getLossAssessmentList(bean);
            if (CollectionUtil.isNotEmpty(list)) {
                String keyWord = bean.getKeyWord();
                // 如果关键字不为空，进行过滤
                if (!StringUtils.isBlank(keyWord)) {
                    list = list.stream()
                            .filter(item -> containsKeyword(item, keyWord))
                            .collect(Collectors.toList());
                }
            }
            return list;
        } catch (Exception e) {
            return new ArrayList<>();
        }

    }

    /**
     * 检查用户是否具有特殊角色
     * @param userRoles
     * @return
     */
    private boolean hasSpecialRole(Set<String> userRoles) {
        if (userRoles == null) {
            return false;
        }
        List<String> allowedRoles = GlobalConstants.allowedRoles;
        return allowedRoles.stream().anyMatch(userRoles::contains);
    }

    @Override
    public List<RepairDeviceSummaryVo> getLossAssessmentDetailsList(RepairTaskDetails bean) {
        try {
            List<RepairDeviceSummaryVo> repairDeviceSummaryVoList = new ArrayList<>();
            List<RepairDeviceVO> repairDeviceList = mapper.getRepairDeviceList(bean);
            Map<Long, List<RepairDeviceVO>> map = repairDeviceList.stream().collect(Collectors.groupingBy(RepairDeviceVO::getTypeId));
            for (Long key : map.keySet()) {
                List<RepairDeviceVO> tempList = map.get(key);
                if (CollectionUtil.isNotEmpty(tempList)) {
                    RepairDeviceSummaryVo vo = new RepairDeviceSummaryVo();
                    Long[] ids = tempList.stream().map(RepairDeviceVO::getId).toArray(Long[]::new);
                    BigDecimal repairNumSum = tempList.stream()
                            .map(RepairDeviceVO::getRepairNum)
                            .map(num -> Optional.ofNullable(num).orElse(BigDecimal.ZERO))
                            .reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal repairedNumSum = tempList.stream()
                            .map(RepairDeviceVO::getRepairedNum)
                            .map(num -> Optional.ofNullable(num).orElse(BigDecimal.ZERO))
                            .reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal scrapNumSum = tempList.stream()
                            .map(RepairDeviceVO::getScrapNum)
                            .map(num -> Optional.ofNullable(num).orElse(BigDecimal.ZERO))
                            .reduce(BigDecimal.ZERO, BigDecimal::add);

                    BigDecimal typeCostSum = tempList.stream().map(RepairDeviceVO::getTotalCost).reduce(BigDecimal.ZERO, BigDecimal::add);
                    // 新增status判断逻辑
                    boolean allStatusOne = tempList.stream().allMatch(device -> device.getStatus() != null && !"".equals(device.getStatus()) && "1".equals(device.getStatus()));
                    vo.setRepairDeviceList(tempList)
                            .setIds(ids).setTypeRepairNum(repairNumSum).setTypeRepairedNum(repairedNumSum)
                            .setTypeScrapNum(scrapNumSum).setTaskId(tempList.get(0).getTaskId())
                            .setCode(tempList.get(0).getCode()).setTypeName(tempList.get(0).getTypeName())
                            .setTypeId(tempList.get(0).getTypeId()).setType(tempList.get(0).getType())
                            .setUnitName(tempList.get(0).getUnitName()).setManageType(tempList.get(0).getManageType())
                            .setTypeCost(typeCostSum)
                            .setStatus(allStatusOne ? "1" : "0");
                    repairDeviceSummaryVoList.add(vo);
                }
            }
            return repairDeviceSummaryVoList;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    /**
     * 定损完成
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult batchLossAssessmentApply(@NotNull List<RepairDeviceVO> repairDeviceVOList) {
        try {
            // 准备业务逻辑数据
            LoginUser loginUser = SecurityUtils.getLoginUser();
            // 配件集合
            List<RepairPartDetails> partList;

            // 校验参数，过滤空数据
            repairDeviceVOList.removeIf(Objects::isNull);

            for (RepairDeviceVO bean : repairDeviceVOList){
                //判断管理方式是否为空
                if (null == bean.getManageType()) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return  AjaxResult.error("请选择物资管理方式");
                }
                //如果是编码设备
                if (Objects.equals(MaTypeManageTypeEnum.CODE_DEVICE.getTypeId(), bean.getManageType())) {
                    partList = bean.getCodeInRepairPartList();
                    // 更新定损数量、并修改定损人员
                    mapper.updateRepairedNumAndStatus(bean.getId(), BigDecimal.ONE, 1, loginUser.getUsername(), loginUser.getUserid());
                    // 更新维修人员信息
                    if (!CollectionUtils.isEmpty(bean.getRepairList())) {
                        RepairPartDetails repairPartDetails = bean.getRepairList().get(0);
                        bean.setRepairer(repairPartDetails.getRepairer());
                        bean.setRemark(repairPartDetails.getRemark());
                        mapper.updateRepairer(bean);
                    } else {
                        bean.setRepairer(SecurityUtils.getLoginUser().getSysUser().getNickName());
                        mapper.updateRepairer(bean);
                    }
                    // 统一处理配件集合数据
                    copeNumberManageInList(bean, partList, loginUser, bean.getManageType());
                } else if (Objects.equals(MaTypeManageTypeEnum.NUMBER_DEVICE.getTypeId(), bean.getManageType())) {
                    //数量设备
                    if (null == bean.getId()) {
                        throw new ServiceException("请完善参数，详情ID为空!");
                    }
                    // 更新维修人员信息
                    if (!CollectionUtils.isEmpty(bean.getRepairList())) {
                        RepairPartDetails repairPartDetails = bean.getRepairList().get(0);
                        bean.setRepairer(repairPartDetails.getRepairer());
                        bean.setRemark(repairPartDetails.getRemark());
                        mapper.updateRepairer(bean);
                        for (RepairPartDetails partDetails : bean.getRepairList()) {
                            if (!CollectionUtils.isEmpty(partDetails.getFileList())) {
                                for (BmFileInfo bmFileInfo : partDetails.getFileList()) {
                                    bmFileInfo.setTaskType(TmTaskTypeEnum.TM_TASK_REPAIR_NUM.getTaskTypeId())
                                            .setTaskId(bean.getTaskId()).setModelId(bean.getId())
                                            .setCreateBy(loginUser.getUsername());
                                    bmFileInfoMapper.insertBmFileInfo(bmFileInfo);
                                }
                            }
                        }
                    }
                    BigDecimal innerRepairNum = BigDecimal.ZERO;
                    BigDecimal outerRepairNum = BigDecimal.ZERO;
                    BigDecimal scrapNum = BigDecimal.ZERO;
                    partList = bean.getNumberInRepairPartList();


                    for (RepairPartDetails list : partList){
                        innerRepairNum = innerRepairNum.add(list.getDsNum());
                    }
                        // 获取维修详情表中的维修详情记录：待维修、已维修、已报废的数量
                        RepairTaskDetails details = mapper.getById(bean.getId());
                        if (Objects.isNull(details)) {
                            throw new ServiceException("此记录不存在，请检查后提交!");
                        }

                        // 处理配件集合数据
                        copeNumberManageInList(bean, partList, loginUser, bean.getManageType());

                    // 拆分维修单
                    splitRepairDetailsToMultiple(bean, innerRepairNum, outerRepairNum, scrapNum, loginUser);
                }
            }
        } catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.error("定损完成操作",e.getMessage());
            return  AjaxResult.error("定损失败");
        }
        return AjaxResult.success("定损完成");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult endLossAssessmentTask(ArrayList<RepairTask> taskList) {
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            for (RepairTask task : taskList) {
                List<RepairTaskDetails> detailsList = mapper.getRepairDetailsWhichNotSent(task.getTaskId());
                //2、修改任务状态
                mapper.updateSingleTaskStatus(task, loginUser.getUserid(), RepairTaskStatusEnum.TASK_LOSS_ASSESSMENT_COMPLETE.getStatus());
                //3、重新生成维修数据
                for (RepairTaskDetails repairTaskDetails : detailsList){
                    repairTaskDetails.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
                    mapper.insertRad(repairTaskDetails);
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.error("定损任务提交操作", e.getMessage());
            return AjaxResult.error("提交失败");
        }
        return AjaxResult.success("提交成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult rejectRepair(RepairTask bean) {
        String res="";
        try {
            if (!CollectionUtils.isEmpty(bean.getTaskIds())) {
            for (Long taskId : bean.getTaskIds()) {
                //判断是否定损完成
                int status = mapper.selectTaskStatus(taskId);
                final TmTask tmTask = mapper.selectTmTaskByTaskId(taskId);
                if (status<=0){
                    //驳回操作不可跨月（退料的创建时间）操作
                    if (!StringHelper.isNullOrEmptyString(String.valueOf(tmTask.getCreateTime()))) {
                        // 将 createTime 转换为 LocalDate
                        LocalDate taskCreationDate = tmTask.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                        // 获取当前日期
                        LocalDate today = LocalDate.now();
                        // 检查是否在同一月
                        if (taskCreationDate.getYear() != today.getYear() || taskCreationDate.getMonth() != today.getMonth()) {
                            throw new ServiceException("只可驳回本月任务");
                        }
                    }
                    //1、清除任务
                    int deleteTask = mapper.deleteTask(taskId);
                    if (deleteTask<0){
                        throw new ServiceException("任务驳回失败，请重试");
                    }
                    if (tmTask.getPreTaskId()!=null){
                        // 2. 根据任务id查询前置任务数据详情
                        BackApplyInfo info = backApplyInfoMapper.selectByTaskId(tmTask.getPreTaskId());
                        // 3. 查询back_check_details表数据
                        BackCheckDetails backCheckDetails = new BackCheckDetails();
                        backCheckDetails.setParentId(info.getId());
                        List<BackCheckDetails> checkDetails = backCheckDetailsMapper.selectBackCheckDetailsList(backCheckDetails);
                        // 4. 更新结算信息表
                        int result = sltAgreementInfoMapper.backRejectSltCope(tmTask.getPreTaskId());
                        if (result < 1) {
                            throw new ServiceException("维修驳回失败，请联系管理员");
                        }
                        // 5. 对于编码设备，更改设备状态为在用
                        if (!CollectionUtils.isEmpty(checkDetails)) {
                            for (BackCheckDetails backCheckDetail : checkDetails) {
                                if (backCheckDetail.getMaId() != null) {
                                    // 更新设备状态为在用
                                    machineMapper.updateStatus(backCheckDetail.getMaId(), MaMachineStatusEnum.IN_USE.getStatus());
                                }
                            }
                        }
                        //6、删除定损数据
                        int detailsRes = mapper.deleteApplyDetailsByTaskId(taskId);
                        int recordRes = mapper.deleteRecordByTaskId(taskId);
                        int partRes = mapper.deletePartByTaskId(taskId);
                        int costRes = mapper.deleteCostByTaskId(taskId);
                    } else {
                        throw new ServiceException("定损任务没有前置任务，不能驳回");
                    }
                } else {
                    res="此任务已定损完成，不可驳回";
                    return AjaxResult.error(res);
                }
            }
        } else if (!CollectionUtils.isEmpty(bean.getIds())) {
                RepairApplyDetails repairApplyDetails = null;
                for (Long id : bean.getIds()) {
                    // 查询明细详情
                    repairApplyDetails = mapper.selectRepairApplyDetailsById(id);
                    if (repairApplyDetails != null) {
                        if (repairApplyDetails.getMaId()!=null){
                            machineMapper.updateStatus(repairApplyDetails.getMaId(), MaMachineStatusEnum.IN_USE.getStatus());
                        }
                    }
                    //驳回后直接清除该条定损记录
                    mapper.delDetailsById(id);
                    mapper.delRecord(repairApplyDetails);
                    mapper.delPart(repairApplyDetails);
                    mapper.delCost(repairApplyDetails);
                    // 更新结算信息表
                    int result = sltAgreementInfoMapper.backInUseNum(repairApplyDetails);
                    if (result < 1) {
                        throw new ServiceException("定损驳回失败，请联系管理员");
                    }
                }
            }
        } catch (final DataAccessException e) {
            log.error("定损任务驳回",e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("定损任务驳回失败");
        }
        return AjaxResult.success("执行完成");
    }

    @Override
    public List<RepairPartDetails> getDetailsListApp(RepairPartDetails bean) {
        return mapper.getDetailsListApp(bean);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult gotoRepair(RepairDeviceVO bean) {
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (bean.getTaskId()!=null){
                List<RepairTaskDetails> detailsList = mapper.getRepairDetailsWhichNotSent(bean.getTaskId());
                //2、修改任务状态
                RepairTask task = new RepairTask();
                task.setTaskId(bean.getTaskId());
                mapper.updateSingleTaskStatus(task, loginUser.getUserid(), RepairTaskStatusEnum.TASK_LOSS_ASSESSMENT_COMPLETE.getStatus());
                //3、重新生成维修数据
                for (RepairTaskDetails repairTaskDetails : detailsList){
                    repairTaskDetails.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
                    mapper.insertRad(repairTaskDetails);
                }
                return AjaxResult.success("提交完成");
            } else {
                throw new ServiceException("任务id为空");
            }
        } catch (Exception e) {
            log.error("定损直通维修", e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("执行失败，请重试");
        }
    }

    private void splitRepairDetailsToMultiple(RepairDeviceVO bean, BigDecimal innerRepairNum, BigDecimal outerRepairNum, BigDecimal scrapNum, LoginUser loginUser) {
        //本次修完了
        if ((bean.getRepairNum().subtract(innerRepairNum).subtract(outerRepairNum).subtract(scrapNum).compareTo(BigDecimal.valueOf(0)) ) == 0) {
            mapper.updateRepairedAndScrapNum(bean.getId(), innerRepairNum.add(outerRepairNum), scrapNum, loginUser.getUserid(), loginUser.getUserid());
        } else {
            mapper.updateRepairedAndScrapNum(bean.getId(), innerRepairNum.add(outerRepairNum), scrapNum, loginUser.getUserid(), loginUser.getUserid());
        }
        mapper.updateStatusByRepairScrapNum(bean.getId());
    }

    /**
     * 处理配件集合数据
     * @param bean 维修申请单
     * @param partList 配件列表
     * @param loginUser 当前登录用户
     * @param manageType 管理方式：0编码管理  1数量管理
     */
    private void copeNumberManageInList(RepairDeviceVO bean, List<RepairPartDetails> partList, LoginUser loginUser, Integer manageType) {
        if (CollectionUtil.isNotEmpty(partList)) {
            if (null == bean.getManageType()) {
                throw new ServiceException("请选择物资管理方式");
            }
            RepairApplyRecord repairApplyRecord = new RepairApplyRecord();
            // 遍历配件列表，判断配件类型，收费还是不收费
            for (RepairPartDetails partDetails : partList) {
                // 维修记录表信息
                repairApplyRecord.setTaskId(bean.getTaskId()).setMaId(bean.getMaId()).setTypeId(bean.getTypeId()).setRepairType(RepairTypeEnum.INNER_REPAIR.getTypeId());
                if (MaTypeManageTypeEnum.CODE_DEVICE.getTypeId().equals(manageType)) {
                    repairApplyRecord.setRepairNum(BigDecimal.valueOf(!RepairTypeEnum.TO_SCRAP.getTypeId().equals(bean.getRepairType()) ? 1 : 0));
                    repairApplyRecord.setScrapNum(BigDecimal.valueOf(RepairTypeEnum.TO_SCRAP.getTypeId().equals(bean.getRepairType()) ? 1 : 0));
                }
                if (MaTypeManageTypeEnum.NUMBER_DEVICE.getTypeId().equals(manageType)) {
                    repairApplyRecord.setRepairNum(partDetails.getDsNum());
                }
                repairApplyRecord.setCreateBy(loginUser.getUsername());
                repairApplyRecord.setStatus(0L);

                if ((null != partDetails.getId() || null != partDetails.getPartId()) && null != partDetails.getPartNum()) {
                    partDetails.setPartId(null != partDetails.getPartId() ? partDetails.getPartId() : partDetails.getId());
                    // 有维修配件时,如果价格为空，设置为0
                    if (null == partDetails.getPartCost()) {
                        partDetails.setPartCost(BigDecimal.ZERO);
                    }

                    partDetails.setTaskId(bean.getTaskId()).setMaId(bean.getMaId()).setTypeId(bean.getTypeId()).setCompanyId(null);
                    partDetails.setCreateBy(String.valueOf(loginUser.getUserid()));

                    if (partDetails.getPartPrice()==null){
                        // 根据 partId  找配件单价
                        BigDecimal partPrice = mapper.selectPartPrice(partDetails.getPartId());
                        // 设置配件单价费用
                        partDetails.setPartPrice(partPrice);
                    }

                    // 添加【维修配件明细表】
                    mapper.addPart(partDetails);

                    // 设置维修记录的配件费用
                    repairApplyRecord.setId(bean.getId())
                            .setPartPrice(partDetails.getPartPrice())
                            .setPartNum(partDetails.getPartNum())
                            .setRepairContent(partDetails.getRepairContent())
                            .setPartType(1)
                            .setPartId(Optional.ofNullable(partDetails.getPartId()).orElse(0L));
                    // 添加【维修记录表】
                    mapper.addRecord(repairApplyRecord);
//                    // 扣减个人库配件库存数量
//                    int res = mapper.updateStorageNum(partDetails);
//                    if (0 == res) {
//                        throw new ServiceException("个人配件库存不足");
//                    }
                } else {
                    // 不选维修配件时, 只添加【维修记录表】
                    mapper.addRecord(repairApplyRecord);
                }
            }
            BigDecimal sfCosts = countPartCosts(partList, BigDecimal.ZERO);
            // SQL: 新增【维修费用记录表】
            repairApplyRecord.setRepairNum(partList.get(0).getPartNum() != null ? BigDecimal.valueOf(partList.get(0).getPartNum()) : BigDecimal.ZERO);
            mapper.addRepairCost(repairApplyRecord, sfCosts, "1");

        } else {
            // 配件为空，添加【维修记录表】
            RepairApplyRecord repairApplyRecord = new RepairApplyRecord();
            repairApplyRecord.setTaskId(bean.getTaskId()).setMaId(bean.getMaId()).setTypeId(bean.getTypeId())
                    .setRepairType(bean.getRepairType()).setStatus(0L)
                    .setRepairNum(BigDecimal.valueOf(!RepairTypeEnum.TO_SCRAP.getTypeId().equals(bean.getRepairType()) ? 1 : 0))
                    .setScrapNum(BigDecimal.valueOf(RepairTypeEnum.TO_SCRAP.getTypeId().equals(bean.getRepairType()) ? 1 : 0))
                    .setCreateBy(loginUser.getUsername());

            // 不选维修配件时, 只添加【维修记录表】
            mapper.addRecord(repairApplyRecord);
        }
    }

    /**
     * 配件列表价格合计
     * @param partList 配件列表
     * @param sfCosts 配件价格合计
     * @return 配件合计后的价格
     */
    private BigDecimal countPartCosts(List<RepairPartDetails> partList, BigDecimal sfCosts) {
        for (RepairPartDetails partDetails : partList) {
            if (null == partDetails.getPartPrice()){
                PartType type = mapper.selectPartTypeByPaId(partDetails.getPartId());
                if (type != null) {
                    partDetails.setPartPrice(type.getBuyPrice());
                }
            }
            if (null != partDetails.getPartPrice() && null != partDetails.getPartNum()) {
                BigDecimal partPrice = partDetails.getPartPrice();
                BigDecimal partNumber = new BigDecimal(partDetails.getPartNum());
                sfCosts = sfCosts.add(partPrice.multiply(partNumber));
            }
        }
        return sfCosts;
    }

    /**
     * 关键字搜索
     * @param item
     * @param keyWord
     * @return
     */
    private boolean containsKeyword(RepairTask item, String keyWord) {
        return (item.getType() != null && item.getType().contains(keyWord)) ||
                (item.getRepairCode() != null && item.getRepairCode().contains(keyWord)) ||
                (item.getBackUnit() != null && item.getBackUnit().contains(keyWord)) ||
                (item.getBackPro() != null && item.getBackPro().contains(keyWord)) ||
                (item.getBackCode() != null && item.getBackCode().contains(keyWord)) ||
                (item.getCreateName() != null && item.getCreateName().contains(keyWord));
    }
}
