package com.mvc.controller.wx;

import com.mvc.model.ResultGeekerModel;
import com.mvc.model.wx.WxUserDevicesModel;
import com.mvc.service.wx.WxUserDevicesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Api(tags = "微信用户设备清单")
@RequestMapping("/WxUserDevicesAction")
@Controller
public class WxUserDevicesAction {

    @Resource
    private WxUserDevicesService wxUserDevicesService;

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

    @ApiOperation("新增微信用户信息记录")
    @RequestMapping(value = "/saveWxUserDevices", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel saveWxUserDevices(@RequestBody WxUserDevicesModel model) {
        ResultGeekerModel resultModel;
        try {
            String info = wxUserDevicesService.checkValid(model);
            if(StringUtils.isNotEmpty(info)){
                resultModel = new ResultGeekerModel(info);
            }else {
                WxUserDevicesModel resModel = wxUserDevicesService.saveWxUserDevices(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 WxUserDevicesModel model) {
        ResultGeekerModel resultModel;
        try {
            WxUserDevicesModel resModel = wxUserDevicesService.updateWxUserDevices(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }
}
