package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjUtil;
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.exception.ServiceException;
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.dromara.system.domain.bo.SysScreeningAssessmentConfigBo;
import org.dromara.system.domain.vo.SysScreeningAssessmentConfigVo;
import org.dromara.system.service.ISysScreeningAssessmentConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 筛查/评估配置
 *
 * @author Lion Li
 * @date 2025-07-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/screeningAssessmentConfig")
public class SysScreeningAssessmentConfigController extends BaseController {

    private final ISysScreeningAssessmentConfigService sysScreeningAssessmentConfigService;

    /**
     * 查询筛查/评估配置列表
     */
    @SaCheckPermission("system:screeningAssessmentConfig:list")
    @GetMapping("/list")
    public TableDataInfo<SysScreeningAssessmentConfigVo> list(SysScreeningAssessmentConfigBo bo, PageQuery pageQuery) {
        return sysScreeningAssessmentConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 状态修改
     */
    @SaCheckPermission("system:screeningAssessmentConfig:edit")
    @Log(title = "筛查/评估配置管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody SysScreeningAssessmentConfigBo configBo) {
        if (ObjUtil.isNull(configBo.getConfigId())) {
            throw new ServiceException("id不能为空！");
        }
        return toAjax(sysScreeningAssessmentConfigService.updateConfigStatus(
            List.of(configBo.getConfigId()), configBo.getStatus()));
    }

    /**
     * 导出筛查/评估配置列表
     */
    @SaCheckPermission("system:screeningAssessmentConfig:export")
    @Log(title = "筛查/评估配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysScreeningAssessmentConfigBo bo, HttpServletResponse response) {
        List<SysScreeningAssessmentConfigVo> list = sysScreeningAssessmentConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "筛查/评估配置", SysScreeningAssessmentConfigVo.class, response);
    }

    /**
     * 获取筛查/评估配置详细信息
     *
     * @param configId 主键
     */
    @SaCheckPermission("system:screeningAssessmentConfig:query")
    @GetMapping("/{configId}")
    public R<SysScreeningAssessmentConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                                     @PathVariable Long configId) {
        return R.ok(sysScreeningAssessmentConfigService.queryById(configId));
    }

    /**
     * 新增筛查/评估配置
     */
    @SaCheckPermission("system:screeningAssessmentConfig:add")
    @Log(title = "筛查/评估配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysScreeningAssessmentConfigBo bo) {
        return toAjax(sysScreeningAssessmentConfigService.insertByBo(bo));
    }

    /**
     * 修改筛查/评估配置
     */
    @SaCheckPermission("system:screeningAssessmentConfig:edit")
    @Log(title = "筛查/评估配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysScreeningAssessmentConfigBo bo) {
        return toAjax(sysScreeningAssessmentConfigService.updateByBo(bo));
    }

    /**
     * 删除筛查/评估配置
     *
     * @param configIds 主键串
     */
    @SaCheckPermission("system:screeningAssessmentConfig:remove")
    @Log(title = "筛查/评估配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] configIds) {
        return toAjax(sysScreeningAssessmentConfigService.deleteWithValidByIds(List.of(configIds), true));
    }
}
