package com.muyu.enterprise.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.muyu.common.core.domain.Result;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.enterprise.domain.FaultMessage;
import com.muyu.enterprise.service.FaultMessageService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 站内信控制层
 * @author  chenruijia
 * @Date 2024/9/28 11:58
 * @Description FaultMessageController:站内信控制层
 */
@RestController
@RequestMapping("/fault-message")
@Tag(name = "站内信的管理",description = "对站内信息的处理与查看")
public class FaultMessageController {
    @Autowired
    private FaultMessageService faultMessageService;

    /**
     * 站内信列表
     * @return
     */
    @RequestMapping(value = "/faultMessageList",method = RequestMethod.GET)
    public Result<List<FaultMessage>> faultMessageList( FaultMessage faultMessage){
        return Result.success(faultMessageService.faultMessageList(faultMessage));
    }

    /**
     * 添加消息
     * @param faultMessage
     * @return
     */
    @RequestMapping(value = "/insertMessage",method = RequestMethod.POST)
    public Result insertMessage(@RequestBody FaultMessage faultMessage){
        String username = SecurityUtils.getUsername();
        //消息发送人
        faultMessage.setFaultMessageSendName(username);
        return Result.success(faultMessageService.save(faultMessage));
    }

    /**
     * 查询未读状态(faultMessageState==1)信息
     * @return
     */
    @RequestMapping(value = "/selectOne",method = RequestMethod.POST)
    public Result<List<FaultMessage>> selectOne(){
        List<FaultMessage> one = faultMessageService.selectOne();
        return Result.success(one);
    }

    /**
     * 查询已读状态(faultMessageState==2)信息
     * @return
     */
    @RequestMapping(value = "/selectTwo",method = RequestMethod.POST)
    public Result<List<FaultMessage>> selectTwo(){
        List<FaultMessage> one = faultMessageService.selectTwo();
        return Result.success(one);
    }

    /**
     * 更改已读状态
     * @param faultMessageId
     * @return
     */
    @RequestMapping(value = "/updateTwo/{faultMessageId}",method = RequestMethod.POST)
    public Result updateTwo(@PathVariable(name = "faultMessageId") Long faultMessageId){
        LambdaUpdateWrapper<FaultMessage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(FaultMessage::getFaultMessageState,2);
        updateWrapper.eq(FaultMessage::getFaultMessageId,faultMessageId);
        faultMessageService.update(updateWrapper);
        return Result.success(faultMessageId,"消息已读");
    }

    /**
     * 删除消息
     * @param faultMessageId
     * @return
     */
    @RequestMapping(value = "/deleteMessageId/{faultMessageId}",method = RequestMethod.POST)
    public Result deleteMessageId(@PathVariable(name = "faultMessageId") Long faultMessageId){
        return Result.success(faultMessageService.removeById(faultMessageId),"删除成功");
    }

}
