package com.hxhq.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.*;
import com.hxhq.business.dto.gyzj.ListDto;
import com.hxhq.business.dto.sj.SjListDto;
import com.hxhq.business.enums.NormalEnum;
import com.hxhq.business.enums.SnTypeEnum;
import com.hxhq.business.form.common.SignForm;
import com.hxhq.business.form.gsp.GspSearchListForm;
import com.hxhq.business.form.gyzj.SearchForm;
import com.hxhq.business.form.sj.SjSearchListForm;
import com.hxhq.business.form.yq.YqSearchForm;
import com.hxhq.business.form.zcg.ZcgSearchForm;
import com.hxhq.business.service.*;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.common.core.web.controller.BaseController;
import com.hxhq.common.core.web.domain.AjaxResult;
import com.hxhq.common.core.web.page.TableDataInfo;
import com.hxhq.common.redis.service.RedisService;
import com.hxhq.common.security.utils.SecurityUtils;
import com.hxhq.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 公共Controller
 *
 * @author hxhq
 * @date 2025-12-23
 */
@RestController
@RequestMapping("/business/public")
public class PublicController extends BaseController {
    @Autowired
    private ITemplateService templateService;
    @Autowired
    private IStudyService studyService;
    @Autowired
    private IStudyFormPreService studyFormPreService;
    @Autowired
    private IRoomService roomService;
    @Autowired
    private IAnimalSpeciesService animalSpeciesService;
    @Autowired
    private IZcgService zcgService;
    @Autowired
    private RedisService redisService;
    @Autowired
    public ISnGenService snGenService;
    @Autowired
    public ISysUserService sysUserService;
    @Autowired
    public ISjService sjService;
    @Autowired
    public IYqService yqService;
    @Autowired
    public IGyzjService gyzjService;
    @Autowired
    public IGspService gspService;

    /**
     * 获取编号
     * @param count
     * @param my 母液编号（分装用）
     * @return
     */
    @GetMapping("/getSn")
    public AjaxResult getSn(Integer count,String my) {
        if (count == null || count.intValue() <= 1) {
            count = 1;
        }
        List<String> list = new ArrayList<>();
        for (int i = 0; i < count;i++){
            list.add(snGenService.getNewSn(SnTypeEnum.wz.getValue(),my));
        }
        return AjaxResult.success(list);
    }

    /**
     * 试验区域-饲养间列表
     *
     * @return
     */
    @GetMapping("/roomList")
    public AjaxResult getRoomList(String syqy) {
        if (StringUtils.isNotEmpty(syqy)) {
            LambdaQueryWrapper<Room> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Room::getSyqy, syqy);
            List<Room> list = roomService.list(queryWrapper);
            return success(list);
        } else {
            List<Room> list = roomService.list();
            return success(list);
        }
    }

    /**
     * 动物种属-笼具列表
     *
     * @return
     */
    @GetMapping("/animalSpeciesList")
    public AjaxResult getAnimalSpeciesList(String animalSpecies) {
        if (StringUtils.isNotEmpty(animalSpecies)) {
            LambdaQueryWrapper<AnimalSpecies> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AnimalSpecies::getAnimalSpecies, animalSpecies);
            List<AnimalSpecies> list = animalSpeciesService.list(queryWrapper);
            return success(list);
        } else {
            List<AnimalSpecies> list = animalSpeciesService.list();
            return success(list);
        }

    }

    /**
     * 查询模板列表
     */
    @GetMapping("/templateList")
    public TableDataInfo list(Template template) {
        startPage();
        template.setStatus(NormalEnum.yes.getValue());
        List<Template> list = templateService.queryList(template);
        return getDataTable(list);
    }

    /**
     * 查询试验列表
     */
    @GetMapping("/studyList")
    public TableDataInfo studyList(Study study) {
        startPage();
        QueryWrapper<Study> queryWrapper = Wrappers.query();
        if (study.getStatus() != null && study.getStatus().intValue() > 0) {
            queryWrapper.eq("status", study.getStatus());
        }
        if (StringUtils.isNoneBlank(study.getName())) {
            queryWrapper.like("name", study.getName());
        }
        if (StringUtils.isNoneBlank(study.getSn())) {
            queryWrapper.like("sn", study.getSn());
        }
        if (StringUtils.isNoneBlank(study.getLeaderName())) {
            queryWrapper.like("leader_name", study.getLeaderName());
        }
        List<Study> list = studyService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询部长列表
     */
    @GetMapping("/bzList")
    public AjaxResult bzList()
    {
        return AjaxResult.success(sysUserService.selectBzList());
    }



    /**
     * 查询暂存柜列表
     */
    @GetMapping("/zcgList")
    public AjaxResult zcgList(ZcgSearchForm form) {
        return AjaxResult.success(zcgService.queryList(form));
    }

    /**
     * 判断签名密码
     * @param sign
     * @return
     */
    @PostMapping("/checkPassword")
    public AjaxResult checkPassword(@RequestBody SignForm sign) {
        sysUserService.checkPassword(SecurityUtils.getLoginUser().getSysUser(),sign.getQmrmm(),false);
        return AjaxResult.success();
    }

    /**
     * 查询试剂列表
     */
    @GetMapping("/sjList")
    public TableDataInfo list(SjSearchListForm form)
    {
        startPage();
        List<SjListDto> list = sjService.queryPublicList(form);
        return getDataTable(list);
    }

    /**
     * 查询仪器列表
     */
    @GetMapping("/yqList")
    public TableDataInfo yqList(YqSearchForm form)
    {
        startPage();
        List<Yq> list = yqService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 查询给药制剂列表
     */
    @GetMapping("/gyzjList")
    public TableDataInfo gyzjList(SearchForm form)
    {
        startPage();
        List<ListDto> list = gyzjService.queryStudyList(form);
        return getDataTable(list);
    }

    /**
     * 查询供试品列表
     */
    @GetMapping("/gspList")
    public TableDataInfo gspList(GspSearchListForm form)
    {
        startPage();
        List<Gsp> list = gspService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 获取模板的步骤库列表
     * @param templateId
     * @return
     */
    @GetMapping("/templateStepList")
    public AjaxResult templateStepList(Long templateId,String templateSn)
    {
        return AjaxResult.success(templateService.getStepListByTemplateId(templateId,templateSn));
    }
}
