package com.winsun.item.modular.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.base.tips.ErrorTip;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.auth.core.util.DateUtil;
import com.winsun.auth.model.user.User;
import com.winsun.bean.HhrUser;
import com.winsun.bean.School;
import com.winsun.bean.SysUser;
import com.winsun.item.core.shiro.ShiroKit;
import com.winsun.item.core.util.AccLoginUtil;
import com.winsun.item.core.util.ResponseEntity;
import com.winsun.item.modular.system.service.IUserService;
import com.winsun.item.util.LoginUtils;
import com.winsun.mapper.HhrUserMapper;
import com.winsun.mapper.SchoolMapper;
import com.winsun.mapper.SysUserMapper;
import com.winsun.smsUtils.SendSmsAndMail;
import com.winsun.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author: chancy
 * @Date: 2020/3/27 16:14
 */
@Slf4j
@RequestMapping("/forgerpw") //
@RestController
public class LoginPwdController extends BaseController {

    private static SysUserMapper sysUserMapper;

    private static IUserService userService;

    private static StringRedisTemplate stringRedisTemplate;

    private static HhrUserMapper hhrUserMapper;

    private static SchoolMapper schoolMapper;

    private static String PATHTYPE = "idCard";

    private static String CODE = "verificationCode";

    private static String ERRCODENAME = "ForgeERRCODENAME";

    private static long CODETIME = 1000 * 60 * 5;

    private static int ERRACCOUNT = 10;

    @Value("${weixin.appid}")
    private String APPID = "";

    @Value("${weixin.appsecret}")
    private String APPSECRET = "";
    //登录验证码标识
    private static String GETPHONECODE = "getPhoneCode";


    @Autowired
    public LoginPwdController(SysUserMapper sysUserMapper, IUserService userService, StringRedisTemplate stringRedisTemplate,
                              HhrUserMapper hhrUserMapper, SchoolMapper schoolMapper) {
        LoginPwdController.sysUserMapper = sysUserMapper;
        LoginPwdController.userService = userService;
        LoginPwdController.hhrUserMapper = hhrUserMapper;
        LoginPwdController.stringRedisTemplate = stringRedisTemplate;
        LoginPwdController.schoolMapper = schoolMapper;
    }

