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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
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.RelA;
import com.hchyun.test.service.RelAService;
import com.hchyun.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;

/**
 * 测试aController
 * 
 * @author hchyun
 * @date 2021-02-26
 */

@Api(value = "测试a管理",tags = "测试a管理")
@RestController
@RequestMapping("/test/a")
public class RelAController extends HcyBaseController {
    protected final Logger logger = LoggerFactory.getLogger(RelAController.class);

    @Autowired
    private RelAService relAService;

    /**
     * 查询测试a列表
     */
    @ApiOperation("查询测试a列表")
    @PreAuthorize("@hchyun.hasPermi('test:a:list')")
    @PutMapping("/list")
    public Serializable list(@Validated @RequestBody RelA relA) {
        try {
            startPage(relA.getPageInfo());
            ServerResult<List<RelA>> genServerResult = relAService.selectRelAList(relA);
            if (genServerResult.isStart()) {
                return getDataTable(genServerResult.getData());
            } else {
                return AjaxResult.info(genServerResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 导出测试a列表
     */
    @ApiOperation("导出测试a列表")
    @PreAuthorize("@hchyun.hasPermi('test:a:export')")
    @Log(title = "测试a", businessType = BusinessType.EXPORT)
    @PutMapping("/export")
    public AjaxResult export(@Validated @RequestBody RelA relA) {
        try {
            ServerResult<List<RelA>> genServerResult = relAService.selectRelAList(relA);
            ExcelUtil<RelA> util = new ExcelUtil<RelA>(RelA. class);
            if (genServerResult.isStart()) {
                return util.exportExcel(genServerResult.getData(), "a");
            } else {
                return AjaxResult.error(genServerResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 获取测试a详细信息
     */
    @ApiOperation("获取测试a详细信息")
    @ApiImplicitParam(name = "aid" , value = "测试aaid" , required = true, dataType = "Long" , paramType = "path")
    @PreAuthorize("@hchyun.hasPermi('test:a:query')")
    @GetMapping(value = "/{aid}")
    public AjaxResult getInfo(@PathVariable("aid") Long aid) {
        try {
            ServerResult<RelA> genServerResult = relAService.selectRelAById(aid);
            if (genServerResult.isStart()) {
                return AjaxResult.success(genServerResult.getData());
            } else {
                return AjaxResult.info(genServerResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 新增测试a
     */
    @ApiOperation("新增测试a")
    @ApiImplicitParam(name = "relA" , value = "新增测试a信息" , dataType = "RelA")
    @PreAuthorize("@hchyun.hasPermi('test:a:add')")
    @Log(title = "测试a", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RelA relA) {
        try {
            ServerResult<Integer> genServerResult = relAService.insertRelA(relA);
            if (genServerResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(genServerResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 修改测试a
     */
    @ApiOperation("修改测试a")
    @ApiImplicitParam(name = "relA" , value = "修改测试a信息" , dataType = "RelA")
    @PreAuthorize("@hchyun.hasPermi('test:a:edit')")
    @Log(title = "测试a", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RelA relA) {
        try {

            ServerResult<Integer> genServerResult = relAService.updateRelA(relA);
            if (genServerResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(genServerResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 删除测试a
     */
    @ApiOperation("删除测试a")
    @ApiImplicitParam(name = "aids" , value = "测试aaids" , required = true, dataType = "Long" , paramType = "path")
    @PreAuthorize("@hchyun.hasPermi('test:a:remove')")
    @Log(title = "测试a", businessType = BusinessType.DELETE)
	@DeleteMapping("/{aids}")
    public AjaxResult remove(@PathVariable Long[] aids) {
        try {
            if (aids.length<0){
                return AjaxResult.error("id不能为空!");
            }
            ServerResult<Integer> genServerResult = relAService.deleteRelAByIds(aids);
            if (genServerResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(genServerResult.getMsg());
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }
}
