package com.mvc.controller.wx;

import com.mvc.model.ResultGeekerModel;
import com.mvc.model.TAireadModel;
import com.mvc.model.wx.WxUserProfileModel;
import com.mvc.service.wx.WxUserProfileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "微信用户信息")
@RequestMapping("/WxUserProfileAction")
@Controller
public class WxUserProfileAction {

    @Resource
    private WxUserProfileService wxUserProfileService;

    @ApiOperation("查询所有数据列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel findAllWxUserProfile() {
        ResultGeekerModel resultModel;
        try {
            //List<WxUserProfileModel> res = wxUserProfileService.findAllWxUserProfile(null);
            resultModel = new ResultGeekerModel(null);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("登陆")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel findAllWxUserProfile(@RequestBody WxUserProfileModel model) {
        ResultGeekerModel resultModel;
        try {
            WxUserProfileModel res = wxUserProfileService.login(model.getJsCode(),model.getAvatarUrl(),model.getNickName());
            if(res.getUseFlag()!=1){
                resultModel = new ResultGeekerModel("用户限制登陆,请联系管理员");
            }else{
                resultModel = new ResultGeekerModel(res);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("新增微信用户信息记录")
    @RequestMapping(value = "/saveWxUserProfile", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel saveWxUserProfile(@RequestBody WxUserProfileModel model) {
        ResultGeekerModel resultModel;
        try {
            WxUserProfileModel temp = wxUserProfileService.saveWxUserProfile(model);
            if (temp != null) {
                resultModel = new ResultGeekerModel("当前类型,编号已经存在");
                return resultModel;
            }

            WxUserProfileModel resModel = wxUserProfileService.saveWxUserProfile(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("更新微信用户信息记录")
    @RequestMapping(value = "/updatexUserProfile", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel updatexUserProfile(@RequestBody WxUserProfileModel model) {
        ResultGeekerModel resultModel;
        try {
            WxUserProfileModel resModel = wxUserProfileService.updateWxUserProfile(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("查询所有数据列表")
    @RequestMapping(value = "/findWxUserDevicesAireads/{userProfileId}", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel findWxUserDevicesAireads(@PathVariable("userProfileId") Integer userProfileId) {
        ResultGeekerModel resultModel;
        try {
            List<TAireadModel> res = wxUserProfileService.findWxUserDevicesAireads(userProfileId);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }


}
