package com.ruoyi.business.controller;

import com.ruoyi.business.domain.ServiceBase;
import com.ruoyi.business.service.IServiceBaseService;
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.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-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceBase")
@RequiredArgsConstructor
public class ServiceBaseController extends BaseController {

    private final IServiceBaseService serviceBaseService;

    /**
     * 查询对外服务基础信息列表
     */
    @RequiresPermissions("business_serviceBase_list")
    @GetMapping
    public AjaxResult list(ServiceBase serviceBase) {
        List<ServiceBase> list = serviceBaseService.selectServiceBaseList(serviceBase);
        return success(list);
    }

    @RequiresPermissions("business_serviceBase_list")
    @GetMapping("/exclude/{id}")
    public AjaxResult excludeChild(@PathVariable(value = "id", required = false) Long id)
    {
        List<ServiceBase> list = serviceBaseService.selectServiceBaseList(new ServiceBase());
        list.removeIf(d -> d.getId().intValue() == id);
        return success(list);
    }

    /**
     * 获取对外服务基础信息详细信息
     */
    @RequiresPermissions("business_serviceBase_query")
    @GetMapping(value = "/{id}")
    public AjaxResult getServiceBaseById(@PathVariable("id") Long id) {
        return success(serviceBaseService.selectServiceBaseById(id));
    }

    /**
     * 新增对外服务基础信息
     */
    @RequiresPermissions("business_serviceBase_add")
    @Log(title = "对外服务基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addServiceBase(@RequestBody ServiceBase serviceBase) {
        serviceBase.setDeptIdPath(SecurityUtils.getDeptIdPath());
        serviceBase.setDeptName(SecurityUtils.getDeptName());
        serviceBase.setCreateUserId(SecurityUtils.getUserId());
        serviceBase.setCreateBy(SecurityUtils.getUsername());
        return toAjax(serviceBaseService.insertServiceBase(serviceBase));
    }

    /**
     * 修改对外服务基础信息
     */
    @RequiresPermissions("business_serviceBase_edit")
    @Log(title = "对外服务基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateServiceBase(@RequestBody ServiceBase serviceBase) {
        serviceBase.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(serviceBaseService.updateServiceBase(serviceBase));
    }

    /**
     * 批量删除对外服务基础信息
     */
    @RequiresPermissions("business_serviceBase_delete")
    @Log(title = "对外服务基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteByIds/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        return toAjax(serviceBaseService.deleteServiceBaseByIds(ids));
    }

    /**
     * 删除对外服务基础信息
     */
    @RequiresPermissions("business_serviceBase_delete")
    @Log(title = "对外服务基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteById/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        return toAjax(serviceBaseService.deleteServiceBaseById(id));
    }

    /**
     * 导出对外服务基础信息列表
     */
    @RequiresPermissions("business_serviceBase_export")
    @Log(title = "对外服务基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ServiceBase serviceBase) {
        List<ServiceBase> list = serviceBaseService.selectServiceBaseList(serviceBase);
        ExcelUtil<ServiceBase> util = new ExcelUtil<ServiceBase>(ServiceBase.class);
        util.exportExcel(response, list, "对外服务基础信息数据");
    }

    /**
     * 获取树列表
     */
    @RequiresPermissions("business_serviceBase_list")
    @GetMapping("/tree")
    public AjaxResult tree(ServiceBase serviceBase)
    {
        return success(serviceBaseService.selecServiceBaseTreeList(serviceBase));
    }
}