    @RequestMapping(value = "/defaultLogin", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseData<Map<String, Object>> defaultLogin(String code) throws Exception {
        if (StringUtils.isBlank(code)) {
            return ResponseData.error("微信默认授权失效，请退出重新进入授权！");
        }
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?";
        url += "appid=";
        url += APPID;
        url += "&secret=";
        url += APPSECRET;
        url += "&code=" + code + "&grant_type=authorization_code";
        String jsonObject = null;
        try {
            jsonObject = HttpHelper.sendGet(url);
        } catch (Exception e) {
            log.error("微信登录获取用户信息失败" + e.getMessage(), e);
            return ResponseData.error("微信默认授权失效，请退出重新进入授权！");
        }
        if (StringUtils.isBlank(jsonObject)) {
            return ResponseData.error("微信默认授权失效，请退出重新进入授权！");
        }
        if (!jsonObject.contains("openid")) {
            return ResponseData.error("微信默认授权失效，请退出重新进入授权！");
        }
        /*jsonObject = HttpHelper.doGet(url);*/
        Map<String, Object> hashMap = new HashMap<>();
        log.info("defaultLogin:"+jsonObject);
        hashMap.put("info", JSONObject.parseObject(jsonObject));
        /*  Map<String,Object> map = JSON.toJavaObject(jsonObject,Map.class);*/
        Map<String, Object> map = JSON.parseObject(jsonObject, Map.class);
        String openId = map.get("openid").toString();
        Wrapper<HhrUser> wrapper = new EntityWrapper<>();
        wrapper.eq("open_id", openId);
        log.info("openId:" + openId);
        List<HhrUser> hhrUsers = hhrUserMapper.selectList(wrapper);
        if (hhrUsers.size() == 0) {
            return ResponseData.error(hashMap, "当前微信未绑定账号，请先登录绑定账号！");
        }

        SysUser sysUser = new SysUser();
        for(HhrUser hhrUser : hhrUsers) {
            sysUser = sysUserMapper.selectById(hhrUser.getId());
            if(sysUser.getStatus().equals("1")) {
                break;
            }else {
                sysUser = new SysUser();
            }
        }
        if (StringUtils.isBlank(sysUser.getAccount())) {
            return ResponseData.error(hashMap, "当前账号异常！");
        }
        // 信息正确，调用登录
        return this.loginvalid(hashMap, sysUser.getAccount(), openId);
    }


    /**
     * 微信登录-- 前端未调用
     *
     * @param openid
     * @return
     */
    @RequestMapping(value = "/weixinLogin", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseData<Map<String, Object>> weixinLogin(String openid) {
        if (StringUtils.isBlank(openid)) {
            return ResponseData.error("微信授权失效，请重新授权！");
        }
        Wrapper<HhrUser> wrapper = new EntityWrapper<>();
        wrapper.eq("open_id", openid);
        log.info("openId:" + openid);
        List<HhrUser> hhrUsers = hhrUserMapper.selectList(wrapper);
        if (hhrUsers.size() == 0) {
            return ResponseData.error("当前微信未绑定账号，请先绑定账号！");
        }
        HhrUser hhrUser = hhrUsers.get(0);
        SysUser sysUser = sysUserMapper.selectById(hhrUser.getId());
        if (StringUtils.isBlank(sysUser.getAccount())) {
            return ResponseData.error("当前账号异常！");
        }
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("username", sysUser.getAccount());
        hashMap.put("openId", openid);
        return ResponseData.success(hashMap);
    }

    /**
     * 重写微信openId登录方法
     *
     * @param username 用户名
     * @param openId   用户openId
     * @return
     */
    private ResponseData<Map<String, Object>> loginvalid(Map<String, Object> hashMap, String username, String openId) {

        try {
            // 关闭错误登录校验
            stringRedisTemplate.opsForValue().set("SYS_LOGIN_ERROR" + username, "1");
            String password = null;
            ResponseData<String> canLogin = LoginUtils.canLogin(username);
            if (!canLogin.isSuccess()) {
                log.info("统一账号或子统一账号错误登录次数较多，不允许登录；请稍后再试！");
                return ResponseData.error(hashMap, "登录失败，由于账号被锁定，请联系管理员或稍后重试！");
            }
            ResponseData<String> checkUserName = LoginUtils.checkUserName(username);
            if (!checkUserName.isSuccess()) {
                return ResponseData.error(hashMap, checkUserName.getMsg());
            }
            // 获取数据库中的账号密码，准备比对
            User user = userService.getByAccount(username);
            if (null == user) {
                log.info("统一账号或子统一账号在系统中找不到或被冻结！");
                return ResponseData.error(hashMap, "统一账号或子统一账号在系统中找不到或被冻结！");
            }
            // 所有信息正确
            Object object = AccLoginUtil.normalLogin(username, user.getPassword(), openId);
            if (object instanceof ErrorTip) {
                return ResponseData.error(hashMap, "授权登录错误");
            }
            Map<String, Object> map = (Map<String, Object>) object;
            log.info("login result :{}", JSON.toJSONString(object));
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("sessionId", map.get("sessionId"));
            resultMap.put("id", map.get("id"));
            return ResponseData.success(resultMap);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseData.error(hashMap, "内部异常");
        }
    }

    /**
     * 微信解绑
     *
     * @param openid
     * @param username
     * @param verificationCode
     * @return
     */
    @RequestMapping(value = "/weixinUnbundling", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseData<String> weixinUnbundling(String openid, String username, String verificationCode) {
        if (StringUtils.isBlank(openid) || StringUtils.isBlank(username) || StringUtils.isBlank(verificationCode)) {
            return ResponseData.error("解绑失败,微信授权失效或账号验证码为空！");
        }
        String str = stringRedisTemplate.opsForValue().get(username + GETPHONECODE);
        if (StringUtils.isBlank(str)) {
            return ResponseData.error("验证码已失效！");
        }
        if (!str.equals(verificationCode)) {
            return ResponseData.error("验证码错误！");
        }
        Wrapper<SysUser> wrapperU = new EntityWrapper<>();
        wrapperU.eq("account", username).eq("status", "1");;
        List<SysUser> users = sysUserMapper.selectList(wrapperU);
        if (users.size() == 0) {
            return ResponseData.error("当前账号不存在！");
        }
        log.info("openId:" + openid);
        SysUser sysUser = users.get(0);
        Wrapper<HhrUser> wrapper = new EntityWrapper<>();
        wrapper.eq("id", sysUser.getId());
        wrapper.eq("open_id", openid);
        List<HhrUser> hhrUsers = hhrUserMapper.selectList(wrapper);
        if (hhrUsers.size() == 0) {
            return ResponseData.error("当前微信和当前账号不匹配！");
        }
        Map<String, Object> dataMapping = new HashMap<>();
        dataMapping.put("open_id", null);
        Integer integer = hhrUserMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper);
        if (integer == 1) {
            return ResponseData.success("解绑成功！");
        }
        return ResponseData.error("绑定失败！");
    }

    /**
     * 绑定微信
     *
     * @param accessToken
     * @param username
     * @return
     */
    @RequestMapping(value = "bindingAccount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseData<Map<String, Object>> bindingAccount(String openid, String accessToken,
                                                            String username, String verificationCode,
                                                            @RequestParam(value = "remember", defaultValue = "false") boolean remember) {
        log.info(accessToken + ":" + openid + ":" + ":" + username + ":" + verificationCode + ":" + remember);
        if (StringUtils.isBlank(username) || StringUtils.isBlank(verificationCode)) {
            return ResponseData.error("绑定失败，授权失效或账号验证码为空！");
        }
        String str = stringRedisTemplate.opsForValue().get(username + GETPHONECODE);
        if (StringUtils.isBlank(str)) {
            return ResponseData.error("验证码已失效！");
        }
        if (!str.equals(verificationCode)) {
            return ResponseData.error("验证码错误！");
        }
        Wrapper<SysUser> wrapperU = new EntityWrapper<>();
        wrapperU.eq("account", username);
        wrapperU.eq("status",1);
        List<SysUser> users = sysUserMapper.selectList(wrapperU);
        if (users.size() == 0) {
            return ResponseData.error("当前账号有误！");
        }
        SysUser sysUser = users.get(0);
        // 没有openid和accessToken直接登录，不访问微信
        if (StringUtils.isNotBlank(openid) && StringUtils.isNotBlank(accessToken)) {
            log.info("openId:" + openid);
            String url1 = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openid + "&lang=zh_CN";
            String jsonObject1 = null;
            try {
                jsonObject1 = HttpHelper.sendGet(url1);
            } catch (Exception e) {
                log.error("微信登录获取用户信息失败" + e.getMessage(), e);
            }
            if (StringUtils.isBlank(jsonObject1)) {
                return ResponseData.error("微信授权失效，请重新授权！");
            }
            if (!jsonObject1.contains("openid")) {
                return ResponseData.error("微信授权失效，请重新授权！");
            }
            log.info(jsonObject1);
            Map<String, Object> map1 = JSON.parseObject(jsonObject1, Map.class);
            String headimgurl = map1.get("headimgurl").toString();
            String nickname = map1.get("nickname").toString();
            Wrapper<HhrUser> wrapper = new EntityWrapper<>();
            wrapper.eq("id", sysUser.getId());
            Map<String, Object> dataMapping = new HashMap<>();
            // 不绑定微信则不保存openId
            if (remember) {
                dataMapping.put("open_id", openid);
            }

            dataMapping.put("wx_head_img_url", headimgurl);
            try {
                dataMapping.put("wx_nick_name", URLEncoder.encode(nickname, "utf-8"));
            } catch (UnsupportedEncodingException u) {
                return ResponseData.error("微信名转码失败！请暂时更换后登录");
            }

            Integer integer = hhrUserMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper);
            if (integer == 1) {
                return this.loginvalid(null, sysUser.getAccount(), openid);
            }
        } else {
            // 查询openid，模拟微信登录
            HhrUser hhrUser = hhrUserMapper.selectById(sysUser.getId());
            if (hhrUser == null) {
                return ResponseData.error("合伙人信息有误！");
            }

            if (StringUtils.isBlank(hhrUser.getOpenId())) {
                return ResponseData.error("请先在一人一码中绑定微信登录");
            }
            return this.loginvalid(null, sysUser.getAccount(), hhrUser.getOpenId());
        }

        return ResponseData.error("绑定失败！");
    }

    /**
     * 忘记密码发送验证码
     *
     * @param username
     * @param
     * @return
     */
    @RequestMapping(value = "verificationCode", method = RequestMethod.POST)
    public Object phoneLoginCode(@RequestParam("username") String username) {
        EntityWrapper<User> userwrapper = new EntityWrapper<>();
        userwrapper.eq("account", username);
        List<User> users = userService.selectList(userwrapper);
        if (CollectionUtils.isEmpty(users)) {
//            return ResponseEntity.newJSON("code", 400, "message", "密码或者账号错误！");
            return ResponseEntity.newJSON("code", 200, "message", "操作成功！");
        }
        User user = users.get(0);
        //5分钟内有效
        String code = username + CODE;
        String verificationCode = null;

        verificationCode = String.valueOf((int) ((RandomUtil.getSecrityRandom() * 9 + 1) * 100000));


        Long expire = stringRedisTemplate.getExpire(code);
        //验证码 有效时间是五分钟倒计时
        if (expire > (60 * 4)) {
            return ResponseEntity.newJSON("code", 400, "message", "请勿频繁发送手机验证码操作！");
        }
        stringRedisTemplate.opsForValue().set(code, verificationCode, CODETIME, TimeUnit.MILLISECONDS);
        Map<String, Object> sent = new HashMap<>();
        sent.put("code", 400);
        try {
            if (user.getPhone().length() == 11) {
                SendSmsAndMail.sendSms(user.getPhone(), verificationCode, "7");
                sent.put("message", "操作成功");
//                    sent.put("message", "发送成功");
                sent.put("code", 200);
            } else {
                sent.put("message", "当前用户手机号码不合法！");
            }

        } catch (Exception e) {
            log.info("错误信息：{}" + e.getMessage(), e);
            sent.put("message", "手机短信发送失败");
        }

        return sent;
    }

    /**
     * 忘记密码
     *
     * @param account
     * @param newPwd
     * @param rePwd
     * @param
     * @return
     */
    @RequestMapping(value = "changePwd", method = RequestMethod.POST)
    @ResponseBody
    public Object ChangePassword(@RequestParam(value = "account") String account, @RequestParam(value = "verificationCode") String verificationCode,
                                 @RequestParam(value = "newPwd") String newPwd, @RequestParam(value = "rePwd") String rePwd) {
        String code = stringRedisTemplate.opsForValue().get(account + CODE);
        if (StringUtils.isBlank(code)) {
            return ResponseEntity.newJSON("code", 400, "data", "验证码错误！");
        }
        String errcount = stringRedisTemplate.opsForValue().get(ERRCODENAME + account);
        if (StringUtils.isNotBlank(errcount)) {
            if (Integer.valueOf(errcount) == ERRACCOUNT) {
                Long expire = stringRedisTemplate.getExpire(account + CODE);
                if (expire > (60 * 4)) {
                    return ResponseEntity.newJSON("code", 500, "data", "频繁操作");
                } else {
                    stringRedisTemplate.delete(account + CODE);
                    stringRedisTemplate.delete(ERRCODENAME + account);
                    return ResponseEntity.newJSON("code", 500, "data", "重新获取验证码");
                }
            }
        }
        if (!code.equals(verificationCode)) {
            if (StringUtils.isBlank(errcount)) {
                errcount = "1";
                stringRedisTemplate.opsForValue().set(ERRCODENAME + account, errcount);
            } else if (Integer.valueOf(errcount) < ERRACCOUNT) {
                errcount = String.valueOf(Integer.valueOf(errcount) + 1);
                stringRedisTemplate.opsForValue().set(ERRCODENAME + account, errcount);
            }
            return ResponseEntity.newJSON("code", 300, "data", "手机验证码第" + errcount + "次不正确");
        } else if (StringUtils.isNotBlank(errcount)) {
            stringRedisTemplate.delete(ERRCODENAME + account);
        }
        // 对加密的密码进行解密处理
        ResponseData<String> pwdDecrypt = LoginUtils.pwdDecrypt(newPwd);
        if (!pwdDecrypt.isSuccess()) {
            return ResponseEntity.newJSON("code", 400, "data", "密码或者账号错误！");
        }
        newPwd = pwdDecrypt.getData();
        String pattern = "^(?![A-Za-z]+$)(?![A-Z\\d]+$)(?![A-Z\\W]+$)(?![a-z\\d]+$)(?![a-z\\W]+$)(?![\\d\\W]+$)\\S{8,}$";
        if (!newPwd.matches(pattern)) {
            return ResponseEntity.newJSON("code", 400, "data", "密码强制等级低，必须要数字、小写英文字母、大写英文字母或特殊字符混合组成，长度至少8位！");
        }
        if (newPwd.contains(account)) {
            return ResponseEntity.newJSON("code", 400, "data", "新密码与账号信息吻合，禁止使用！");
        }
        ResponseData<String> pwdDecryptre = LoginUtils.pwdDecrypt(rePwd);
        if (!pwdDecryptre.isSuccess()) {
            return ResponseEntity.newJSON("code", 400, "data", "操作失败！");
//            return ResponseEntity.newJSON("code", 400, "data", "密码或者账号错误！");
        }
        rePwd = pwdDecryptre.getData();
        if (!newPwd.equals(rePwd)) {
            return ResponseEntity.newJSON("code", 400, "data", "两次密码不一致！");
        }
        Wrapper<SysUser> wrapper = new EntityWrapper<>();
        wrapper.eq("account", account).eq("status", "1");
        List<SysUser> sysUsers = sysUserMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(sysUsers)) {
            return ResponseEntity.newJSON("code", 400, "data", "操作失败！");
//            return ResponseEntity.newJSON("code", 400, "data", "账号不存在,请重新输入！");
        }
        SysUser sysUser = sysUsers.get(0);
        String[] split = null;
        if (StringUtils.isNotBlank(sysUser.getOldPassward())) {
            split = sysUser.getOldPassward().split(",");
        } else {
            String oldMd5 = sysUser.getPassword();
            split = new String[]{oldMd5};
        }
        String newMd5 = ShiroKit.md5(newPwd, sysUser.getSalt());
        for (String pwd : split) {
            if (newMd5.equals(pwd)) {
                return ResponseEntity.newJSON("code", 400, "data", "新密码不得与最近两次密码相同！");
            }
        }
        Map<String, Object> dataMapping = new HashMap<>();
        dataMapping.put("password", newMd5);
        dataMapping.put("updateTime", new Date());
        if (split.length == 2) {
            dataMapping.put("old_passward", split[1] + "," + newMd5);
        } else {
            dataMapping.put("old_passward", split[0] + "," + newMd5);
        }
        Integer integer = sysUserMapper.updateForSet(com.winsun.auth.core.util.MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper);
        if (integer > 0) {
            return ResponseEntity.newJSON("code", 200, "data", "修改成功！");
        }
        return ResponseEntity.newJSON("code", 300, "data", "修改失败！");
    }

