ConfigWarningService.java 9.04 KB
package com.huaheng.pc.config.configWarning.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.sendMail.domain.SendMail;
import com.huaheng.pc.config.sendMail.service.MailService;
import com.huaheng.pc.config.sendMail.service.SendMailService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
import com.huaheng.pc.system.user.service.UserServiceImpl;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.config.configWarning.domain.ConfigWarning;
import com.huaheng.pc.config.configWarning.mapper.ConfigWarningMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Enzo Cotter on 2019/11/11.
 */

@Service
public class ConfigWarningService extends ServiceImpl<ConfigWarningMapper, ConfigWarning> {

    @Resource
    private MaterialService materialService;
    @Resource
    private SendMailService sendMailService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private MailService mailService;
    @Resource
    private LocationService locationService;

    /**
     * 保存预警配置
     * @param configWarning
     * @return
     */
    @Transactional(rollbackFor = SecurityException.class)
    public AjaxResult saveConfigWarning(ConfigWarning configWarning){
        /* 设置仓库编码*/
        configWarning.setWarehouseCode(ShiroUtils.getWarehouseCode());

        /* 设置物料信息*/
        if (configWarning.getMaterialCode() != null){
            Material material = materialService.findAllByCode(configWarning.getMaterialCode());
            configWarning.setMaterialName(material.getName());
            configWarning.setMaterialSpec(material.getSpec());
            configWarning.setMaterialUnit(material.getUnit());
        }

        /* 设置创建人、更新人*/
        configWarning.setCreatedBy(ShiroUtils.getLoginName());
        configWarning.setLastUpdatedBy(ShiroUtils.getLoginName());

        /* 保存*/
        if (!save(configWarning)){
            throw new SecurityException("保存配置信息失败");
        }
        return AjaxResult.success("");
    }

    /**
     * 保存预警配置
     * @param configWarning
     * @return
     */
    @Transactional(rollbackFor = SecurityException.class)
    public AjaxResult updateConfigWarning(ConfigWarning configWarning){

        /* 设置物料信息*/
        if (configWarning.getMaterialCode() != null){
            Material material = materialService.findAllByCode(configWarning.getMaterialCode());
            configWarning.setMaterialName(material.getName());
            configWarning.setMaterialSpec(material.getSpec());
            configWarning.setMaterialUnit(material.getUnit());
        }

        /* 设置更新人*/
        configWarning.setLastUpdatedBy(ShiroUtils.getLoginName());

        if (!updateById(configWarning)){
            throw new SecurityException("修改配置信息失败");
        }
        return AjaxResult.success("");
    }

    /**
     * 物料预警
     * @param materialCode,materialName 任务明细
     */
    public void materialWarning(String materialCode, String materialName){
        /* 查询预警配置*/
        LambdaQueryWrapper<ConfigWarning> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ConfigWarning::getMaterialCode, materialCode)
                .eq(ConfigWarning::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(ConfigWarning::getType, 0);
        ConfigWarning configWarning = this.getOne(lambdaQueryWrapper);

        /* 当预警配置存在时*/
        if (configWarning != null) {
            String[] emailArray = configWarning.getEmail().split(";");
            /* 收件人*/
            String[] to = {emailArray[0]};
            /* 抄送人*/
            List<String> ccList = new ArrayList<>();
            for (int i = 1; i < emailArray.length; i++) {
                ccList.add(emailArray[i]);
            }
            String[] cc = ccList.toArray(new String[0]);

            InventoryDetail inventoryDetail = new InventoryDetail();
            inventoryDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
            inventoryDetail.setMaterialCode(materialCode);
            inventoryDetail.setInventorySts("good");
            inventoryDetail.setCompanyCode(configWarning.getCompanyCode());
            BigDecimal sum = inventoryDetailService.getSumQty(inventoryDetail);
            SendMail sendMail = new SendMail();
            sendMail.setWarehouseCode(ShiroUtils.getWarehouseCode());
            sendMail.setCompanyCode(configWarning.getCompanyCode());
            sendMail.setType(0);
            sendMail.setEmail(configWarning.getEmail());
            sendMail.setAlarmTime(new Date());
            if (sum.compareTo(configWarning.getMax()) == 1) {
                sendMail.setTitle(materialName + "超过最大库存");
                sendMail.setBody(materialName + "超过最大库存,当前数量为" + sum);
                if (!sendMailService.save(sendMail)) {
                    throw new SecurityException("保存失败");
                }
                boolean resultMail = mailService.sendSimpleMail(to, sendMail.getTitle(), sendMail.getBody(), cc);
                sendMail.setEnable(resultMail);
                if (!sendMailService.updateById(sendMail)) {
                    throw new SecurityException("更新失败");
                }
            } else if (sum.compareTo(configWarning.getMin()) == -1) {
                sendMail.setTitle(materialName + "低于最低库存");
                sendMail.setBody(materialName + "低于最低库存,当前数量为" + sum);
                if (!sendMailService.save(sendMail)) {
                    throw new SecurityException("保存失败");
                }
                boolean resultMail = mailService.sendSimpleMail(to, sendMail.getTitle(), sendMail.getBody(), cc);
                sendMail.setEnable(resultMail);
                if (!sendMailService.updateById(sendMail)) {
                    throw new SecurityException("更新失败");
                }
            }
        }
    }

    /**
     * 空库位剩余数量预警
     */
    public void emptyLocationWarning(){
        /* 查询预警配置*/
        LambdaQueryWrapper<ConfigWarning> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ConfigWarning::getWarehouseCode, ShiroUtils.getWarehouseCode())
            .eq(ConfigWarning::getType, 1);
        ConfigWarning configWarning = this.getOne(lambdaQueryWrapper);
        /* 当预警配置存在时*/
        if (configWarning != null) {
            String[] emailArray = configWarning.getEmail().split(";");
            /* 收件人*/
            String[] to = {emailArray[0]};
            /* 抄送人*/
            List<String> ccList = new ArrayList<>();
            for (int i = 1; i < emailArray.length; i++) {
                ccList.add(emailArray[i]);
            }
            String[] cc = ccList.toArray(new String[0]);

            LambdaQueryWrapper<Location> locationLambda = Wrappers.lambdaQuery();
            locationLambda.eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode())
                    .eq(Location::getStatus, QuantityConstant.STATUS_LOCATION_EMPTY);
            BigDecimal sum = new BigDecimal(locationService.list(locationLambda).size());
            SendMail sendMail = new SendMail();
            sendMail.setWarehouseCode(ShiroUtils.getWarehouseCode());
            sendMail.setType(1);
            sendMail.setEmail(configWarning.getEmail());
            sendMail.setAlarmTime(new Date());
            if (sum.compareTo(configWarning.getMin()) == -1) {
                sendMail.setTitle("空库位剩余"+sum+"个");
                sendMail.setBody("空库位剩余"+sum+"个");
                if (!sendMailService.save(sendMail)) {
                    throw new SecurityException("保存失败");
                }
                boolean resultMail = mailService.sendSimpleMail(to, sendMail.getTitle(), sendMail.getBody(), cc);
                sendMail.setEnable(resultMail);
                if (!sendMailService.updateById(sendMail)) {
                    throw new SecurityException("更新失败");
                }
            }
        }
    }
}