package com.hchyun.test.controller;

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


import com.hchyun.common.constant.ReturnConstants;
import com.hchyun.common.core.controller.HcyBaseController;
import com.hchyun.common.utils.ServerResult;
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;
import com.hchyun.common.annotation.Log;
import com.hchyun.common.core.entity.AjaxResult;
import com.hchyun.common.enums.BusinessType;
import com.hchyun.test.entity.TestTree;
import com.hchyun.test.service.TestTreeService;
import com.hchyun.common.utils.poi.ExcelUtil;

/**
 * 树测试Controller
 *
 * @author hchyun
 * @date 2021-01-23
 */
@RestController
@RequestMapping("/test/tree")
public class TestTreeController extends HcyBaseController {
    protected final Logger logger = LoggerFactory.getLogger(TestTreeController.class);

    @Autowired
    private TestTreeService testTreeService;

    /**
     * 查询树测试列表
     */
    @PreAuthorize("@hchyun.hasPermi('test:tree:list')")
    @GetMapping("/list")
    public AjaxResult list(TestTree testTree) {
        ServerResult<List<TestTree>> serverResult = testTreeService.selectTestTreeList(testTree);
        return AjaxResult.success(serverResult.getData());
    }

    /**
     * 导出树测试列表
     */
    @PreAuthorize("@hchyun.hasPermi('test:tree:export')")
    @Log(title = "树测试", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TestTree testTree) {
        try {
            ServerResult<List<TestTree>> serverResult = testTreeService.selectTestTreeList(testTree);
            ExcelUtil<TestTree> util = new ExcelUtil<TestTree>(TestTree.class);
            if (serverResult.isStart()) {
                return util.exportExcel(serverResult.getData(), "tree");
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 获取树测试详细信息
     */
    @PreAuthorize("@hchyun.hasPermi('test:tree:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        try {
            ServerResult<TestTree> serverResult = testTreeService.selectTestTreeById(id);
            if (serverResult.isStart()) {
                return AjaxResult.success(serverResult.getData());
            } else {
                return AjaxResult.info(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 新增树测试
     */
    @PreAuthorize("@hchyun.hasPermi('test:tree:add')")
    @Log(title = "树测试", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TestTree testTree) {
        if (testTree.getContent() == null || testTree.getContent().equals("")) {
            return AjaxResult.error("内容不能为空!");
        }
        try {
            ServerResult<Integer> serverResult = testTreeService.insertTestTree(testTree);
            if (serverResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 修改树测试
     */
    @PreAuthorize("@hchyun.hasPermi('test:tree:edit')")
    @Log(title = "树测试", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TestTree testTree) {
        try {

            if (testTree.getContent() == null || testTree.getContent().equals("")) {
                return AjaxResult.error("内容不能为空!");
            }
            ServerResult<Integer> serverResult = testTreeService.updateTestTree(testTree);
            if (serverResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 删除树测试
     */
    @PreAuthorize("@hchyun.hasPermi('test:tree:remove')")
    @Log(title = "树测试", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        try {
            if (ids.length < 0) {
                return AjaxResult.error("id不能为空!");
            }
            ServerResult<Integer> serverResult = testTreeService.deleteTestTreeByIds(ids);
            if (serverResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }
}
