package com.ruoyi.business.controller;

import com.ruoyi.business.domain.Adapter;
import com.ruoyi.business.service.IAdapterService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 适配器Controller
 * 
 * @author gx
 * @date 2024-02-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/adapter")
@RequiredArgsConstructor
public class AdapterController extends BaseController {

    private final IAdapterService adapterService;

    /**
     * 查询适配器列表
     */
    @RequiresPermissions("business_adapter_list")
    @GetMapping
    public AjaxResult list(Adapter adapter) {
        List<Adapter> list = adapterService.selectAdapterList(adapter);
        return success(list);
    }

    /**
     * 查询适配器列表(分页)
     */
    @RequiresPermissions("business_adapter_list")
    @GetMapping("/list")
    public TableDataInfo pageList(Adapter adapter) {
        startPage();
        List<Adapter> list = adapterService.selectAdapterList(adapter);
        return getDataTable(list);
    }

    /**
     * 获取适配器详细信息
     */
    @RequiresPermissions("business_adapter_query")
    @GetMapping(value = "/{id}")
    public AjaxResult getAdapterById(@PathVariable("id") Long id) {
        return success(adapterService.selectAdapterById(id));
    }

    /**
     * 新增适配器
     */
    @RequiresPermissions("business_adapter_add")
    @Log(title = "适配器", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addAdapter(@RequestBody Adapter adapter) {
        adapter.setDeptIdPath(SecurityUtils.getDeptIdPath());
        adapter.setDeptName(SecurityUtils.getDeptName());
        adapter.setCreateUserId(SecurityUtils.getUserId());
        adapter.setCreateBy(SecurityUtils.getUsername());
        return toAjax(adapterService.insertAdapter(adapter));
    }

    /**
     * 修改适配器
     */
    @RequiresPermissions("business_adapter_edit")
    @Log(title = "适配器", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateAdapter(@RequestBody Adapter adapter) {
        adapter.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(adapterService.updateAdapter(adapter));
    }

    /**
     * 批量删除适配器
     */
    @RequiresPermissions("business_adapter_delete")
    @Log(title = "适配器", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteByIds/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        return toAjax(adapterService.deleteAdapterByIds(ids));
    }

    /**
     * 删除适配器
     */
    @RequiresPermissions("business_adapter_delete")
    @Log(title = "适配器", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteById/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        return toAjax(adapterService.deleteAdapterById(id));
    }

    /**
     * 导出适配器列表
     */
    @RequiresPermissions("business_adapter_export")
    @Log(title = "适配器", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Adapter adapter) {
        List<Adapter> list = adapterService.selectAdapterList(adapter);
        ExcelUtil<Adapter> util = new ExcelUtil<Adapter>(Adapter.class);
        util.exportExcel(response, list, "适配器数据");
    }
}
