package vip.xiaonuo.money.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.money.entity.MoneyProduce;
import vip.xiaonuo.money.entity.MoneyProduceIncome;
import vip.xiaonuo.money.mapper.MoneyProduceIncomeMapper;
import vip.xiaonuo.money.mapper.MoneyProduceMapper;
import vip.xiaonuo.money.param.MoneyAnalysisParam;
import vip.xiaonuo.money.param.MoneyInOutParam;
import vip.xiaonuo.money.param.MoneyProduceIncomeParam;
import vip.xiaonuo.money.param.MoneyProduceParam;
import vip.xiaonuo.money.service.IMoneyProduceService;
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.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 财务产品销售收入 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-19
 */
@Service
public class MoneyProduceServiceImpl extends ServiceImpl<MoneyProduceMapper, MoneyProduce> implements IMoneyProduceService {

    @Autowired
    private MoneyProduceMapper produceMapper;
    @Autowired
    private MoneyProduceIncomeMapper incomeMapper;

    @Override
    public MoneyProduce analysisIncome(MoneyAnalysisParam analysisParam) {
        String orgId = analysisParam.getOrgId();
        Integer type = analysisParam.getType();//是否包含关联交易
        String time = analysisParam.getTime();//2024-10
        QueryWrapper<MoneyProduce> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId).eq("transaction_type", type);
        if ("".equals(time) || null == time) {
            DateTime dateTime = DateUtil.beginOfYear(new Date());
            queryWrapper.ge("create_time", dateTime);
        } else {
            String year = time.substring(0, 4);
            queryWrapper.between("create_time", year + "-01-01 00:00:00", time + "-31 23:59:59");
        }
        queryWrapper.select(" IFNULL(sum(nf_cholesterol),'0') nfCholesterol," +
                "IFNULL(sum(breed_cholesterol),'0') breedCholesterol," +
                "IFNULL(sum(sheep_alcohol),'0') sheepAlcohol," +
                "IFNULL(sum(sheep_sour),'0') sheepSour," +
                "IFNULL(sum(sheep_sour_bing),'0') sheepSourBing," +
                "IFNULL(sum(other),'0') other");
        MoneyProduce produce1 = produceMapper.selectOne(queryWrapper);

        MoneyProduce produce = new MoneyProduce();
        BigDecimal math = new BigDecimal("10000");
        BigDecimal bd = new BigDecimal(produce1.getNfCholesterol());
        bd = bd.divide(math, 2, RoundingMode.HALF_UP);
        BigDecimal bd1 = new BigDecimal(produce1.getBreedCholesterol());
        bd1 = bd1.divide(math, 2, RoundingMode.HALF_UP);
        BigDecimal bd2 = new BigDecimal(produce1.getSheepAlcohol());
        bd2 = bd2.divide(math, 2, RoundingMode.HALF_UP);
        BigDecimal bd3 = new BigDecimal(produce1.getSheepSour());
        bd3 = bd3.divide(math, 2, RoundingMode.HALF_UP);
        BigDecimal bd4 = new BigDecimal(produce1.getSheepSourBing());
        bd4 = bd4.divide(math, 2, RoundingMode.HALF_UP);
        BigDecimal bd5 = new BigDecimal(produce1.getOther());
        bd5 = bd5.divide(math, 2, RoundingMode.HALF_UP);


        produce.setNfCholesterol(bd.toString());
        produce.setBreedCholesterol(bd1.toString());
        produce.setSheepAlcohol(bd2.toString());
        produce.setSheepSour(bd3.toString());
        produce.setSheepSourBing(bd4.toString());
        produce.setOther(bd5.toString());

