package com.sifang.modules.supply.stock.service;

import com.jfinal.kit.StrKit;
import com.sifang.modules.supply.stock.dao.CrmSupplyAlarmDao;
import com.sifang.modules.supply.stock.dao.MaterialStockDao;
import com.sifang.modules.supply.stock.dao.MaterialStockWarningDao;
import com.sifang.modules.supply.stock.entity.MaterialStockWarning;
import com.sifang.modules.sys.common.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class MaterialStockWaringService extends BaseService {
    @Autowired
    private MaterialStockWarningDao stockWarningDao;
    @Autowired
    private MaterialStockDao materialStockDao;
    @Autowired
    private CrmSupplyAlarmDao alarmDao;
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 判断低库存预警
     */
    @Transactional(rollbackFor = Exception.class)
    public void checkLowStockWarning(String materialId, String ascriptionId) {
        MaterialStockWarning warning = stockWarningDao.findByMaterialIdAndAscriptionId(materialId, ascriptionId);
        if (StrKit.notNull(warning)) {
            BigDecimal realNum = materialStockDao.findWarningByAscriptionId(materialId, ascriptionId);
            if (StrKit.notNull(realNum) && realNum.compareTo(warning.getLowStockWarning()) <= 0) {
                alarmDao.createAlarm(materialId, warning.getType(), warning.getAscriptionId());
                logger.info("判断低库存预警 checkLowStockWarning warning:{}  ,realNum:{}", warning, realNum);

            }
        }
    }
}
