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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.artificer.entity.Artificer;
import com.sqx.modules.artificer.service.ArtificerService;
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;
    @Autowired
    private ArtificerService artificerService;
    @Autowired
    private UserService userService;


    @RequestMapping(value = "/selectMessageList", method = RequestMethod.GET)
    @ApiOperation("查询用户消息")
    @ResponseBody
    public Result selectMessageList(int page, int limit,Integer state){
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        map.put("state",state);
        PageUtils pageUtils = messageService.selectMessageList(map);
        return Result.success().put("data",pageUtils);
    }

    @GetMapping("/selectCityCount")
    @ApiOperation("查询城市投票总条数")
    public Result selectCityCount(String city){
        int count = messageService.count(new QueryWrapper<MessageInfo>().eq("state", 11).eq("content", city));
        return Result.success().put("data",count);
    }


    @Login
    @RequestMapping(value = "/selectMessageByUserId", method = RequestMethod.GET)
    @ApiOperation("查询用户消息")
    @ResponseBody
    public Result selectUserRecharge(int page, int limit,@RequestAttribute("userId") Long userId,Integer state){
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        map.put("userId",userId);
        map.put("state",state);
        PageUtils pageUtils = messageService.selectMessageList(map);
        messageService.updateSendState(userId,state);
        return Result.success().put("data",pageUtils);
    }

    @Login
    @RequestMapping(value = "/selectMessageByUserIds", method = RequestMethod.GET)
    @ApiOperation("查询用户消息")
    @ResponseBody
    public Result selectMessageLists(int page, int limit,@RequestAttribute("userId") Long userId,Integer state){
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        map.put("userId",userId);
        map.put("state",state);
        PageUtils pageUtils = messageService.selectMessageLists(map);
        messageService.updateSendState(userId,state);
        return Result.success().put("data",pageUtils);
    }

    @Login
    @RequestMapping(value = "/selectMessageByUserIdLimit1", method = RequestMethod.GET)
    @ApiOperation("查询用户消息")
    @ResponseBody
    public Result selectMessageByUserIdLimit1(@RequestAttribute("userId") Long userId){
        Map<String,Object> map=new HashMap<>();
        map.put("page",1);
        map.put("limit",1);
        map.put("userId",userId);
        return Result.success().put("data",messageService.selectMessageList(map));
    }


    @Login
    @PostMapping("/insertMessage")
    @ApiOperation("添加投诉")
    public Result insertMessage(@RequestBody MessageInfo messageInfo){
        messageService.saveBody(messageInfo);
        return Result.success();
    }

    @Login
    @PostMapping("/insertArtificerLocation")
    @ApiOperation("更新经纬度")
    public Result insertArtificerLocation(@RequestBody MessageInfo messageInfo){
        return messageService.insertArtificerLocation(messageInfo);
    }

    @Login
    @PostMapping("/insertUpMessage")
    @ApiOperation("通知技师上线")
    public Result insertUpMessage(MessageInfo messageInfo){
        UserEntity userEntity = userService.queryByUserId(Long.valueOf(messageInfo.getByUserId()));
        Artificer artificer = artificerService.getById(Long.valueOf(messageInfo.getUserId()));
        UserEntity artificerUser = userService.queryByUserId(artificer.getUserId());
        String content =  userEntity.getUserName()+"邀请您上线！";
        messageInfo.setContent(content);
        messageInfo.setIsSee("0");
        messageInfo.setState("52");
        messageInfo.setTitle("通知上线");
        messageInfo.setByUserName(userEntity.getUserName());
        messageInfo.setUserName(artificer.getArtificerName());
        messageService.saveBody(messageInfo);


        if (StringUtils.isNotEmpty(artificerUser.getPhone())) {
            userService.sendMsg(artificerUser.getPhone(), "qtsx");
        }


        return Result.success();
    }


}