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.SaleDeliver;
import vip.xiaonuo.sale.entity.SaleOrder;
import vip.xiaonuo.sale.mapper.SaleDeliverMapper;
import vip.xiaonuo.sale.param.SaleDeliverParam;
import vip.xiaonuo.sale.service.ISaleDeliverService;
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 SaleDeliverServiceImpl extends ServiceImpl<SaleDeliverMapper, SaleDeliver> implements ISaleDeliverService {

    @Autowired
    private SaleDeliverMapper deliverMapper;

    @Override
    public List<SaleDeliver> getList(SaleDeliverParam deliverParam) {
        String orgId = deliverParam.getOrgId();
        Integer timeType = deliverParam.getTimeType();
        QueryWrapper<SaleDeliver> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.select(" CONVERT(IFNULL(sum(sale_deliver_number),'0'),decimal(10,2)) saleDeliverNumber,create_time createTime,IFNULL(sum(sale_deliver_money),'0') saleDeliverMoney");
        if (0 == timeType) {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
        } else {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
        }
        queryWrapper.orderByAsc("create_time");
        List<SaleDeliver> saleDelivers = deliverMapper.selectList(queryWrapper);
        BigDecimal math = new BigDecimal("10000");
        for (SaleDeliver saleDeliver : saleDelivers) {
            BigDecimal bg = new BigDecimal(saleDeliver.getSaleDeliverMoney());
            bg = bg.divide(math, 2, RoundingMode.HALF_UP);
            saleDeliver.setSaleDeliverMoney(bg.toString());
        }
        return saleDelivers;
    }
}
