
package com.lideeyunji.core.framework.config.validate;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lideeyunji.core.framework.entity.ReportEntity;
import com.lideeyunji.core.framework.service.IReportService;
import com.lideeyunji.core.framework.utils.FuncWeb;
import com.lideeyunji.tool.framework.exception.lideeYunJiException;
import com.lideeyunji.tool.framework.yunji.adapter.IlideeYunjiValidate;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;
import com.lideeyunji.tool.framework.yunji.utils.lideeYunJiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 复制时
 * 校验表名是否合法
 */
@Component
public class ReportCopyValidate implements IlideeYunjiValidate {

    @Lazy
    @Autowired
    private IReportService reportService;

    @Override
    public void validate(HttpServletRequest req) throws lideeYunJiException {
        Map<String, Object> parameterMap = FuncWeb.getParameterMap(req);
        String reportCode = lideeYunJiUtils.getMap2Str(parameterMap, "reportCode");

        if (FuncBase.isEmpty(reportCode)) {
            throw new lideeYunJiException("报表编号不允许为空");
        }

        LambdaQueryWrapper<ReportEntity> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ReportEntity::getReportCode,reportCode);
        long count = reportService.count(wrapper);

        //校验表名是否重复
        if (count>0) {
            String errorStr = String.format("报表编号【%s】已存在", reportCode);
            throw new lideeYunJiException(errorStr);
        }

    }
}
