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.SaleCustomer;
import vip.xiaonuo.sale.mapper.SaleCustomerMapper;
import vip.xiaonuo.sale.param.SaleCustomerParam;
import vip.xiaonuo.sale.service.ISaleCustomerService;
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 SaleCustomerServiceImpl extends ServiceImpl<SaleCustomerMapper, SaleCustomer> implements ISaleCustomerService {

    @Autowired
    private SaleCustomerMapper customerMapper;

    @Override
    public List<SaleCustomer> getList(SaleCustomerParam customerParam) {
        String orgId = customerParam.getOrgId();
        QueryWrapper<SaleCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.select(" customer customer,IFNULL(sum(sale_money),'0') saleMoney,create_time createTime");
        queryWrapper.groupBy("customer,DATE_FORMAT(create_time,'%Y-%m')");
        List<SaleCustomer> saleCustomers = customerMapper.selectList(queryWrapper);
        BigDecimal math = new BigDecimal("10000");
        for (SaleCustomer saleCustomer : saleCustomers) {
            BigDecimal bg = new BigDecimal(saleCustomer.getSaleMoney());
            bg = bg.divide(math, 2, RoundingMode.HALF_UP);
            saleCustomer.setSaleMoney(bg.toString());
        }
        return saleCustomers;
    }

}
