package com.unis.projectManagement.modular.pmBudget.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.projectManagement.modular.pmBudget.entity.FundPlan;
import com.unis.projectManagement.modular.pmBudget.entity.FundPlanInfo;
import com.unis.projectManagement.modular.pmBudget.mapper.FundPlanMapper;
import com.unis.projectManagement.modular.pmBudget.param.FundPlanPageParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @author qingsong.han
 * @description:
 * @create 2024-05-30 11:27
 */
public interface FundPlanService extends IService<FundPlan> {


    Page<FundPlan> getPageList(FundPlanPageParam pageParam);

    List<FundPlanInfo> detailList(String pmApplicationId);

    @Service
    class FundPlanServiceImpl extends ServiceImpl<FundPlanMapper, FundPlan> implements FundPlanService {
        @Resource
        private FundPlanMapper fundPlanMapper;

        @Override
        public Page<FundPlan> getPageList(FundPlanPageParam pageParam) {
            QueryWrapper<FundPlan> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(pageParam.getProjectName()))
                wrapper.like("bpa.project_name", pageParam.getProjectName());
            return fundPlanMapper.getPageList(new Page<>(pageParam.getCurrent(), pageParam.getSize()), wrapper);
        }

        @Override
        public List<FundPlanInfo> detailList(String pmApplicationId) {
            List<FundPlanInfo> planList = fundPlanMapper.detailList(pmApplicationId);
            planList.forEach(plan -> {
                /*
                 * 实施预警：
                 * 触发规则：实际完成进度<计划完成进度。
                 * 提示语：实际进度滞后${rate}%
                 * rate=(计划完成进度-实际完成进度) / 计划完成进度 × 100%
                 */
                // 实际完成进度
                BigDecimal actualCompletionProgress = plan.getActualCompletionProgress();
                float acp = actualCompletionProgress.floatValue();
                // 计划完成进度
                BigDecimal planCompletionProgress = plan.getPlanCompletionProgress();
                float pcp = planCompletionProgress.floatValue();
                if (acp < pcp) {
                    plan.setApplyWarn("实际进度滞后" + new BigDecimal((pcp - acp) / pcp * 100).setScale(2, RoundingMode.HALF_UP).toString());
                }
                /*
                 * 资金预警：
                 * 触发规则：资金计划支出<资金实际支出。
                 * 提示语：资金超支${rate}%
                 * rate=(资金实际支出-资金计划支出) / 资金计划支出 × 100%
                 */
                // 资金计划支出
                BigDecimal fundsPlannedExpenditure = plan.getFundsPlannedExpenditure();
                float fpe = fundsPlannedExpenditure.floatValue();
                // 资金实际支出
                BigDecimal fundsActualExpenditure = plan.getFundsActualExpenditure();
                float fae = fundsActualExpenditure.floatValue();
                if (fpe < fae) {
                    plan.setFundWarn("资金超支" + new BigDecimal((fae - fpe) / fpe * 100).setScale(2, RoundingMode.HALF_UP).toString());
                }
            });
            return planList;
        }
    }

}
