package com.xjrsoft.module.textbook.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.base.entity.BaseCourseSubject;
import com.xjrsoft.module.base.mapper.BaseClassMapper;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import com.xjrsoft.module.textbook.dto.*;
import com.xjrsoft.module.textbook.entity.*;
import com.xjrsoft.module.textbook.mapper.TextbookSubscriptionRecordMapper;
import com.xjrsoft.module.textbook.mapper.WfTextbookSubscriptionItemHistoryMapper;
import com.xjrsoft.module.textbook.mapper.WfTextbookSubscriptionItemMapper;
import com.xjrsoft.module.textbook.mapper.WfTextbookSubscriptionMapper;
import com.xjrsoft.module.textbook.service.ITextbookService;
import com.xjrsoft.module.textbook.service.IWfTextbookSubscriptionService;
import com.xjrsoft.module.textbook.vo.TextbookInstockroomListVo;
import com.xjrsoft.module.textbook.vo.TextbookSubscriptionExportQueryListVo;
import com.xjrsoft.module.textbook.vo.WfTextbookSubscriptionItemVo;
import com.xjrsoft.module.textbook.vo.WfTextbookSubscriptionVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * @title: 教材教辅征订
 * @Author szs
 * @Date: 2024-06-04
 * @Version 1.0
 */
@Service
@AllArgsConstructor
public class WfTextbookSubscriptionServiceImpl extends MPJBaseServiceImpl<WfTextbookSubscriptionMapper, WfTextbookSubscription> implements IWfTextbookSubscriptionService {
    private final WfTextbookSubscriptionMapper wfTextbookSubscriptionWfTextbookSubscriptionMapper;

    private final WfTextbookSubscriptionItemMapper wfTextbookSubscriptionWfTextbookSubscriptionItemMapper;

    private final TextbookSubscriptionRecordMapper textbookTextbookSubscriptionRecordMapper;

    private final WfTextbookSubscriptionItemHistoryMapper wfTextbookSubscriptionItemHistoryMapper;

    private final ITextbookService textbookService;

