package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.PcFactoryPollutantEmissionsRank;
import cn.cecep.talroad.mapper.PcFactoryPollutantEmissionsRankMapper;
import cn.cecep.talroad.service.IPcFactoryPollutantEmissionsRankService;
import cn.cecep.talroad.vo.IndustryEmissionTrackingOverviewVo;
import cn.cecep.talroad.vo.query.PcFactoryPollutantEmissionsRankQuery;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 污染排放跟踪(实际排放/许可排放)Service业务层处理
 *
 * @author szhpt
 * @date 2023-02-10*/
@Service
public class PcFactoryPollutantEmissionsRankServiceImpl extends ServiceImpl<PcFactoryPollutantEmissionsRankMapper,PcFactoryPollutantEmissionsRank> implements IPcFactoryPollutantEmissionsRankService
{
    @Autowired
    private PcFactoryPollutantEmissionsRankMapper pcFactoryPollutantEmissionsRankMapper;

    /**
     * 查询污染排放跟踪(实际排放/许可排放)列表
     *
     * @param pcFactoryPollutantEmissionsRank 污染排放跟踪(实际排放/许可排放)
     * @return 污染排放跟踪(实际排放/许可排放)
     */
    @Override
    public List<PcFactoryPollutantEmissionsRank> selectPcFactoryPollutantEmissionsRankList(PcFactoryPollutantEmissionsRank pcFactoryPollutantEmissionsRank)
    {
        return pcFactoryPollutantEmissionsRankMapper.selectPcFactoryPollutantEmissionsRankList(pcFactoryPollutantEmissionsRank);
    }

    @Override
    public List<PcFactoryPollutantEmissionsRank> selectRankList(PcFactoryPollutantEmissionsRankQuery pcFactoryPollutantEmissionsRank) {
        String factoryId = pcFactoryPollutantEmissionsRank.getFactoryId();
        String dataSource = pcFactoryPollutantEmissionsRank.getDataSource();

        List<IndustryEmissionTrackingOverviewVo> industryEmissionTrackingOverviewVos = pcFactoryPollutantEmissionsRankMapper.selectRankList(factoryId, StringUtils.equals(dataSource, "1") ? "执行报告" : "系统核算");
        return industryEmissionTrackingOverviewVos.stream()
                .map(x -> {
                    PcFactoryPollutantEmissionsRank rank = new PcFactoryPollutantEmissionsRank();
                    rank.setFactoryId(factoryId);
                    rank.setPollName(x.getPollName());
                    rank.setEmissionsActual(String.valueOf(Objects.isNull(x.getEmissionsActual()) ? BigDecimal.ZERO : x.getEmissionsActual().setScale(4, RoundingMode.HALF_UP)));
                    rank.setEmissionsPermit(String.valueOf(Objects.isNull(x.getEmissionsPermit()) ? BigDecimal.ZERO : x.getEmissionsPermit().setScale(4, RoundingMode.HALF_UP)));
                    rank.setEmissionsRatio(String.valueOf(Objects.isNull(x.getCalculate()) ? BigDecimal.ZERO : x.getCalculate().multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP)));
                    rank.setEmissionsRank(String.valueOf(x.getRank()));
                    return rank;

                })
                .collect(Collectors.toList());
    }

}
