package com.bonus.material.basic.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.bonus.common.log.enums.OperaType;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.material.basic.domain.BmMessage;
import com.bonus.material.basic.service.IBmMessageService;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.utils.poi.ExcelUtil;

/**
 * 消息Controller
 * 
 * @author xsheng
 * @date 2024-12-20
 */
@Api(tags = "消息接口")
@RestController
@RequestMapping("/bm_message")
public class BmMessageController extends BaseController {
    @Autowired
    private IBmMessageService bmMessageService;

    /**
     * 查询消息列表
     */
    @ApiOperation(value = "查询消息列表")
    //@RequiresPermissions("basic:message:list")
    @GetMapping("/list")
    public AjaxResult list(BmMessage bmMessage) {
        List<BmMessage> list = bmMessageService.selectBmMessageListFromCache(bmMessage);
        return AjaxResult.success(list);
    }

    /**
     * 根据所属公司查询消息未读数量
     * @param bmMessage
     * @return
     */
    @ApiOperation(value = "根据所属公司查询消息未读数量")
    //@RequiresPermissions("basic:message:list")
    @GetMapping("/getNoReadNum")
    public AjaxResult getNoReadNum(BmMessage bmMessage) {
        BmMessage message = bmMessageService.getNoReadNum(bmMessage);
        return AjaxResult.success(message);
    }

    @ApiOperation(value = "查询消息列表")
    //@RequiresPermissions("basic:message:list")
    @GetMapping("/listOneToOne")
    public AjaxResult listOneToOne(BmMessage bmMessage) throws JsonProcessingException {
        List<BmMessage> list = bmMessageService.selectBmMessageListFromCacheOne(bmMessage);
        return AjaxResult.success(list);
    }

    /**
     * 查询消息列表
     */
    @ApiOperation(value = "查询消息列表")
    //@RequiresPermissions("basic:message:list")
    @GetMapping("/listFromDB")
    public AjaxResult listFromDB(BmMessage bmMessage) {
        List<BmMessage> list = bmMessageService.selectBmMessageList(bmMessage);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "查询消息列表")
    //@RequiresPermissions("basic:message:list")
    @GetMapping("/listOneToOneFromDB")
    public AjaxResult listOneToOneFromDB(BmMessage bmMessage) {
        List<BmMessage> list = bmMessageService.selectBmMessageListOne(bmMessage);
        return AjaxResult.success(list);
    }

    /**
     * 导出消息列表
     */
    @ApiOperation(value = "导出消息列表")
    //@PreventRepeatSubmit
    //@RequiresPermissions("basic:message:export")
    @SysLog(title = "消息", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出消息")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmMessage bmMessage) {
        List<BmMessage> list = bmMessageService.selectBmMessageList(bmMessage);
        ExcelUtil<BmMessage> util = new ExcelUtil<BmMessage>(BmMessage.class);
        util.exportExcel(response, list, "消息数据");
    }

    /**
     * 获取消息详细信息
     */
    @ApiOperation(value = "获取消息详细信息")
    //@RequiresPermissions("basic:message:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bmMessageService.selectBmMessageById(id));
    }

    /**
     * 新增消息
     */
    @ApiOperation(value = "新增消息")
    //@PreventRepeatSubmit
    //@RequiresPermissions("basic:message:add")
    @SysLog(title = "消息", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->新增消息")
    @PostMapping
    public AjaxResult add(@RequestBody BmMessage bmMessage) {
        try {
            return toAjax(bmMessageService.insertBmMessage(bmMessage));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 修改消息
     */
    @ApiOperation(value = "已读修改")
    //@PreventRepeatSubmit
    //@RequiresPermissions("basic:message:edit")
    @SysLog(title = "消息", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改消息")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BmMessage bmMessage) {
        try {
            return toAjax(bmMessageService.updateBmMessage(bmMessage));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 修改消息
     */
    @ApiOperation(value = "全部已读修改")
    //@PreventRepeatSubmit
    //@RequiresPermissions("basic:message:edit")
    @SysLog(title = "消息", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改消息")
    @PostMapping("/editAll")
    public AjaxResult editAll(@RequestBody BmMessage bmMessage) {
        try {
            return toAjax(bmMessageService.updateBmMessageAll(bmMessage));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 删除消息
     */
    @ApiOperation(value = "删除消息")
    //@PreventRepeatSubmit
    //@RequiresPermissions("basic:message:remove")
    @SysLog(title = "消息", businessType = OperaType.DELETE, logType = 1,module = "仓储管理->删除消息")
	@PostMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bmMessageService.deleteBmMessageByIds(ids));
    }
}
