package com.ruoyi.mall.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mall.domain.MallCompany;
import com.ruoyi.mall.domain.MallCompanyUser;
import com.ruoyi.mall.service.IMallCompanyService;
import com.ruoyi.mall.service.IMallCompanyUserService;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.domain.MallWarnOperateLog;
import com.ruoyi.mall.service.IMallWarnOperateLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 告警操作记录Controller
 *
 * @author LCL
 * @date 2024-03-18
 */
@RestController
@RequestMapping("/mall/mallWarnOperateLog")
public class MallWarnOperateLogController extends BaseController
{
    @Resource
    private IMallWarnOperateLogService mallWarnOperateLogService;
    @Resource
    private IMallCompanyService mallCompanyService;
    @Resource
    private IMallCompanyUserService mallCompanyUserService;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询告警操作记录列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallWarnOperateLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallWarnOperateLog mallWarnOperateLog)
    {
        // 获取登陆用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUserRole sysUserRole = sysUserRoleMapper.selectSysUserRoleByUserId(loginUser.getUserId());

        if (sysUserRole != null) {
            // 公司角色
            if (sysUserRole.getRoleId() == 100) {
                MallCompany company = mallCompanyService.selectMallCompanyByCompanySysUserId(loginUser.getUserId());
                if (company != null) {
                    mallWarnOperateLog.setCompanyId(company.getCompanyId());
                }
            }
            // 基站管理员角色
            if (sysUserRole.getRoleId() == 101) {
                MallCompanyUser companyUser = mallCompanyUserService.selectMallCompanyUserByCompanySysUserId(loginUser.getUserId());
                if (companyUser != null) {
                    mallWarnOperateLog.setUserId(companyUser.getCompanyUserId());
                }
            }
        }

        startPage();
        List<MallWarnOperateLog> list = mallWarnOperateLogService.selectMallWarnOperateLogList(mallWarnOperateLog);
        return getDataTable(list);
    }

    /**
     * 导出告警操作记录列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallWarnOperateLog:export')")
    @Log(title = "告警操作记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallWarnOperateLog mallWarnOperateLog)
    {
        List<MallWarnOperateLog> list = mallWarnOperateLogService.selectMallWarnOperateLogList(mallWarnOperateLog);
        ExcelUtil<MallWarnOperateLog> util = new ExcelUtil<MallWarnOperateLog>(MallWarnOperateLog.class);
        util.exportExcel(response, list, "告警操作记录数据");
    }

    /**
     * 获取告警操作记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallWarnOperateLog:query')")
    @GetMapping(value = "/{warnOperateLogId}")
    public AjaxResult getInfo(@PathVariable("warnOperateLogId") Long warnOperateLogId)
    {
        return AjaxResult.success(mallWarnOperateLogService.selectMallWarnOperateLogByWarnOperateLogId(warnOperateLogId));
    }

    /**
     * 新增告警操作记录
     */
    @PreAuthorize("@ss.hasPermi('mall:mallWarnOperateLog:add')")
    @Log(title = "告警操作记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallWarnOperateLog mallWarnOperateLog)
    {
        return toAjax(mallWarnOperateLogService.insertMallWarnOperateLog(mallWarnOperateLog));
    }

    /**
     * 修改告警操作记录
     */
    @PreAuthorize("@ss.hasPermi('mall:mallWarnOperateLog:edit')")
    @Log(title = "告警操作记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallWarnOperateLog mallWarnOperateLog)
    {
        return toAjax(mallWarnOperateLogService.updateMallWarnOperateLog(mallWarnOperateLog));
    }

    /**
     * 删除告警操作记录
     */
    @PreAuthorize("@ss.hasPermi('mall:mallWarnOperateLog:remove')")
    @Log(title = "告警操作记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{warnOperateLogIds}")
    public AjaxResult remove(@PathVariable Long[] warnOperateLogIds)
    {
        return toAjax(mallWarnOperateLogService.deleteMallWarnOperateLogByWarnOperateLogIds(warnOperateLogIds));
    }
}
