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.AssFather;
import com.hchyun.test.service.AssFatherService;
import com.hchyun.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;

/**
 * 关联查询父Controller
 *
 * @author hchyun
 * @date 2021-02-10
 */

@Api(value = "关联查询父管理", tags = "关联查询父管理")
@RestController
@RequestMapping("/test/father")
public class AssFatherController extends HcyBaseController {
    protected final Logger logger = LoggerFactory.getLogger(AssFatherController.class);

    @Autowired
    private AssFatherService assFatherService;

    /**
     * 查询关联查询父列表
     */
    @ApiOperation("查询关联查询父列表")
    @PreAuthorize("@hchyun.hasPermi('test:father:list')")
    @PutMapping("/list")
    public Serializable list(@Validated @RequestBody AssFather assFather) {
        try {
            startPage(assFather.getParams());
            ServerResult<List<AssFather>> serverResult = assFatherService.selectAssFatherList(assFather);
            if (serverResult.isStart()) {
                return getDataTable(serverResult.getData());
            } else {
                return AjaxResult.info(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 导出关联查询父列表
     */
    @ApiOperation("导出关联查询父列表")
    @PreAuthorize("@hchyun.hasPermi('test:father:export')")
    @Log(title = "关联查询父", businessType = BusinessType.EXPORT)
    @PutMapping("/export")
    public AjaxResult export(@Validated @RequestBody AssFather assFather) {
        try {
            ServerResult<List<AssFather>> serverResult = assFatherService.selectAssFatherList(assFather);
            ExcelUtil<AssFather> util = new ExcelUtil<AssFather>(AssFather.class);
            if (serverResult.isStart()) {
                return util.exportExcel(serverResult.getData(), "father");
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 获取关联查询父详细信息
     */
    @ApiOperation("获取关联查询父详细信息")
    @ApiImplicitParam(name = "id", value = "关联查询父id", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@hchyun.hasPermi('test:father:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        try {
            ServerResult<AssFather> serverResult = assFatherService.selectAssFatherById(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);
        }
    }

    /**
     * 新增关联查询父
     */
    @ApiOperation("新增关联查询父")
    @ApiImplicitParam(name = "assFather", value = "新增关联查询父信息", dataType = "AssFather")
    @PreAuthorize("@hchyun.hasPermi('test:father:add')")
    @Log(title = "关联查询父", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssFather assFather) {
        if (assFather.getName() == null || assFather.getName().equals("")) {
            return AjaxResult.error("名称不能为空!");
        }
        if (assFather.getInfo() == null || assFather.getInfo().equals("")) {
            return AjaxResult.error("信息不能为空!");
        }
        try {
            ServerResult<Integer> serverResult = assFatherService.insertAssFather(assFather);
            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);
        }
    }

    /**
     * 修改关联查询父
     */
    @ApiOperation("修改关联查询父")
    @ApiImplicitParam(name = "assFather", value = "修改关联查询父信息", dataType = "AssFather")
    @PreAuthorize("@hchyun.hasPermi('test:father:edit')")
    @Log(title = "关联查询父", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssFather assFather) {
        try {

            if (assFather.getName() == null || assFather.getName().equals("")) {
                return AjaxResult.error("名称不能为空!");
            }
            if (assFather.getInfo() == null || assFather.getInfo().equals("")) {
                return AjaxResult.error("信息不能为空!");
            }
            ServerResult<Integer> serverResult = assFatherService.updateAssFather(assFather);
            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);
        }
    }

    /**
     * 删除关联查询父
     */
    @ApiOperation("删除关联查询父")
    @ApiImplicitParam(name = "ids", value = "关联查询父ids", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@hchyun.hasPermi('test:father: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 = assFatherService.deleteAssFatherByIds(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);
        }
    }
}
