package com.hchyun.generator.controller;

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

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.generator.entity.Apiclass;
import com.hchyun.generator.service.ApiclassService;
import com.hchyun.common.utils.poi.ExcelUtil;

/**
 * 接口类名Controller
 * 
 * @author hchyun
 * @date 2021-01-24
 */
@RestController
@RequestMapping("/tool/apiclass")
public class ApiclassController extends HcyBaseController {
    protected final Logger logger = LoggerFactory.getLogger(ApiclassController.class);

    @Autowired
    private ApiclassService apiclassService;


    /**
     * 获取到api类的select选项
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi('tool:apiclass:querylist')")
    @GetMapping("querylist")
    public AjaxResult queryList(){
        try {
            ServerResult<List<Apiclass>> serverResult = apiclassService.selectApiclassList(new Apiclass());
            if (serverResult.isStart()){
                return AjaxResult.success(serverResult.getData());
            }else {
                return AjaxResult.error(serverResult.getMsg());
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 查询接口类名列表
     */
    @PreAuthorize("@ss.hasPermi('tool:apiclass:list')")
    @GetMapping("/list")
    public Serializable list(Apiclass apiclass) {
        try {
            startPage();
            ServerResult<List<Apiclass>> serverResult = apiclassService.selectApiclassList(apiclass);
            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);
        }
    }

    /**
     * 导出接口类名列表
     */
    @PreAuthorize("@ss.hasPermi('tool:apiclass:export')")
    @Log(title = "接口类名", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Apiclass apiclass) {
        try {
            ServerResult<List<Apiclass>> serverResult = apiclassService.selectApiclassList(apiclass);
            ExcelUtil<Apiclass> util = new ExcelUtil<Apiclass>(Apiclass. class);
            if (serverResult.isStart()) {
                return util.exportExcel(serverResult.getData(), "apiclass");
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 获取接口类名详细信息
     */
    @PreAuthorize("@ss.hasPermi('tool:apiclass:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        try {
            ServerResult<Apiclass> serverResult = apiclassService.selectApiclassById(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("@ss.hasPermi('tool:apiclass:add')")
    @Log(title = "接口类名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Apiclass apiclass) {
        if (apiclass.getmId() == null || apiclass.getmId()<0) {
            return AjaxResult.error("模块id不能为空!");
        }
        if (apiclass.getcName() == null || apiclass.getcName().equals("")) {
            return AjaxResult.error("类名不能为空!");
        }
        if (apiclass.getcDescribe() == null || apiclass.getcDescribe().equals("")) {
            return AjaxResult.error("类描述不能为空!");
        }
        try {
            ServerResult<Integer> serverResult = apiclassService.insertApiclass(apiclass);
            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("@ss.hasPermi('tool:apiclass:edit')")
    @Log(title = "接口类名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Apiclass apiclass) {
        try {

            if (apiclass.getmId() == null || apiclass.getmId()<0) {
                return AjaxResult.error("模块id不能为空!");
            }
            if (apiclass.getcName() == null || apiclass.getcName().equals("")) {
                return AjaxResult.error("类名不能为空!");
            }
            if (apiclass.getcDescribe() == null || apiclass.getcDescribe().equals("")) {
                return AjaxResult.error("类描述不能为空!");
            }
            ServerResult<Integer> serverResult = apiclassService.updateApiclass(apiclass);
            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("@ss.hasPermi('tool:apiclass: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 = apiclassService.deleteApiclassByIds(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);
        }
    }
}
