package com.ebts.generator.controller;

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

import com.ebts.common.annotation.Log;
import com.ebts.common.core.controller.BaseController;
import com.ebts.common.core.entity.AjaxResult;
import com.ebts.common.enums.BusinessType;
import com.ebts.common.utils.ServerResult;
import com.ebts.common.utils.poi.ExcelUtil;
import com.ebts.generator.entity.Regular;
import com.ebts.generator.service.RegularService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 校验规则Controller
 *
 * @author binlin
 * @date 2021-01-18
 */
@RestController
@RequestMapping("/tool/regular")
public class RegularController extends BaseController {
    protected final Logger logger = LoggerFactory.getLogger(RegularController.class);

    @Autowired
    private RegularService regularService;

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

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

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

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

    /**
     * 新增校验规则
     *
     * @param regular
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('tool:regular:add')")
    @Log(title = "校验规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Regular regular) {
        if (regular.getName() == null || regular.getName().equals("")) {
            return AjaxResult.error("正则名称不能为空!");
        }
        if (regular.getRegular() == null || regular.getRegular().equals("")) {
            return AjaxResult.error("正则内容不能为空!");
        }
        if (regular.getEnable() == null || regular.getEnable() < 0) {
            return AjaxResult.error("是否启用不能为空!");
        }
        if (!verifyRegular(regular)) {
            return AjaxResult.error("正则表达式校验不通过!");
        }
        ServerResult<Integer> ServerResult = regularService.insertRegular(regular);
        if (ServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(ServerResult.getMsg());
        }
    }

    /**
     * 修改校验规则
     *
     * @param regular
     * @return
     */
    @PreAuthorize("@ebts.hasPermi('tool:regular:edit')")
    @Log(title = "校验规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Regular regular) {
        if (regular.getName() == null || regular.getName().equals("")) {
            return AjaxResult.error("正则名称不能为空!");
        }
        if (regular.getRegular() == null || regular.getRegular().equals("")) {
            return AjaxResult.error("正则内容不能为空!");
        }
        if (regular.getEnable() == null || regular.getEnable() < 0) {
            return AjaxResult.error("是否启用不能为空!");
        }
        if (!verifyRegular(regular)) {
            return AjaxResult.error("正则表达式校验不通过!");
        }
        ServerResult<Integer> ServerResult = regularService.updateRegular(regular);
        if (ServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(ServerResult.getMsg());
        }
    }

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

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