package com.ruoyi.business.controller;

import com.ruoyi.business.domain.ServiceBusiness;
import com.ruoyi.business.domain.ServiceGrant;
import com.ruoyi.business.service.IServiceBusinessService;
import com.ruoyi.business.service.IServiceGrantService;
import com.ruoyi.common.auth.entity.OauthClientDetails;
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 com.ruoyi.system.domain.vo.ServiceGrantVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 对外服务授权Controller
 * 
 * @author wzq
 * @date 2024-01-10
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/dataExchange/serviceGrant")
@RequiredArgsConstructor
public class ServiceGrantController extends BaseController {

    private final IServiceGrantService serviceGrantService;

    private final IServiceBusinessService serviceBusinessService;

    /**
     * 查询对外服务授权列表
     */
    @RequiresPermissions("business_serviceGrant_list")
    @GetMapping
    public AjaxResult list(ServiceGrant serviceGrant) {
        List<ServiceGrant> list = serviceGrantService.selectServiceGrantList(serviceGrant);
        return success(list);
    }

    /**
     * 查询对外服务授权列表(分页)
     */
    @RequiresPermissions("business_serviceGrant_list")
    @GetMapping("/list")
    public TableDataInfo pageList(ServiceGrant serviceGrant) {
        startPage();
        List<ServiceGrant> list = serviceGrantService.selectServiceGrantList(serviceGrant);
        return getDataTable(list);
    }

    /**
     * 获取对外服务授权详细信息
     */
    @RequiresPermissions("business_serviceGrant_query")
    @GetMapping(value = "/{id}")
    public AjaxResult getServiceGrantById(@PathVariable("id") Long id) {
        return success(serviceGrantService.selectServiceGrantById(id));
    }

    @RequiresPermissions("business_serviceGrant_list")
    @PostMapping(value = "/generateDocument")
    public AjaxResult getGenerateDocument(@RequestBody OauthClientDetails oAuthClientDetails) {
        return success(serviceGrantService.generateDocument(oAuthClientDetails));
    }

    /**
     * 新增对外服务授权
     */
    @RequiresPermissions("business_serviceGrant_add")
    @Log(title = "对外服务授权", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addServiceGrant(@RequestBody ServiceGrant serviceGrant) {
        serviceGrant.setDeptIdPath(SecurityUtils.getDeptIdPath());
        serviceGrant.setDeptName(SecurityUtils.getDeptName());
        serviceGrant.setCreateUserId(SecurityUtils.getUserId().toString());
        serviceGrant.setCreateBy(SecurityUtils.getUsername());
        return toAjax(serviceGrantService.insertServiceGrant(serviceGrant));
    }

    /**
     * 修改对外服务授权
     */
    @RequiresPermissions("business_serviceGrant_edit")
    @Log(title = "对外服务授权", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateServiceGrant(@RequestBody ServiceGrant serviceGrant) {
        serviceGrant.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(serviceGrantService.updateServiceGrant(serviceGrant));
    }

    /**
     * 批量删除对外服务授权
     */
    @RequiresPermissions("business_serviceGrant_delete")
    @Log(title = "对外服务授权", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteByIds/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        return toAjax(serviceGrantService.deleteServiceGrantByIds(ids));
    }

    /**
     * 删除对外服务授权
     */
    @RequiresPermissions("business_serviceGrant_delete")
    @Log(title = "对外服务授权", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteById/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        return toAjax(serviceGrantService.deleteServiceGrantById(id));
    }

    /**
     * 对外服务授权
     */
    @RequiresPermissions("business_serviceGrant_edit")
    @Log(title = "对外服务授权", businessType = BusinessType.UPDATE)
    @PutMapping("updateGrant")
    public AjaxResult updateGrant(@RequestBody ServiceGrantVo serviceGrantVo) {
        serviceGrantVo.setDeptIdPath(SecurityUtils.getDeptIdPath());
        serviceGrantVo.setDeptName(SecurityUtils.getDeptName());
        serviceGrantVo.setCreateUserId(SecurityUtils.getUserId().toString());
        serviceGrantVo.setCreateBy(SecurityUtils.getUsername());
        return toAjax(serviceGrantService.updateGrant(serviceGrantVo));
    }

    /**
     * 加载对应客户端id菜单列表树
     */
    @RequiresPermissions("business_serviceGrant_query")
    @GetMapping(value = "/grantInfoTreeselect")
    public AjaxResult roleMenuTreeselect(ServiceGrant serviceGrant)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", serviceGrantService.selectServiceBusinessIdByClientId(serviceGrant.getClientId(), serviceGrant.getBelongSystem()));
        ajax.put("menus", serviceBusinessService.selectGrantTreeList(new ServiceBusiness().setBelongSystem(serviceGrant.getBelongSystem())));
        return ajax;
    }
}
