package org.dromara.art.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.art.domain.vo.ArtSchoolVo;
import org.dromara.art.domain.bo.ArtSchoolBo;
import org.dromara.art.domain.bo.ArtSchoolSubmitBo;
import org.dromara.art.domain.vo.ArtSchoolImportPreviewVo;
import org.dromara.art.domain.vo.ArtSchoolImportResultVo;
import org.dromara.art.domain.vo.ArtSchoolSubmitResultVo;
import org.dromara.art.service.IArtSchoolService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 学校基础信息主表
 *
 * @author ZhouWenTao
 * @date 2026-02-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/art/school")
public class ArtSchoolController extends BaseController {

    private final IArtSchoolService artSchoolService;

    /**
     * 查询学校基础信息主表列表
     */
    @SaCheckPermission("art:school:list")
    @GetMapping("/list")
    public TableDataInfo<ArtSchoolVo> list(ArtSchoolBo bo, PageQuery pageQuery) {
        return artSchoolService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出学校基础信息主表列表
     */
    @SaCheckPermission("art:school:export")
    @Log(title = "学校基础信息主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ArtSchoolBo bo, HttpServletResponse response) {
        artSchoolService.exportTemplateData(bo, response);
    }

    /**
     * 导出学校导入模板（空模板）
     */
    @SaCheckPermission("art:school:export")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        artSchoolService.exportImportTemplate(response);
    }

    /**
     * 导入预检（用于前端弹窗确认替换）
     */
    @SaCheckPermission("art:school:import")
    @PostMapping(value = "/importPreview", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<ArtSchoolImportPreviewVo> importPreview(@RequestPart("file") MultipartFile file) {
        return R.ok(artSchoolService.previewImport(file));
    }

    /**
     * 导入执行
     */
    @SaCheckPermission("art:school:import")
    @Log(title = "学校基础信息主表", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<ArtSchoolImportResultVo> importData(@RequestPart("file") MultipartFile file,
                                                 @RequestParam(value = "replaceMainCodes", required = false) List<String> replaceMainCodes,
                                                 @RequestParam(value = "replaceAll", defaultValue = "false") boolean replaceAll) {
        return R.ok(artSchoolService.importData(file, replaceMainCodes, replaceAll));
    }

    /**
     * 获取学校基础信息主表详细信息
     *
     * @param schoolId 主键
     */
    @SaCheckPermission("art:school:query")
    @GetMapping("/{schoolId}")
    public R<ArtSchoolVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long schoolId) {
        return R.ok(artSchoolService.queryById(schoolId));
    }

    /**
     * 新增学校基础信息主表
     */
    @SaCheckPermission("art:school:add")
    @Log(title = "学校基础信息主表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<ArtSchoolSubmitResultVo> add(@Validated @RequestBody ArtSchoolSubmitBo bo) {
        return R.ok(artSchoolService.insertWithDetailByBo(bo));
    }

    /**
     * 修改学校基础信息主表
     */
    @SaCheckPermission("art:school:edit")
    @Log(title = "学校基础信息主表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<ArtSchoolSubmitResultVo> edit(@Validated @RequestBody ArtSchoolSubmitBo bo) {
        return R.ok(artSchoolService.updateWithDetailByBo(bo));
    }

    /**
     * 删除学校基础信息主表
     *
     * @param schoolIds 主键串
     */
    @SaCheckPermission("art:school:remove")
    @Log(title = "学校基础信息主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{schoolIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] schoolIds) {
        return toAjax(artSchoolService.deleteWithValidByIds(List.of(schoolIds), true));
    }
}
