package com.njcn.csdevice.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.csdevice.pojo.dto.CsEquipmentProcessDTO;
import com.njcn.csdevice.pojo.vo.CsEventSendMsgVO;
import com.njcn.system.pojo.po.EleEpdPqd;
import com.njcn.user.api.UserFeignClient;
import com.njcn.user.pojo.po.User;
import com.njcn.web.pojo.param.BaseParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.csdevice.mapper.CsEquipmentProcessPOMapper;
import com.njcn.csdevice.pojo.po.CsEquipmentProcessPO;
import com.njcn.csdevice.service.CsEquipmentProcessPOService;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 
 * Description:
 * Date: 2023/9/12  16:10【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class CsEquipmentProcessPOServiceImpl extends ServiceImpl<CsEquipmentProcessPOMapper, CsEquipmentProcessPO> implements CsEquipmentProcessPOService{
    private final UserFeignClient userFeignClient;

    @Override
    public List<CsEquipmentProcessDTO> queryqueryList(CsEquipmentProcessDTO csEquipmentProcessDTO) {
        List<CsEquipmentProcessPO> list = this.lambdaQuery().eq(StringUtils.isNotBlank(csEquipmentProcessDTO.getId()), CsEquipmentProcessPO::getId, csEquipmentProcessDTO.getId()).
                eq(StringUtils.isNotBlank(csEquipmentProcessDTO.getDevId()), CsEquipmentProcessPO::getDevId, csEquipmentProcessDTO.getDevId()).
                eq(StringUtils.isNotBlank(csEquipmentProcessDTO.getProcess()), CsEquipmentProcessPO::getProcess, csEquipmentProcessDTO.getProcess()).list();
        List<CsEquipmentProcessDTO> collect = list.stream().map(temp -> {
            CsEquipmentProcessDTO csEquipmentProcessDTO1 = new CsEquipmentProcessDTO();
            BeanUtils.copyProperties(temp, csEquipmentProcessDTO1);
            return csEquipmentProcessDTO1;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Page<CsEquipmentProcessDTO> queryPage(BaseParam baseParam) {
        Page<CsEquipmentProcessDTO> returnpage = new Page<> (baseParam.getPageNum ( ), baseParam.getPageSize ( ));

        Page<CsEquipmentProcessDTO> result = this.getBaseMapper().selectPages(returnpage,baseParam);
        List<String> collect1 = result.getRecords().stream().map(CsEquipmentProcessDTO::getOperator).distinct().collect(Collectors.toList());
        Map<String, String> collect2;
        if(!CollectionUtils.isEmpty(collect1)){
            List<User> userTem = userFeignClient.appuserByIdList(collect1).getData();
            collect2 = userTem.stream().collect(Collectors.toMap(User::getId, User::getName, (e1, e2) -> e1 + "," + e2));

        } else {
            collect2 = new HashMap<>();
        }
        result.getRecords().forEach(temp->{
            temp.setOperatorName(collect2.get(temp.getOperator()));
        });
        return result;
    }
}
