package vip.xiaonuo.money.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.money.entity.MoneyForecast;
import vip.xiaonuo.money.mapper.MoneyForecastMapper;
import vip.xiaonuo.money.param.MoneyInOutParam;
import vip.xiaonuo.money.service.IMoneyForecastService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.sale.entity.SaleCost;
import vip.xiaonuo.sale.entity.SaleDetail;
import vip.xiaonuo.sale.mapper.SaleCostMapper;
import vip.xiaonuo.sale.mapper.SaleDetailMapper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 财务费用预测 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@Service
public class MoneyForecastServiceImpl extends ServiceImpl<MoneyForecastMapper, MoneyForecast> implements IMoneyForecastService {

    @Autowired
    private MoneyForecastMapper forecastMapper;
    @Autowired
    private SaleDetailMapper detailMapper;
    @Autowired
    private SaleCostMapper costMapper;

    @Override
    public List<MoneyForecast> listForecast(MoneyInOutParam outParam) {
        String orgId = outParam.getOrgId();
        DateTime time = DateUtil.beginOfMonth(new Date());
        DateTime dateTime2 = DateUtil.offsetMonth(time, -1);
        QueryWrapper<SaleCost> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.ge("create_time", time);
        queryWrapper.select(" IFNULL(sum(sale_cost_money),'0') saleCostMoney");
        SaleCost saleCost = costMapper.selectOne(queryWrapper);//本月销售成本
        queryWrapper.clear();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.between("create_time", dateTime2, time);
        queryWrapper.select(" IFNULL(sum(sale_cost_money),'0') saleCostMoney");
        SaleCost saleCost1 = costMapper.selectOne(queryWrapper);//上月销售成本

        QueryWrapper<SaleDetail> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper1.ge("create_time", time);
        queryWrapper1.select(" IFNULL(sum(sale_money),'0') saleMoney");
        SaleDetail saleDetail = detailMapper.selectOne(queryWrapper1);//本月销售额
        queryWrapper1.clear();
        queryWrapper1.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper1.between("create_time", dateTime2, time);
        queryWrapper1.select(" IFNULL(sum(sale_money),'0') saleMoney");
        SaleDetail saleDetail1 = detailMapper.selectOne(queryWrapper1);//上月销售额

        List<MoneyForecast> list = new ArrayList<>();
        BigDecimal math = new BigDecimal("10000");


        MoneyForecast moneyForecast = new MoneyForecast();
        moneyForecast.setType(0);
        BigDecimal bg = new BigDecimal(saleDetail.getSaleMoney());
        bg = bg.divide(math, 2, RoundingMode.HALF_UP);
        moneyForecast.setSaleNumber(bg.toString());
        BigDecimal bg1 =new BigDecimal(saleCost.getSaleCostMoney());
        bg1 = bg1.divide(math, 2, RoundingMode.HALF_UP);
        moneyForecast.setSaleCost(bg1.toString());
        double v = Double.valueOf(saleDetail.getSaleMoney()) - Double.valueOf(saleCost.getSaleCostMoney());
        BigDecimal fit = new BigDecimal(String.valueOf(v));
        moneyForecast.setSaleProfit(fit.divide(math, 2, RoundingMode.HALF_UP).toString());

        MoneyForecast moneyForecast1 = new MoneyForecast();
        moneyForecast1.setType(1);
        BigDecimal bg2 = new BigDecimal(saleDetail1.getSaleMoney());
        bg2 = bg2.divide(math, 2, RoundingMode.HALF_UP);
        moneyForecast1.setSaleNumber(bg2.toString());
        BigDecimal bg3 = new BigDecimal(saleCost1.getSaleCostMoney());
        bg3 = bg3.divide(math, 2, RoundingMode.HALF_UP);
        moneyForecast1.setSaleCost(bg3.toString());
        double v1 = Double.valueOf(saleDetail1.getSaleMoney()) - Double.valueOf(saleCost1.getSaleCostMoney());
        BigDecimal profit = new BigDecimal(String.valueOf(v1));
        moneyForecast.setSaleProfit(profit.divide(math, 2, RoundingMode.HALF_UP).toString());

        list.add(moneyForecast);
        list.add(moneyForecast1);

        return list;
    }
}
