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.SaleOrder;
import vip.xiaonuo.sale.entity.SalePrice;
import vip.xiaonuo.sale.mapper.SaleOrderMapper;
import vip.xiaonuo.sale.param.SaleOrderParam;
import vip.xiaonuo.sale.service.ISaleOrderService;
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 SaleOrderServiceImpl extends ServiceImpl<SaleOrderMapper, SaleOrder> implements ISaleOrderService {

    @Autowired
    private SaleOrderMapper orderMapper;

    @Override
    public List<SaleOrder> getList(SaleOrderParam saleOrderParam) {
        String orgId = saleOrderParam.getOrgId();
        Integer timeType = saleOrderParam.getTimeType();
        QueryWrapper<SaleOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.select(" CONVERT(IFNULL(sum(sale_number),'0'),decimal(10,2)) saleNumber,create_time createTime,IFNULL(sum(sale_money),'0') saleMoney");
        if (0 == timeType) {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
        } else {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
        }
        queryWrapper.orderByAsc("create_time");
        List<SaleOrder> saleOrders = orderMapper.selectList(queryWrapper);
        BigDecimal math = new BigDecimal("10000");
        for (SaleOrder saleOrder : saleOrders) {
            BigDecimal bg = new BigDecimal(saleOrder.getSaleMoney());
            saleOrder.setSaleMoney(bg.divide(math, 2, RoundingMode.HALF_UP).toString());
        }
        return saleOrders;
    }

}
