package com.njcn.advance.service.carrycapacity.impl;

import cn.hutool.json.JSONUtil;
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.njcn.advance.mapper.carrycapacity.CarryCapacityResultPOMapper;
import com.njcn.advance.pojo.carrycapacity.param.CarryCapacityQueryDataParam;
import com.njcn.advance.pojo.carrycapacity.param.CarryCapacityResultParam;
import com.njcn.advance.pojo.carrycapacity.po.CarryCapacityResultPO;
import com.njcn.advance.pojo.carrycapacity.po.CarryCapacityUserPO;
import com.njcn.advance.pojo.carrycapacity.vo.CarryCapacityDResultVO;
import com.njcn.advance.service.carrycapacity.CarryCapacityResultPOService;
import com.njcn.advance.service.carrycapacity.CarryCapacityUserPOService;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.pq.pojo.vo.LineDetailVO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 
 * Description:
 * Date: 2024/3/1  15:38【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class CarryCapacityResultPOServiceImpl extends ServiceImpl<CarryCapacityResultPOMapper, CarryCapacityResultPO> implements CarryCapacityResultPOService{
    private final CarryCapacityUserPOService carryCapacityUserPOService;
    private  final LineFeignClient lineFeignClient;
    @Override
    public IPage<CarryCapacityDResultVO> queryResultList(CarryCapacityResultParam.CarryCapacityResultPageParam queryParam) {
        Page<CarryCapacityDResultVO> returnpage = new Page<> (queryParam.getPageNum ( ), queryParam.getPageSize ( ));
        Page<CarryCapacityResultPO> temppage = new Page<> (queryParam.getPageNum ( ), queryParam.getPageSize ( ));


        QueryWrapper<CarryCapacityResultPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(StringUtils.isNotBlank(queryParam.getEvaluateType()) ,CarryCapacityResultPO::getEvaluateType,queryParam.getEvaluateType())
                .between(StringUtils.isNotBlank(queryParam.getStartTime()) && StringUtils.isNotBlank(queryParam.getEndTime()) ,CarryCapacityResultPO::getEvaluateDate,queryParam.getStartTime()+" 00:00:00",queryParam.getEndTime()+" 23:59:59")
                .eq(CarryCapacityResultPO::getStatus,1)
                .orderByDesc(CarryCapacityResultPO::getEvaluateDate);

        IPage<CarryCapacityResultPO> page = this.page(temppage, queryWrapper);
        List<CarryCapacityDResultVO> collect = page.getRecords().stream().map(temp -> {
            CarryCapacityDResultVO vo = new CarryCapacityDResultVO();
            BeanUtils.copyProperties(temp, vo);
            String[] split = temp.getUserId().split(",");
            List<String> collect1 = Arrays.stream(split).sequential().map(userId -> {
                CarryCapacityUserPO carryCapacityUser = carryCapacityUserPOService.queyDetailUserById(userId);
                return carryCapacityUser.getUserName();
            }).collect(Collectors.toList());
            vo.setUserName(String.join(",", collect1));
            if (ObjectUtils.isNotEmpty(temp.getIResultList()) ){
                String iResultList = temp.getIResultList();
                List<CarryCapacityDResultVO.CarryCapacityIResult> list = JSONUtil.toList(JSONUtil.toJsonStr(iResultList), CarryCapacityDResultVO.CarryCapacityIResult.class);
                vo.setIResultList(list);
            }
            if(StringUtils.isNotBlank(vo.getLineId())){
                LineDetailVO data = lineFeignClient.getLineSubGdDetail(vo.getLineId()).getData();
                if(Objects.nonNull(data)){
                    vo.setLineName(data.getGdName()+"->"
                            +data.getSubName()+"->"
                            +data.getDevName()+"->"
                            +data.getLineName());
                }

            }

            return vo;
        }).collect(Collectors.toList());
        returnpage.setRecords(collect);
        returnpage.setTotal(page.getTotal());

        return returnpage;
    }

    @Override
    public CarryCapacityDResultVO queryResultbyCondition(CarryCapacityQueryDataParam queryParam) {
        CarryCapacityDResultVO vo = new CarryCapacityDResultVO();

        CarryCapacityResultPO one = this.lambdaQuery().eq(CarryCapacityResultPO::getLineId, queryParam.getLineId())
                .eq(CarryCapacityResultPO::getStartTime, queryParam.getStartTime())
                .eq(CarryCapacityResultPO::getEndTime, queryParam.getEndTime())
                .eq(CarryCapacityResultPO::getUserId, queryParam.getUserId())
                .eq(CarryCapacityResultPO::getStatus, 1).one();
        if(Objects.nonNull(one)){
            BeanUtils.copyProperties(one, vo);
        }
        return vo;


    }
}
