package org.dromara.system.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
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.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.SysDiseaseLabelVo;
import org.dromara.system.domain.bo.SysDiseaseLabelBo;
import org.dromara.system.service.ISysDiseaseLabelService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 疾病/部位标签
 *
 * @author Lion Li
 * @date 2025-06-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/diseaseLabel")
public class SysDiseaseLabelController extends BaseController {

    private final ISysDiseaseLabelService sysDiseaseLabelService;

    /**
     * 查询疾病/部位标签列表
     */
    @SaCheckPermission("system:diseaseLabel:list")
    @GetMapping("/list")
    public TableDataInfo<SysDiseaseLabelVo> list(SysDiseaseLabelBo bo, PageQuery pageQuery) {
        return sysDiseaseLabelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出疾病/部位标签列表
     */
    @SaCheckPermission("system:diseaseLabel:export")
    @Log(title = "疾病/部位标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysDiseaseLabelBo bo, HttpServletResponse response) {
        List<SysDiseaseLabelVo> list = sysDiseaseLabelService.queryList(bo);
        ExcelUtil.exportExcel(list, "疾病/部位标签", SysDiseaseLabelVo.class, response);
    }

    /**
     * 获取疾病/部位标签详细信息
     *
     * @param labelId 主键
     */
    @SaCheckPermission("system:diseaseLabel:query")
    @GetMapping("/{labelId}")
    public R<SysDiseaseLabelVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable Long labelId) {
        return R.ok(sysDiseaseLabelService.queryById(labelId));
    }

    /**
     * 新增疾病/部位标签
     */
    @SaCheckPermission("system:diseaseLabel:add")
    @Log(title = "疾病/部位标签", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<String> add(@Validated(AddGroup.class) @RequestBody SysDiseaseLabelBo bo) {
        sysDiseaseLabelService.insertByBo(bo);
        return R.ok("成功！", String.valueOf(bo.getLabelId()));
    }

    /**
     * 修改疾病/部位标签
     */
    @SaCheckPermission("system:diseaseLabel:edit")
    @Log(title = "疾病/部位标签", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysDiseaseLabelBo bo) {
        return toAjax(sysDiseaseLabelService.updateByBo(bo));
    }

    /**
     * 删除疾病/部位标签
     *
     * @param labelIds 主键串
     */
    @SaCheckPermission("system:diseaseLabel:remove")
    @Log(title = "疾病/部位标签", businessType = BusinessType.DELETE)
    @DeleteMapping("/{labelIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] labelIds) {
        return toAjax(sysDiseaseLabelService.deleteWithValidByIds(List.of(labelIds), true));
    }

    @SaCheckPermission("system:diseaseLabel:downLoadTemplate")
    @RequestMapping(value = "/downLoadTemplate", method = RequestMethod.GET)
    public void downLoadTemplate(HttpServletResponse response) {
        ExcelUtil.downLoadTemplate("diseaseLabel.xlsx", "疾病_部位标签模版", response);
    }

    @SaCheckPermission("system:diseaseLabel:import")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public R<?> importExcel(MultipartFile file) throws Exception {
        return sysDiseaseLabelService.importExcel(file);
    }
}
