package com.sunda.spmswms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmswms.entity.SapBoxNote;
import com.sunda.spmswms.entity.ShelfOnTaskDtl;
import com.sunda.spmswms.entity.WithdrawTaskDtl;
import com.sunda.spmswms.mapper.WithdrawTaskDtlMapper;
import com.sunda.spmswms.service.IWithdrawTaskDtlService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 冲销任务表明细 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2021-04-25
 */
@Service
public class WithdrawTaskDtlServiceImpl extends ServiceImpl<WithdrawTaskDtlMapper, WithdrawTaskDtl> implements IWithdrawTaskDtlService {

    @Autowired
    WithdrawTaskDtlMapper withdrawTaskDtlMapper;

    @Override
    public ResponseResult generateWithdrawTaskDtlX(String uuid, String deliveryNoteSap, List<?> shelfOnTaskDtls) {
        try {
            for (int i = 0; i < shelfOnTaskDtls.size(); i++) {
                ShelfOnTaskDtl shelfOnTaskDtl =(ShelfOnTaskDtl) shelfOnTaskDtls.get(i);
                WithdrawTaskDtl withdrawTaskDtl = new WithdrawTaskDtl();
                withdrawTaskDtl.setUuid(uuid);
                withdrawTaskDtl.setDeliveryNoteSap(deliveryNoteSap);
                withdrawTaskDtl.setBoxNote(shelfOnTaskDtl.getBoxNote());
                withdrawTaskDtl.setQuantity(1.0);
                withdrawTaskDtl.setStorage(shelfOnTaskDtl.getTargetStorage());

                int insertRes = withdrawTaskDtlMapper.insert(withdrawTaskDtl);
                if (insertRes <= 0) {
                    return ResponseResult.error("Error when create WithDrawTask Dtls");
                }
            }
            return ResponseResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("Error when create WithDrawTask Dtls");
        }
    }

    @Override
    public ResponseResult generateWithdrawTaskDtlM(String uuid, String deliveryNoteSap, List<?> storageList) {
        try {
            for (int i = 0; i < storageList.size(); i++) {
                JSONObject storageObj =(JSONObject) storageList.get(i);
                WithdrawTaskDtl withdrawTaskDtl = new WithdrawTaskDtl();
                withdrawTaskDtl.setUuid(uuid);
                withdrawTaskDtl.setDeliveryNoteSap(deliveryNoteSap);
                withdrawTaskDtl.setMaterialNo(storageObj.getString("materialNo"));
                withdrawTaskDtl.setQuantity(storageObj.getDouble("qty"));
                withdrawTaskDtl.setStorage(storageObj.getString("storageUuid"));

                int insertRes = withdrawTaskDtlMapper.insert(withdrawTaskDtl);
                if (insertRes <= 0) {
                    return ResponseResult.error("Error when create WithDrawTask Dtls");
                }
            }
            return ResponseResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("Error when create WithDrawTask Dtls");
        }
    }
}
