package com.hzs.report.scm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.achieve.CuMemberAchieve;
import com.hzs.report.report.mapper.ReportCuMemberAchieveMapper;
import com.hzs.report.scm.InventorySaleParam;
import com.hzs.report.scm.mapper.InventoryMapper;
import com.hzs.report.scm.service.IInventoryService;
import com.hzs.report.scm.vo.InventorySaleVO;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * @description: 库存服务类
 * @author: sui q
 * @time: 2024/8/2 11:06
 * @classname: InventoryServiceImpl
 * @package_name: com.hzs.report.scm.service.impl
 * version 1.0.0
 */
@Service
public class InventoryServiceImpl  extends ServiceImpl<InventoryMapper, InventorySaleVO> implements IInventoryService {

    @Override
    public List<InventorySaleVO> queryInventorySaleListByParam(InventorySaleParam inventorySaleParam) {
        inventorySaleParam.setEndDate(DateUtils.afterDate(1, ChronoUnit.DAYS, inventorySaleParam.getEndDate()));
        Period period = DateUtils.betweenPeriod(inventorySaleParam.getEndDate(), inventorySaleParam.getStartDate());
        int month = period.getYears() *12 + period.getMonths() +1;
        inventorySaleParam.setMonthNum(month);
        List<InventorySaleVO> inventorySaleVoList = baseMapper.queryInventorySaleListByParam(inventorySaleParam);
        BigDecimal totalNum = BigDecimal.ZERO;
        for (InventorySaleVO inventorySaleVO : inventorySaleVoList) {
            if(inventorySaleVO.getSaleNum()!=null){
                totalNum = ComputeUtil.computeAdd(inventorySaleVO.getSaleNum(), totalNum);
            }
            if(inventorySaleVO.getAverageNum()!=null){
                inventorySaleVO.setSaleTime(ComputeUtil.computeDivide(inventorySaleVO.getNonHandNum(),inventorySaleVO.getAverageNum()));
            }

            if(inventorySaleVO.getSaleTime()!=null&&ComputeUtil.compareValue(BigDecimal.valueOf(3), inventorySaleVO.getSaleTime())){
                inventorySaleVO.setIsDue(EYesNo.YES.getIntValue());
            }else{
                inventorySaleVO.setIsDue(EYesNo.NO.getIntValue());
            }
        }
        if(ComputeUtil.compareValue(totalNum)) {
            for (InventorySaleVO inventorySaleVO : inventorySaleVoList) {
                if(inventorySaleVO.getSaleNum()!=null){
                    inventorySaleVO.setSaleRatio(ComputeUtil.computeDivide(inventorySaleVO.getSaleNum(), totalNum));
                }
            }
        }
        return inventorySaleVoList;
    }
}
