package com.mvc.controller.taizhang;

import com.mvc.constant.OperationType;
import com.mvc.entity.CustomUser;
import com.mvc.interceptor.JwtInfoMap;
import com.mvc.model.*;
import com.mvc.service.TRgeAuthService;
import com.mvc.service.log.OperationLogService;
import com.mvc.service.taizhang.CustomUserService;
import com.mvc.utils.JwtUtil;
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.*;

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

@Api(tags = "客户用户接口")
@RequestMapping("/CustomUserAction")
@Controller
public class CustomUserAction {

    @Resource
    private CustomUserService customUserService;

    @Resource
    private TRgeAuthService tRgeAuthService;

    @Resource
    private OperationLogService operationLogService;

    @ApiOperation("客户分页查询")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> findAllCustomUser(@RequestBody CustomUserModel model){
        try {
            Map<String,Object> res= customUserService.findAllCustomUser(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 = "/saveCustomUser",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> saveCustomUser(@RequestBody CustomUserModel model){
        try {
            CustomUser entity= customUserService.findCustomUserByLoginId(model.getLoginId());
            if(entity!=null){
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error","["+model.getLoginId()+"]登陆ID已经存在");
                return resMap;
            }

            CustomUserModel resModel= customUserService.saveCustomUser(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 = "/updateCustomUser",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateCustomUser(@RequestBody CustomUserModel model){
        try {
            CustomUser entity= customUserService.findCustomUserByLoginId(model.getLoginId());
            if(entity!=null&&!entity.getUserId().equals(model.getUserId())){
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error","["+model.getLoginId()+"]登陆ID已经存在");
                return resMap;
            }

            CustomUserModel resModel= customUserService.updateCustomUser(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 = "/deleteCustomUser/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> deleteCustomUser(@PathVariable("id") int id){
        try {
            int  res= customUserService.deleteCustomUser(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;
        }
    }

    @ApiOperation("客户登录接口")
    @RequestMapping(value = "/loginCustomUser/{loginId}/{loginPwd}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> login(@PathVariable("loginId") String loginId, @PathVariable("loginPwd") String loginPwd){
        try {
            CustomUserModel resModel= customUserService.logincustomUser(loginId);
            if(resModel==null){
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error","账号不存在!");
                return resMap;
            }else if(!resModel.getLoginPwd().equals(loginPwd)){
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error","密码错误!");
                return resMap;
            }else if(!resModel.getUseFlag().equals("1")) {
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error","账户已被禁用,请联系管理员!");
                return resMap;
            }

            //加Token
            //准备存放在IWT中的自定义数据
            Map<String, Object> info = new JwtInfoMap(resModel);
            resModel.setAccessToken(JwtUtil.sign(resModel.getUserId(), info));

            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 = "/registCustomUser/{authCode}/{userName}/{loginId}/{loginPwd}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> regist(@PathVariable("authCode") String authCode,@PathVariable("userName") String userName,
                                     @PathVariable("loginId") String loginId, @PathVariable("loginPwd") String loginPwd){
        try {
            TRgeAuthModel tRgeAuthModel=tRgeAuthService.getTRgeAuthModelByAuthCode(authCode);
            if(tRgeAuthModel==null){
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error","授权码无效!");
                return resMap;
            }else if(tRgeAuthModel.getAuthUseNum()>=tRgeAuthModel.getAuthNumber()) {
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error","授权数量达到上限!");
                return resMap;
            }

            String msg  = customUserService.checkDataLegitimacy(userName,loginId,loginPwd);
            if(StringUtils.isNotEmpty(msg)){
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error",msg);
                return resMap;
            }

            CustomUser entity= customUserService.findCustomUserByLoginId(loginId);
            if(entity!=null){
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error","["+loginId+"]登陆ID已经存在");
                return resMap;
            }

            CustomUserModel resModel= customUserService.registCustomUser(tRgeAuthModel,userName,loginId,loginPwd);
            if(resModel!=null){
                tRgeAuthService.updateTRgeAuth(tRgeAuthModel);
            }
            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 = "/changePassword",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> changePassword(@RequestBody CustomUserModel model){
        try {
            CustomUserModel userModel= customUserService.findCustomUserModelByUserId(model.getUserId());
            String error = "";
            if(userModel==null){
                error = "此账户已不存在";

            }
            if(!userModel.getLoginPwd().equals(model.getLoginPwd())){
                error = "旧密码不正确";
            }
            if(StringUtils.isNotEmpty(error)){
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error",error);
                return resMap;
            }
            userModel.setLoginPwd(model.getNewPassword());
            CustomUserModel resModel= customUserService.updateCustomUser(userModel);
            operationLogService.saveOperationLog(1, OperationType.CHANGE_PWD,resModel,"");
            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;
        }
    }
}
