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

import com.alipay.sofa.runtime.api.annotation.SofaReference;
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.entity.TenantService;
import com.winsea.svc.base.plugin.service.ITenantServService;
import org.springframework.web.bind.annotation.*;

/**
 * 租户服务接口
 *
 * @author SunTianjun
 */
@RequestMapping("/operationTenantService")
@RestController
public class OperationTenantServiceController {

    @SofaReference
    private ITenantServService tenantServService;

//    @Autowired
//    private IOperationTenantService tenantService;

    @SofaReference
    private ICommonTenantService tenantService;


    /**
     * 激活插件服务
     *
     * @param tenantPlugin 服务激活信息<br/>
     *                     租户服务：tenantPlugin.activatedServices<br/>
     *                     租户ID  ：tenantPlugin.tenantId
     */
    @PostMapping("/api/activate")
    public void activate(@RequestBody TenantPlugin tenantPlugin) {
        tenantServService.activate(tenantPlugin.getActivatedServices(), tenantPlugin.getTenantId(), tenantPlugin.getPluginAttributes());
    }

    /**
     * 启用服务
     *
     * @param tenantService 插件服务信息<br/>
     *                      服务ID ：tenantService.serviceId
     */
    @PostMapping("/api/enable")
    public void enable(@RequestBody TenantService tenantService) {
        tenantServService.enable(tenantService.getServiceId());
    }

    /**
     * 禁用服务
     *
     * @param tenantService 插件服务信息<br/>
     *                      服务ID ：tenantService.serviceId
     */
    @PostMapping("/api/disable")
    public void disable(@RequestBody TenantService tenantService) {
        tenantServService.disable(tenantService.getServiceId());
    }

    /**
     * 查询当前租户是否购买了相应服务
     *
     * @param tenantId    租户id
     * @param pluginCode  pluginCode 业务code
     * @param serviceCode serviceCode
     * @return 是否有数据
     */
    @GetMapping("/queyr/findTenantServiceOpening")
    public TenantService findTenantServiceOpening(@RequestParam String tenantId, @RequestParam String pluginCode, @RequestParam String serviceCode) {
        return tenantServService.findTenantServiceOpening(tenantId, pluginCode, serviceCode);
    }

    /**
     * 产品激活服务
     *
     * @param tenantPlugin 服务激活信息<br/>
     *                     租户服务：tenantPlugin.activatedServices<br/>
     *                     租户ID  ：tenantPlugin.tenantId
     */
    @PostMapping("/api/productActivate")
    public void productActivate(@RequestBody TenantPlugin tenantPlugin) {
        CommonTenant commonTenant = tenantService.findTenantIdInfo(tenantPlugin.getTenantId());

        tenantServService.productActivate(tenantPlugin.getActivatedServices(), tenantPlugin.getTenantId(), tenantPlugin.getPluginAttributes());
    }
}
