package com.mvc.controller.app;

import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.model.AppUserModel;
import com.mvc.service.app.AppUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "手持机用户控制类")
@RequestMapping("/AppUserAction")
@Controller
public class AppUserAction {

    @Resource
    private AppUserService appUserService;

    @ApiOperation("手持机用户分页查询")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> findAllAppUser(@RequestBody AppUserModel model){
        try {
            Map<String,Object> res= appUserService.findAllAppUser(model);
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("手持机用户登录")
    @RequestMapping(value = "/loginAppUser/{loginId}/{loginPwd}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> login(@PathVariable("loginId") String loginId, @PathVariable("loginPwd") String loginPwd){
        try {
            AppUserModel resModel= appUserService.loginAppUser(loginId,loginPwd);
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("新增手持机用户")
    @RequestMapping(value = "/saveAppUser",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> saveAppUser(@RequestBody AppUserModel model){
        try {
            if(appUserService.getModelByLoginid(model.getLoginId())!=null){
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error","手机号码:"+model.getLoginId()+" 已经存在!");
                return resMap;
            }
            AppUserModel resModel= appUserService.saveAppUser(model);
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("更新手持机用户")
    @RequestMapping(value = "/updateAppUser",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateDeviceAcc(@RequestBody AppUserModel model){
        try {
            AppUserModel cModel=appUserService.getModelByLoginid(model.getLoginId());
            if(cModel!=null&&cModel.getId()!=model.getId()){
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error","手机号码:"+model.getLoginId()+" 已经存在!");
                return resMap;
            }
            AppUserModel resModel= appUserService.updateAppUser(model);
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("删除手持机用户")
    @RequestMapping(value = "/deleteAppUser/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> deleteAppUser(@PathVariable("id") int id){
        try {
            int  res= appUserService.deleteAppUser(id);
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }
}
