package vip.xiaonuo.money.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.money.entity.MoneyIncome;
import vip.xiaonuo.money.entity.vo.MoneyIncomeVo;
import vip.xiaonuo.money.mapper.MoneyIncomeMapper;
import vip.xiaonuo.money.param.MoneyIncomeParam;
import vip.xiaonuo.money.service.IMoneyIncomeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 财务收入达成 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@Service
public class MoneyIncomeServiceImpl extends ServiceImpl<MoneyIncomeMapper, MoneyIncome> implements IMoneyIncomeService {

    @Autowired
    private MoneyIncomeMapper incomeMapper;

    @Override
    public MoneyIncomeVo getMoneyIncome(MoneyIncomeParam incomeParam) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = "";
        if ("".equals(incomeParam.getTime()) || null == incomeParam.getTime()) {
             year = sdf.format(new Date());
        } else {
             year = incomeParam.getTime();
        }
        QueryWrapper<MoneyIncome> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", year).groupBy("company_type");
        queryWrapper.select(" IFNULL(sum(year_target),'0') yearTarget,IFNULL(sum(year_actual),'0') yearActual,company_name companyName," +
                "company_type companyType");
        List<MoneyIncome> moneyIncomes = incomeMapper.selectList(queryWrapper);
        DecimalFormat def = new DecimalFormat("0.00");
        MoneyIncomeVo incomeVo = new MoneyIncomeVo();
        double target = 0.00;
        double actual = 0.00;
        if (moneyIncomes.size() != 0) {
            for (MoneyIncome moneyIncome : moneyIncomes) {
                target = target + Double.valueOf(moneyIncome.getYearTarget());
                actual = actual + Double.valueOf(moneyIncome.getYearActual());
                if (moneyIncome.getCompanyType() == 1) {
                    incomeVo.setShengWu(moneyIncome.getYearActual());
                } else if (moneyIncome.getCompanyType() == 2) {
                    incomeVo.setYingYang(moneyIncome.getYearActual());
                } else if (moneyIncome.getCompanyType() == 3) {
                    incomeVo.setXiaSha(moneyIncome.getYearActual());
                }
            }
            incomeVo.setYearTarget(def.format(target));
            incomeVo.setYearActual(def.format(actual));
            incomeVo.setRank(def.format(actual / target));
        } else {
            incomeVo.setRank("0");
            incomeVo.setYearActual("0");
            incomeVo.setYearTarget("0");
            incomeVo.setShengWu("0");
            incomeVo.setYingYang("0");
            incomeVo.setXiaSha("0");
        }

        return incomeVo;
    }

    @Override
    public List<MoneyIncome> MoneyIncomeList(MoneyIncomeParam incomeParam) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = "";
        if ("".equals(incomeParam.getTime()) || null == incomeParam.getTime()) {
            year = sdf.format(new Date());
        } else {
            year = incomeParam.getTime();
        }
        QueryWrapper<MoneyIncome> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", year).groupBy("company_type");
        queryWrapper.select(" IFNULL(sum(year_target),'0') yearTarget,IFNULL(sum(year_actual),'0') yearActual,company_name companyName," +
                "company_type companyType");
        List<MoneyIncome> moneyIncomes = incomeMapper.selectList(queryWrapper);
        return moneyIncomes;
    }

}
