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

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.supply.stock.dao.CrmSupplyAlarmDao;
import com.sifang.modules.supply.stock.entity.CrmSupplyAlarm;
import com.sifang.modules.sys.util.LayuiPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class AlarmService {
    @Autowired
    private CrmSupplyAlarmDao alarmDao;

    public LayuiPage getAlarmPage(LayuiPage page, Map<String, String> map) {
        page.setData(alarmDao.listAlarm(page, map));
        page.setCount(alarmDao.countAlarm(map));
        return page;
    }

    public void allMarkAsRead(String ascriptionId) {
        Db.update("update crm_supply_alarm set status = '1' where ascription_id = ?", ascriptionId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void markAsRead(String alarmId) {
        CrmSupplyAlarm alarm = alarmDao.get(alarmId);
        if (!StrKit.notNull(alarm)) {
            throw new BusinessException("预警消息不存在");
        }
        alarm.setStatus("1");
        alarmDao.save(alarm);
    }
}
