package com.ebts.generator.controller;

import com.ebts.common.annotation.Log;
import com.ebts.common.core.controller.BaseController;
import com.ebts.common.core.entity.Result;
import com.ebts.common.enums.BusinessType;
import com.ebts.common.utils.poi.ExcelUtil;
import com.ebts.generator.entity.Regular;
import com.ebts.generator.service.RegularService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 校验规则Controller
 *
 * @author binlin
 * @date 2021-01-18
 */
@RestController
@RequestMapping("/tool/regular")
public class RegularController extends BaseController {

    @Autowired
    private RegularService regularService;

    /**
     * 查询校验规则列表
     *
     * @param regular
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('tool:regular:list')")
    @GetMapping("/list")
    public Serializable list(Regular regular) {
        startPage();
        Result<List<Regular>> result = regularService.selectRegularList(regular);
        if (result.isSuccess()) {
            return getDataTable(result.getData());
        } else {
            return result;
        }
    }

    /**
     * 查询校验规则列表
     *
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('tool:regular:option')")
    @GetMapping("/option")
    public Serializable option() {
        startPage();
        Result<List<Regular>> result = regularService.selectRegularOption();
        if (result.isSuccess()) {
            return getDataTable(result.getData());
        } else {
            return result;
        }
    }

    /**
     * 导出校验规则列表
     *
     * @param regular
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('tool:regular:export')")
    @Log(title = "校验规则", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public Result<Regular> export(Regular regular) {
        Result<List<Regular>> result = regularService.selectRegularList(regular);
        ExcelUtil<Regular> util = new ExcelUtil<Regular>(Regular.class);
        if (result.isSuccess()) {
            return util.exportExcel(result.getData(), "regular");
        } else {
            return Result.error(result.getMsg());
        }
    }

    /**
     * 获取校验规则详细信息
     *
     * @param id
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('tool:regular:query')")
    @GetMapping(value = "/{id}")
    public Result<Regular> getInfo(@PathVariable("id") Long id) {
        return regularService.selectRegularById(id);
    }

    /**
     * 新增校验规则
     *
     * @param regular
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('tool:regular:add')")
    @Log(title = "校验规则", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Integer> add(@RequestBody Regular regular) {
        if (!verifyRegular(regular)) {
            return Result.error("正则表达式校验不通过!");
        }
        return regularService.insertRegular(regular);
    }

    /**
     * 修改校验规则
     *
     * @param regular
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('tool:regular:edit')")
    @Log(title = "校验规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Integer> edit(@RequestBody Regular regular) {
        if (!verifyRegular(regular)) {
            return Result.error("正则表达式校验不通过!");
        }
        return regularService.updateRegular(regular);
    }

    /**
     * 删除校验规则
     *
     * @param ids
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('tool:regular:remove')")
    @Log(title = "校验规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable Long[] ids) {
        if (ids.length == 0) {
            return Result.error("需要删除id数不能为空!");
        }
        return regularService.deleteRegularByIds(ids);
    }

    public boolean verifyRegular(Regular regular) {
        return Pattern.matches(regular.getRegular(), regular.getValidation());
    }
}
