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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.core.util.TotalDateUtil;
import com.kxs.store.api.dto.kxsapp.storeOrder.OrderMonthTotalDTO;
import com.kxs.store.api.dto.kxsapp.storeOrder.OrderMonthTotalDetailDTO;
import com.kxs.store.api.model.KxsSendTotal;
import com.kxs.store.api.vo.kxsapp.storeOrder.OrderMonthTotalDetailVO;
import com.kxs.store.api.vo.kxsapp.storeOrder.OrderMonthTotalVO;
import com.kxs.store.biz.mapper.KxsMachineApplyDetailMapper;
import com.kxs.store.biz.mapper.KxsSendTotalMapper;
import com.kxs.store.biz.service.KxsSendTotalService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

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

    private final KxsMachineApplyDetailMapper kxsMachineApplyDetailMapper;

    @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);
        }

    }

    @Override
    public IPage<OrderMonthTotalVO> orderMonthTotal(Page<OrderMonthTotalVO> page, OrderMonthTotalDTO param) {

        return baseMapper.orderMonthTotal(page, param);
    }

    @Override
    public IPage<OrderMonthTotalDetailVO> orderMonthTotalDetail(Page<OrderMonthTotalDetailVO> page, OrderMonthTotalDetailDTO param) {

        IPage<OrderMonthTotalDetailVO> detailList = baseMapper.orderMonthTotalDetail(page, param);

        for (OrderMonthTotalDetailVO record : detailList.getRecords()) {

            LocalDateTime parse = LocalDateTimeUtil.parse(record.getTime(), DatePattern.PURE_DATE_PATTERN);

            LocalDateTime startTime = LocalDateTimeUtil.beginOfDay(parse);
            LocalDateTime endTime = LocalDateTimeUtil.endOfDay(parse);

            //查询当天发出的机器
            List<String> snList = kxsMachineApplyDetailMapper.listDateQueries(param.getWarehouseId(), param.getBrandId(),
                    startTime, endTime);
            record.setSnList(snList);
        }

        return detailList;
    }
}

