package com.prolog.cs.biz.outbound.service.impl;


import com.prolog.cs.biz.outbound.dao.BillDPrePositionMapper;
import com.prolog.cs.biz.outbound.model.entity.BillDPrePositionDO;
import com.prolog.cs.biz.outbound.model.vo.BillDPrePositionVO;
import com.prolog.cs.biz.outbound.service.BillDPrePositionService;
import com.prolog.framework.authority.core.dto.UserDTO;
import com.prolog.framework.authority.user.UserTemplate;
import com.prolog.framework.bz.core.model.ModelUtils;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.utils.MapUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author denglt
 * @since 2021-06-12
 */
@Service
//指定库位逻辑
public class BillDPrePositionServiceImpl implements BillDPrePositionService {
    @Autowired
    private BillDPrePositionMapper billDPrePositionMapper;

    @Autowired
    private UserTemplate userTemplate;

    @Override
    public RestMessage<Boolean> assignPosition(List<BillDPrePositionVO> positions) {
        UserDTO userDTO = userTemplate.getUserDTO();
        List<BillDPrePositionDO> billDPrePositionDOS = new ArrayList<>();

        for (BillDPrePositionVO position : positions) {
            String billDId = position.getBillDId();
            String billId = position.getBillId();

            Map<String, Object> map = MapUtils.put("billDId", billDId).put("billId", billId).getMap();
            List<BillDPrePositionDO> byMap = billDPrePositionMapper.findByMap(map, BillDPrePositionDO.class);
            if ((byMap != null) && (!byMap.isEmpty())) {
                List<String> collect = byMap.stream().map(BillDPrePositionDO::getId).collect(Collectors.toList());
                billDPrePositionMapper.deleteByIds(collect.toArray(), BillDPrePositionDO.class);
            }
            BillDPrePositionDO billDPrePositionDO = new BillDPrePositionDO();
            BeanUtils.copyProperties(position, billDPrePositionDO);
            billDPrePositionDOS.add(billDPrePositionDO);

            ModelUtils.setBaseInfo(billDPrePositionDO, ModelUtils.OpType.INSERT, userDTO);
        }
        if (!billDPrePositionDOS.isEmpty()) {
            billDPrePositionMapper.saveBatch(billDPrePositionDOS);
        }
        return RestMessage.doSuccess(Boolean.TRUE);
    }

    @Override
    public Boolean edit(BillDPrePositionDO entity) {
        return null;
    }

    @Override
    public Boolean isExist(String id) {
        return null;
    }

    @Override
    public RestMessage<List<BillDPrePositionDO>> getPositions(String billDId) {
        Map<String, Object> map = MapUtils.put("billDId", billDId).getMap();
        List<BillDPrePositionDO> byMap = billDPrePositionMapper.findByMap(map, BillDPrePositionDO.class);
        return RestMessage.doSuccess(byMap);
    }
}
