package com.izouma.awesomeadmin.web;

import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.dto.Result;
import com.izouma.awesomeadmin.model.ChatInfo;
import com.izouma.awesomeadmin.service.ChatInfoService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping("/chatInfo")
@RequiresAuthentication
public class ChatInfoController {

    @Autowired
    private ChatInfoService chatInfoService;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public List<ChatInfo> all() {

        List<ChatInfo> pp = chatInfoService.getChatInfoList();
        return pp;
    }

    @RequestMapping(value = "/getChatInfo", method = RequestMethod.GET)
    @ResponseBody
    public Result getChatInfo(@RequestParam(required = false, value = "id") String id) {

        ChatInfo data = chatInfoService.getChatInfoById(id);
        return new Result(true, data);
    }

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ResponseBody
    public Result getOne(ChatInfo chatInfo) {

        ChatInfo data = chatInfoService.getChatInfo(chatInfo);
        return new Result(true, data);
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Result page(Page page, ChatInfo chatInfo) {

        Map<String, Object> result = new HashMap<>();

        List<ChatInfo> pp = chatInfoService.getChatInfoByPage(page, chatInfo);

        result.put(AppConstant.PAGE, page);
        result.put("pp", pp);
        return new Result(true, result);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(ChatInfo chatInfo) {


        boolean num = chatInfoService.createChatInfo(chatInfo);
        if (num) {
            return new Result(true, chatInfo);
        }
        return new Result(false, "保存异常");

    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result updateChatInfo(ChatInfo chatInfo) {

        boolean num = chatInfoService.updateChatInfo(chatInfo);
        if (num) {
            return new Result(true, "保存成功");
        }
        return new Result(false, "保存异常");
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteChatInfoId(@RequestParam("id") String id) {

        boolean num = chatInfoService.deleteChatInfo(id);
        if (num) {
            return new Result(true, "删除成功");
        }
        return new Result(false, "删除异常");
    }


    @RequestMapping(value = "/readAll", method = RequestMethod.GET)
    @ResponseBody
    public Result readAll(ChatInfo chatInfo) {
        return new Result(chatInfoService.readAll(chatInfo), "");
    }

}
