package com.yh.saas.plugin.operation.controller;

import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.winsea.svc.base.base.entity.CommonTenant;
import com.winsea.svc.base.base.service.ICommonTenantService;
import com.winsea.svc.base.plugin.entity.TenantPlugin;
import com.winsea.svc.base.plugin.service.ITenantPluginService;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.yh.saas.common.support.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 租户插件接口
 *
 * @author SunTianjun
 */
@RequestMapping("/operationTenantPlugin")
@RestController
public class OperationTenantPluginController {

    @SofaReference(binding = @SofaReferenceBinding(bindingType = RpcConstants.PROTOCOL_TYPE_BOLT, timeout = 30000))
    private ITenantPluginService tenantPluginService;

    @SofaReference
    private ICommonTenantService tenantService;

    /**
     * 修改租户插件信息
     *
     * @param tenantPlugin 租户插件信息
     */
    @PostMapping("/api/changeInfo")
    public void changeInfo(@RequestBody TenantPlugin tenantPlugin) {
        tenantPluginService.changeInfo(tenantPlugin);
    }

    /**
     * 启用插件
     *
     * @param tenantPlugin 租户插件信息<br/>
     *                     插件ID ：tenantPlugin.pluginId
     */
    @PostMapping("/api/enable")
    public void enable(@RequestBody TenantPlugin tenantPlugin) {
        tenantPluginService.enable(tenantPlugin.getPluginId());
    }

    /**
     * 禁用插件
     *
     * @param tenantPlugin 租户插件信息<br/>
     *                     插件ID ：tenantPlugin.pluginId
     */
    @PostMapping("/api/disable")
    public void disable(@RequestBody TenantPlugin tenantPlugin) {
        tenantPluginService.disable(tenantPlugin.getPluginId());
    }

    /**
     * 获取公司插件列表
     *
     * @param tmpTenantId 租户ID
     * @param status      插件状态
     * @return 公司插件列表
     */
    @GetMapping("/query/plugins")
    public List<TenantPlugin> getPlugins(@RequestParam(name = "tenantId", required = false) String tmpTenantId,
                                         @RequestParam(required = false) TenantPlugin.PluginStatus status) {
        String tenantId = tmpTenantId;
        if (null == tmpTenantId) {
            tenantId = AuthSecurityUtils.getCurrentUserInfo().getTenantId();
        }
        return tenantPluginService.findTenantPlugins(tenantId, status);
    }

    /**
     * 获取公司插件详情列表
     *
     * @param tmpTenantId 租户ID
     * @param status      插件状态
     * @return 公司插件详情列表
     */
    @GetMapping("/query/pluginDetails")
    public List<TenantPlugin> getPluginDetails(@RequestParam(name = "tenantId", required = false) String tmpTenantId,
                                               @RequestParam(required = false) TenantPlugin.PluginStatus status) throws ParseException {
        String tenantId = tmpTenantId;
        if (null == tmpTenantId) {
            tenantId = AuthSecurityUtils.getCurrentUserInfo().getTenantId();
        }
        CommonTenant commonTenant = tenantService.findTenantIdInfo(tenantId);
        if (null == commonTenant) {
            return new ArrayList<>();
        }
        return tenantPluginService.findTenantPluginDetails(tenantId, status);
    }

    /**
     * 获取公司插件详情
     *
     * @param pluginId 插件ID
     * @return 公司插件详情
     */
    @GetMapping("/query/pluginDetail")
    public TenantPlugin getPluginDetails(@RequestParam String pluginId) {
        return tenantPluginService.getTenantPluginDetail(pluginId);
    }

    /**
     * 获取公司可开通的权限服务
     */
    @GetMapping("/query/findTenantResource")
    public List<TenantPlugin> findTenantResource(@RequestParam String tenantId) {
        if (StringUtils.isEmpty(tenantId)) {
            tenantId = AuthSecurityUtils.getCurrentUserInfo().getTenantId();
        }
        return tenantPluginService.findTenantResource(tenantId);
    }

}
