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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.product.carrycapacity.enums.CarryCapacityResponseEnum;
import com.njcn.product.carrycapacity.mapper.CarryCapacityStrategyDhlPOMapper;
import com.njcn.product.carrycapacity.mapper.CarryCapacityStrategyPOMapper;
import com.njcn.product.carrycapacity.pojo.param.CarryCapacityStrategyParam;
import com.njcn.product.carrycapacity.pojo.po.CarryCapacityStrategyDhlPO;
import com.njcn.product.carrycapacity.pojo.po.CarryCapacityStrategyPO;
import com.njcn.product.carrycapacity.pojo.vo.CarryCapacityStrategyVO;
import com.njcn.product.carrycapacity.service.CarryCapacityStrategyPOService;
import lombok.RequiredArgsConstructor;
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.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 
 * Description:
 * Date: 2024/3/5  10:33【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class CarryCapacityStrategyPOServiceImpl extends ServiceImpl<CarryCapacityStrategyPOMapper, CarryCapacityStrategyPO> implements CarryCapacityStrategyPOService {
        private final CarryCapacityStrategyDhlPOMapper carryCapacityStrategyDhlPOMapper;
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean add(CarryCapacityStrategyParam carryCapacityStrategyParam) {

        CarryCapacityStrategyPO carryCapacityStrategy = new CarryCapacityStrategyPO();
        BeanUtils.copyProperties(carryCapacityStrategyParam, carryCapacityStrategy);
        //将原始策略处理为不启用
        this.lambdaUpdate().eq(CarryCapacityStrategyPO::getResult, carryCapacityStrategyParam.getResult())
                .eq(CarryCapacityStrategyPO::getId, carryCapacityStrategyParam.getId())
                .eq(CarryCapacityStrategyPO::getIndexResult, carryCapacityStrategyParam.getIndexResult())
                .eq(CarryCapacityStrategyPO::getProtoFlag, 1)
                .set(CarryCapacityStrategyPO::getUserFlag,2)
                .update();
        QueryWrapper<CarryCapacityStrategyPO> lambdaQuery = new QueryWrapper<>();
        lambdaQuery.lambda()
                .eq(CarryCapacityStrategyPO::getResult, carryCapacityStrategyParam.getResult())
                .eq(CarryCapacityStrategyPO::getId, carryCapacityStrategyParam.getId())
                .eq(CarryCapacityStrategyPO::getIndexResult, carryCapacityStrategyParam.getIndexResult())
                .eq(CarryCapacityStrategyPO::getProtoFlag, 2);
        //将客户对应策略删除
       this.remove(lambdaQuery);
        //新增客户策略;
        carryCapacityStrategy.setProtoFlag(2);
        carryCapacityStrategy.setUserFlag(1);
        return this.save(carryCapacityStrategy);
    }

    @Override
    public List<CarryCapacityStrategyVO> queyDetail() {
        List<CarryCapacityStrategyVO> result = new ArrayList<>();
        List<CarryCapacityStrategyPO> list = this.lambdaQuery().eq(CarryCapacityStrategyPO::getUserFlag, 1).list();
        Map<Integer, Map<String, List<CarryCapacityStrategyPO>>> collect = list.stream().collect(Collectors.groupingBy(CarryCapacityStrategyPO::getResult,
                Collectors.groupingBy(CarryCapacityStrategyPO::getId)));
        collect.forEach((key, value) -> {
            CarryCapacityStrategyVO vo = new CarryCapacityStrategyVO();
            vo.setResult(key);
            List<CarryCapacityStrategyVO.CarryCapacityStrategysingleVO> capacityStrategysingleVOList =new ArrayList<>();
            value.forEach((k, v) -> {
                CarryCapacityStrategyVO.CarryCapacityStrategysingleVO vo1 = new CarryCapacityStrategyVO.CarryCapacityStrategysingleVO();
                vo1.setId(k);
                vo1.setCarryCapacityStrategyIndexVOList(v.stream().map(temp -> {
                    CarryCapacityStrategyVO.CarryCapacityStrategysingleVO.CarryCapacityStrategyIndexVO vo2 = new CarryCapacityStrategyVO.CarryCapacityStrategysingleVO.CarryCapacityStrategyIndexVO();
                    BeanUtils.copyProperties(temp, vo2);
                    return vo2;
                }).collect(Collectors.toList()));
                capacityStrategysingleVOList.add(vo1);
            });
            vo.setCapacityStrategysingleVOList(capacityStrategysingleVOList);
            result.add(vo);
        });

        return result;
    }

    @Override
    public Boolean restore() {
        //将客户对应策略删除
        QueryWrapper<CarryCapacityStrategyPO> lambdaQuery = new QueryWrapper<>();
        lambdaQuery.lambda()
                .eq(CarryCapacityStrategyPO::getProtoFlag, 2);
        this.remove(lambdaQuery);

        //将原始策略处理为启用
        boolean update = this.lambdaUpdate().eq(CarryCapacityStrategyPO::getProtoFlag, 1)
                .set(CarryCapacityStrategyPO::getUserFlag, 1)
                .update();

        //电弧炉初始化
        QueryWrapper<CarryCapacityStrategyDhlPO> lambdaQuery2 = new QueryWrapper<>();
        lambdaQuery2.lambda()
                .eq(CarryCapacityStrategyDhlPO::getProtoFlag, 2);
        carryCapacityStrategyDhlPOMapper.delete(lambdaQuery2);
        UpdateWrapper<CarryCapacityStrategyDhlPO> lambdaQuery3 = new UpdateWrapper<>();
        lambdaQuery3.lambda()
                .eq(CarryCapacityStrategyDhlPO::getProtoFlag, 1)
                .set(CarryCapacityStrategyDhlPO::getUserFlag, 1);

        carryCapacityStrategyDhlPOMapper.update(null,lambdaQuery3);

        return update;
    }

    @Override
    public Boolean addList(List<CarryCapacityStrategyParam> carryCapacityStrategyParamList) {
        UUID uuid = UUID.randomUUID();
        if(4!=carryCapacityStrategyParamList.size()){
            throw new BusinessException(CarryCapacityResponseEnum.UNCOMPLETE_STRATEGY);

        }
        List<CarryCapacityStrategyPO> collect = carryCapacityStrategyParamList.stream().map(temp -> {
            CarryCapacityStrategyPO po = new CarryCapacityStrategyPO();
            BeanUtils.copyProperties(temp, po);
            po.setId(uuid.toString());
            //新增客户策略;
            po.setProtoFlag(2);
            po.setUserFlag(1);
            return po;
        }).collect(Collectors.toList());
        return this.saveBatch(collect);
    }
}
