package cn.cecep.talroad.service.impl.effective;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.domain.effective.RectificationEffectQueryDTO;
import cn.cecep.talroad.mapper.effective.RectificationEffectEvaluationMapper;
import cn.cecep.talroad.service.effective.IRectificationEffectEvaluationService;
import cn.cecep.talroad.vo.effective.FactoryRectificationEffectCountVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;


@Service
public class RectificationEffectEvaluationServiceImpl implements IRectificationEffectEvaluationService {

    @Resource
    private RectificationEffectEvaluationMapper rectificationEffectEvaluationMapper;


    @Override
    public AjaxResults<Page<FactoryRectificationEffectCountVO>> list(RectificationEffectQueryDTO rectificationEffectQueryDTO) {
        //处理模糊查询
        if (StringUtils.isNotBlank(rectificationEffectQueryDTO.getFactoryName())) {
            rectificationEffectQueryDTO.setFactoryName("%"+rectificationEffectQueryDTO.getFactoryName()+"%");
        }
        Page<FactoryRectificationEffectCountVO> page = new Page(rectificationEffectQueryDTO.getPageNum(),rectificationEffectQueryDTO.getPageSize());
        page = rectificationEffectEvaluationMapper.selectFinishCountByFactory(page,rectificationEffectQueryDTO);
        for (FactoryRectificationEffectCountVO record : page.getRecords()) {
            //按期整改完成率：按期整改完成数量/（按期整改完成数+延期整改完成数+整改延时未完成数+待整改数）。
            BigDecimal inTimeFinishCount = new BigDecimal(record.getInTimeFinishCount());
            BigDecimal totalFinishCount = inTimeFinishCount
                    .add(new BigDecimal(record.getOutTimeFinishCount()))
                    .add(new BigDecimal(record.getOutTimeFinishCount()))
                    .add(new BigDecimal(record.getUnFinishCount()));
            BigDecimal inTimeFinishedRate = inTimeFinishCount.divide(totalFinishCount, 2,RoundingMode.HALF_EVEN).multiply(new BigDecimal(100));
            record.setInTimeFinishedRate(inTimeFinishedRate+"%");
        }
        return AjaxResults.success(page);
    }
}
