package com.persagy.proxy.object.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.persagy.proxy.adm.request.AdmCreateRequest;
import com.persagy.proxy.adm.request.AdmCreateResponse;
import com.persagy.proxy.adm.request.AdmQueryCriteria;
import com.persagy.proxy.adm.request.AdmResponse;
import com.persagy.proxy.adm.utils.AdmContextUtil;
import com.persagy.proxy.adm.utils.ObjectNameUtil;
import com.persagy.proxy.object.model.AdmTenant;
import com.persagy.proxy.object.service.IAdmTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Adm 楼层 API
 * @author lvxy
 * @date 2021/8/16
 */

@RestController
@RequestMapping("/object/tenant")
public class AdmTenantController {
    @Autowired
    private IAdmTenantService service;
    /**
     * 统计
     *
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/count")
    public AdmResponse count(@RequestBody AdmQueryCriteria request) throws Exception {
        request.setName(AdmTenant.OBJ_TYPE);
        request.setOnlyCount(true);
        return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmTenant.class);
    }

    /**
     * 创建
     * @param createRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/create")
    public AdmCreateResponse create(@RequestBody AdmCreateRequest<AdmTenant> createRequest,
                                    @RequestParam(value = "projectId",required = false) String projectId) throws Exception {
        List<AdmTenant> vos = CollUtil.newArrayList(createRequest.getContent());
        vos.stream().forEach(admTenant -> {
            if (StrUtil.isEmpty(admTenant.getName())){
                admTenant.setName(ObjectNameUtil.objectName("租户-"));
            }
        });
        vos = service.doInsert(AdmContextUtil.toDmpContext(), AdmTenant.class, vos);
        return AdmCreateResponse.success(vos);
    }

    /**
     * 根据ID删除
     * @param vos
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public AdmResponse delete( @RequestBody List<AdmTenant> vos) throws Exception {
        service.doDelete(AdmContextUtil.toDmpContext(), vos);
        return AdmResponse.success();
    }

    /**
     * 更新
     * @param createRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public AdmResponse update(@RequestBody AdmCreateRequest<AdmTenant> createRequest,
                              @RequestParam(value = "projectId",required = false) String projectId) throws Exception {
        List<AdmTenant> vos = CollUtil.newArrayList(createRequest.getContent());
        vos = service.doUpdate(AdmContextUtil.toDmpContext(), AdmTenant.class, vos);
        return AdmResponse.success(vos);
    }

    /**
     * 查询
     *
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/query")
    public AdmResponse query(@RequestBody AdmQueryCriteria request) throws Exception {
        request.setName(AdmTenant.OBJ_TYPE);
        return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmTenant.class);
    }

    /**
     * 查询租户-包含统计信息
     * 此方法可能会被废弃
     *
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/query-with-count")
    public AdmResponse queryWithCount(@RequestBody AdmQueryCriteria request) throws Exception {
        request.setName(AdmTenant.OBJ_TYPE);
        return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmTenant.class);
    }

}
