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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.money.entity.MoneyBusiness;
import vip.xiaonuo.money.mapper.MoneyBusinessMapper;
import vip.xiaonuo.money.param.MoneyBusinessPageParam;
import vip.xiaonuo.money.param.MoneyBusinessParam;
import vip.xiaonuo.money.service.IMoneyBusinessService;
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 MoneyBusinessServiceImpl extends ServiceImpl<MoneyBusinessMapper, MoneyBusiness> implements IMoneyBusinessService {

    @Autowired
    private MoneyBusinessMapper businessMapper;

    @Override
    public JSONArray listBusiness(MoneyBusinessParam moneyBusinessParam) {
        String orgId = moneyBusinessParam.getOrgId();
        String time = moneyBusinessParam.getTime();//2024-10
        QueryWrapper<MoneyBusiness> queryWrapper = new QueryWrapper<>();
        QueryWrapper<MoneyBusiness> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId);
        queryWrapper1.eq(StringUtils.isNotBlank(orgId),"org_id", orgId);

        JSONArray jsonArray = new JSONArray();
        if ("".equals(time) || null == time) {
            DateTime dateTime = DateUtil.beginOfMonth(new Date());
            queryWrapper.ge("create_time", dateTime);
            DateTime beginOfYear = DateUtil.beginOfYear(new Date());
            queryWrapper1.ge("create_time", beginOfYear);
        } else {
            String year = time.substring(0, 4);
            queryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
            queryWrapper1.between("create_time", year + "-01-01 00:00:00", time + "-31 23:59:59");
        }
        MoneyBusiness moneyBusiness = new MoneyBusiness();
        queryWrapper.select(" IFNULL(sum(business_income),'0') business_income," +
                "IFNULL(sum(business_cost),'0') businessCost," +
                "IFNULL(sum(profit),'0') profit," +
                "IFNULL(sum(taxes),'0') taxes," +
                "IFNULL(sum(balance),'0') balance");
        queryWrapper1.select(" IFNULL(sum(business_income),'0') business_income," +
                "IFNULL(sum(business_cost),'0') businessCost," +
                "IFNULL(sum(profit),'0') profit," +
                "IFNULL(sum(taxes),'0') taxes," +
                "IFNULL(sum(balance),'0') balance");
        MoneyBusiness selectOne = businessMapper.selectOne(queryWrapper);
        MoneyBusiness selectOne1 = businessMapper.selectOne(queryWrapper1);


        JSONObject jsonObject = new JSONObject();
        BigDecimal math = new BigDecimal("10000");

        BigDecimal bd = new BigDecimal(selectOne.getBusinessIncome());
        BigDecimal bd1 = new BigDecimal(selectOne.getBusinessCost());
        BigDecimal bd2 = new BigDecimal(selectOne.getProfit());
        BigDecimal bd3 = new BigDecimal(selectOne.getTaxes());
        BigDecimal bd4 = new BigDecimal(selectOne.getBalance());
        bd = bd.divide(math, 2, RoundingMode.HALF_UP);
        bd1 = bd1.divide(math, 2, RoundingMode.HALF_UP);
        bd2 = bd2.divide(math, 2, RoundingMode.HALF_UP);
        bd3 = bd3.divide(math, 2, RoundingMode.HALF_UP);
        bd4 = bd4.divide(math, 2, RoundingMode.HALF_UP);


        moneyBusiness.setBusinessIncome(bd.toString());
        moneyBusiness.setBusinessCost(bd1.toString());
        moneyBusiness.setProfit(bd2.toString());
        moneyBusiness.setTaxes(bd3.toString());
        moneyBusiness.setBalance(bd4.toString());
        jsonObject.put("type", "month");
        jsonObject.put("object", moneyBusiness);

        JSONObject jsonObject1 = new JSONObject();
        MoneyBusiness moneyBusiness1 = new MoneyBusiness();
        BigDecimal bd5 = new BigDecimal(selectOne1.getBusinessIncome());
        BigDecimal bd6 = new BigDecimal(selectOne1.getBusinessCost());
        BigDecimal bd7 = new BigDecimal(selectOne1.getProfit());
        BigDecimal bd8 = new BigDecimal(selectOne1.getTaxes());
        BigDecimal bd9 = new BigDecimal(selectOne1.getBalance());
        BigDecimal divide = bd5.divide(math);
        divide = divide.setScale(2, RoundingMode.HALF_UP);
//        bd5 = bd5.divide(math, 2, RoundingMode.HALF_UP);
        bd6 = bd6.divide(math, 2, RoundingMode.HALF_UP);
        bd7 = bd7.divide(math, 2, RoundingMode.HALF_UP);
        bd8 = bd8.divide(math, 2, RoundingMode.HALF_UP);
        bd9 = bd9.divide(math, 2, RoundingMode.HALF_UP);

        moneyBusiness1.setBusinessIncome(divide.toString());
        moneyBusiness1.setBusinessCost(bd6.toString());
        moneyBusiness1.setProfit(bd7.toString());
        moneyBusiness1.setTaxes(bd8.toString());
        moneyBusiness1.setBalance(bd9.toString());
        jsonObject1.put("type", "year");
        jsonObject1.put("object", moneyBusiness1);
        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject1);
        return jsonArray;
    }

    @Override
    public Page<MoneyBusiness> getPage(MoneyBusinessPageParam pageParam) {
        String orgId = pageParam.getOrgId();
        Page<MoneyBusiness> page = new Page(pageParam.getPageNum(), pageParam.getPageSize());
        QueryWrapper<MoneyBusiness> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        return businessMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void addBusiness(MoneyBusiness business) {
        business.setCreateTime(new Date());
        businessMapper.insert(business);
    }

    @Override
    public void updateBusiness(MoneyBusiness business) {
        businessMapper.updateById(business);
    }

    @Override
    public void deleteBusiness(MoneyBusiness business) {
        businessMapper.deleteById(business);
    }
}