        return produce;
    }

    @Override
    public JSONArray produceAnalysis(MoneyProduceParam moneyProduceParam) {
        String orgId = moneyProduceParam.getOrgId();
        Integer type = moneyProduceParam.getType();//是否包含关联交易
        Integer timeType = moneyProduceParam.getTimeType();
        QueryWrapper<MoneyProduce> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId)
                .eq("transaction_type", type);

        queryWrapper.select(" IFNULL(sum(nf_cholesterol),0) nfCholesterol,IFNULL(sum(breed_cholesterol),0) breedCholesterol," +
                "IFNULL(sum(sheep_alcohol),0) sheepAlcohol,IFNULL(sum(sheep_sour),0) sheepSour," +
                "IFNULL(sum(sheep_sour_bing),0) sheepSourBing,IFNULL(sum(other),0) other," +
                "DATE_FORMAT(create_time,'%Y-%m-%d') createTime");
        if (0 == timeType) {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')").orderByAsc("create_time");
        } else {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')").orderByAsc("create_time");
        }
        List<MoneyProduce> list = produceMapper.selectList(queryWrapper);
        JSONArray jsonArray = new JSONArray();
        BigDecimal math = new BigDecimal("10000");
        for (MoneyProduce produce : list) {
            JSONObject jsonObject = new JSONObject();
            switch (moneyProduceParam.getPType()) {
                case 0:
                    BigDecimal bg=new BigDecimal(produce.getNfCholesterol());
                    bg = bg.divide(math, 2, RoundingMode.HALF_UP);
                    jsonObject.put("value", bg.toString());
                    jsonObject.put("createTime", produce.getCreateTime());
                    break;
                case 1:
                    BigDecimal bg1=new BigDecimal(produce.getBreedCholesterol());
                    bg1 = bg1.divide(math, 2, RoundingMode.HALF_UP);
                    jsonObject.put("value", bg1.toString());
                    jsonObject.put("createTime", produce.getCreateTime());
                    break;
                case 2:
                    BigDecimal bg2=new BigDecimal(produce.getSheepAlcohol());
                    bg2 = bg2.divide(math, 2, RoundingMode.HALF_UP);
                    jsonObject.put("value", bg2.toString());
                    jsonObject.put("createTime", produce.getCreateTime());
                    break;
                case 3:
                    BigDecimal bg3=new BigDecimal(produce.getSheepSour());
                    bg3 = bg3.divide(math, 2, RoundingMode.HALF_UP);
                    jsonObject.put("value", bg3.toString());
                    jsonObject.put("createTime", produce.getCreateTime());
                    break;
                case 4:
                    BigDecimal bg4=new BigDecimal(produce.getSheepSourBing());
                    bg4 = bg4.divide(math, 2, RoundingMode.HALF_UP);
                    jsonObject.put("value",bg4.toString());
                    jsonObject.put("createTime", produce.getCreateTime());
                    break;
                case 5:
                    BigDecimal bg5=new BigDecimal(produce.getOther());
                    bg5 = bg5.divide(math, 2, RoundingMode.HALF_UP);
                    jsonObject.put("value", bg5.toString());
                    jsonObject.put("createTime", produce.getCreateTime());
                    break;
            }
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    @Override
    public JSONObject getInOut(MoneyInOutParam inOutParam) {
        String orgId = inOutParam.getOrgId();
        QueryWrapper<MoneyProduce> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.select(" IFNULL(sum(in_money),'0') inMoney,IFNULL(sum(out_money),'0') outMoney");
        MoneyProduce moneyProduce = produceMapper.selectOne(queryWrapper);

        JSONObject jsonObject = new JSONObject();
        BigDecimal math = new BigDecimal("10000");
        BigDecimal bg=new BigDecimal(moneyProduce.getInMoney());
        bg = bg.divide(math, 2, RoundingMode.HALF_UP);
        BigDecimal bg1=new BigDecimal(moneyProduce.getOutMoney());
        bg1 = bg1.divide(math, 2, RoundingMode.HALF_UP);
        jsonObject.put("in", bg.toString());
        jsonObject.put("out", bg1.toString());
        return jsonObject;
    }

    @Override
    public JSONArray produceIncomeAnalysis(MoneyProduceIncomeParam moneyProduceParam) {
        String orgId = moneyProduceParam.getOrgId();
        Integer timeType = moneyProduceParam.getTimeType();
        QueryWrapper<MoneyProduceIncome> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);

        queryWrapper.select(" IFNULL(sum(nf_cholesterol),0) nfCholesterol,IFNULL(sum(breed_cholesterol),0) breedCholesterol," +
                "IFNULL(sum(sheep_alcohol),0) sheepAlcohol,IFNULL(sum(sheep_sour),0) sheepSour," +
                "IFNULL(sum(sheep_sour_bing),0) sheepSourBing,IFNULL(sum(other),0) other," +
                "DATE_FORMAT(create_time,'%Y-%m-%d') createTime");
        if (0 == timeType) {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')").orderByAsc("create_time");
        } else {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')").orderByAsc("create_time");
        }
        List<MoneyProduceIncome> list = incomeMapper.selectList(queryWrapper);
        JSONArray jsonArray = new JSONArray();
        BigDecimal math = new BigDecimal("10000");
        for (MoneyProduceIncome produce : list) {
            JSONObject jsonObject = new JSONObject();
            switch (moneyProduceParam.getPType()) {
                case 0:
                    BigDecimal bd = new BigDecimal(produce.getNfCholesterol());
                    bd = bd.divide(math, 2, RoundingMode.HALF_UP);
                    jsonObject.put("value", bd.toString());
                    jsonObject.put("createTime", produce.getCreateTime());
                    break;
                case 1:
                    BigDecimal bd1 = new BigDecimal(produce.getBreedCholesterol());
                    bd1 = bd1.divide(math, 2, RoundingMode.HALF_UP);
                    jsonObject.put("value", bd1.toString());
                    jsonObject.put("createTime", produce.getCreateTime());
                    break;
                case 2:
                    BigDecimal bd2 = new BigDecimal(produce.getSheepAlcohol());
                    bd2 = bd2.divide(math, 2, RoundingMode.HALF_UP);
                    jsonObject.put("value", bd2.toString());
                    jsonObject.put("createTime", produce.getCreateTime());
                    break;
                case 3:
                    BigDecimal bd3 = new BigDecimal(produce.getSheepSour());
                    bd3 = bd3.divide(math, 2, RoundingMode.HALF_UP);
                    jsonObject.put("value",bd3.toString());
                    jsonObject.put("createTime", produce.getCreateTime());
                    break;
                case 4:
                    BigDecimal bd4 = new BigDecimal(produce.getSheepSourBing());
                    bd4 = bd4.divide(math, 2, RoundingMode.HALF_UP);
                    jsonObject.put("value", bd4.toString());
                    jsonObject.put("createTime", produce.getCreateTime());
                    break;
                case 5:
                    BigDecimal bd5 = new BigDecimal(produce.getOther());
                    bd5 = bd5.divide(math, 2, RoundingMode.HALF_UP);
                    jsonObject.put("value",bd5.toString());
                    jsonObject.put("createTime", produce.getCreateTime());
                    break;
            }
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }
}
