package org.dromara.business.controller;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.bo.CaseTenantOrganizationBo;
import org.dromara.business.domain.vo.CaseTenantOrganizationVo;
import org.dromara.business.service.ICaseTenantOrganizationService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 合作组织
 *
 * @author wuzhigang
 * @date 2023-12-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/tenantOrganization")
public class CaseTenantOrganizationController extends BaseController {

    private final ICaseTenantOrganizationService caseTenantOrganizationService;

    /**
     * 查询合作组织分页列表
     */
    @GetMapping("/list")
    public TableDataInfo<CaseTenantOrganizationVo> list(CaseTenantOrganizationBo bo, PageQuery pageQuery) {
        return caseTenantOrganizationService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询合作组织选择列表
     */
    @GetMapping("/selectList")
    public R<List<CaseTenantOrganizationVo>> selectList() {
        return R.ok(caseTenantOrganizationService.querySelectList());
    }

    /**
     * 导出合作组织列表
     */
    @Log(title = "合作组织", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CaseTenantOrganizationBo bo, HttpServletResponse response) {
        List<CaseTenantOrganizationVo> list = caseTenantOrganizationService.queryList(bo);
        ExcelUtil.exportExcel(list, "合作组织", CaseTenantOrganizationVo.class, response);
    }

    /**
     * 获取合作组织详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<CaseTenantOrganizationVo> getInfo(@NotNull(message = "主键不能为空")
                                               @PathVariable Long id) {
        return R.ok(caseTenantOrganizationService.queryById(id));
    }

    /**
     * 新增合作组织
     */
    @Log(title = "合作组织", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CaseTenantOrganizationBo bo) {
        return toAjax(caseTenantOrganizationService.insertByBo(bo));
    }

    /**
     * 修改合作组织
     */
    @Log(title = "合作组织", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CaseTenantOrganizationBo bo) {
        return toAjax(caseTenantOrganizationService.updateByBo(bo));
    }

    /**
     * 删除合作组织
     *
     * @param ids 主键串
     */
    @Log(title = "合作组织", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(caseTenantOrganizationService.deleteWithValidByIds(List.of(ids), true));
    }
}
