package vip.xiaonuo.money.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.money.entity.MoneyBusiness;
import vip.xiaonuo.money.entity.MoneyIncome;
import vip.xiaonuo.money.entity.vo.MoneyIncomeVo;
import vip.xiaonuo.money.mapper.MoneyBusinessMapper;
import vip.xiaonuo.money.mapper.MoneyIncomeMapper;
import vip.xiaonuo.money.param.MoneyIncomeAddParam;
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.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
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;
    @Autowired
    private MoneyBusinessMapper businessMapper;


    @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();
        }
        MoneyIncomeVo incomeVo = new MoneyIncomeVo();

        QueryWrapper<MoneyIncome> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", year);
        queryWrapper.select(" IFNULL(sum(year_target),'0') yearTarget");
        MoneyIncome income = incomeMapper.selectOne(queryWrapper);//
        BigDecimal math = new BigDecimal("10000");
        BigDecimal bg = new BigDecimal(income.getYearTarget());
        bg = bg.divide(math, 2, RoundingMode.HALF_UP);
        incomeVo.setYearTarget(bg.toString());

        QueryWrapper<MoneyBusiness> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.between("create_time", year + "-01-01 00:00:00", year + "-12-31 23:59:59");
        queryWrapper1.select(" IFNULL(sum(business_income),'0') businessIncome,org_id orgId").groupBy("org_id");
        List<MoneyBusiness> businesses = businessMapper.selectList(queryWrapper1);//
        double actual = 0.00;

        if (businesses.size() == 0) {
            incomeVo.setYearActual("0");
            incomeVo.setXiaSha("0");
            incomeVo.setYingYang("0");
            incomeVo.setShengWu("0");
        } else {
            for (MoneyBusiness business : businesses) {
                if (business.getOrgId() != null) {
                    if (business.getOrgId().equals("1543842934270394368")) {
                        BigDecimal bg1 = new BigDecimal(business.getBusinessIncome());
                        bg1 = bg1.divide(math, 2, RoundingMode.HALF_UP);
                        incomeVo.setShengWu(bg1.toString());
                        actual = actual + Double.valueOf(business.getBusinessIncome());
                    } else if (business.getOrgId().equals("1856160630562635777")) {
                        BigDecimal bg2 = new BigDecimal(business.getBusinessIncome());
                        bg2 = bg2.divide(math, 2, RoundingMode.HALF_UP);
                        incomeVo.setYingYang(bg2.toString());
                        actual = actual + Double.valueOf(business.getBusinessIncome());
                    } else if (business.getOrgId().equals("1856160687106048001")) {
                        BigDecimal bg3 = new BigDecimal(business.getBusinessIncome());
                        bg3 = bg3.divide(math, 2, RoundingMode.HALF_UP);
                        incomeVo.setXiaSha(bg3.toString());
                        actual = actual + Double.valueOf(business.getBusinessIncome());
                    }
                }
            }
            BigDecimal bg4 = new BigDecimal(String.valueOf(actual));
            bg4 = bg4.divide(math, 2, RoundingMode.HALF_UP);
            incomeVo.setYearActual(bg4.toString());
        }

        DecimalFormat def = new DecimalFormat("0.00");
        if (Double.valueOf(income.getYearTarget()) == 0) {
            incomeVo.setRank("100");
        }else{
            double v = actual / Double.valueOf(incomeVo.getYearTarget());
            if (v > 100) {
                incomeVo.setRank("100");
            } else {
                incomeVo.setRank(def.format(v));
            }
        }

        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();
        }
        List<MoneyIncome> moneyIncomes = new ArrayList<>();
        BigDecimal math = new BigDecimal("10000");

        MoneyIncome income = new MoneyIncome();
        MoneyIncome income1 = new MoneyIncome();
        MoneyIncome income2 = new MoneyIncome();


        QueryWrapper<MoneyIncome> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", year);
        queryWrapper.select(" IFNULL(sum(year_target),'0') yearTarget,org_id,company_name companyName").groupBy("org_id");
        List<MoneyIncome> incomes = incomeMapper.selectList(queryWrapper);//
        if (incomes.size() == 0) {
            income.setYearTarget("0");
            income1.setYearTarget("0");
            income2.setYearTarget("0");
        } else {
            for (MoneyIncome moneyIncome : incomes) {
                if (moneyIncome.getOrgId().equals("1543842934270394368")) {
                    BigDecimal bg1 = new BigDecimal(moneyIncome.getYearTarget());
                    bg1 = bg1.divide(math, 2, RoundingMode.HALF_UP);
                    income.setYearTarget(bg1.toString());
                    income.setCompanyName(moneyIncome.getCompanyName());
                    income.setCompanyType(1);
                } else if (moneyIncome.getOrgId().equals("1856160630562635777")) {
                    BigDecimal bg2 = new BigDecimal(moneyIncome.getYearTarget());
                    bg2 = bg2.divide(math, 2, RoundingMode.HALF_UP);
                    income1.setYearTarget(bg2.toString());
                    income1.setCompanyName(moneyIncome.getCompanyName());
                    income1.setCompanyType(2);
                } else if (moneyIncome.getOrgId().equals("1856160687106048001")) {
                    BigDecimal bg3 = new BigDecimal(moneyIncome.getYearTarget());
                    bg3 = bg3.divide(math, 2, RoundingMode.HALF_UP);
                    income2.setYearTarget(bg3.toString());
                    income2.setCompanyName(moneyIncome.getCompanyName());
                    income2.setCompanyType(3);
                }
            }
        }
        QueryWrapper<MoneyBusiness> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.between("create_time", year + "-01-01 00:00:00", year + "-12-31 23:59:59");
        queryWrapper1.select(" IFNULL(sum(business_income),'0') businessIncome,org_id orgId").groupBy("org_id");
        List<MoneyBusiness> businesses = businessMapper.selectList(queryWrapper1);//
        if (businesses.size() == 0) {
            income.setYearActual("0");
            income1.setYearActual("0");
            income2.setYearActual("0");
        } else {
            for (MoneyBusiness business : businesses) {
                if (business.getOrgId() != null) {
                    if (business.getOrgId().equals("1543842934270394368")) {
                        BigDecimal bg1 = new BigDecimal(business.getBusinessIncome());
                        bg1 = bg1.divide(math, 2, RoundingMode.HALF_UP);
                        income.setYearActual(bg1.toString());
                    } else if (business.getOrgId().equals("1856160630562635777")) {
                        BigDecimal bg2 = new BigDecimal(business.getBusinessIncome());
                        bg2 = bg2.divide(math, 2, RoundingMode.HALF_UP);
                        income1.setYearActual(bg2.toString());
                    } else if (business.getOrgId().equals("1856160687106048001")) {
                        BigDecimal bg3 = new BigDecimal(business.getBusinessIncome());
                        bg3 = bg3.divide(math, 2, RoundingMode.HALF_UP);
                        income2.setYearActual(bg3.toString());
                    }
                }
            }
        }
        income.setCompanyName("花园生物");
        income1.setCompanyName("花园营养");
        income2.setCompanyName("花园下沙");

        moneyIncomes.add(income);
        moneyIncomes.add(income1);
        moneyIncomes.add(income2);

        return moneyIncomes;
    }

    @Override
    public CommonResult add(MoneyIncomeAddParam addParam) {
        String orgId = addParam.getOrgId();
        String time = addParam.getTime();
        QueryWrapper<MoneyIncome> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", time).eq("org_id", orgId);
        MoneyIncome moneyIncome = incomeMapper.selectOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(moneyIncome)) {
            moneyIncome.setCompanyName(addParam.getCompany());
            moneyIncome.setYear(addParam.getTime());
            moneyIncome.setYearTarget(addParam.getYearTarget());
            incomeMapper.updateById(moneyIncome);
        } else {
            MoneyIncome income = new MoneyIncome();
            income.setCompanyName(addParam.getCompany());
            income.setYear(addParam.getTime());
            income.setYearTarget(addParam.getYearTarget());
            income.setOrgId(orgId);
            incomeMapper.insert(income);
        }
        return CommonResult.ok();
    }

}
