package com.hxhq.business.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxhq.business.domain.StudyMethodRead;
import com.hxhq.business.form.study.StudyMethodForm;
import com.hxhq.business.form.study.StudyMethodReadForm;
import com.hxhq.business.form.study.StudyMethodSearchForm;
import com.hxhq.business.service.IStudyMethodReadService;
import com.hxhq.business.utils.StudyMethodFileUtil;
import com.hxhq.common.core.domain.MpBaseEntity;
import com.hxhq.common.security.annotation.RequiresPermissions;
import com.hxhq.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.hxhq.business.domain.StudyMethod;
import com.hxhq.business.service.IStudyMethodService;
import com.hxhq.common.core.web.controller.BaseController;
import com.hxhq.common.core.web.domain.AjaxResult;
import com.hxhq.common.core.web.page.TableDataInfo;


/**
 * 试验-试验方法Controller
 *
 * @author hxhq
 * @date 2025-12-29
 */
@RestController
@RequestMapping("/business/studyMethod")
public class StudyMethodController extends BaseController {
    @Autowired
    private IStudyMethodService studyMethodService;

    @Autowired
    private IStudyMethodReadService studyMethodReadService;

    @Autowired
    private StudyMethodFileUtil studyMethodFileUtil;

    /**
     * 查询试验-试验方法列表
     */
    @GetMapping("/list")
    @RequiresPermissions("business:studyMethod:list")
    public TableDataInfo list(StudyMethodSearchForm form) {
        startPage();
        List<StudyMethod> list = studyMethodService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 获取试验-试验方法详细信息
     */
    @GetMapping(value = "/info")
    public AjaxResult getInfo(Long id) {
        return AjaxResult.success(studyMethodService.getById(id));
    }

    /**
     * 新增试验-试验方法信息
     */
    @PostMapping("/save")
    @RequiresPermissions("business:studyMethod:upload")
    public AjaxResult save(@RequestBody @Validated StudyMethodForm form) {
        form.setQmrId(SecurityUtils.getUserId());
        studyMethodService.save(form);
        return AjaxResult.success();
    }

    @PostMapping("/read")
    @RequiresPermissions("business:studyMethod:read")
    public AjaxResult read(@RequestBody @Validated StudyMethodReadForm form) {
        form.setQmrId(SecurityUtils.getUserId());
        studyMethodService.read(form);
        return AjaxResult.success();
    }

    @GetMapping(value = "/read/list")
    public AjaxResult getReadList(Long studyMethodId) {
        List<StudyMethodRead> list = studyMethodReadService.queryList(studyMethodId);
        return AjaxResult.success(list);
    }

    @GetMapping(value = "/download")
    @RequiresPermissions("business:studyMethod:download")
    public AjaxResult test(Long studyMethodId) {
        StudyMethod studyMethod = studyMethodService.getById(studyMethodId);
        if (studyMethod == null) {
            return error("试验方法不存在或已删除");
        }
        List<StudyMethodRead> list = studyMethodReadService.queryList(studyMethodId);

        String exportStudyMethodFilePath = studyMethodFileUtil.exportStudyMethodFile(studyMethod, list);
        Map<String, String> result = new HashMap<>();
        result.put("fileUrl", exportStudyMethodFilePath);
        return AjaxResult.success(result);
    }

    /**
     * 判断用户是否全部已读试验/学科下的试验方法
     */
    @GetMapping(value = "/getReadAllMethodStatus")
    public AjaxResult getReadAllMethodStatus(Long userId, Long studyId, Long studySubjectId) {
        return AjaxResult.success(studyMethodService.checkAllMethodReadStatus(userId, studyId, studySubjectId));
    }


}
