package com.muyu.fault.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.fault.common.FaultMessage;
import com.muyu.fault.service.impl.FaultMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
public class FaultMessageController {
    @Autowired
    private FaultMessageService faultMessageService;

    /**
     * 站内信列表
     * @return
     */
    @RequestMapping(value = "/faultMessageList",method = RequestMethod.GET)
    public Result faultMessageList(@RequestBody FaultMessage faultMessage){
        String username = SecurityUtils.getUsername();
        LambdaQueryWrapper<FaultMessage> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(faultMessage.getFaultMessageSendName())) {
            queryWrapper.eq(FaultMessage::getFaultMessageSendName,username);
        }
        return Result.success(faultMessageService.list(queryWrapper));
    }

    /**
     * 添加消息
     * @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 selectOne(){
        List<FaultMessage> one = faultMessageService.selectOne();
        return Result.success(one);
    }

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

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