package com.hotwj.platform.resourceManagement.govEnterpriseUnit.controller;

import com.hotwj.platform.resourceManagement.govEnterpriseUnit.domain.bo.HotGovEnterpriseUnitBo;
import com.hotwj.platform.resourceManagement.govEnterpriseUnit.domain.vo.HotGovEnterpriseUnitVo;
import com.hotwj.platform.resourceManagement.govEnterpriseUnit.service.IHotGovEnterpriseUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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 shihongwei
 * @date 2026-01-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/resourceManagement/govEnterpriseUnit")
@Tag(name = "政企单位", description = "政企单位管理")
public class HotGovEnterpriseUnitController extends BaseController {

    private final IHotGovEnterpriseUnitService hotGovEnterpriseUnitService;

    /**
     * 查询政企单位列表
     */
    //@SaCheckPermission("resourceManagement:govEnterpriseUnit:list")
    @GetMapping("/list")
    @Operation(summary = "分页查询政企单位列表")
    public TableDataInfo<HotGovEnterpriseUnitVo> list(HotGovEnterpriseUnitBo bo, PageQuery pageQuery) {
        return hotGovEnterpriseUnitService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询可选的上级单位列表
     */
    //@SaCheckPermission("resourceManagement:govEnterpriseUnit:list")
    @GetMapping("/listParent")
    @Operation(summary = "查询可选的上级单位列表")
    public R<List<HotGovEnterpriseUnitVo>> listParent(@Parameter(name = "id", description = "当前单位ID", required = false) @RequestParam(required = false) Long id,
                                                      @Parameter(name = "unitType", description = "单位类型", required = false) @RequestParam(required = false) Long unitType) {
        return R.ok(hotGovEnterpriseUnitService.queryParentList(id, unitType));
    }

    /**
     * 导出政企单位列表
     */
    //@SaCheckPermission("resourceManagement:govEnterpriseUnit:export")
    @Log(title = "政企单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出政企单位列表")
    public void export(HotGovEnterpriseUnitBo bo, HttpServletResponse response) {
        List<HotGovEnterpriseUnitVo> list = hotGovEnterpriseUnitService.queryList(bo);
        ExcelUtil.exportExcel(list, "政企单位", HotGovEnterpriseUnitVo.class, response);
    }

    /**
     * 获取政企单位详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("resourceManagement:govEnterpriseUnit:query")
    @GetMapping("/{id}")
    @Operation(summary = "获取政企单位详细信息")
    public R<HotGovEnterpriseUnitVo> getInfo(@NotNull(message = "主键不能为空")
                                             @Parameter(name = "id", description = "主键", required = true, example = "1")
                                             @PathVariable Long id) {
        return R.ok(hotGovEnterpriseUnitService.queryById(id));
    }

    /**
     * 新增政企单位
     */
    //@SaCheckPermission("resourceManagement:govEnterpriseUnit:add")
    @Log(title = "政企单位", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Operation(summary = "新增政企单位")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HotGovEnterpriseUnitBo bo) {
        return toAjax(hotGovEnterpriseUnitService.insertByBo(bo));
    }

    /**
     * 修改政企单位
     */
    //@SaCheckPermission("resourceManagement:govEnterpriseUnit:edit")
    @Log(title = "政企单位", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Operation(summary = "修改政企单位")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HotGovEnterpriseUnitBo bo) {
        return toAjax(hotGovEnterpriseUnitService.updateByBo(bo));
    }

    /**
     * 删除政企单位
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("resourceManagement:govEnterpriseUnit:remove")
    @Log(title = "政企单位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除政企单位")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @Parameter(name = "ids", description = "主键串", required = true, example = "1,2,3")
                          @PathVariable Long[] ids) {
        return toAjax(hotGovEnterpriseUnitService.deleteWithValidByIds(List.of(ids), true));
    }
}
