package com.sqx.modules.message.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fang
 * @date 2020/7/13
 */
@RestController
@Api(value = "消息管理", tags = {"消息管理"})
@RequestMapping(value = "/app/message")
public class AppMessageController {

    @Autowired
    private MessageService messageService;

    @Login
    @RequestMapping(value = "/selectMessageByUserId", method = RequestMethod.GET)
    @ApiOperation("查询用户消息")
    @ResponseBody
    public Result selectUserRecharge(Integer page, Integer limit, @RequestAttribute("userId") Long userId, MessageInfo messageInfo) {
        messageInfo.setUserId(String.valueOf(userId));
        IPage<MessageInfo> messageInfoIPage = messageService.selectMessageList(page, limit, messageInfo);
        return Result.success().put("data", messageInfoIPage);
    }


    @RequestMapping(value = "/selectMessageList", method = RequestMethod.GET)
    @ApiOperation("查询消息")
    @ResponseBody
    public Result selectMessageList(String state) {
        return Result.success().put("data", messageService.list(new QueryWrapper<MessageInfo>().eq("state",state)));
    }

    @Login
    @RequestMapping(value = "/selectMessageByUserIdLimit1", method = RequestMethod.GET)
    @ApiOperation("查询用户消息")
    @ResponseBody
    public Result selectMessageByUserIdLimit1(@RequestAttribute("userId") Long userId, MessageInfo messageInfo) {
        return Result.success().put("data", messageService.selectMessageList(1, 1, messageInfo));
    }


    @Login
    @PostMapping("/insertMessage")
    @ApiOperation("添加投诉")
    public Result insertMessage(@RequestBody MessageInfo messageInfo,@RequestAttribute Long userId) {
        if("2".equals(messageInfo.getState())){
            messageInfo.setUserId(String.valueOf(userId));
        }
        messageService.saveBody(messageInfo);
        return Result.success();
    }
    @Login
    @GetMapping("/getOrderMessage")
    @ApiOperation("获取新订单通知")
    public Result getOrderMessage(@RequestAttribute("userId")Long userId){
        return messageService.getOrderMessage(userId);
    }

    @Login
    @GetMapping("/getRouteOrdersCount")
    @ApiOperation("获取邀请订单通知")
    public Result getRouteOrdersCount(@RequestAttribute("userId")Long userId){
        int count = messageService.count(new QueryWrapper<MessageInfo>().eq("user_id", userId).eq("is_see", 0).in("state", 5,6));
        return Result.success().put("data",count);
    }

    @Login
    @GetMapping("/getUserMessageCount")
    @ApiOperation("获取邀请订单通知")
    public Result getUserMessageCount(@RequestAttribute("userId")Long userId){
        int count = messageService.count(new QueryWrapper<MessageInfo>().eq("user_id", userId).eq("is_see", 0).eq("state", 5));
        return Result.success().put("data",count);
    }

}