package com.njcn.device.pq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pq.enums.PvDeviceResponseEnum;
import com.njcn.device.pq.mapper.PqsTflgployMapper;
import com.njcn.device.pq.pojo.param.TflgployParam;
import com.njcn.device.pq.pojo.po.PqsTflgploy;
import com.njcn.device.pq.pojo.po.PqsTflgployass;
import com.njcn.device.pq.pojo.po.PqsTransformer;
import com.njcn.device.pq.service.IPqsTflgployService;
import com.njcn.device.pq.service.IPqsTflgployassService;
import com.njcn.web.pojo.param.BaseParam;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wr
 * @since 2023-07-19
 */
@Service
@RequiredArgsConstructor
public class PqsTflgployServiceImpl extends ServiceImpl<PqsTflgployMapper, PqsTflgploy> implements IPqsTflgployService {

    private final IPqsTflgployassService iPqsTflgployassService;
    @Override
    public Page<PqsTflgploy> flgployPage(BaseParam param) {
        return this.page(new Page<>(param.getPageNum(), param.getPageSize()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertFlgPloy(TflgployParam param) {
        List<PqsTflgploy> list = this.list(new LambdaQueryWrapper<PqsTflgploy>()
                .eq(PqsTflgploy::getTpName, param.getTpName())

        );
        if (CollectionUtil.isNotEmpty(list)) {
            throw new BusinessException(PvDeviceResponseEnum.TFLGPLOY_NAME_REPEAT);
        }

        //创建新
        PqsTflgploy tflgploy= BeanUtil.copyProperties(param,PqsTflgploy.class);
        tflgploy.setStatus(1);
        boolean save = this.save(tflgploy);
        List<PqsTflgployass> info=new ArrayList();
        addList(param, tflgploy, save, info);
        return iPqsTflgployassService.saveBatch(info);
    }

    @Override
    public TflgployParam.TflgployUpdateParam flgPloyInfo(String id) {
        PqsTflgploy tflgploy = this.baseMapper.selectById(id);
        TflgployParam.TflgployUpdateParam param=BeanUtil.copyProperties(tflgploy,TflgployParam.TflgployUpdateParam.class);
        List<PqsTflgployass> list = iPqsTflgployassService.list(new LambdaQueryWrapper<PqsTflgployass>().eq(
                PqsTflgployass::getTpIndex, id
        ));
        List<String> tfIndexList = list.stream().map(PqsTflgployass::getTfIndex).collect(Collectors.toList());
        param.setTfIndexs(tfIndexList);
        return param;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateFlgPloy(TflgployParam.TflgployUpdateParam param) {
        //根据信息查询当前策略是否存在
        int count = this.count(new LambdaQueryWrapper<PqsTflgploy>()
                .eq(PqsTflgploy::getTpIndex, param.getTpIndex()));

        Assert.isTrue(count>0 , "当前变压器策略不存在,请检查信息");
        List<PqsTflgploy> list = this.list(new LambdaQueryWrapper<PqsTflgploy>()
                .ne(PqsTflgploy::getTpIndex, param.getTpIndex())
                .and(wrapper ->
                        wrapper.eq(PqsTflgploy::getTpName, param.getTpName())
                )

        );
        if (CollectionUtil.isNotEmpty(list)) {
            throw new BusinessException(PvDeviceResponseEnum.TFLGPLOY_NAME_REPEAT);
        }
        PqsTflgploy tflgploy= BeanUtil.copyProperties(param,PqsTflgploy.class);
        boolean update = this.updateById(tflgploy);
        List<PqsTflgployass> info=new ArrayList();
        if(update){
            boolean remove = iPqsTflgployassService.remove(new LambdaQueryWrapper<PqsTflgployass>()
                    .eq(PqsTflgployass::getTpIndex, param.getTpIndex()));
            addList(param, tflgploy, remove, info);
        }
        return iPqsTflgployassService.saveBatch(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delFlgPloy(String ids) {
        int count = this.count(new LambdaQueryWrapper<PqsTflgploy>()
                .eq(PqsTflgploy::getTpIndex, ids));

        Assert.isTrue(count>0 , "当前变压器策略不存在,请检查信息");
        this.removeById(ids);
        boolean remove = iPqsTflgployassService.remove(new LambdaQueryWrapper<PqsTflgployass>()
                .eq(PqsTflgployass::getTpIndex, ids));
        return remove;
    }

    private void addList(TflgployParam param, PqsTflgploy tflgploy, boolean save, List<PqsTflgployass> info) {
//        if(save){
            List<String> tfIndexs = param.getTfIndexs();
            if(CollUtil.isNotEmpty(tfIndexs)){
                PqsTflgployass ass;
                for (String tfIndex : tfIndexs) {
                    ass=new PqsTflgployass();
                    ass.setTpIndex(tflgploy.getTpIndex());
                    ass.setTfIndex(tfIndex);
                    info.add(ass);
                }
//            }

        }
    }
}
