package com.muchinfo.mtp3century.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.muchinfo.mtp3century.service.ISystemmanagerService;
import com.muchinfo.mtp3century.utils.CenturyDecryptUtil;
import com.muchinfo.mtp3century.vo.LoginCodeVo;
import com.muchinfo.mtp3century.vo.systemlogin.LoginReq;
import com.muchinfo.mtp3century.vo.systemlogin.LoginRsp;
import com.muchinfo.mtp3common.enumtype.Constants;
import com.muchinfo.mtp3common.enumtype.MessageType;
import com.muchinfo.mtp3common.enumtype.SysConstantsEnum;
import com.muchinfo.mtp3century.utils.AjaxResult;
import com.muchinfo.mtp3common.utils.JsonUtils;
import com.muchinfo.mtp3common.vo.PageResult;
import com.muchinfo.mtp3interface.mapper.ISystemmanagerMapper;
import com.muchinfo.mtp3interface.mapper.IUserloginflowMapper;
import com.muchinfo.mtp3pojos.entity.Systemmanager;
import com.muchinfo.mtp3pojos.entity.Userloginflow;
import com.muchinfo.mtp3pojos.vo.systemuser.SystemUserPwdParam;
import com.muchinfo.mtp3pojos.vo.systemuser.SystemUserReqParam;
import com.muchinfo.mtp3pojos.vo.systemuser.SystemUserResResult;
import com.muchinfo.mtp3pojos.vo.systemuser.SystemUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;


/**
 * 管理员管理，登录
 */
@Controller
@RequestMapping("/systemmanager")
@Api(value = "管理员API", description = "管理员管理")
public class SystemmanagerController {

    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private ISystemmanagerService iSystemmanagerService;
    @Resource
    private IUserloginflowMapper iUserloginflowMapper;
    @Resource
    private ISystemmanagerMapper iSystemmanagerMapper;

    /**
     * @param loginReq
     * @return message 登录接口
     */
    @ApiOperation("登录接口")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.POST, value = "/loginIn")
    @ResponseBody
    public AjaxResult<LoginRsp> loginIn(@RequestBody String loginReq, HttpServletRequest request) {
        try {
            LoginReq currentVo = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(loginReq, request), LoginReq.class);
            Systemmanager systemmanagers = iSystemmanagerMapper.selectOne(new QueryWrapper<Systemmanager>().eq("LOGINCODE", currentVo.getAccountCode()));
            AjaxResult ajaxResult = iSystemmanagerService.loginIn(currentVo, request, systemmanagers);
            //增加登录流水
            Long autoId = null != systemmanagers ? systemmanagers.getAutoid() : Long.valueOf(currentVo.getAccountCode());
            Userloginflow userloginflow = Userloginflow.builder()
                    .loginid(autoId)
                    .clienttype(SysConstantsEnum.SYS_TYPE_MANAGE.getCode())
                    .operatetime(new Date())
                    .loginip(currentVo.getLoginIp())
                    .loginretcode(200 == ajaxResult.getCode() ? 0 : ajaxResult.getCode())
                    .remark(ajaxResult.getMessage())
                    .build();
            iUserloginflowMapper.insert(userloginflow);
            return ajaxResult;
        } catch (Exception e) {
            log.error("登录失败，失败原因：" + Arrays.toString(e.getStackTrace()));
            return AjaxResult.error(Constants.Error_Code_Design, MessageType.MESSAGE_ERROR_CODE_SYSTEM002.getCode(), request.getHeader("Accept-Language"));
        }
    }

    /**
     * @return message 获取验证码，60s过期
     */
    @RequestMapping(method = RequestMethod.GET, value = "/verifyCode")
    @ResponseBody
    @ApiOperation("获取验证码")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult<LoginCodeVo> getVerifyCode(HttpServletRequest request) {
        return iSystemmanagerService.getVerifyCode(request);
    }


    @ApiOperation("管理员查询")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    @RequestMapping(method = RequestMethod.GET, value = "/query")
    @ResponseBody
    public PageResult<SystemUserResResult> query(SystemUserReqParam systemUserReqParam, HttpServletRequest request) {
        try {
            return iSystemmanagerService.queryUserMsg(request, systemUserReqParam);
        } catch (Exception e) {
            log.error("管理员查询信息失败，失败原因："  + Arrays.toString(e.getStackTrace()));
            PageResult<SystemUserResResult> pageResult = new PageResult<>();
            pageResult.setCode(Constants.Error_Code_Design);
            pageResult.setMessage(AjaxResult.dealMsg(MessageType.MESSAGE_ERROR_CODE_SYSTEM002.getCode(), request.getHeader("Accept-Language")));
            return pageResult;
        }
    }

    @ApiOperation("获取当前管理员详情信息")
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/queryDetail")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<SystemUserResResult> queryDetail(Integer autoId, HttpServletRequest request) {
        return iSystemmanagerService.queryDetail(autoId, request);
    }

    @ApiOperation("处理管理员信息")
    @RequestMapping(method = RequestMethod.POST, value = "/dealUserMsg")
    @ResponseBody
    private AjaxResult dealUserMsg(@RequestBody String systemUserVo, HttpServletRequest request) {
        try {
            SystemUserVo currentVo = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(systemUserVo, request), SystemUserVo.class);
            return iSystemmanagerService.dealUserMsg(currentVo, request);
        } catch (Exception e) {
            log.error("处理管理员信息失败，失败原因：" + Arrays.toString(e.getStackTrace()));
            return AjaxResult.error(Constants.Error_Code_Design, MessageType.MESSAGE_ERROR_CODE_SYSTEM002.getCode(), request.getHeader("Accept-Language"));
        }
    }

    @ApiOperation("处理管理员状态")
    @RequestMapping(method = RequestMethod.POST, value = "/dealUserStatus")
    @ResponseBody
    private AjaxResult dealUserStatus(@RequestBody String systemUserVo, HttpServletRequest request) {
        try {
            SystemUserVo currentVo = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(systemUserVo, request), SystemUserVo.class);
            return iSystemmanagerService.dealUserStatus(currentVo, request);
        } catch (Exception e) {
            log.error("处理管理员状态失败，失败原因："  + Arrays.toString(e.getStackTrace()));
            return AjaxResult.error(Constants.Error_Code_Design, MessageType.MESSAGE_ERROR_CODE_SYSTEM002.getCode(), request.getHeader("Accept-Language"));
        }
    }

    @ApiOperation("获取当前角色关联关系")
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/queryRoleUser")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<Systemmanager> queryRoleUser(Integer autoId, HttpServletRequest request) {
        return iSystemmanagerService.queryRoleUser(autoId, request);
    }

    @ApiOperation("修改密码")
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/updatePwd")
    private AjaxResult updatePwd(@RequestBody String systemUserPwdParam, HttpServletRequest request) {
        try {
            SystemUserPwdParam currentVo = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(systemUserPwdParam, request), SystemUserPwdParam.class);
            return iSystemmanagerService.updatePwd(currentVo, request);
        } catch (Exception e) {
            log.error("修改密码失败，失败原因："  + Arrays.toString(e.getStackTrace()));
            return AjaxResult.error(Constants.Error_Code_Design, MessageType.MESSAGE_ERROR_CODE_SYSTEM002.getCode(), request.getHeader("Accept-Language"));
        }
    }
}