package com.jzdsh.auto.app.controller.admin.mvalidator;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.validate.Validator;
import com.jzdsh.auto.model.Owner;

/**
 * @ProjectName: auto
 * @Package: com.jzdsh.auto.app.controller.admin.mvalidator
 * @ClassName: AdminOwnerValidator
 * @Author: Jzdsh
 * @Description:
 * @Date: 2020/3/27 19:17
 * @Version: 1.0
 */
public class AdminOwnerValidator extends Validator {
    @Override
    protected void validate(Controller c) {
        setShortCircuit(true);
        setRet(Ret.fail( ));
        c.getFile("owner.img", "/owner/");
        String title = c.get("owner.title");
        Integer type = c.getInt("owner.typeId");
        String keyWord = c.get("owner.keyWord");
        String sort = c.get("owner.sort");
        String info = c.get("owner.info");
        if (title == null) {
            addError("msg", "车主服务   请填写标题!");
        }
        if (type == null) {
            addError("msg", "车主服务   请选择类型!");
        }
        if (keyWord == null) {
            addError("msg", "车主服务   请输入关键字信息");
        }
        if (info == null || info.length( ) < 20) {
            addError("msg", "车主服务   详情内容太少了点吧");
        }
        if (title.length( ) > 49) {
            addError("msg", "车主服务 标题太长!");
        }
        if (keyWord.length( ) > 49) {
            addError("msg", "车主服务 关键字太长!");
        }
        String reg = "^[0-9]{1,4}$";
        if (!sort.matches(reg)) {
            addError("msg", "车主服务 排序只允许1-4位数字!!");
        }
    }

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