package com.energy.manage.service.service.windfieldbatch.impl;

import com.energy.manage.common.constant.redis.ManagerRedisKeyConstant;
import com.energy.manage.common.enums.IdPrefixEnum;
import com.energy.manage.common.po.analysis.AnalysisResultPo;
import com.energy.manage.common.util.IdGeneratorUtil;
import com.energy.manage.service.domain.dto.windfieldbatch.WindFieldBatchDto;
import com.energy.manage.service.domain.dto.windfieldbatch.WindFileBatchPageDto;
import com.energy.manage.service.domain.vo.windfield.WindFieldVo;
import com.energy.manage.service.domain.vo.windfieldbatch.WindFieldBatchVo;
import com.energy.manage.service.mappers.analysis.AnalysisResultMapper;
import com.energy.manage.service.mappers.windfield.WindFieldMapper;
import com.energy.manage.service.mappers.windfieldbatch.WindFieldBatchMapper;
import com.energy.manage.service.service.cache.CacheService;
import com.energy.manage.service.service.windfieldbatch.WindFieldBatchService;
import com.energy.manage.service.util.PinYinUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author chy
 * @date 2024/5/21 10:51
 * @desc
 */
@Service
public class WindFieldBatchServiceImpl implements WindFieldBatchService {

    private final static String SEPARATOR = "-";

     @Autowired
     private WindFieldBatchMapper windFieldBatchMapper;

     @Autowired
     private WindFieldMapper windFieldMapper;

    @Autowired
    private AnalysisResultMapper analysisResultMapper;

     @Autowired
     private CacheService cacheService;

    /**
     * 批次号新增
     * @param windEgineBatchDto
     */
    @Override
    public int addWindFieldBatch(WindFieldBatchDto windEgineBatchDto) {
        WindFieldBatchVo exsitBatchVo = windFieldBatchMapper.selectByBatchName(windEgineBatchDto.getBatchName());
        if(exsitBatchVo != null){
            return -1;
        }
        windEgineBatchDto.setBatchCode(createWindFieldBatchCode(windEgineBatchDto.getFieldCode()));
        WindFieldVo windFieldVo = windFieldMapper.selectByCodeNumber(windEgineBatchDto.getFieldCode());
        windEgineBatchDto.setFieldName(windFieldVo.getFieldName());
        windFieldBatchMapper.addWindFieldBatch(windEgineBatchDto);
        //添加风场批次后，添加分析结果初始数据
        AnalysisResultPo analysisResultPo = new AnalysisResultPo();
        analysisResultPo.setBatchCode(windEgineBatchDto.getBatchCode());
        return analysisResultMapper.insertSelective(analysisResultPo);
    }

    /**
     * 批次号更新
     * @param windEgineBatchDto
     */
    @Override
    public int updateWindFieldBatch(WindFieldBatchDto windEgineBatchDto) {
        return windFieldBatchMapper.updateWindFieldBatch(windEgineBatchDto);
    }

    /**
     * 批次启用或停用
     * @param windEgineBatchDto
     */
    @Override
    public int onOrOffFieldBatch(WindFieldBatchDto windEgineBatchDto) {
        return windFieldBatchMapper.updateWindFieldBatch(windEgineBatchDto);
    }

    /**
     * 批次号删除
     * @param windFieldBatchDto
     */
    @Override
    public int deleteWindFieldBatch(WindFieldBatchDto windFieldBatchDto) {
        windFieldBatchDto.setIsDelete(1);
        return windFieldBatchMapper.updateWindFieldBatch(windFieldBatchDto);
    }

    /**
     * 查询批次列表
     * @param windFieldBatchDto
     * @return
     */
    @Override
    public List<WindFieldBatchVo> queryWindFieldBatch(WindFileBatchPageDto windFieldBatchDto) {
        PageHelper.startPage(windFieldBatchDto);
        return windFieldBatchMapper.selectWindFieldBatch(windFieldBatchDto);
    }

    /**
     * 生成批次名称
     * @param fieldName
     * @return
     */
    @Override
    public String createBatchName(String fieldName){
        String fieldPYHeader = PinYinUtils.getPinYinHeadChar(fieldName);
        return fieldPYHeader.concat(IdGeneratorUtil.zeroFillUtil(cacheService.incr(ManagerRedisKeyConstant.build(ManagerRedisKeyConstant.IDGENERATOR_CONSTANTS_KEY, fieldPYHeader)),6));
    }

    /**
     * 生成风场批次号
     * @param fieldCode
     * @return
     */
    public String createWindFieldBatchCode(String fieldCode){
        String sequece = IdPrefixEnum.WIND_ENGINE_BATCH_NUMBER.getCode().concat(IdGeneratorUtil.zeroFillUtil(cacheService.incr(ManagerRedisKeyConstant.build(ManagerRedisKeyConstant.IDGENERATOR_CONSTANTS_KEY, fieldCode,IdPrefixEnum.WIND_ENGINE_BATCH_NUMBER.getCode())),6));
        return fieldCode + SEPARATOR + sequece;
    }
}