    private final BaseClassMapper baseClassMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(WfTextbookSubscription wfTextbookSubscription) {
        wfTextbookSubscription.setApplicantUserId(StpUtil.getLoginIdAsLong());
        wfTextbookSubscription.setStatus(1);
        wfTextbookSubscription.setCreateDate(new Date());
        wfTextbookSubscriptionWfTextbookSubscriptionMapper.insert(wfTextbookSubscription);
        for (WfTextbookSubscriptionItem wfTextbookSubscriptionItem : wfTextbookSubscription.getWfTextbookSubscriptionItemList()) {
            wfTextbookSubscriptionItem.setWfTextbookSubscriptionId(wfTextbookSubscription.getId());
            wfTextbookSubscriptionItem.setCreateDate(new Date());
            wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.insert(wfTextbookSubscriptionItem);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(WfTextbookSubscription wfTextbookSubscription) {
        wfTextbookSubscriptionWfTextbookSubscriptionMapper.updateById(wfTextbookSubscription);
        //********************************* WfTextbookSubscriptionItem  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<WfTextbookSubscriptionItem> wfTextbookSubscriptionItemList = wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.selectList(Wrappers.lambdaQuery(WfTextbookSubscriptionItem.class).eq(WfTextbookSubscriptionItem::getWfTextbookSubscriptionId, wfTextbookSubscription.getId()).select(WfTextbookSubscriptionItem::getId));
            List<Long> wfTextbookSubscriptionItemIds = wfTextbookSubscriptionItemList.stream().map(WfTextbookSubscriptionItem::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> wfTextbookSubscriptionItemOldIds = wfTextbookSubscription.getWfTextbookSubscriptionItemList().stream().map(WfTextbookSubscriptionItem::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> wfTextbookSubscriptionItemRemoveIds = wfTextbookSubscriptionItemIds.stream().filter(item -> !wfTextbookSubscriptionItemOldIds.contains(item)).collect(Collectors.toList());

            for (WfTextbookSubscriptionItem wfTextbookSubscriptionItem : wfTextbookSubscription.getWfTextbookSubscriptionItemList()) {
                //如果不等于空则修改
                if (wfTextbookSubscriptionItem.getId() != null) {
                    wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.updateById(wfTextbookSubscriptionItem);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    wfTextbookSubscriptionItem.setWfTextbookSubscriptionId(wfTextbookSubscription.getId());
                    wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.insert(wfTextbookSubscriptionItem);
                }
            }
            //已经不存在的id 删除
            if (wfTextbookSubscriptionItemRemoveIds.size() > 0) {
                wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.deleteBatchIds(wfTextbookSubscriptionItemRemoveIds);
            }
        }
        //********************************* WfTextbookSubscriptionItem  增删改  结束 *******************************************/

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean alteration(UpdateWfTextbookSubscriptionItemDto dto) {
        WfTextbookSubscriptionItem old = wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.selectById(dto.getId());
        if(old == null){
            return false;
        }

        //将历史记录保存到历史变更记录表
        LambdaQueryWrapper<WfTextbookSubscriptionItemHistory> wfTextbookSubscriptionItemHistoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wfTextbookSubscriptionItemHistoryLambdaQueryWrapper
                .eq(WfTextbookSubscriptionItemHistory::getWfTextbookSubscriptionItemId, old.getId())
                .orderByDesc(WfTextbookSubscriptionItemHistory::getHistoryVersion)
                .last("limit 1")
                ;
        WfTextbookSubscriptionItemHistory lastOne = wfTextbookSubscriptionItemHistoryMapper.selectOne(wfTextbookSubscriptionItemHistoryLambdaQueryWrapper);

        WfTextbookSubscriptionItemHistory wfTextbookSubscriptionItemHistory = new WfTextbookSubscriptionItemHistory();
        BeanUtils.copyProperties(old, wfTextbookSubscriptionItemHistory);
        wfTextbookSubscriptionItemHistory.setId(null);
        wfTextbookSubscriptionItemHistory.setWfTextbookSubscriptionItemId(old.getId());
        wfTextbookSubscriptionItemHistory.setCreateDate(new Date());
        wfTextbookSubscriptionItemHistory.setCreateUserId(StpUtil.getLoginIdAsLong());
        if(lastOne != null){
            wfTextbookSubscriptionItemHistory.setHistoryVersion(lastOne.getHistoryVersion() + 1);
        } else {
            wfTextbookSubscriptionItemHistory.setHistoryVersion(1);
        }

        wfTextbookSubscriptionItemHistoryMapper.insert(wfTextbookSubscriptionItemHistory);

        //变更当前记录
        WfTextbookSubscriptionItem updateItem = new WfTextbookSubscriptionItem();
        BeanUtils.copyProperties(dto, updateItem);

        wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.updateById(updateItem);

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        wfTextbookSubscriptionWfTextbookSubscriptionMapper.deleteBatchIds(ids);
        wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.delete(Wrappers.lambdaQuery(WfTextbookSubscriptionItem.class).in(WfTextbookSubscriptionItem::getWfTextbookSubscriptionId, ids));

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean changeStatus(UpdateWfTextbookSubscriptionDto dto) {
        WfTextbookSubscription wfTextbookSubscription = this.getById(dto.getId());
        if (wfTextbookSubscription == null) {
            return false;
        }
        //点击发起征订
        if (wfTextbookSubscription.getStatus() == 1 && dto.getStatus() == 2) {
            WfTextbookSubscription newW = BeanUtil.toBean(dto, WfTextbookSubscription.class);
            //添加征订记录
            List<WfTextbookSubscriptionItem> wfTextbookSubscriptionItemList = wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.selectList(Wrappers.<WfTextbookSubscriptionItem>query().lambda().eq(WfTextbookSubscriptionItem::getWfTextbookSubscriptionId, wfTextbookSubscription.getId()));
            for (WfTextbookSubscriptionItem w : wfTextbookSubscriptionItemList) {
                textbookTextbookSubscriptionRecordMapper.insert(new TextbookSubscriptionRecord() {{
                    setCreateDate(new Date());
                    setTextbookId(w.getTextbookId());
                    setWfTextbookSubscriptionId(wfTextbookSubscription.getId());
                    setWfTextbookSubscriptionItemId(w.getId());//该字段存入征订项的id，征订项中有所属征订记录
                }});
            }
            return this.updateById(newW);
        }


        //点击完成征订
        if (wfTextbookSubscription.getStatus() == 2 && dto.getStatus() == 4) {
            WfTextbookSubscription newW = BeanUtil.toBean(dto, WfTextbookSubscription.class);
            return this.updateById(newW);
        }
        return false;
    }

    @Override
    public WfTextbookSubscriptionVo getInfo(WfTextbookSubscriptionDto dto) {
        WfTextbookSubscription wfTextbookSubscription = this.getById(dto.getId());
        if (wfTextbookSubscription == null) {
            return null;
        }
        WfTextbookSubscriptionVo wfTextbookSubscriptionVo = BeanUtil.toBean(wfTextbookSubscription, WfTextbookSubscriptionVo.class);

        MPJLambdaWrapper<WfTextbookSubscriptionItem> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .select(WfTextbookSubscriptionItem::getId)
                .selectAs(BaseCourseSubject::getName, WfTextbookSubscriptionItemVo::getCourseSubjectIdCn)
                .select(Textbook.class, x -> VoToColumnUtil.fieldsToColumns(WfTextbookSubscriptionItemVo.class).contains(x.getProperty()))
                .select(WfTextbookSubscriptionItem.class, x -> VoToColumnUtil.fieldsToColumns(WfTextbookSubscriptionItemVo.class).contains(x.getProperty()))
                .leftJoin(Textbook.class, Textbook::getId, WfTextbookSubscriptionItem::getTextbookId)
                .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, Textbook::getTextbookType,
                        ext -> ext.selectAs(DictionaryDetail::getName, WfTextbookSubscriptionItemVo::getTextbookTypeCn))
                .leftJoin(BaseCourseSubject.class, BaseCourseSubject::getId, Textbook::getCourseSubjectId)
                .eq(WfTextbookSubscriptionItem::getWfTextbookSubscriptionId, wfTextbookSubscription.getId())
                .eq(dto.getWfTextbookSubscriptionItemId() != null && dto.getWfTextbookSubscriptionItemId() > 0, WfTextbookSubscriptionItem::getId, dto.getWfTextbookSubscriptionItemId())
                .eq(dto.getTextbookType() != null && !dto.getTextbookType().isEmpty(), Textbook::getTextbookType, dto.getTextbookType())
                .like(dto.getBookName() != null && !dto.getBookName().isEmpty(), Textbook::getBookName, dto.getBookName())
                .like(dto.getCourseSubjectIdCn() != null && !dto.getCourseSubjectIdCn().isEmpty(), BaseCourseSubject::getName, dto.getCourseSubjectIdCn())
        ;
        List<WfTextbookSubscriptionItemVo> itemList = wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.selectJoinList(WfTextbookSubscriptionItemVo.class, mpjLambdaWrapper);

        //处理班级
        String classIds = "";
        List<String> classIdList = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        if (!itemList.isEmpty() && itemList.get(0).getClassIds() != null && !itemList.get(0).getClassIds().equals("")) {
            classIds = itemList.get(0).getClassIds();
            String[] classIdArr = classIds.split(",");
            classIdList = Arrays.asList(classIdArr);
            if (!classIdList.isEmpty()) {
                List<BaseClass> baseClassList = baseClassMapper.selectList(Wrappers.<BaseClass>query().lambda().in(BaseClass::getId, classIdList));
                Map<Long, BaseClass> baseClassMap = baseClassList.stream()
                        .collect(Collectors.toMap(BaseClass::getId, baseClass -> baseClass));

                for (String classId : classIdList) {
                    BaseClass baseClass = baseClassMap.get(Long.parseLong(classId));
                    if (baseClass != null) {
                        sb.append(baseClass.getName());
                        sb.append(",");
                    }
                }
                sb.deleteCharAt(sb.length() - 1);
            }
        }

        //遍历处理所有项
        int stuSum = 0;
        int teaSum = 0;
        for (WfTextbookSubscriptionItemVo w : itemList) {
            int sum = 0;
            if (!classIdList.isEmpty()) {
                w.setClassNum(classIdList.size());
            }
            if (w.getStudentSubscriptionNumber() != null) {
                stuSum += w.getStudentSubscriptionNumber();
                sum += w.getStudentSubscriptionNumber();
            }
            if (w.getTeacherSubscriptionNumber() != null) {
                teaSum += w.getTeacherSubscriptionNumber();
                sum += w.getTeacherSubscriptionNumber();
            }
            w.setSumNumber(sum);
        }

        wfTextbookSubscriptionVo.setClassIdsCn(sb.toString());
        wfTextbookSubscriptionVo.setClassIds(classIds);
        wfTextbookSubscriptionVo.setStudentSubscriptionNumberSum(stuSum);
        wfTextbookSubscriptionVo.setTeacherSubscriptionNumberSum(teaSum);
        wfTextbookSubscriptionVo.setSum(stuSum + teaSum);
        wfTextbookSubscriptionVo.setWfTextbookSubscriptionItemList(itemList);

        return wfTextbookSubscriptionVo;
    }

    @Override
    public List<TextbookSubscriptionExportQueryListVo> textbookSubscriptionExportQuery(TextbookSubscriptionExportQueryListDto dto) {
        WfTextbookSubscription wfTextbookSubscription = this.getById(dto.getWfTextbookSubscriptionId());
        if (wfTextbookSubscription == null) {
            return null;
        }

        MPJLambdaWrapper<WfTextbookSubscriptionItem> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .selectAs(BaseCourseSubject::getName, TextbookSubscriptionExportQueryListVo::getCourseName)
                .select(WfTextbookSubscriptionItem.class, x -> VoToColumnUtil.fieldsToColumns(TextbookSubscriptionExportQueryListVo.class).contains(x.getProperty()))
                .select(Textbook.class, x -> VoToColumnUtil.fieldsToColumns(TextbookSubscriptionExportQueryListVo.class).contains(x.getProperty()))
                .leftJoin(Textbook.class, Textbook::getId, WfTextbookSubscriptionItem::getTextbookId)
                .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, Textbook::getTextbookType,
                        ext -> ext.selectAs(DictionaryDetail::getName, TextbookSubscriptionExportQueryListVo::getTextbookTypeCn))
                .leftJoin(BaseCourseSubject.class, BaseCourseSubject::getId, Textbook::getCourseSubjectId)
                .eq(WfTextbookSubscriptionItem::getWfTextbookSubscriptionId, wfTextbookSubscription.getId())
        ;
        List<TextbookSubscriptionExportQueryListVo> itemList = wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.selectJoinList(TextbookSubscriptionExportQueryListVo.class, mpjLambdaWrapper);

        //处理班级
        String classIds = "";
        List<String> classIdList = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        if (!itemList.isEmpty() && itemList.get(0).getClassIds() != null && !itemList.get(0).getClassIds().equals("")) {
            classIds = itemList.get(0).getClassIds();
            String[] classIdArr = classIds.split(",");
            classIdList = Arrays.asList(classIdArr);
            /*if(!classIdList.isEmpty()){
                List<BaseClass> baseClassList = baseClassMapper.selectList(Wrappers.<BaseClass>query().lambda().in(BaseClass::getId, classIdList));
                Map<Long, BaseClass> baseClassMap = baseClassList.stream()
                        .collect(Collectors.toMap(BaseClass::getId, baseClass -> baseClass));

                for (String classId : classIdList){
                    BaseClass baseClass = baseClassMap.get(Long.parseLong(classId));
                    if(baseClass != null){
                        sb.append(baseClass.getName());
                        sb.append(",");
                    }
                }
                sb.deleteCharAt(sb.length() - 1);
            }*/
        }

        for (TextbookSubscriptionExportQueryListVo w : itemList) {
            w.setClassNum(classIdList.size());
        }

        return itemList;
    }

    @Override
    public List<TextbookInstockroomListVo> instockroomList(TextbookInstockroomListDto dto) {
        MPJLambdaWrapper<WfTextbookSubscriptionItem> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .select(WfTextbookSubscriptionItem::getId)
                .selectAs(BaseCourseSubject::getName, TextbookInstockroomListVo::getCourseName)
                .selectAs(DictionaryDetail::getName, TextbookInstockroomListVo::getTextbookTypeCn)
                .select("(t.student_subscription_number + t.teacher_subscription_number) as subscriptionNumber")
                .select(Textbook.class, x -> VoToColumnUtil.fieldsToColumns(TextbookInstockroomListVo.class).contains(x.getProperty()))
                .select(WfTextbookSubscriptionItem.class, x -> VoToColumnUtil.fieldsToColumns(TextbookInstockroomListVo.class).contains(x.getProperty()))
                .leftJoin(Textbook.class, Textbook::getId, WfTextbookSubscriptionItem::getTextbookId)
                .leftJoin(BaseCourseSubject.class, BaseCourseSubject::getId, Textbook::getCourseSubjectId)
                .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, Textbook::getTextbookType)
                .eq(WfTextbookSubscriptionItem::getWfTextbookSubscriptionId, dto.getWfTextbookSubscriptionId())
                .orderByAsc(WfTextbookSubscriptionItem::getInStockroomNum)
        ;

        return wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.selectJoinList(TextbookInstockroomListVo.class, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean instockroom(List<TextbookInstockroomDto> dtos) {
        if (dtos.isEmpty()) {
            return false;
        }
        Map<String, Object> result = saveData(dtos);
        return result != null && (int) result.get("successNum") > 0;
    }

    @Override
    public String excelImport(InputStream inputStream, Long wfTextbookSubscriptionId) {
        AtomicReference<Map<String, Object>> atomicMap = new AtomicReference<>(new HashMap<>());
        EasyExcel.read(inputStream, TextbookInstockroomImportDto.class, new PageReadListener<TextbookInstockroomImportDto>(dataList -> {
            if (dataList.isEmpty()) {
                throw new MyException("导入数据为空");
            }

            List<TextbookInstockroomDto> textbookInstockroomDtoList = new ArrayList<>();
            for (TextbookInstockroomImportDto item : dataList){
                textbookInstockroomDtoList.add(new TextbookInstockroomDto(){{
                    setWfTextbookSubscriptionId(wfTextbookSubscriptionId);
                    setIssn(item.getIssn());
                    setInNum(item.getInNum());
                }});
            }
            atomicMap.set(saveData(textbookInstockroomDtoList));
        })).sheet().doRead();

        if(atomicMap.get() != null){
            return "计划入库教材" + atomicMap.get().get("allNum") + "样，成功入库" + atomicMap.get().get("successNum") + "样，成功入库" + atomicMap.get().get("instockroomNum") + "本";
        }
        return "计划入库教材0样，成功入库0样，成功入库0本";
    }

    private Map<String, Object> saveData(List<TextbookInstockroomDto> dtos) {
        Map<String, Object> result = new HashMap<>();
        int successNum = 0;
        int instockroomNum = 0;
        //获取需要修改的list
        MPJLambdaWrapper<WfTextbookSubscriptionItem> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .select(WfTextbookSubscriptionItem::getId)
                .selectAs(Textbook::getIssn, WfTextbookSubscriptionItem::getIssn)
                .select(WfTextbookSubscriptionItem.class, x -> VoToColumnUtil.fieldsToColumns(WfTextbookSubscriptionItem.class).contains(x.getProperty()))
                .leftJoin(Textbook.class, Textbook::getId, WfTextbookSubscriptionItem::getTextbookId)
                .eq(WfTextbookSubscriptionItem::getWfTextbookSubscriptionId, dtos.get(0).getWfTextbookSubscriptionId())
                .eq(WfTextbookSubscriptionItem::getDeleteMark, DeleteMark.NODELETE.getCode())
        ;
        List<WfTextbookSubscriptionItem> wfTextbookSubscriptionItemList = wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.selectList(queryWrapper);

        if (wfTextbookSubscriptionItemList.isEmpty()) {
            return null;
        }

        Map<Long, WfTextbookSubscriptionItem> itemByIdMap = wfTextbookSubscriptionItemList
                .stream()
                .collect(Collectors.toMap(
                        WfTextbookSubscriptionItem::getId,
                        item -> item
                ));

        Map<String, WfTextbookSubscriptionItem> itemByIssnMap = wfTextbookSubscriptionItemList
                .stream()
                .collect(Collectors.toMap(
                        WfTextbookSubscriptionItem::getIssn,
                        item -> item
                ));

        Date now = new Date();
        Long loginId = StpUtil.getLoginIdAsLong();
        for (TextbookInstockroomDto dto : dtos) {
            WfTextbookSubscriptionItem old;
            if(dto.getId() != null && dto.getId() > 0){
                old = itemByIdMap.get(dto.getId());
            } else {
                old = itemByIssnMap.get(dto.getIssn());
            }

            if(old == null){
                continue;
            }

            WfTextbookSubscriptionItem updateItem = new WfTextbookSubscriptionItem();
            updateItem.setId(old.getId());
            updateItem.setInStockroomNum(old.getInStockroomNum() + dto.getInNum());
            updateItem.setModifyDate(now);
            updateItem.setModifyUserId(loginId);
            wfTextbookSubscriptionWfTextbookSubscriptionItemMapper.updateById(updateItem);

            //新增入库记录
            textbookService.deliverWarehouse(new AddTextbookWarehouseRecordDto() {{
                setTextbookId(old.getTextbookId());
                setWarehouseNumber(dto.getInNum());
                setSource("wm_manual");
            }});
            successNum++;
            instockroomNum += dto.getInNum();
        }
        result.put("allNum", dtos.size());
        result.put("successNum", successNum);
        result.put("instockroomNum", instockroomNum);

        return result;
    }
}
