package jnpf.personnels.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import jnpf.model.personnels.dto.range.FtbRangeConfigDIYDTO;
import jnpf.model.personnels.dto.range.FtbRangeConfigDTO;
import jnpf.model.personnels.dto.range.FtbRangeDiyQueryDTO;
import jnpf.model.personnels.po.FtbPersonnlesInfoConfig;
import jnpf.model.personnels.po.FtbPersonnlesInfoDiyRangeConfig;
import jnpf.model.personnels.po.FtbPersonnlesInfoRangeConfig;
import jnpf.model.personnels.vo.range.FtbRangeConfigDIYVO;
import jnpf.model.personnels.vo.range.FtbRangeConfigVO;
import jnpf.personnels.mapper.FtbPersonnlesInfoConfigMapper;
import jnpf.personnels.mapper.FtbPersonnlesInfoDiyRangeConfigMapper;
import jnpf.personnels.mapper.FtbPersonnlesInfoRangeConfigMapper;
import jnpf.personnels.service.FtbPersonnlesInfoConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
*
*
*@Author: peng.hao
*@create: 2025/4/7
*
*/
@Service
public class FtbPersonnlesInfoConfigServiceImpl implements FtbPersonnlesInfoConfigService{

    @Resource
    private FtbPersonnlesInfoConfigMapper ftbPersonnlesInfoConfigMapper;

    @Resource
    private FtbPersonnlesInfoDiyRangeConfigMapper diyRangeConfigMapper;
    @Resource
    private FtbPersonnlesInfoRangeConfigMapper rangeConfigMapper;

    @Override
    public FtbRangeConfigVO queryInfo(Integer type) {
        LambdaQueryWrapper<FtbPersonnlesInfoConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(FtbPersonnlesInfoConfig::getConfigType,1);
        wrapper.eq(FtbPersonnlesInfoConfig::getType,type);
        FtbPersonnlesInfoConfig config = ftbPersonnlesInfoConfigMapper.selectOne(wrapper);
        LambdaQueryWrapper<FtbPersonnlesInfoRangeConfig> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(FtbPersonnlesInfoRangeConfig::getConfigId,config.getId());
        FtbPersonnlesInfoRangeConfig rangeConfig = rangeConfigMapper.selectOne(lambdaQuery);
        return FtbRangeConfigVO.covert(rangeConfig);
    }
    @Override
    public List<FtbRangeConfigDIYVO> queryDiyInfo(Integer type) {
        LambdaQueryWrapper<FtbPersonnlesInfoConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(FtbPersonnlesInfoConfig::getConfigType,2);
        wrapper.eq(FtbPersonnlesInfoConfig::getType,type);
        FtbPersonnlesInfoConfig config = ftbPersonnlesInfoConfigMapper.selectOne(wrapper);
        if (config == null){
            throw new RuntimeException("该配置不存在");
        }
        LambdaQueryWrapper<FtbPersonnlesInfoDiyRangeConfig> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(FtbPersonnlesInfoDiyRangeConfig::getConfigId,config.getId());
        List<FtbPersonnlesInfoDiyRangeConfig> rangeConfigs = diyRangeConfigMapper.selectList(lambdaQuery);
        return rangeConfigs.stream().map(FtbRangeConfigDIYVO::covert).collect(Collectors.toList());
    }

    @Override
    public Integer queryType(Integer type) {
        LambdaQueryWrapper<FtbPersonnlesInfoConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(FtbPersonnlesInfoConfig::getType,type);
        FtbPersonnlesInfoConfig config = ftbPersonnlesInfoConfigMapper.selectOne(wrapper);
        return config.getConfigType();
    }

    @Override
    public void updateInfo(FtbRangeConfigDTO dto) {
        LambdaQueryWrapper<FtbPersonnlesInfoConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(FtbPersonnlesInfoConfig::getType,dto.getType());
        FtbPersonnlesInfoConfig config = ftbPersonnlesInfoConfigMapper.selectOne(wrapper);
        //List<RangeDTO> dtoList = RangeConfigUtil.generateRanges(dto.getFirstGear(), dto.getIntervalValue(), dto.getNumberOfGears());
        FtbPersonnlesInfoRangeConfig entity = FtbPersonnlesInfoRangeConfig.covert(dto);
        entity.setPreviewValue(dto.getPreviewValue());
        entity.setConfigId(config.getId());
        LambdaQueryWrapper<FtbPersonnlesInfoRangeConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnlesInfoRangeConfig::getConfigId,config.getId());
        rangeConfigMapper.delete(queryWrapper);
        rangeConfigMapper.insert(entity);
        changeTheDefaultType(1, config.getId());
        // 清理 自定义数据
        LambdaUpdateWrapper<FtbPersonnlesInfoDiyRangeConfig> update = Wrappers.lambdaUpdate();
        update.eq(FtbPersonnlesInfoDiyRangeConfig::getConfigId, config.getId());
        diyRangeConfigMapper.delete(update);
    }
    @Override
    public void updateDiyInfo(FtbRangeDiyQueryDTO diyQueryDTO) {
        List<FtbRangeConfigDIYDTO> dto = diyQueryDTO.getDiydtos();
        LambdaQueryWrapper<FtbPersonnlesInfoConfig> wrapper = Wrappers.lambdaQuery();
        String type = diyQueryDTO.getType();
        wrapper.eq(FtbPersonnlesInfoConfig::getType, type);
        FtbPersonnlesInfoConfig config = ftbPersonnlesInfoConfigMapper.selectOne(wrapper);
        List<FtbPersonnlesInfoDiyRangeConfig> diyRangeConfig = dto.stream().map(item->{
                    FtbPersonnlesInfoDiyRangeConfig covert = FtbPersonnlesInfoDiyRangeConfig.covert(item);
                    covert.setConfigId(config.getId());
                    return covert;})
                .collect(Collectors.toList());
        LambdaQueryWrapper<FtbPersonnlesInfoDiyRangeConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnlesInfoDiyRangeConfig::getConfigId, config.getId());
        diyRangeConfigMapper.delete(queryWrapper);
        Db.saveBatch(diyRangeConfig);
        changeTheDefaultType(2,config.getId());
        // 清理 平均数据
        LambdaUpdateWrapper<FtbPersonnlesInfoRangeConfig> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(FtbPersonnlesInfoRangeConfig::getConfigId,config.getId());
        rangeConfigMapper.delete(updateWrapper);
    }
    /**
     * 更改默认类型
     * @param configType 配置类型
     *@param   configId
     */
    private void changeTheDefaultType(Integer configType,String configId) {
        // 更改默认类型
        FtbPersonnlesInfoConfig infoConfig = new FtbPersonnlesInfoConfig();
        infoConfig.setId(configId);
        infoConfig.setConfigType(configType);
        ftbPersonnlesInfoConfigMapper.updateById(infoConfig);
    }

}
