package com.njcn.csdevice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.csdevice.mapper.CsEngineeringMapper;
import com.njcn.csdevice.mapper.CsMarketDataMapper;
import com.njcn.csdevice.pojo.param.CsMarketDataParam;
import com.njcn.csdevice.pojo.param.EngineerIdParam;
import com.njcn.csdevice.pojo.po.CsEngineeringPO;
import com.njcn.csdevice.pojo.po.CsLedger;
import com.njcn.csdevice.pojo.po.CsMarketData;
import com.njcn.csdevice.pojo.vo.CsMarketDataVO;
import com.njcn.csdevice.service.CsMarketDataService;
import com.njcn.csdevice.service.ICsLedgerService;
import com.njcn.web.utils.RequestUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * Description:
 * Date: 2023/7/31  9:24【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@AllArgsConstructor

public class CsMarketDataServiceImpl extends ServiceImpl<CsMarketDataMapper, CsMarketData> implements CsMarketDataService {
    private final CsEngineeringMapper csEngineeringMapper;
    private final ICsLedgerService csLedgerService;
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean add(List<CsMarketDataParam> csMarketDataParams) {
        QueryWrapper<CsMarketData> queryWrap = new QueryWrapper<>();
        queryWrap.eq("user_id",csMarketDataParams.get(0).getUserId());
        this.remove(queryWrap);

        csMarketDataParams.forEach(temp->{
            CsMarketData csMarketData =new CsMarketData();
            BeanUtils.copyProperties(temp,csMarketData);
            this.save(csMarketData);
        });

        return true;
    }

    @Override
    public List<CsMarketDataVO> queryByUseId(String userId) {
        List<CsMarketData> list = this.lambdaQuery().eq(CsMarketData::getUserId, userId).list();
        List<CsMarketDataVO> collect = list.stream().map(temp -> {
            CsMarketDataVO cs = new CsMarketDataVO();
            BeanUtils.copyProperties(temp, cs);
            CsEngineeringPO csEngineeringPO = csEngineeringMapper.selectById(temp.getEngineerId());
            cs.setEngineerName(csEngineeringPO.getName());
            return cs;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<CsLedger> queryEnginnerByUseId(String userId) {
        List<CsLedger> list = csLedgerService.lambdaQuery().eq(CsLedger::getState, 1).eq(CsLedger::getLevel, 0).list();
        List<CsMarketDataVO> csMarketDataVOS = this.queryByUseId(userId);
        List<String> collect = csMarketDataVOS.stream().map(CsMarketDataVO::getEngineerId).collect(Collectors.toList());
        List<CsLedger> collect1 = list.stream().filter(temp -> !collect.contains(temp.getId())).collect(Collectors.toList());

        return collect1;
    }

    @Override
    public List<CsMarketDataVO> queryAllEnginner(String userId) {
        List<CsMarketDataVO> result = new ArrayList<>();
        List<CsLedger> list = csLedgerService.lambdaQuery()
                .eq(CsLedger::getState, 1)
                .eq(CsLedger::getLevel, 0)
                .ne(CsLedger::getName, "便携式工程")
                .list();
        List<CsMarketDataVO> csMarketDataVOS = this.queryByUseId(userId);
        List<String> collect = csMarketDataVOS.stream().map(CsMarketDataVO::getEngineerId).collect(Collectors.toList());
        List<CsLedger> collect1 = list.stream().filter(temp -> !collect.contains(temp.getId())).collect(Collectors.toList());
        csMarketDataVOS.forEach(temp->{
            temp.setIsSelect("1");
            result.add(temp);
        });
        collect1.forEach(temp->{
            CsMarketDataVO vo= new CsMarketDataVO();
            vo.setEngineerName(temp.getName());
            vo.setEngineerId(temp.getId());
            vo.setIsSelect("0");
            result.add(vo);
        });

        return result;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean follow(EngineerIdParam engineerIdParams) {
        String userIndex = RequestUtil.getUserIndex();
        QueryWrapper<CsMarketData> queryWrap = new QueryWrapper<>();
        queryWrap.eq("user_id",userIndex);
        this.remove(queryWrap);
        List<CsMarketData> csMarketData = new ArrayList<>();
        engineerIdParams.getEngineerIds().forEach(temp->{
            CsMarketData cs = new CsMarketData();
            cs.setEngineerId(temp);
            cs.setUserId(userIndex);
            csMarketData.add(cs);
        });
        this.saveBatch(csMarketData,20);
        return true;
    }

    @Override
    public Boolean insertData(String userId, String engineeringId) {
        LambdaQueryWrapper<CsMarketData> queryWrap = new LambdaQueryWrapper<>();
        queryWrap.eq(CsMarketData::getUserId,userId);
        queryWrap.eq(CsMarketData::getEngineerId,engineeringId);
        CsMarketData csMarketData = this.getOne(queryWrap);
        if (csMarketData == null) {
            csMarketData = new CsMarketData();
            csMarketData.setUserId(userId);
            csMarketData.setEngineerId(engineeringId);
            this.save(csMarketData);
        }
        return true;
    }
}
