/**
 * Project Name:eis-jxyj-shelf-work
 * File Name:TaskToHisServiceImpl.java
 * Package Name:com.prolog.eis.service.history.impl
 * Date:2022年11月2日下午3:59:37
 * Copyright (c) 2022, bluemobi All Rights Reserved.
 *
*/

package com.prolog.eis.service.shelves.impl;

import com.prolog.eis.dao.shelves.BizInboundTaskDetailHisMapper;
import com.prolog.eis.dao.shelves.BizInboundTaskDetailMapper;
import com.prolog.eis.dao.shelves.BizInboundTaskHisMapper;
import com.prolog.eis.enums.base.BaseStatusEnum;
import com.prolog.eis.model.inbound.BizInboundTask;
import com.prolog.eis.model.inbound.BizInboundTaskDetail;
import com.prolog.eis.model.inbound.BizInboundTaskDetailHis;
import com.prolog.eis.model.inbound.BizInboundTaskHis;
import com.prolog.eis.service.shelves.TaskToHisService;
import com.prolog.framework.beans.BeanUtils;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.framework.utils.MapUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:任务转历史 <br/>
 * Date: 2022年11月2日 下午3:59:37 <br/>
 * 
 * @version
 * @see
 */
@Service
public class TaskToHisServiceImpl implements TaskToHisService {

    @Resource
    BizInboundTaskHisMapper bizInboundTaskHisMapper;

    @Resource
    BizInboundTaskDetailMapper bizInboundTaskDetailMapper;

    @Resource
    BizInboundTaskDetailHisMapper bizInboundTaskDetailHisMapper;

    @Override
    public void toInHistory(BizInboundTask inBoundTask) {
        BizInboundTaskHis wmsInTaskHis = new BizInboundTaskHis();
        inBoundTask.setStatus(BaseStatusEnum.CALLBACK_FINISH.getCode());
        inBoundTask.setCallBackTime(LocalDateTime.now());
        BeanUtils.copyProperties(inBoundTask, wmsInTaskHis);
        bizInboundTaskHisMapper.save(wmsInTaskHis);
        List<BizInboundTaskDetail> detailList = bizInboundTaskDetailMapper.findByMap(MapUtils.put("inboundId", inBoundTask.getId()).getMap(), BizInboundTaskDetail.class);
        if (CollectionUtils.isEmpty(detailList)) {
            throw new PrologException("入库明细查询为空");
        }
        ArrayList<BizInboundTaskDetailHis> taskDetailHis = new ArrayList<>();
        for (BizInboundTaskDetail bizInboundTaskDetail : detailList) {
            BizInboundTaskDetailHis bizInboundTaskDetailHis = new BizInboundTaskDetailHis();
            BeanUtils.copyProperties(bizInboundTaskDetail, bizInboundTaskDetailHis);
            taskDetailHis.add(bizInboundTaskDetailHis);
        }
        bizInboundTaskDetailHisMapper.saveBatch(taskDetailHis);
    }

}
