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

import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.supply.stock.service.AlarmService;
import com.sifang.modules.sys.entity.Ascription;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.GuardUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/alarm")
public class AlarmController extends BaseExceptionController {
    Logger logger = LoggerFactory.getLogger(AlarmController.class);

    @Autowired
    private AlarmService alarmService;

    /**
     * 预警消息页面
     */
    @RequestMapping(value = "toAlarmPage")
    public String toAlarmPage() {

        return "modules/supply/warehouse/alarmPage";
    }

    /**
     * 预警消息列表数据
     */
    @ResponseBody
    @RequestMapping(value = "ajaxForAlarmData")
    public Object ajaxForAlarmData(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        Ascription ascription = CrmUtils.getAscription();
        map.put("ascriptionId", ascription.getAscriptionId());
        logger.info(" into ajaxForAlarmData, map={}", map);
        alarmService.getAlarmPage(page, map);
        return page.renderJson();
    }

    /**
     * 标记全部已读
     */
    @ResponseBody
    @RequestMapping(value = "allMarkAsRead")
    public Result allMarkAsRead() {
        Ascription ascription = CrmUtils.getAscription();
        alarmService.allMarkAsRead(ascription.getAscriptionId());
        return Result.ok();
    }

    /**
     * 根据预警id 标记已读
     */
    @ResponseBody
    @RequestMapping(value = "markAsRead")
    public Result markAsRead(String alarmId) {
        GuardUtils.notBlank(alarmId, "预警id不能为空");
        alarmService.markAsRead(alarmId);
        return Result.ok();
    }
}