    @RequestMapping(value = "selectAddress", method = RequestMethod.POST)
    public ResponseData<List<String>> selectAddress() {
        return ResponseData.success(schoolMapper.selectAdress());
    }

    @RequestMapping(value = "selectSchool", method = RequestMethod.POST)
    public ResponseData<List<Map<String, Object>>> selectSchool(String subName) {
        Wrapper<School> wrapper = new EntityWrapper<>();
        wrapper.eq("sub_name", subName);
        wrapper.setSqlSelect("id", "school_name");
        List<Map<String, Object>> list = schoolMapper.selectMaps(wrapper);
        return ResponseData.success(list);
    }

    /**
     * 打开无人厅
     *
     * @return
     */
    @PostMapping("openHall")
    public ResponseData<String> openHall() {

        // 查询当前合伙人学校，合伙人添加appId字段
        ShiroUser user = ShiroKit.getUser();
        if (user == null || user.getId() == null || user.getId() == 0) {
            return ResponseData.error("当前登录用户信息有误，请重新登录。");
        }
        List<Map<String, Object>> schools = hhrUserMapper.selectHehuorenSchool(user.getId().toString());
        if (schools == null || schools.size() == 0) {
            return ResponseData.error("当前合伙人未设置学校");
        }
        if (schools.size() > 1) {
            return ResponseData.error("当前合伙人学校配置有误，请重新配置");
        }
        // 判断接入号终端是否存在
        Map<String, Object> school = schools.get(0);
        if (school.get("app_id") == null || school.get("app_key") == null
                || StringUtils.isBlank(school.get("app_id").toString())
                || StringUtils.isBlank(school.get("app_key").toString())) {
            return ResponseData.error("当前学校未配置校园随身厅。");
        }
        if (school.get("lzgh") == null || StringUtils.isBlank(school.get("lzgh").toString())) {
            return ResponseData.error("当前学校未配置揽装工号。");
        }

        String appId = school.get("app_id").toString();
        String appKey = school.get("app_key").toString();

        Map params = new HashMap();
        params.put("r", RandomUtil.ramdomNum(10));

        params.put("20210114_1", appId);

        // 组装请求信息
        String key2 = appKey.substring(0, 24);
        Map<String, Object> paramsMap = new HashMap<>();

        paramsMap.put("apph5", "1"); //APPH5标识 固定：1
        paramsMap.put("appid", appId); // 终端编码
        paramsMap.put("apph5Type", "1"); // 入口类型  网迅固定：1
        paramsMap.put("lzstaffcity", ""); // 揽装人县分
        paramsMap.put("lzstaffmsc", ""); // 揽装人营服
        paramsMap.put("lzstaffname", ""); // 揽装人姓名
        paramsMap.put("lzstaffno", school.get("lzgh")); // 揽装工号
        paramsMap.put("lzstaffphone", ""); // 揽装人手机
        paramsMap.put("meid", ""); // 终端meid
        paramsMap.put("sgstaffno", ""); // 施工工号
        paramsMap.put("timestamp", new Date().getTime()); // 时间戳
        paramsMap.put("username", appId + user.getAccount()); // 登录标识
        paramsMap.put("xxstaffno1", ""); // 协销工号1
        paramsMap.put("xxstaffno2", ""); // 协销工号2

        List<Map.Entry<String, Object>> list = new ArrayList<>(paramsMap.entrySet());

        // 对所有传入参数按照字段名的 ASCII 码从小到大排序（字典序）
        Collections.sort(list, new Comparator<Map.Entry<String, Object>>() {
            public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
                return (o1.getKey()).toString().compareTo(o2.getKey());
            }
        });

