package com.jfinal.club._admin.admins;

import com.jfinal.club.common.model.Admins;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.validate.Validator;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @descriptions:
 * @author: lc
 * @date: 2020-06-30 17:53:21
 * @Version: 1.0
 **/
public class AdminsValidator extends Validator {


    @Override
    protected void validate(Controller c) {
        setShortCircuit(true);
        setRet(Ret.fail( ));
        String username = c.get("adm.username");
        String id = c.get("adm.id");

        if(StringUtils.isBlank(username)){
            addError("msg","用户名不能为空!");
        }

        List< Admins > admins = Admins.dao.find("select * from admins where id != ? and username = ?",id,username.trim( ));
        if(admins != null && admins.size() > 0){
            addError("msg","该用户名已存在,请更换用户名!");
        }

        if(username.length() > 20){
            addError("msg","用户名太长,请设置20字符以内！");
        }
    }

    @Override
    protected void handleError(Controller c) {
        c.renderJson(getRet( ));
    }
}
