AlarmScanningService.java 7.91 KB
package com.huaheng.common.mail.alarmmail.service;

import com.huaheng.common.mail.alarmmail.domain.AlarmFlow;
import com.huaheng.common.mail.alarmmail.domain.AlarmType;
import com.huaheng.common.mail.alarmmail.domain.SendEmail;
import com.huaheng.common.mail.alarmmail.mapper.AlarmFlowMapper;
import com.huaheng.common.mail.alarmmail.mapper.AlarmTypeMapper;
import com.huaheng.common.mail.alarmmail.mapper.SendEmailMapperAuto;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.zoneCapacity.domain.ZoneCapacity;
import com.huaheng.pc.config.zoneCapacity.service.IZoneCapacityService;
import com.huaheng.pc.general.location.service.ILocationService;
import com.huaheng.pc.general.material.service.IMaterialService;
import com.huaheng.pc.inventory.inventory.service.IInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 库存报警 主要逻辑层
 * @author ZengBo
 * @date 2019-03-08
 */
@Service
@Transactional
public class AlarmScanningService {

    @Resource
    private AlarmTypeMapper alarmTypeMapper;

    @Resource
    private AlarmFlowMapper alarmFlowMapper;

    @Resource
    private SendEmailMapperAuto sendEmailMapperAuto;

    @Autowired
    private IInventoryService iInventoryService;

    @Autowired
    private IZoneCapacityService iZoneCapacityService;

    @Autowired
    IMaterialService iMaterialService;

    /** 库存预警主逻辑控制方法 */
    public AjaxResult Mastercontrol() throws Exception {
        /** 上限预警值 */
        BigDecimal uphAlarm = null;
        /** 上限值 */
        BigDecimal uph = null;
        /** 下限预警值 */
        BigDecimal lphAlarm = null;
        /** 下限值 */
        BigDecimal lph = null;
        List<AlarmType> alarmTypeList = alarmTypeMapper.selectAllalarm(true);
        for (AlarmType item : alarmTypeList) {
            BigDecimal qty = iInventoryService.getSumQtyByZoneId(item.getZoneId(),item.getMaterialcode());
            ZoneCapacity zoneCapacity1 = new ZoneCapacity();
            zoneCapacity1.setZoneId(item.getZoneId());
            zoneCapacity1.setMaterialCode(item.getMaterialcode());
            ZoneCapacity zoneCapacity = iZoneCapacityService.selectFirstEntity(zoneCapacity1);

            SendEmail sendEmailEnd = new SendEmail();
            sendEmailEnd.setAlarmtypeid(item.getId());
            sendEmailEnd.setDeleted(false);

            uphAlarm = zoneCapacity.getUphAlarm();
            uph = zoneCapacity.getUph();
            lphAlarm = zoneCapacity.getLphAlarm();
            lph = zoneCapacity.getLph();
            SendEmail sendEmail = sendEmailMapperAuto.selectFirstEntity(sendEmailEnd);
            sendEmailEnd.setId(sendEmail.getId());
            sendEmailEnd.setLastUpdatedBy(ShiroUtils.getLoginName());
            boolean y = false;
            if (sendEmail == null){
                List<AlarmFlow> alarmFlows = alarmFlowMapper.selectEntityByAlarmidList(item.getId(),1);
                sendEmailEnd.setWarninglevel(1);
                List<String> strings = Assemble(alarmFlows);
                sendEmailEnd.setAddressee(strings.get(0));
                sendEmailEnd.setAddresseeemail(strings.get(1));
                sendEmailEnd.setCreatedBy(ShiroUtils.getLoginName());
            }else {
                Integer garade = sendEmail.getWarninglevel();
                List<AlarmFlow> alarmFlows = alarmFlowMapper.selectEntityByAlarmidList(item.getId(), garade);
                sendEmailEnd.setWarninglevel(garade);
                List<String> strings = Assemble(alarmFlows);
                sendEmailEnd.setAddressee(strings.get(0));
                sendEmailEnd.setAddresseeemail(strings.get(1));
                y = true;
            }
            if (qty.compareTo(uphAlarm) == 0 || qty.compareTo(uphAlarm) == 1){
                if (qty.compareTo(uph) == 0 || qty.compareTo(uph) == 1){
                    //超过或等于上限值
                    return saveSendEmail(sendEmailEnd,item,1,y);
                }else {
                    //超过或等于上限预警值
                    return saveSendEmail(sendEmailEnd,item,2,y);
                }
            }
            if (qty.compareTo(lphAlarm) == 0 || qty.compareTo(lphAlarm) == -1){
                if (qty.compareTo(lph) == 0 || qty.compareTo(lph) == -1){
                    //低于或等于下限值
                    return saveSendEmail(sendEmailEnd,item,3,y);
                }else {
                    //低于或等于下限预警值
                    return saveSendEmail(sendEmailEnd,item,4,y);
                }
            }
            //修改处理状态
            if (sendEmail != null){
                sendEmail.setWarninglevel(1);
                sendEmail.setHandle(true);
                sendEmail.setAddressee("");
                sendEmail.setAddresseeemail("");
                Integer result = sendEmailMapperAuto.updateByModel(sendEmail);
                if (result < 1 ){
                    throw new Exception("修改邮件表ID为: " + sendEmail.getId() + "  的处理状态失败!");
                }
            }
        }
        return AjaxResult.success("成功!");
    }



