package vip.xiaonuo.sale.service.impl;

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.sale.entity.SaleCost;
import vip.xiaonuo.sale.mapper.SaleCostMapper;
import vip.xiaonuo.sale.param.SaleCostParam;
import vip.xiaonuo.sale.service.ISaleCostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * <p>
 * 销售费用 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-13
 */
@Service
public class SaleCostServiceImpl extends ServiceImpl<SaleCostMapper, SaleCost> implements ISaleCostService {

    @Autowired
    private SaleCostMapper costMapper;

    @Override
    public List<SaleCost> getList(SaleCostParam costParam) {
        String orgId = costParam.getOrgId();
        String saleCostName = costParam.getSaleCostName();
        QueryWrapper<SaleCost> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.select(" IFNULL(sum(sale_cost_money),'0') saleCostMoney,sale_cost_name saleCostName");
        if (StringUtils.isNotBlank(saleCostName)) {
            queryWrapper.eq("sale_cost_name", saleCostName);
        } else {
            queryWrapper.groupBy("sale_cost_name");
        }
        List<SaleCost> saleCosts = costMapper.selectList(queryWrapper);
        BigDecimal math = new BigDecimal("10000");
        for (SaleCost saleCost : saleCosts) {
            BigDecimal bg = new BigDecimal(saleCost.getSaleCostMoney());
            bg = bg.divide(math, 2, RoundingMode.HALF_UP);
            saleCost.setSaleCostMoney(bg.toString());
        }
        return saleCosts;
    }
}
