package com.jeeplus.test.cw.workClientInfo.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.domain.User;
import com.jeeplus.sys.mapper.UserMapper;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.cw.projectRecords.domain.CwProjectClientInfo;
import com.jeeplus.test.cw.projectRecords.mapper.CwProjectClientInfoMapper;
import com.jeeplus.test.cw.workClientInfo.domain.*;
import com.jeeplus.test.cw.workClientInfo.mapper.*;
import com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientBaseDTO;
import com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientBillingDTO;
import com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientContactDTO;
import com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientTempDTO;
import com.jeeplus.test.cw.workClientInfo.service.mapstruct.CwWorkClientBaseWrapper;
import com.jeeplus.test.cw.workClientInfo.service.mapstruct.CwWorkClientBillingWrapper;
import com.jeeplus.test.cw.workClientInfo.service.mapstruct.CwWorkClientContactWrapper;
import com.jeeplus.test.cw.workClientInfo.service.mapstruct.CwWorkClientTypeWrapper;
import com.jeeplus.test.mould.service.SerialnumTplService;
import com.jeeplus.test.oss.service.OssService;
import com.jeeplus.flowable.utils.FlowTaskUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class CwWorkClientService extends ServiceImpl<CwWorkClientBaseMapper, CwWorkClientBase> {

    @Resource
    private CwWorkClientBaseMapper cwWorkClientBaseMapper;
    @Resource
    private CwWorkClientBillingService cwWorkClientBillingService;
    @Resource
    private CwWorkClientContactService cwWorkClientContactService;
    @Resource
    private CwWorkClientTypeService cwWorkClientTypeService;
    @Resource
    private CwManageLevelTypeService cwManageLevelTypeService;
    @Resource
    private CwOrganizationTypeService cwOrganizationTypeService;
    @Resource
    private SerialnumTplService serialnumTplService;
    @Resource
    private OssService ossService;
    @Resource
    private CwProjectClientInfoMapper cwProjectClientInfoMapper;
    @Resource
    private CwWorkClientTempService cwWorkClientTempService;
    @Resource
    private UserMapper userMapper;


    public IPage<CwWorkClientBaseDTO> findList(Page<CwWorkClientBaseDTO> page, CwWorkClientBaseDTO cwWorkClientBaseDTO) throws Exception{
        QueryWrapper<CwWorkClientBase> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( CwWorkClientBaseWrapper.INSTANCE.toEntity(cwWorkClientBaseDTO), CwWorkClientBase.class );
        queryWrapper.eq("cw_wcb.del_flag","0");
        queryWrapper.orderByDesc("cw_wcb.create_date");
        if (ObjectUtil.isNotEmpty(cwWorkClientBaseDTO)) {
            if (StringUtils.isNotBlank(cwWorkClientBaseDTO.getIsTrue())) {
                if ("1".equals(cwWorkClientBaseDTO.getIsTrue())){
                    queryWrapper.eq("cw_wcb.status","0").or().eq("cw_wcb.status","5");
                }
            }
            if (ArrayUtil.isNotEmpty(cwWorkClientBaseDTO.getCreateDates())) {
                queryWrapper.between("cw_wcb.create_date", cwWorkClientBaseDTO.getCreateDates()[0], cwWorkClientBaseDTO.getCreateDates()[1]);
            }
            if (ObjectUtil.isNotEmpty(cwWorkClientBaseDTO.getCwWorkClientTypeDTO())){
                if (StringUtils.isNotBlank(cwWorkClientBaseDTO.getCwWorkClientTypeDTO().getOwnershipType())) {
                    queryWrapper.eq("cw_wct.ownership_type",cwWorkClientBaseDTO.getCwWorkClientTypeDTO().getOwnershipType());
                }
            }
            if (StringUtils.isNotBlank(cwWorkClientBaseDTO.getProjectId())){
                List<CwProjectClientInfo> cwProjectClientInfos = cwProjectClientInfoMapper.selectList(new QueryWrapper<CwProjectClientInfo>().lambda().eq(CwProjectClientInfo::getProjectId, cwWorkClientBaseDTO.getProjectId()));
                List<String> ids = cwProjectClientInfos.stream().distinct().map(CwProjectClientInfo::getClientId).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(ids)) {
                    queryWrapper.in("cw_wcb.id",ids);
                } else {
                    return new Page<>();
                }
            }
            if (StringUtils.isNotBlank(cwWorkClientBaseDTO.getUpdateUserName())) {
                List<User> userList = userMapper.selectList(new QueryWrapper<User>().lambda().like(User::getName, cwWorkClientBaseDTO.getUpdateUserName()));
                if (CollectionUtil.isNotEmpty(userList)) {
                    List<String> userIds = userList.stream().distinct().map(User::getId).collect(Collectors.toList());
                    if (CollectionUtil.isNotEmpty(userIds)) {
                        queryWrapper.in("cw_wcb.update_user",userIds);
                    } else {
                        return new Page<>();
                    }
                } else {
                    return new Page<>();
                }
            }
        }
        IPage<CwWorkClientBaseDTO> list = cwWorkClientBaseMapper.findList(page, queryWrapper);
        List<CwWorkClientBaseDTO> records = list.getRecords();
        //处理数据
        FlowTaskUtil util = new FlowTaskUtil();
        List<CwWorkClientBaseDTO> cwWorkClientBaseDTOS = util.taskDispose(records);
        list.setRecords(cwWorkClientBaseDTOS);
        return list;
    }

    public IPage<CwWorkClientBaseDTO> findListTree(Page<CwWorkClientBaseDTO> page, CwWorkClientBaseDTO cwWorkClientBaseDTO) throws Exception{
        QueryWrapper<CwWorkClientBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cw_wcb.del_flag","0");
        queryWrapper.orderByDesc("cw_wcb.create_date");
        if (ObjectUtil.isNotEmpty(cwWorkClientBaseDTO)) {
            if (StringUtils.isNotBlank(cwWorkClientBaseDTO.getName()) ||
                    StringUtils.isNotBlank(cwWorkClientBaseDTO.getNo()) ||
                    StringUtils.isNotBlank(cwWorkClientBaseDTO.getType()) ||
                    StringUtils.isNotBlank(cwWorkClientBaseDTO.getIndustry())) {
                List<String> ids = new ArrayList<>();
                List<CwWorkClientBase> list = this.list(new QueryWrapper<CwWorkClientBase>().lambda()
                        .like(StringUtils.isNotBlank(cwWorkClientBaseDTO.getName()),CwWorkClientBase::getName, cwWorkClientBaseDTO.getName())
                        .like(StringUtils.isNotBlank(cwWorkClientBaseDTO.getNo()),CwWorkClientBase::getNo, cwWorkClientBaseDTO.getNo())
                        .eq(StringUtils.isNotBlank(cwWorkClientBaseDTO.getType()),CwWorkClientBase::getType, cwWorkClientBaseDTO.getType())
                        .eq(StringUtils.isNotBlank(cwWorkClientBaseDTO.getIndustry()),CwWorkClientBase::getIndustry, cwWorkClientBaseDTO.getIndustry())
                );
                list.stream().forEach(item->{
                    ids.add(item.getId());
                    if (StringUtils.isNotBlank(item.getOneUpCompany())) {
                        String parentId = item.getOneUpCompany();
                        while (true) {
                            CwWorkClientBase byId = this.getOne(new QueryWrapper<CwWorkClientBase>().lambda().eq(CwWorkClientBase::getId, parentId));
                            if (ObjectUtil.isNotEmpty(byId)) {
                                ids.add(byId.getId());
                                if (StringUtils.isBlank(byId.getOneUpCompany())) {
                                    break;
                                } else {
                                    parentId = byId.getOneUpCompany();
                                }
                            }
                        }
                    }
                    List<String> childIdList = this.list(new QueryWrapper<CwWorkClientBase>().lambda().eq(CwWorkClientBase::getOneUpCompany, item.getId())).stream().map(CwWorkClientBase::getId).collect(Collectors.toList());
                    while (true) {
                        if (CollectionUtil.isNotEmpty(childIdList)) {
                            childIdList.stream().forEach(childId->{
                                ids.add(childId);
                            });
                            List<String> childIdS = this.list(new QueryWrapper<CwWorkClientBase>().lambda().in(CwWorkClientBase::getOneUpCompany, childIdList)).stream().map(CwWorkClientBase::getId).collect(Collectors.toList());
                            if (CollectionUtil.isNotEmpty(childIdS)) {
                                childIdList = childIdS;
                            } else {
                                break;
                            }
                        } else {
                            break;
                        }
                    }
                });
                List<String> lastIdList = ids.stream().distinct().collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(lastIdList)){
                    queryWrapper.in("cw_wcb.id",lastIdList);
                } else {
                    return new Page<>();
                }
            }
            if (StringUtils.isNotBlank(cwWorkClientBaseDTO.getStatus())) {
                queryWrapper.eq("cw_wcb.status",cwWorkClientBaseDTO.getStatus());
            }
            if (StringUtils.isNotBlank(cwWorkClientBaseDTO.getIsTrue())) {
                if ("1".equals(cwWorkClientBaseDTO.getIsTrue())){
                    queryWrapper.eq("cw_wcb.status","0").or().eq("cw_wcb.status","5");
                }
            }
        }
        return cwWorkClientBaseMapper.findList(page, queryWrapper);
    }

    public CwWorkClientBaseDTO queryById(String id) {

        CwWorkClientBaseDTO cwWorkClientBaseDTO = cwWorkClientBaseMapper.queryById(id);

        return cwWorkClientBaseDTO;
    }

    public String saveForm(CwWorkClientBaseDTO cwWorkClientBaseDTO) throws Exception{
        if (ObjectUtil.isNotEmpty(cwWorkClientBaseDTO)) {
            if (StringUtils.isEmpty(cwWorkClientBaseDTO.getId())) {
                cwWorkClientBaseDTO.setCreateBy(new UserDTO());
            }
        }
        CwWorkClientBase cwWorkClientBase = CwWorkClientBaseWrapper.INSTANCE.toEntity(cwWorkClientBaseDTO);
        if(ObjectUtil.isNotEmpty(cwWorkClientBase)){
            if(StringUtils.isBlank(cwWorkClientBase.getId())){
                UserDTO userDTO = UserUtils.getCurrentUserDTO();
                String no = serialnumTplService.genSerialNum(userDTO.getCompanyDTO().getId(), CwWorkClientBaseDTO.BIZ_CODE);
                cwWorkClientBase.setNo(no);
                cwWorkClientBase.setUpdateUser(userDTO.getId());
                cwWorkClientBase.setStatus("0");
            }
        }
        this.saveOrUpdate(cwWorkClientBase);
        if (ObjectUtil.isNotEmpty(cwWorkClientBaseDTO)) {
            // 附件
            ossService.saveOrUpdateFileList(cwWorkClientBaseDTO.getWorkAttachmentDtoList(),cwWorkClientBase.getId(),"cw_work_client");
            // 客户类型
            if (ObjectUtil.isNotEmpty(cwWorkClientBaseDTO.getCwWorkClientTypeDTO())){
                CwWorkClientType cwWorkClientType = CwWorkClientTypeWrapper.INSTANCE.toEntity(cwWorkClientBaseDTO.getCwWorkClientTypeDTO());
                cwWorkClientType.setClientId(cwWorkClientBase.getId());
                cwWorkClientTypeService.saveOrUpdate(cwWorkClientType);
            }
            // 开票信息
            if (ObjectUtil.isNotEmpty(cwWorkClientBaseDTO.getCwWorkClientBillingDTOList())){
                List<String> ids = cwWorkClientBaseDTO.getCwWorkClientBillingDTOList().stream().filter(item -> {
                    if (StringUtils.isNotBlank(item.getId())) {
                        return true;
                    }
                    return false;
                }).distinct().map(CwWorkClientBillingDTO::getId).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(ids)){
                    cwWorkClientBillingService.remove(new QueryWrapper<CwWorkClientBilling>().lambda()
                            .eq(CwWorkClientBilling::getClientId,cwWorkClientBase.getId()).notIn(CwWorkClientBilling::getId,ids));
                } else {
                    cwWorkClientBillingService.remove(new QueryWrapper<CwWorkClientBilling>().lambda().eq(CwWorkClientBilling::getClientId,cwWorkClientBase.getId()));
                }
                cwWorkClientBaseDTO.getCwWorkClientBillingDTOList().stream().forEach(item->{
                    CwWorkClientBilling cwWorkClientBilling = CwWorkClientBillingWrapper.INSTANCE.toEntity(item);
                    if (StringUtils.isEmpty(item.getId())) {
                        cwWorkClientBilling.setClientId(cwWorkClientBase.getId());
                    }
                    cwWorkClientBillingService.saveOrUpdate(cwWorkClientBilling);
                });
            } else {
                cwWorkClientBillingService.remove(new QueryWrapper<CwWorkClientBilling>().lambda().eq(CwWorkClientBilling::getClientId,cwWorkClientBase.getId()));
            }
            // 联系人信息
            if (ObjectUtil.isNotEmpty(cwWorkClientBaseDTO.getCwWorkClientContactDTOList())){
                List<String> ids = cwWorkClientBaseDTO.getCwWorkClientContactDTOList().stream().filter(item -> {
                    if (StringUtils.isNotBlank(item.getId())) {
                        return true;
                    }
                    return false;
                }).distinct().map(CwWorkClientContactDTO::getId).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(ids)){
                    cwWorkClientContactService.remove(new QueryWrapper<CwWorkClientContact>().lambda()
                            .eq(CwWorkClientContact::getClientId,cwWorkClientBase.getId()).notIn(CwWorkClientContact::getId,ids));
                } else {
                    cwWorkClientContactService.remove(new QueryWrapper<CwWorkClientContact>().lambda().eq(CwWorkClientContact::getClientId,cwWorkClientBase.getId()));
                }
                cwWorkClientBaseDTO.getCwWorkClientContactDTOList().stream().forEach(item->{
                    CwWorkClientContact cwWorkClientContact = CwWorkClientContactWrapper.INSTANCE.toEntity(item);
                    if (StringUtils.isEmpty(item.getId())) {
                        cwWorkClientContact.setClientId(cwWorkClientBase.getId());
                    }
                    cwWorkClientContactService.saveOrUpdate(cwWorkClientContact);
                });
            } else {
                cwWorkClientContactService.remove(new QueryWrapper<CwWorkClientContact>().lambda().eq(CwWorkClientContact::getClientId,cwWorkClientBase.getId()));
            }
        }
        return cwWorkClientBase.getId();
    }

    public ResponseEntity saveClient(CwWorkClientBaseDTO cwWorkClientBaseDTO) throws Exception{
        this.saveForm(cwWorkClientBaseDTO);
        return ResponseEntity.ok("保存成功");
    }

    public ResponseEntity deleteByIds(String ids) {
        String idArray[] =ids.split(",");
        this.removeByIds (Lists.newArrayList (idArray));
        return ResponseEntity.ok ("删除成功");
    }

    public String updateStatusById(CwWorkClientBaseDTO cwWorkClientBaseDTO) {
        CwWorkClientBase cwWorkClientBase = CwWorkClientBaseWrapper.INSTANCE.toEntity(cwWorkClientBaseDTO);
        cwWorkClientBaseMapper.update(cwWorkClientBase, new QueryWrapper<CwWorkClientBase>().lambda().eq(CwWorkClientBase::getId, cwWorkClientBase.getId()));
        return "操作成功";
    }

    // 其他人发起修改申请
    public synchronized ResponseEntity<Map<String,String>> pushUpdate(String id) {
        CwWorkClientBaseDTO cwWorkClientBaseDTO = this.queryById(id);
        String jsonString = JSON.toJSONString(cwWorkClientBaseDTO);
        CwWorkClientTemp temp = new CwWorkClientTemp();
        temp.setSourceData(jsonString);
        temp.setClientId(id);
        temp.setStatus("2");
        cwWorkClientTempService.save(temp);
        Map<String,String> map = new HashMap<>();
        map.put("id", temp.getId());
        return ResponseEntity.ok (map);
    }

    // 终止修改申请
    public ResponseEntity stopUpdate(String id) throws Exception{
        CwWorkClientBase cwWorkClientBase = this.getById(id);
        CwWorkClientTemp cwWorkClientTemp = cwWorkClientTempService.getById(cwWorkClientBase.getUpdateRequestId());
        cwWorkClientTemp.setStatus("6"); // 6取消申请
        cwWorkClientTempService.updateById(cwWorkClientTemp);
        CwWorkClientBaseDTO cwWorkClientBaseDTO = JSON.parseObject(cwWorkClientTemp.getSourceData(), CwWorkClientBaseDTO.class);
        cwWorkClientBaseDTO.setUpdateRequestId("");
        cwWorkClientBaseDTO.setRequestUserId("");
        this.saveForm(cwWorkClientBaseDTO);
        return ResponseEntity.ok ("操作成功");
    }

    // 驳回/修改/通过申请
    public ResponseEntity rebackOrAgreeUpdate(String id, String type) {
        CwWorkClientBase cwWorkClientBase = this.getById(id);
        CwWorkClientTemp cwWorkClientTemp = cwWorkClientTempService.getById(cwWorkClientBase.getUpdateRequestId());
        if ("reject".equals(type)){
            cwWorkClientTemp.setStatus("4");
        }
        if ("reback".equals(type)){
            cwWorkClientTemp.setStatus("3");
        }
        if ("reback_push".equals(type)){
            // 撤回后重新发起修改
            cwWorkClientTemp.setStatus("2");
        }
        if ("agree".equals(type)){
            cwWorkClientTemp.setStatus("5");
            cwWorkClientBase.setUpdateUser(cwWorkClientBase.getRequestUserId()); // 审核通过后，拥有人改为当前申请人
            cwWorkClientBase.setRequestUserId(""); // 审核通过后，当前申请人清空
            cwWorkClientBase.setUpdateRequestId(""); // 审核通过后，修改申请临时表id清空
            this.updateById(cwWorkClientBase);
        }
        if ("reapply".equals(type)){
            cwWorkClientTemp.setStatus("2");
        }
        cwWorkClientTempService.updateById(cwWorkClientTemp);
        return ResponseEntity.ok ("操作成功");
    }

    // 查询社会统一信用代码是否存在
    public ResponseEntity<Map<String,String>> queryUscCodeIsUse(String uscCode) {
        UserDTO userDTO = UserUtils.getCurrentUserDTO();
        Map<String,String> map = new HashMap<>();
        // 根据社会统一信用代码查询
        List<CwWorkClientBase> list = this.list(new QueryWrapper<CwWorkClientBase>().lambda().eq(CwWorkClientBase::getUscCode, uscCode).orderByDesc(CwWorkClientBase::getCreateDate).last("limit 1"));
        if (CollectionUtil.isNotEmpty(list)) {
            CwWorkClientBase cwWorkClientBase = list.get(0);
            if (ObjectUtil.isNotEmpty(cwWorkClientBase)) {
                // 客户数据拥有人不是空时,按正常流程走数据，数据的拥有人是不可能为空的
                if (StringUtils.isNotBlank(cwWorkClientBase.getUpdateUser())){
                    // updateUser和当前登录人相等，说明这条数据的拥有人是自己，反之不是自己
                    if (userDTO.getId().equals(cwWorkClientBase.getUpdateUser())) {
                        map.put("use","1"); // 数据拥有人是自己，则返回1，打开修改弹出框
                    } else {
                        map.put("use","0"); // 数据拥有人不是自己，则返回0，打开修改申请
                    }
                    if ("0".equals(cwWorkClientBase.getStatus()) || "5".equals(cwWorkClientBase.getStatus())) {
                        map.put("isUpdate","true"); // 查询到的数据目前可修改
                    } else {
                        map.put("isUpdate","false"); // 查询到的数据目前不可修改，数据正在修改中
                    }
                }
                map.put("id",cwWorkClientBase.getId());
                return ResponseEntity.ok(map);
            }
        }
        map.put("use","false"); // 能走到这行代码，说明就没查到数据，uscCode不存在，可以使用
        return ResponseEntity.ok(map);
    }

    // 查询客户名称是否存在
    public ResponseEntity<Map<String,String>> queryNameIsUse(String name) {
        UserDTO userDTO = UserUtils.getCurrentUserDTO();
        Map<String,String> map = new HashMap<>();
        // 根据客户名称查询
        List<CwWorkClientBase> list = this.list(new QueryWrapper<CwWorkClientBase>().lambda().eq(CwWorkClientBase::getName, name).orderByDesc(CwWorkClientBase::getCreateDate).last("limit 1"));
        if (CollectionUtil.isNotEmpty(list)) {
            CwWorkClientBase cwWorkClientBase = list.get(0);
            if (ObjectUtil.isNotEmpty(cwWorkClientBase)) {
                // 客户数据拥有人不是空时,按正常流程走数据，正确数据的拥有人是不可能为空的
                if (StringUtils.isNotBlank(cwWorkClientBase.getUpdateUser())){
                    // updateUser和当前登录人相等，说明这条数据的拥有人是自己，反之不是自己
                    if (userDTO.getId().equals(cwWorkClientBase.getUpdateUser())) {
                        map.put("use","1"); // 数据拥有人是自己，则返回1，打开修改弹出框
                    } else {
                        map.put("use","0"); // 数据拥有人不是自己，则返回0，打开修改申请
                    }
                    if ("0".equals(cwWorkClientBase.getStatus()) || "5".equals(cwWorkClientBase.getStatus())) {
                        map.put("isUpdate","true"); // 查询到的数据目前可修改
                    } else {
                        map.put("isUpdate","false"); // 查询到的数据目前不可修改，数据正在修改中
                    }
                }
                map.put("id",cwWorkClientBase.getId());
                return ResponseEntity.ok(map);
            }
        }
        map.put("use","false"); // 能走到这行代码，说明就没查到数据，客户名称不存在，可以使用
        return ResponseEntity.ok(map);
    }

    // 根据客户id查询
    public ResponseEntity<String> queryUscCodeById(String id) {
        CwWorkClientBase cwWorkClientBase = this.getById(id);
        if (ObjectUtil.isNotEmpty(cwWorkClientBase)) {
            return ResponseEntity.ok(cwWorkClientBase.getUscCode());
        } else {
            return ResponseEntity.ok("");
        }
    }

    // 根据客户id查询
    public ResponseEntity<String> queryNameById(String id) {
        CwWorkClientBase cwWorkClientBase = this.getById(id);
        if (ObjectUtil.isNotEmpty(cwWorkClientBase)) {
            return ResponseEntity.ok(cwWorkClientBase.getName());
        } else {
            return ResponseEntity.ok("");
        }
    }

    // 根据客户id查询修改历史
    public ResponseEntity<List<CwWorkClientTempDTO>> queryUpdateLogList(CwWorkClientTempDTO tempDto) {
        QueryWrapper<CwWorkClientTemp> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("temp.create_date");
        if (ObjectUtil.isNotEmpty(tempDto)) {
            if (StringUtils.isNotBlank(tempDto.getClientId())) {
                queryWrapper.eq("temp.client_id", tempDto.getClientId());
            }
            if (StringUtils.isNotBlank(tempDto.getCreateName())) {
                List<User> userList = userMapper.selectList(new QueryWrapper<User>().lambda().like(User::getName, tempDto.getCreateName()));
                if (CollectionUtil.isNotEmpty(userList)) {
                    List<String> userIds = userList.stream().distinct().map(User::getId).collect(Collectors.toList());
                    if (CollectionUtil.isNotEmpty(userIds)) {
                        queryWrapper.in("temp.create_by",userIds);
                    } else {
                        return ResponseEntity.ok(new ArrayList<CwWorkClientTempDTO>());
                    }
                } else {
                    return ResponseEntity.ok(new ArrayList<CwWorkClientTempDTO>());
                }
            }
            if (StringUtils.isNotBlank(tempDto.getStatus())) {
                queryWrapper.eq("temp.status",tempDto.getStatus());
            }
            if (StringUtils.isNotBlank(tempDto.getClientNo())) {
                queryWrapper.like("cw_wcb.no",tempDto.getClientNo());
            }
        }
        return ResponseEntity.ok(cwWorkClientBaseMapper.queryUpdateLogList(queryWrapper));
    }

}
