package com.ag.demo.service;

import cn.hutool.core.collection.CollUtil;
import com.ag.base.DefaultBussniessImpl;
import com.ag.demo.entity.UserPwdConfig;
import com.ag.util.PwdCheckUtil;
import com.ag.util.StrUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Service
public class PwdCheckService {

    @Autowired
    private DefaultBussniessImpl bizDao;

    public void execute(JSONObject param, HttpServletResponse response, HttpServletRequest request){
        String pwd = param.getString("pwd");
        if (StrUtil.equals(pwd, "******")){
            return;
        }
        List<UserPwdConfig> configList = bizDao.findAll(UserPwdConfig.class);
        if (CollUtil.isEmpty(configList)){
            return;
        }
        UserPwdConfig config = configList.get(0);
        if (config.getPwdLength() > 0){
            Assert.isTrue(pwd.length() >= config.getPwdLength(), "密码长度不得小于"+config.getPwdLength()+"位");
        }
        if (config.getContainsNum() == 1){
            Assert.isTrue(PwdCheckUtil.isContainsNum(pwd), "密码必须包含数字!");
        }
        if (config.getContainsUppchar() == 1){
            Assert.isTrue(PwdCheckUtil.isContainsUppChar(pwd), "密码必须包含大写字符!");
        }
        if (config.getContainsLowchar() == 1){
            Assert.isTrue(PwdCheckUtil.isContainsLowChar(pwd), "密码必须包含小写字符!");
        }
        if (config.getContainsSpechar() == 1){
            Assert.isTrue(PwdCheckUtil.isContainsSpeChar(pwd), "密码必须包含特殊字符!");
        }

    }
}