        log.info("获取的参数名和参数值：" + JSONObject.toJSONString(list));

        // 获取map已有值
        StringBuilder values = new StringBuilder();
        for (Map.Entry<String, Object> map : list) {
            values.append(map.getValue());
        }

        // 拼接key2
        values.append(appKey);
        log.info("升序拼接参数值字符串：" + values.toString());
        // md5加密
        paramsMap.put("sign", MD5Utils.md5(values.toString())); // 签名

        log.info("接口入参明文对象：" + JSONObject.toJSONString(paramsMap));

        // 组装参数字符串
        String interfaceParams = new String();
        Iterator<Map.Entry<String, Object>> mapIterator = paramsMap.entrySet().iterator();
        while (mapIterator.hasNext()) {
            Map.Entry<String, Object> next = mapIterator.next();
            interfaceParams += (next.getKey() + "=" + next.getValue() + "&");
        }
        if (interfaceParams.lastIndexOf("&") == (interfaceParams.length() - 1)) {
            interfaceParams = interfaceParams.substring(0, (interfaceParams.lastIndexOf("&")));
        }

        log.info("接口入参明文字符串：" + interfaceParams);

        // 3DES加密
        byte[] eBy = EThreeDES.encryptMode(key2.getBytes(), interfaceParams.getBytes());
        String eBase64 = "";
        try {
            eBase64 = EThreeDES.enBase64(eBy);
        } catch (IOException e) {
            log.error("3DES加密失败");
            e.printStackTrace();
            return ResponseData.error("数据加密失败");
        }