    /**
     * 组装邮件发送报表
     * x : 四种报警情况 1:大于上限值 , 2:大于上限预警值 ,3:小于下限值 , 4:小于下限预警值
     * @param sendEmail
     */
    public AjaxResult saveSendEmail(SendEmail sendEmail,AlarmType alarmType,Integer x,boolean y) throws Exception {
        String warehousecode = ShiroUtils.getWarehouseCode();
        String materialname = iMaterialService.selectNameBycode(alarmType.getMaterialcode()).getName();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sendEmail.setAlarmtime(new Date());
        String date = simpleDateFormat.format(sendEmail.getAlarmtime());
        String alarm = null;
        if (x == 1) {
            alarm = " 时超过了上限值!请及时处理...";
        }if (x == 2){
            alarm = " 时超过了上限预警值!请及时处理...";
        }if (x == 3){
            alarm = " 时低于了下限值!请及时处理...";
        }if (x == 4){
            alarm = " 时低于了下限预警值!请及时处理...";
        }
        String titile = warehousecode + " : 仓库物料报警---" + sendEmail.getWarninglevel() + "级";
        String body = "您好!" + "仓库: " + warehousecode + "  区域ID为: " + alarmType.getZoneId() + "的物料: " + materialname + " 在 " + date + alarm;
        sendEmail.setTitle(titile);
        sendEmail.setBody(body);
        sendEmail.setEnable(false);
        sendEmail.setHandle(false);
        if (y) {
            if (sendEmailMapperAuto.updateByModel(sendEmail)<1){
                throw new Exception("更新SendMail失败!");
            }else {
                return AjaxResult.success("更新SendMail成功!");
            }
        } else {
            if (sendEmailMapperAuto.insert(sendEmail)<1){
                throw new Exception("新增SendMail失败!");
            }else {
                return AjaxResult.success("新增SendMail成功!");
            }
        }
    }



    public List<String> Assemble(List<AlarmFlow> alarmFlows){
        StringBuilder addressee = new StringBuilder();
        StringBuilder addresseeemail = new StringBuilder();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < alarmFlows.size(); i++) {
            addressee.append(alarmFlows.get(i).getAddressee()).append(";");
            addresseeemail.append(alarmFlows.get(i).getAddresseeemail()).append(";");
        }
        list.add(String.valueOf(addressee));
        list.add(String.valueOf(addresseeemail));
        return list;
    }
}