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.gsp.GspListDto;
import com.hxhq.business.dto.gyzj.ListDto;
import com.hxhq.business.dto.sj.SjListDto;
import com.hxhq.business.enums.NormalEnum;
import com.hxhq.business.form.common.GenSnForm;
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.study.StudyCellSearchForm;
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.api.domain.SysDept;
import com.hxhq.system.service.ISysDeptService;
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;
import java.util.stream.Collectors;


/**
 * 公共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;
    @Autowired
    public ICellService cellService;
    @Autowired
    public IBacteriaService bacteriaService;
    @Autowired
    public ISysDeptService sysDeptService;
    @Autowired
    public ISystemLogService systemLogService;
    @Autowired
    public IJcbService jcbService;
    @Autowired
    public IStudyFormFillService studyFormFillService;


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

    /**
     * 获取编号(数组)
     * @param preList
     * @return
     */
    @PostMapping("/getSnArr")
    public AjaxResult getSnArr(@RequestBody List<GenSnForm> preList) {
        List<String> list = new ArrayList<>();
        for(GenSnForm pre : preList){
            list.add(snGenService.getNewSn(pre.getPre(),pre.getType()));
        }
        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)
    {
        form.setCcwz(NormalEnum.no.getValue());
        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<GspListDto> list = gspService.queryStudyList(form);
        return getDataTable(list);
    }

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

    /**
     * 细胞列表
     * @return
     */
    @GetMapping("/cellList")
    public TableDataInfo getCellList(StudyCellSearchForm form) {
        startPage();
        form.setStudyId(null);
        List<Cell> cells = cellService.queryStudyList(form);
        return getDataTable(cells);
    }

    /**
     * 细菌列表
     * @return
     */
    @GetMapping("/bacteriaList")
    public TableDataInfo getBacteriaList(StudyCellSearchForm form) {
        startPage();
        form.setStudyId(null);
        List<Bacteria> list = bacteriaService.queryStudyList(form);
        return getDataTable(list);
    }

    /**
     * 存储位置列表
     * @return
     */
    @GetMapping("/storageLocation")
    public AjaxResult getStorageLocation(YqSearchForm form) {
        List<SysDept> superiorAndSubordinate = sysDeptService.getSuperiorAndSubordinate(SecurityUtils.getLoginUser().getSysUser().getDeptId());
        List<Long> deptIdList = superiorAndSubordinate.stream().map(SysDept::getDeptId).collect(Collectors.toList());
        form.setDeptIdList(deptIdList);
        form.setCcwz(NormalEnum.yes.getValue());
        List<Yq> list = yqService.queryList(form);
        return success(list);
    }

    /**
     * 记录日志
     * @param name
     * @param nameEn
     * @param jcmc
     * @param jcmcEn
     * @return
     */
    @GetMapping("/saveSimpleLog")
    public AjaxResult saveSimpleLog(String name,String nameEn,String jcmc,String jcmcEn) {
        systemLogService.saveSimpleInfo(name,nameEn,jcmc,jcmcEn);
        return success();
    }

    /**
     * 查询检测板列表查询检测板列表
     * @param jcb
     * @return
     */
    @GetMapping("/jcbList")
    public TableDataInfo jcbList(Jcb jcb)
    {
        startPage();
        List<Jcb> list = jcbService.queryList(jcb);
        return getDataTable(list);
    }

    /**
     * 前序表单列表
     * @param formFill
     * @return
     */
    @GetMapping("/qxFormFillList")
    public TableDataInfo qxFormFillList(StudyFormFill formFill)
    {
        startPage();
        List<StudyFormFill> list = studyFormFillService.queryQxList(formFill);
        return getDataTable(list);
    }
}