package vip.xiaonuo.money.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
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.MoneyProducePrice;
import vip.xiaonuo.money.entity.vo.MoneyBorrowPriceVo;
import vip.xiaonuo.money.entity.vo.MoneyProduceVo;
import vip.xiaonuo.money.mapper.MoneyProducePriceMapper;
import vip.xiaonuo.money.param.MoneyInOutParam;
import vip.xiaonuo.money.service.IMoneyProducePriceService;
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.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 财务产品成本收入 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@Service
public class MoneyProducePriceServiceImpl extends ServiceImpl<MoneyProducePriceMapper, MoneyProducePrice> implements IMoneyProducePriceService {

    @Autowired
    private MoneyProducePriceMapper producePriceMapper;

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


    @Override
    public List<MoneyProducePrice> listProduceProfit(MoneyInOutParam outParam) {
        QueryWrapper<MoneyProducePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(outParam.getOrgId()), "org_id", outParam.getOrgId());
        queryWrapper.select(" produce_name produceName,profit profit,create_time createTime");
        queryWrapper.groupBy("produce_name,DATE_FORMAT(create_time,'%Y-%m')");
        queryWrapper.orderByAsc("create_time");
        return producePriceMapper.selectList(queryWrapper);
    }

    @Override
    public List<MoneyProduceVo> listProduce(MoneyInOutParam outParam) {
        String orgId = outParam.getOrgId();
        DateTime year = DateUtil.beginOfYear(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String format = sdf.format(year);
        List<MoneyProduceVo> list = new ArrayList<>();
        BigDecimal math = new BigDecimal("10000");
        for (int i = 1; i < 13; i++) {
            MoneyProduceVo vo = new MoneyProduceVo();
            QueryWrapper<SaleCost> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
            queryWrapper.select(" IFNULL(sum(sale_cost_money),'0') saleCostMoney");
            QueryWrapper<SaleDetail> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
            queryWrapper1.select(" IFNULL(sum(sale_money),'0') saleMoney");
            if (i < 10) {
                vo.setTime(format+"-0"+i);
                queryWrapper.between("create_time", format + "-0" + i + "-01 00:00:00", format + "-0" + i + "-31 23:59:59");
                queryWrapper1.between("create_time", format + "-0" + i + "-01 00:00:00", format + "-0" + i + "-31 23:59:59");
            } else {
                vo.setTime(format+"-"+i);
                queryWrapper.between("create_time", format + "-" + i + "-01 00:00:00", format + "-" + i + "-31 23:59:59");
                queryWrapper1.between("create_time", format + "-" + i + "-01 00:00:00", format + "-" + i + "-31 23:59:59");
            }
            SaleCost saleCost = costMapper.selectOne(queryWrapper);//销售成本
            SaleDetail saleDetail = detailMapper.selectOne(queryWrapper1);//销售额
            if (ObjectUtil.isEmpty(saleCost)) {
                vo.setCost("0");
            } else {
                BigDecimal bg = new BigDecimal(saleCost.getSaleCostMoney());
                bg = bg.divide(math, 2, RoundingMode.HALF_UP);
                vo.setCost(bg.toString());
            }
            if (ObjectUtil.isEmpty(saleDetail)) {
                vo.setCost("0");
            } else {
                BigDecimal bg = new BigDecimal(saleDetail.getSaleMoney());
                bg = bg.divide(math, 2, RoundingMode.HALF_UP);
                vo.setCost(bg.toString());
            }
            list.add(vo);
        }

        return list;
    }

}
