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

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsea.svc.base.base.entity.CommonCompany;
import com.winsea.svc.base.base.entity.CommonTenant;
import com.winsea.svc.base.base.service.ICommonCompanyService;
import com.winsea.svc.base.base.service.ICommonTenantService;
import com.winsea.svc.base.plugin.entity.TenantService;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.yh.saas.common.support.util.StringUtils;
import com.yh.saas.plugin.operation.qo.TenantQO;
import com.yh.saas.plugin.operation.service.IOperationTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;

/**
 * 租户API
 *
 * @author gdc
 */
@RequestMapping("/tenant")
@RestController
public class TenantController {

    @Autowired
    private IOperationTenantService tenantService;
    @Autowired
    private ICommonCompanyService commonCompanyService;

    @SofaReference
    private ICommonTenantService commonTenantService;

    @GetMapping("/query/findTenantServiceType")
    public String findTenantServiceType() {
        CommonTenant tenant = this.commonTenantService.findTenantIdInfo(AuthSecurityUtils.getCurrentUserInfo().getTenantId());
        if (null == tenant) {
            return "";
        }
        return String.valueOf(tenant.getServiceType());
    }

    /**
     * 创建租户
     *
     * @param tenant 租户信息
     * @return 租户ID
     */
    @PostMapping("/api/create")
    public String create(@RequestBody @Valid CommonTenant tenant) {
        String tenantId = tenantService.create(tenant);
        return tenantId;
    }


    /**
     * 修改租户信息
     *
     * @param tenant 租户信息
     */
    @PostMapping("/api/change")
    public void change(@RequestBody CommonCompany tenant) {
        tenantService.change(tenant);
    }

    /**
     * 修改租户信息
     *
     * @param tenant 租户信息
     */
    @PostMapping("/api/changeInfo")
    public void changeInfo(@RequestBody CommonTenant tenant) {
        tenantService.changeInfo(tenant);
    }

    /**
     * 启用租户
     *
     * @param tenant->tenantId 租户ID
     */
    @PostMapping("/api/enable")
    public void enable(@RequestBody CommonTenant tenant) {
        tenantService.enable(tenant.getTenantId());
    }

    /**
     * 禁用租户
     *
     * @param tenant->tenantId 租户ID
     */
    @PostMapping("/api/disable")
    public void disable(@RequestBody CommonTenant tenant) {
        tenantService.disable(tenant.getTenantId());
    }

    /**
     * 搜索租户
     *
     * @param tenantQO 租户查询对象
     * @return 租户分页列表
     */
    @GetMapping("/query/tenants")
    public Page<CommonTenant> searchTenants(TenantQO tenantQO) {
        return tenantService.search(tenantQO.getKeyword(), tenantQO.getQueryPage());
    }

    /**
     * 获取租户详情
     *
     * @param tenantId 租户ID
     * @return 租户详情
     */
    @GetMapping("/query/tenantDetail")
    public CommonTenant getTenantDetail(@RequestParam String tenantId) throws ParseException {
        return tenantService.getTenantDetail(tenantId);
    }


    /**
     * 获取详情
     *
     * @param compId 租户ID
     * @return 租户详情
     */
    @GetMapping("/query/getCompIpm")
    public CommonCompany getCompIpm(@RequestParam String compId) throws ParseException {
        return commonCompanyService.selectById(compId);
    }

    /**
     * 租户名称重复检查
     *
     * @param tenant 租户对象
     * @return
     */
    @PostMapping("/query/checkoutTenantName")
    public Boolean checkoutPhone(@RequestBody CommonTenant tenant) {
        return tenantService.checkoutTenantName(tenant.getTenantName(), tenant.getTenantId(), tenant.getCompSimpleNameEn());
    }

    /**
     * 获取租户已购服务
     *
     * @param tenantId 租户id
     * @return 返回服务
     */
   @GetMapping("/query/tenantServiceForm")
    public List<TenantService> findTenantServiceForm(@RequestParam String tenantId) {
        if (StringUtils.isEmpty(tenantId)) {
            return null;
        }
        return tenantService.getTenantService(tenantId);
    }

}
