package jnpf.cultivate.v2.controller.web.teaching;

import jnpf.base.ActionResult;
import jnpf.cultivate.v2.service.V2TeachingSkillService;
import jnpf.model.cultivate.v2.exam.req.SkillDto;
import jnpf.model.cultivate.v2.exam.vo.SkillUploadInfoVo;
import jnpf.model.cultivate.v2.exam.vo.SkillVo;
import jnpf.model.cultivate.v2.label.vo.FtbCultivateLabelVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 技能点配置管理
 */
@RestController
@RequestMapping("/v2/teaching-skill")
public class V2TeachingSkillController {

    @Resource
    private V2TeachingSkillService v2TeachingSkillService;

    /**
     * 技能点 - 模板下载
     * @return jnpf.base.ActionResult<jnpf.base.vo.DownloadVO>
     */
    @GetMapping(value = "/template-download")
    public ActionResult<String> templateDownload() throws Exception {

        String base64 = v2TeachingSkillService.templateDownload();
        return ActionResult.success(base64);
    }

    /**
     * 技能点 - 导入
     * @param file 文件
     * @return jnpf.base.ActionResult<java.lang.Object>
     */
    @PostMapping(value = "/import")
    public ActionResult<SkillUploadInfoVo> teachingSkillImport(@RequestParam MultipartFile file) throws Exception {

        if (file == null || file.isEmpty()) {
            return ActionResult.fail("请上传文件");
        }
        // 校验文件后缀
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null ||
                !(originalFilename.endsWith(".xlsx") || originalFilename.endsWith(".xls"))) {
            return ActionResult.fail("文件格式错误，仅支持 .xlsx 或 .xls");
        }
        // 校验 Content-Type（防止随便改后缀）
        String contentType = file.getContentType();
        if (!isExcelContentType(contentType)) {
            return ActionResult.fail("文件类型非法，请上传 Excel 文件");
        }
        SkillUploadInfoVo uploadInfo = v2TeachingSkillService.teachingSkillImport(file);
        return ActionResult.success(uploadInfo);
    }

    private boolean isExcelContentType(String contentType) {
        if (contentType == null) {
            return false;
        }
        return contentType.equals("application/vnd.ms-excel")
                || contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    }

    /**
     * 查询分类列表[下拉]
     * @param searchSource 类型(0: 自定义, 1: 系统默认, null: 全部)
     * @return jnpf.base.ActionResult<java.util.List<jnpf.model.cultivate.v2.label.vo.FtbCultivateLabelVo>>
     */
    @GetMapping(value = "/down-list/category")
    public ActionResult<List<FtbCultivateLabelVo>> getDownCategoryList(@RequestParam(value = "searchSource", required = false) Integer searchSource) {

        List<FtbCultivateLabelVo> list = v2TeachingSkillService.getDownCategoryList(searchSource);
        return ActionResult.success(list);
    }

    /**
     * 根据分类查询技能点[下拉]
     * @param skillDto 技能点dto
     * @return jnpf.base.ActionResult<java.util.List<jnpf.model.cultivate.v2.exam.vo.SkillVo>>
     */
    @GetMapping(value = "/down-list")
    public ActionResult<List<SkillVo>> getDownSkillList(SkillDto skillDto) {

        List<SkillVo> list = v2TeachingSkillService.getDownSkillList(skillDto);
        return ActionResult.success(list);
    }
}