package com.hxhq.business.service.impl;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hxhq.business.enums.study.StudyRoomStatusEnum;
import com.hxhq.business.enums.zykgl.JcgjlxEnum;
import com.hxhq.business.enums.zykgl.JcmcysEnum;
import com.hxhq.business.form.study.StudyRoomForm;
import com.hxhq.business.form.study.StudyRoomSearchForm;
import com.hxhq.business.service.IStudyRoomHistoryService;
import com.hxhq.business.service.IStudyRoomJcgjService;
import com.hxhq.business.utils.JctUtil;
import com.hxhq.common.core.exception.ServiceException;
import com.hxhq.system.api.domain.SysUser;
import com.hxhq.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.StudyRoomMapper;
import com.hxhq.business.domain.StudyRoom;
import com.hxhq.business.service.IStudyRoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 试验-饲养间Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-29
 */
@Service
public class StudyRoomServiceImpl extends ServiceImpl<StudyRoomMapper, StudyRoom> implements IStudyRoomService
{

    @Autowired
    private IStudyRoomHistoryService studyRoomHistoryService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IStudyRoomJcgjService studyRoomJcgjService;

    @Override
    public List<StudyRoom> queryList(StudyRoomSearchForm form) {
        LambdaQueryWrapper<StudyRoom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudyRoom::getStudyId, form.getStudyId());
        if(StringUtils.isNotEmpty(form.getSyjh())) {
            queryWrapper.eq(StudyRoom::getSyjh, form.getSyjh());
        }
        if(StringUtils.isNotEmpty(form.getSyqy())) {
            queryWrapper.eq(StudyRoom::getSyqy, form.getSyqy());
        }
        if(StringUtils.isNotEmpty(form.getQyrMc())) {
            queryWrapper.like(StudyRoom::getQyrMc, form.getQyrMc());
        }
        if(StringUtils.isNotEmpty(form.getJsrMc())) {
            queryWrapper.like(StudyRoom::getJsrMc, form.getJsrMc());
        }
        if(StringUtils.isNotEmpty(form.getStartDate())) {
            queryWrapper.ge(StudyRoom::getKssj, form.getStartDate());
        }
        if(StringUtils.isNotEmpty(form.getEndDate())) {
            queryWrapper.le(StudyRoom::getKssj, form.getEndDate());
        }
        queryWrapper.orderByDesc(StudyRoom::getKssj);
        return this.list(queryWrapper);
    }

    @Override
    public void add(StudyRoomForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        StudyRoom studyRoom = new StudyRoom();
        BeanUtils.copyProperties(form, studyRoom);
        studyRoom.setKssj(new Date());
        studyRoom.setQyrId(qmr.getUserId());
        studyRoom.setQyrMc(qmr.getNickName());
        studyRoom.setZt(StudyRoomStatusEnum.syz.getValue());
        this.save(studyRoom);

        studyRoomHistoryService.addStudyRoomHistory(studyRoom, null, qmr);

        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("试验区域", studyRoom.getSyqy());
        formData.put("饲养间号", studyRoom.getSyjh());
        formData.put("动物种属", studyRoom.getDwzs());
        formData.put("笼具", studyRoom.getLj());
        formData.put("开始使用原因", studyRoom.getKssyyl());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Area", studyRoom.getSyqy());
        formDataEn.put("Room ID", studyRoom.getSyjh());
        formDataEn.put("Species", studyRoom.getDwzs());
        formDataEn.put("Cage", studyRoom.getLj());
        formDataEn.put("Start Reason", studyRoom.getKssyyl());
        studyRoomJcgjService.saveJcgj(studyRoom.getId(), JcgjlxEnum.lc.getValue(), "启用饲养间", "Start Usage", JcmcysEnum.blue.getValue(),
                JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
    }

    @Override
    public void updateLj(StudyRoomForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);

        StudyRoom studyRoom = this.getById(form.getId());
        if(studyRoom == null) {
            throw new ServiceException("饲养间不存在或已删除");
        }
        if(studyRoom.getZt() != StudyRoomStatusEnum.syz.getValue()) {
            throw new ServiceException("饲养间【" + studyRoom.getSyjh() + "】处于已结束状态，无法更换笼具");
        }
        String ljOld = studyRoom.getLj();
        studyRoom.setLj(form.getLj());
        this.updateById(studyRoom);

        studyRoomHistoryService.addStudyRoomHistory(studyRoom, ljOld, qmr);

        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原笼具", ljOld);
        formData.put("新笼具", studyRoom.getLj());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("原笼具", ljOld);
        formDataEn.put("新笼具", studyRoom.getLj());
        studyRoomJcgjService.saveJcgj(studyRoom.getId(), JcgjlxEnum.bj.getValue(), "更换笼具", "End Usage", JcmcysEnum.blue.getValue(),
                JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
    }

    @Override
    public void js(StudyRoomForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);

        StudyRoom studyRoom = this.getById(form.getId());
        if(studyRoom == null) {
            throw new ServiceException("饲养间不存在或已删除");
        }
        if(studyRoom.getZt() != StudyRoomStatusEnum.syz.getValue()) {
            throw new ServiceException("饲养间【" + studyRoom.getSyjh() + "】处于已结束状态，无法重复操作");
        }
        studyRoom.setZt(StudyRoomStatusEnum.yjs.getValue());
        studyRoom.setJssyyl(form.getJssyyl());
        studyRoom.setJsrId(qmr.getUserId());
        studyRoom.setJsrMc(qmr.getNickName());
        studyRoom.setJssj(new Date());
        this.updateById(studyRoom);

        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("试验区域", studyRoom.getSyqy());
        formData.put("饲养间号", studyRoom.getSyjh());
        formData.put("动物种属", studyRoom.getDwzs());
        formData.put("笼具", studyRoom.getLj());
        formData.put("结束使用原因", studyRoom.getJssyyl());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Area", studyRoom.getSyqy());
        formDataEn.put("Room ID", studyRoom.getSyjh());
        formDataEn.put("Species", studyRoom.getDwzs());
        formDataEn.put("Cage", studyRoom.getLj());
        formDataEn.put("End Reason", studyRoom.getJssyyl());
        studyRoomJcgjService.saveJcgj(studyRoom.getId(), JcgjlxEnum.lc.getValue(), "饲养间使用结束", "End Usage", JcmcysEnum.blue.getValue(),
                JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
    }

}
