package com.ktg.iscs.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.iscs.domain.IsMaterials;
import com.ktg.iscs.domain.IsMaterialsChangeRecord;
import com.ktg.iscs.domain.IsMaterialsCheckRecord;
import com.ktg.iscs.domain.dto.change.ChangeRecordDTO;
import com.ktg.iscs.domain.vo.change.ChangeRecordVO;
import com.ktg.iscs.mapper.IsMaterialsChangeRecordMapper;
import com.ktg.iscs.service.IIsMaterialsChangeRecordService;
import com.ktg.iscs.service.IIsMaterialsCheckRecordService;
import com.ktg.iscs.service.IIsMaterialsService;
import com.ktg.system.domain.SysUserCharacteristic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 物资更换记录Service业务层处理
 *
 * @author cgj
 * @date 2025-01-14
 */
@Service
public class IsMaterialsChangeRecordServiceImpl extends ServiceImpl<IsMaterialsChangeRecordMapper, IsMaterialsChangeRecord> implements IIsMaterialsChangeRecordService {

    @Autowired
    private IsMaterialsChangeRecordMapper isMaterialsChangeRecordMapper;
    @Autowired
    private IIsMaterialsService iIsMaterialsService;
    @Autowired
    private IIsMaterialsCheckRecordService isMaterialsCheckRecordService;

    @Override
    public Page<ChangeRecordVO> getIsMaterialsChangeRecordPage(Page<IsMaterialsChangeRecord> page, ChangeRecordDTO dto) {
        Page<ChangeRecordVO> result = isMaterialsChangeRecordMapper.getIsMaterialsChangeRecordPage(page, dto);
        return result;
    }

    @Override
    public Boolean insertIsMaterialsChangeRecord(IsMaterialsChangeRecord changeRecord) {
        // Assert.notNull(changeRecord.getCheckRecordId(), "检查记录的ID不能为空!");
        Assert.notNull(changeRecord.getOldMaterialsId(), "原物资ID不能为空!");
        Assert.notNull(changeRecord.getNewMaterialsId(), "新物资ID不能为空，若物资不变填写原物资ID即可!");
        Assert.isFalse(changeRecord.getNewMaterialsId().equals(changeRecord.getOldMaterialsId())
                && changeRecord.getNewMaterialsRfid().equals(changeRecord.getOldMaterialsRfid()),
                "物资更换数据相同，更换失败!");
        // 查询改物资的最新报异常记录
        IsMaterialsCheckRecord checkRecord = isMaterialsCheckRecordService.getOne(Wrappers.<IsMaterialsCheckRecord>lambdaQuery()
                .eq(IsMaterialsCheckRecord::getMaterialsId, changeRecord.getOldMaterialsId())
                .orderByDesc(IsMaterialsCheckRecord::getRecordId)
                .last("offset 0 rows fetch next 1 rows only"));
        if (checkRecord != null) {
            changeRecord.setCheckRecordId(checkRecord.getRecordId());
        } else {
            changeRecord.setCheckRecordId(0L);
        }
        if (!changeRecord.getOldMaterialsId().equals(changeRecord.getNewMaterialsId())) {
            // 1.物资全部换新
            // 原物资的数据
            IsMaterials oldMaterials = iIsMaterialsService.getById(changeRecord.getOldMaterialsId());
            Assert.isFalse(oldMaterials == null, "原物资不存在");
            // 新物资的数据
            IsMaterials newMaterials = iIsMaterialsService.getById(changeRecord.getNewMaterialsId());
            Assert.isFalse(newMaterials == null, "新物资不存在");
            // 检测新物资有没有绑定该物资柜，那现在绑定下，如果已经绑定，判断是否跟原来的一样，不一样直接报错
            if (newMaterials.getMaterialsCabinetId() != null && newMaterials.getMaterialsCabinetId() != 0) {
                Assert.isTrue(oldMaterials.getMaterialsCabinetId().equals(newMaterials.getMaterialsCabinetId()), "新物资不属于当前物资柜，请检查数据！");
            } else {
                // 如果新物资的物资柜属性是空的，现在绑定下
                iIsMaterialsService.update(Wrappers.<IsMaterials>lambdaUpdate()
                        .eq(IsMaterials::getMaterialsId, changeRecord.getNewMaterialsId())
                        .set(IsMaterials::getMaterialsCabinetId, oldMaterials.getMaterialsCabinetId())
                        .set(IsMaterials::getLoanState, "1"));
            }

            // 因为id变更了，则原物资绑定物资柜的信息清理
            iIsMaterialsService.update(Wrappers.<IsMaterials>lambdaUpdate()
                    .eq(IsMaterials::getMaterialsId, changeRecord.getOldMaterialsId())
                    .set(IsMaterials::getMaterialsCabinetId, null)
                    .set(IsMaterials::getLoanState, "0"));
        } else {
            // 2.如果只是更换rfid
            iIsMaterialsService.update(Wrappers.<IsMaterials>lambdaUpdate()
                    .eq(IsMaterials::getMaterialsId, changeRecord.getOldMaterialsId())
                    .set(IsMaterials::getMaterialsRfid, changeRecord.getNewMaterialsRfid())
                    .set(IsMaterials::getStatus, 0));
        }
        changeRecord.setChangeDate(new Date());
        return save(changeRecord);
    }

}
