package com.ktg.iscs.service.impl;

import cn.hutool.core.lang.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.google.common.collect.Lists;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.iscs.domain.IsMaterials;
import com.ktg.iscs.domain.IsMaterialsCabinet;
import com.ktg.iscs.domain.IsMaterialsCheckRecord;
import com.ktg.iscs.domain.IsMaterialsPlanCabinet;
import com.ktg.iscs.mapper.IsMaterialsPlanCabinetMapper;
import com.ktg.iscs.service.IIsMaterialsCabinetService;
import com.ktg.iscs.service.IIsMaterialsCheckRecordService;
import com.ktg.iscs.service.IIsMaterialsPlanCabinetService;
import com.ktg.iscs.service.IIsMaterialsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 物资检查计划关联物资柜Service业务层处理
 *
 * @author cgj
 * @date 2025-01-14
 */
@Service
public class IsMaterialsPlanCabinetServiceImpl extends ServiceImpl<IsMaterialsPlanCabinetMapper, IsMaterialsPlanCabinet> implements IIsMaterialsPlanCabinetService {

    @Autowired
    private IsMaterialsPlanCabinetMapper isMaterialsPlanCabinetMapper;
    @Autowired
    private IIsMaterialsCabinetService iIsMaterialsCabinetService;
    @Autowired
    private IIsMaterialsCheckRecordService isMaterialsCheckRecordService;
    @Autowired
    private IIsMaterialsService iIsMaterialsService;

    @Override
    public Page<IsMaterialsPlanCabinet> getIsMaterialsPlanCabinetPage(Page<IsMaterialsPlanCabinet> page, IsMaterialsPlanCabinet isMaterialsPlanCabinet) {
        Page<IsMaterialsPlanCabinet> result = page(page, Wrappers.<IsMaterialsPlanCabinet>lambdaQuery()
                .orderByDesc(IsMaterialsPlanCabinet::getPlanId));
        return result;
    }

    @Override
    public List<IsMaterialsPlanCabinet> getCheckPlanCabinetList(IsMaterialsPlanCabinet dto) {
        return isMaterialsPlanCabinetMapper.getCheckPlanCabinetList(dto);
    }

    @Override
    public Boolean autoChangeCheckRecord(String cabinetCode) {
        IsMaterialsCabinet cabinet = iIsMaterialsCabinetService.getCabinetByCode(cabinetCode);
        Assert.isFalse(cabinet == null, "柜子不存在！");
        // 1.获取这个物资柜当前最近一次未完成的检查计划
        IsMaterialsPlanCabinet planCabinet = getOne(Wrappers.<IsMaterialsPlanCabinet>lambdaQuery()
                .eq(IsMaterialsPlanCabinet::getCabinetId, cabinet.getCabinetId())
                .ne(IsMaterialsPlanCabinet::getStatus, "1")
                .orderByDesc(IsMaterialsPlanCabinet::getPlanId)
                .last("offset 0 rows fetch next 1 rows only"));
        if (planCabinet != null) {
            // 2.检查有没有生成检查记录
            List<IsMaterialsCheckRecord> checkRecords = isMaterialsCheckRecordService.list(Wrappers.<IsMaterialsCheckRecord>lambdaQuery()
                    .eq(IsMaterialsCheckRecord::getPlanId, planCabinet.getPlanId())
                    .eq(IsMaterialsCheckRecord::getCabinetId, cabinet.getCabinetId()));
            if (checkRecords.isEmpty()) {
                return true;
            } else {
                // 柜子中的物资信息
                List<IsMaterials> cabinetMaterials = iIsMaterialsService.list(Wrappers.<IsMaterials>lambdaQuery()
                        .eq(IsMaterials::getMaterialsCabinetId, cabinet.getCabinetId())
                        .eq(IsMaterials::getLoanState, "1"));
                // 3.0如果有借出则需要删除
                needDel(Lists.newArrayList(checkRecords), Lists.newArrayList(cabinetMaterials));
                // 3.1如果有还入需要新增
                needAdd(planCabinet.getPlanId(), cabinet.getCabinetId(), Lists.newArrayList(checkRecords), Lists.newArrayList(cabinetMaterials));
            }
        }
        return true;
    }

    private List<Long> needDel(List<IsMaterialsCheckRecord> checkRecords, List<IsMaterials> cabinetMaterials) {
        // 需要删除的物资
        checkRecords.removeIf(item -> cabinetMaterials.stream().map(IsMaterials::getMaterialsId).collect(Collectors.toList()).contains(item.getMaterialsId()));
        List<Long> collect = checkRecords.stream().map(IsMaterialsCheckRecord::getRecordId).collect(Collectors.toList());
        if (!collect.isEmpty()) {
            isMaterialsCheckRecordService.removeByIds(collect);
        }
        return collect;
    }

    private List<IsMaterials> needAdd(Long planId, Long cabinetId, List<IsMaterialsCheckRecord> checkRecords, List<IsMaterials> cabinetMaterials) {
        // 需要删除的物资
        cabinetMaterials.removeIf(item -> checkRecords.stream().map(IsMaterialsCheckRecord::getMaterialsId).collect(Collectors.toList()).contains(item.getMaterialsId()));
        for (IsMaterials vo : cabinetMaterials) {
            IsMaterialsCheckRecord checkRecord = new IsMaterialsCheckRecord();
            checkRecord.setPlanId(planId);
            checkRecord.setCabinetId(cabinetId);
            checkRecord.setMaterialsId(vo.getMaterialsId());
            checkRecord.setCheckUserId(SecurityUtils.getUserId());
            checkRecord.setCheckDate(new Date());
            checkRecord.setMaterialsRfid(vo.getMaterialsRfid());
            isMaterialsCheckRecordService.save(checkRecord);
        }
        return cabinetMaterials;
    }

}
