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

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsun.auth.core.annotion.Permission;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.bean.SysUser;
import com.winsun.item.core.shiro.ShiroKit;
import com.winsun.item.core.util.ResponseEntity;
import com.winsun.mapper.AppMapper;
import com.winsun.mapper.HhrUserMapper;
import com.winsun.mapper.SysUserMapper;
import com.winsun.utils.MyBatisPlusUpdateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: hsl
 * @Date: 2020/4/24 9:28
 */
@RequestMapping("Personal")
@RestController
@Slf4j
public class PersonalController extends BaseController {

    private static HhrUserMapper hhrUserMapper;

    private static SysUserMapper sysUserMapper;

    private static AppMapper appMapper;

    private static StringRedisTemplate stringRedisTemplate;

    private String EDITPWD = "EditPwd";

    @Autowired
    public PersonalController(SysUserMapper sysUserMapper, HhrUserMapper hhrUserMapper, AppMapper appMapper, StringRedisTemplate stringRedisTemplate) {
        PersonalController.sysUserMapper = sysUserMapper;
        PersonalController.hhrUserMapper = hhrUserMapper;
        PersonalController.appMapper = appMapper;
        PersonalController.stringRedisTemplate = stringRedisTemplate;
    }

    @RequestMapping(value = "editPwdSent", method = RequestMethod.POST)
    public ResponseData<String> editPwdSent(String partner, String verificationCode, String NPwd, String RPwd) {
        if(!partner.equals(String.valueOf(ShiroKit.getUser().getId()))){
            return ResponseData.error("当前登录账户和修改账号冲突！");
        }
        if(!NPwd.equals(RPwd)){
            return ResponseData.error("两次密码输入不一致！");
        }
        String key = ShiroKit.getUser().getAccount() + EDITPWD;
        String code = stringRedisTemplate.opsForValue().get(key);
        if(!verificationCode.equals(code)){
            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 (!NPwd.matches(pattern)) {
            return ResponseData.error("密码强制等级低，必须要数字、小写英文字母、大写英文字母或特殊字符混合组成，长度至少8位！");
        }
        if (NPwd.contains(ShiroKit.getUser().getAccount())) {
            return  ResponseData.error("密码与账号信息吻合，禁止使用！");
        }
        SysUser sysUser = sysUserMapper.selectById(ShiroKit.getUser().getId());
        if(sysUser == null){
            return  ResponseData.error("修改账号不存在！");
        }
        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(NPwd, sysUser.getSalt());
        for (String pwd : split) {
            if (newMd5.equals(pwd)) {
                return  ResponseData.error("新密码不得与最近两次密码相同！");
            }
        }
        Wrapper<SysUser> wrapper = new EntityWrapper<>();
        wrapper.eq("id",ShiroKit.getUser().getId());
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("password", newMd5);
        dataMap.put("updateTime",new Date());
        if (split.length == 2) {
            dataMap.put("old_passward", split[1] + "," + newMd5);
        } else {
            dataMap.put("old_passward", split[0] + "," + newMd5);
        }
        Integer integer = sysUserMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMap), wrapper);
        if(integer == 1){
            return ResponseData.success("修改成功！");
        }else {
            return ResponseData.error("修改失败！");
        }
    }

    @RequestMapping(value = "editPwdOPwd", method = RequestMethod.POST)
    public ResponseData<String> editPwdOPwd(String partner,String NPwd,String  OPwd,String RPwd) {
        if(!partner.equals(String.valueOf(ShiroKit.getUser().getId()))){
            return ResponseData.error("当前登录账户和修改账号冲突！");
        }
        if(!NPwd.equals(RPwd)){
            return ResponseData.error("密码和确认密码不同！");
        }
        SysUser sysUser = sysUserMapper.selectById(ShiroKit.getUser().getId());
        if(sysUser == null){
            return  ResponseData.error("修改账号不存在！");
        }
        if(!sysUser.getPassword().equals(ShiroKit.md5(OPwd,sysUser.getSalt()))){
            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 (!NPwd.matches(pattern)) {
            return ResponseData.error("密码强制等级低，必须要数字、小写英文字母、大写英文字母或特殊字符混合组成，长度至少8位！");
        }
        if (NPwd.contains(ShiroKit.getUser().getAccount())) {
            return  ResponseData.error("密码与账号信息吻合，禁止使用！");
        }
        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(NPwd, sysUser.getSalt());
        for (String pwd : split) {
            if (newMd5.equals(pwd)) {
                return  ResponseData.error("新密码不得与最近两次密码相同！");
            }
        }
        Wrapper<SysUser> wrapper = new EntityWrapper<>();
        wrapper.eq("id",ShiroKit.getUser().getId());
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("password", newMd5);
        dataMap.put("updateTime",new Date());
        if (split.length == 2) {
            dataMap.put("old_passward", split[1] + "," + newMd5);
        } else {
            dataMap.put("old_passward", split[0] + "," + newMd5);
        }
        Integer integer = sysUserMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMap), wrapper);
        if(integer == 1){
            return ResponseData.success("修改成功！");
        }else {
            return ResponseData.error("修改失败！");
        }
    }



}