        log.info("3DES加密参数：" + eBase64);

        // URL编码
        String urlEncode = "";
        try {
            urlEncode = URLEncoder.encode(eBase64, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("url编码失败");
            e.printStackTrace();
            return ResponseData.error("url编码失败");
        }

        //发送请求
        params.put("20210114_8", urlEncode);

        log.info("最终请求参数：" + JSONObject.toJSONString(params));

        StringBuilder url = new StringBuilder("http://121.8.177.157:8443/UstpCustWeb/ustpSstAccess?");

        url.append("r" + "=" + params.get("r") + "&");
        url.append("20210114_1" + "=" + params.get("20210114_1") + "&");
        url.append("20210114_8" + "=" + params.get("20210114_8"));

        log.info("完整url：" + url);

        return ResponseData.success(url.toString(), "打开成功！");
    }

    @RequestMapping(value = "registeredAccount", method = RequestMethod.POST)
    public ResponseData<String> registeredAccount(@RequestParam("gender") String gender, @RequestParam("username") String username, @RequestParam("phone") String phone,
                                                  @RequestParam("idCard") String idCard, @RequestParam(value = "stuCard") String stuCard, @RequestParam("idcardZ") String idcardZ,
                                                  @RequestParam("password") String password, @RequestParam("cpassword") String cpassword, @RequestParam("school") String school,
                                                  @RequestParam(value = "classZ") String classZ, @RequestParam("substName") String substName, @RequestParam("account") String account) throws NoSuchAlgorithmException, NoSuchAlgorithmException {
        if (StringUtils.isBlank(gender)) {
            return ResponseData.error("性别不能为空！");
        }
        if (StringUtils.isBlank(username)) {
            return ResponseData.error("姓名不能为空！");
        }
        if (StringUtils.isBlank(phone)) {
            return ResponseData.error("电话不能为空！");
        }
        if (phone.length() != 11) {
            return ResponseData.error("手机号码有误！");
        }
        if (StringUtils.isBlank(idCard)) {
            return ResponseData.error("身份证号码不能为空！");
        }
//        if (!IDCardUtil.IDCardValidate(idCard)) {
//            return ResponseData.error("身份证号码有误！");
//        }
        if (StringUtils.isBlank(stuCard)) {
            return ResponseData.error("请上传学生证！");
        }
        if (StringUtils.isBlank(idcardZ)) {
            return ResponseData.error("请上传身份证！");
        }
        if (StringUtils.isBlank(password)) {
            return ResponseData.error("密码不能为空！");
        }
        if (StringUtils.isBlank(cpassword)) {
            return ResponseData.error("原密码不能为空！");
        }
        if (StringUtils.isBlank(school)) {
            return ResponseData.error("学校不能为空！");
        }
        if (StringUtils.isBlank(substName)) {
            return ResponseData.error("县分不能为空！");
        }
        if (StringUtils.isBlank(classZ)) {
            return ResponseData.error("年级不能为空！");
        }
        if (StringUtils.isBlank(account)) {
            return ResponseData.error("账号不能为空！");
        }
        String pattern = "^(?![A-Za-z]+$)(?![A-Z\\d]+$)(?![A-Z\\W]+$)(?![a-z\\d]+$)(?![a-z\\W]+$)(?![\\d\\W]+$)\\S{8,}$";
        if (!password.matches(pattern)) {
            return ResponseData.error("密码强制等级低，必须要数字、小写英文字母、大写英文字母或特殊字符混合组成，长度至少8位！");
        }
        if (password.contains(account)) {
            return ResponseData.error("密码与账号信息吻合，禁止使用！");
        }
        // 对加密的密码进行解密处理
        /*ResponseData<String> pwdDecrypt = LoginUtils.pwdDecrypt(password);
        if (!pwdDecrypt.isSuccess()) {
            return  ResponseData.error("密码错误！");
        }
        password = pwdDecrypt.getData();
        ResponseData<String> pwdDecryptc = LoginUtils.pwdDecrypt(cpassword);
        if (!pwdDecryptc.isSuccess()) {
            return  ResponseData.error("密码错误！");
        }
        cpassword = pwdDecryptc.getData();*/
        if (!password.equals(cpassword)) {
            return ResponseData.error("密码和原密码不同！");
        }
        Wrapper<SysUser> wrapper = new EntityWrapper<>();
        wrapper.eq("account", account);
        wrapper.ne("status", "3");
        List<SysUser> users = sysUserMapper.selectList(wrapper);
        if (users.size() != 0) {
            return ResponseData.error("账号已被使用，请联系管理员！");
        }

        Wrapper<HhrUser> hhrWrapper = new EntityWrapper<HhrUser>();
        hhrWrapper.eq("id_card", idCard);
        List<HhrUser> hhrList = hhrUserMapper.selectList(hhrWrapper);
        if(hhrList.size() > 0) {
            for(HhrUser hhrUser : hhrList) {
                SysUser sysUser = sysUserMapper.selectById(hhrUser.getId());
                if(!sysUser.getStatus().equals("3")) {
                    // 判断剔除已删除的用户
                    return ResponseData.error("身份证已存在");
                }
            }
        }

        int insert = 0;
        SysUser sysUser = new SysUser();
        sysUser.setSex(gender);
        sysUser.setAccount(account);
        sysUser.setName(username);
        sysUser.setPhone(phone);
        sysUser.setSalt(ShiroKit.getRandomSalt(5));
        sysUser.setSubstName(substName);
        sysUser.setRoleid("243"); // 合伙人
        sysUser.setDeptid("24"); // 广州分公司
        sysUser.setCreatetime(new Date());
        sysUser.setStatus("4");
        sysUser.setPassword(ShiroKit.md5(password, sysUser.getSalt()));
        sysUser.setOldPassward(ShiroKit.md5(password, sysUser.getSalt()));
        insert += sysUserMapper.insert(sysUser);
        insert += sysUserMapper.insertUserSchool(sysUser.getId(), school);
        HhrUser hhrUser = new HhrUser();
        hhrUser.setId(sysUser.getId());
        hhrUser.setIdCard(idCard);
        hhrUser.setIdcardImgUrl(PicturesUtil.uploadPictures(idcardZ, sysUser.getId(), PATHTYPE));
        hhrUser.setStudentImgUrl(PicturesUtil.uploadPictures(stuCard, sysUser.getId(), PATHTYPE));
        hhrUser.setGrade(classZ);
        hhrUser.setUploadState(1);
        hhrUser.setFxs("");
        insert += hhrUserMapper.insert(hhrUser);
        if (insert == 3) {
            return ResponseData.success("账号创建成功，待管理员审核！");
        }
        if (insert == 0) {
            return ResponseData.error("账号创建失败！");
        }
        return ResponseData.error("账号创建异常，请联系管理员！");
    }

}
