package com.hxhq.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.StudyRoom;
import com.hxhq.business.form.study.StudyRoomHistorySearchForm;
import com.hxhq.business.form.study.StudyRoomSearchForm;
import com.hxhq.common.core.domain.MpBaseEntity;
import com.hxhq.system.api.domain.SysUser;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.StudyRoomHistoryMapper;
import com.hxhq.business.domain.StudyRoomHistory;
import com.hxhq.business.service.IStudyRoomHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 试验-饲养间更换笼具记录Service业务层处理
 *
 * @author HanLong
 * @date 2026-01-07
 */
@Service
public class StudyRoomHistoryServiceImpl extends ServiceImpl<StudyRoomHistoryMapper, StudyRoomHistory> implements IStudyRoomHistoryService
{
    /**
     * 查询试验-饲养间更换笼具记录列表
     *
     * @param form 试验-饲养间更换笼具记录
     * @return 试验-饲养间更换笼具记录
     */
    @Override
    public List<StudyRoomHistory> queryList(StudyRoomHistorySearchForm form)
    {
        LambdaQueryWrapper<StudyRoomHistory> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(form.getQyrMc())) {
            queryWrapper.like(StudyRoomHistory::getQyrMc, form.getQyrMc());
        }
        if(form.getStudyRoomId() != null) {
            queryWrapper.eq(StudyRoomHistory::getStudyRoomId, form.getStudyRoomId());
        }
        if(StringUtils.isNotEmpty(form.getStartDate())) {
            queryWrapper.ge(MpBaseEntity::getCreateTime, form.getStartDate());
        }
        if(StringUtils.isNotEmpty(form.getEndDate())) {
            queryWrapper.le(MpBaseEntity::getCreateTime, form.getEndDate());
        }
        queryWrapper.orderByDesc(MpBaseEntity::getId);
        return this.list(queryWrapper);
    }

    @Override
    public void addStudyRoomHistory(StudyRoom studyRoom, String oldLj, SysUser ghr) {
        StudyRoomHistory studyRoomHistory = new StudyRoomHistory();
        studyRoomHistory.setStudyRoomId(studyRoom.getId());
        studyRoomHistory.setLj(studyRoom.getLj());
        studyRoomHistory.setLjOld(oldLj);
        studyRoomHistory.setQyrId(ghr.getUserId());
        studyRoomHistory.setQyrMc(ghr.getNickName());
        this.save(studyRoomHistory);
    }

}
