package com.kxs.store.biz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.core.util.TotalDateUtil;
import com.kxs.store.api.model.KxsSendTotal;
import com.kxs.store.biz.mapper.KxsSendTotalMapper;
import com.kxs.store.biz.service.KxsSendTotalService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * 客小爽仓库发货统计表(KxsSendTotal)表服务实现类
 *
 * @author 系统
 * @since 2024-01-08 09:46:02
 */
@Service("kxsSendTotalService")
public class KxsSendTotalServiceImpl extends ServiceImpl<KxsSendTotalMapper, KxsSendTotal> implements KxsSendTotalService {

    @Override
    public void shipmentStatistics(Integer count, Integer warehouseId, Integer brandId) {

        //日统计
        Integer dateNumber = TotalDateUtil.getDateNumber(LocalDate.now());
        //月统计
        Integer monthNumber = TotalDateUtil.getMonthNumber(LocalDate.now());
        KxsSendTotal dateTotal = baseMapper.selectOne(Wrappers.lambdaQuery(KxsSendTotal.class)
                .eq(KxsSendTotal::getTotalDate, dateNumber)
                .eq(KxsSendTotal::getTotalMonth, monthNumber)
                .eq(KxsSendTotal::getBrandId, brandId)
                .eq(KxsSendTotal::getWarehouseId, warehouseId));
        if (dateTotal == null) {
            dateTotal = new KxsSendTotal();
            dateTotal.setTotalDate(dateNumber);
            dateTotal.setTotalMonth(monthNumber);
            dateTotal.setBrandId(brandId);
            dateTotal.setWarehouseId(warehouseId);
            dateTotal.setTotalNum(count);
            baseMapper.insert(dateTotal);
        } else {
            dateTotal.setTotalNum(dateTotal.getTotalNum() + count);
            baseMapper.updateById(dateTotal);
        }

    }
}

