package com.mvc.controller.dic;


import com.mvc.constant.QueryUserType;
import com.mvc.entity.FacUser;
import com.mvc.interceptor.JwtInfoMap;
import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.model.FacUserModel;
import com.mvc.service.dic.FacUserService;
import com.mvc.utils.AESUtil;
import com.mvc.utils.JwtUtil;
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.HashMap;
import java.util.Map;


@Api(tags = "内部用户接口")
@RequestMapping("/FacUserAction")
@Controller
public class FacUserAction {

    @Resource
    private FacUserService facUserService;

    @ApiOperation("分页查询内部用户")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllFacUser(@RequestBody FacUserModel model) {
        try {
            Map<String, Object> res = facUserService.findAllFacUser(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 = "/loginFacuser/{loginId}/{loginPwd}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> login(@PathVariable("loginId") String loginId, @PathVariable("loginPwd") String loginPwd) {
        try {
            FacUserModel resModel = facUserService.loginFacuser(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 = "/saveFacUser", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveFacUser(@RequestBody FacUserModel model) {
        try {
            FacUser entity = facUserService.findFacUserByLoginId(model.getLoginId());
            if (entity != null) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", "[" + model.getLoginId() + "]登陆ID已经存在");
                return resMap;
            }

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

            FacUserModel resModel = facUserService.updateFacUser(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 = "/deleteFacUser/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteFacUser(@PathVariable("id") int id) {
        try {
            int res = facUserService.deleteFacUser(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 = "/loginFacuser", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> loginFacuser(@RequestBody FacUserModel model) {
        try {
            FacUserModel resModel = facUserService.loginFacuser(model.getLoginId());
            String loginPwd = AESUtil.webDecrypt(model.getLoginPwd());
            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 = "/loginOut", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> loginOut() {
        try {
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", 1);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }
}
