package com.yh.saas.plugin.yiliangyiyun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yh.saas.plugin.yiliangyiyun.entity.GrainDeliveryRegistration;
import com.yh.saas.plugin.yiliangyiyun.entity.ShippingInformation;
import com.yh.saas.plugin.yiliangyiyun.mapper.ShippingInformationMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IGrainDeliveryRegistrationService;
import com.yh.saas.plugin.yiliangyiyun.service.IShippingInformationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 送货信息 服务实现类
 * </p>
 *
 * @author Gdc
 * @since 2021-11-27
 */
@Service
public class ShippingInformationServiceImpl extends ServiceImpl<ShippingInformationMapper, ShippingInformation> implements IShippingInformationService {
    @Autowired
    private IGrainDeliveryRegistrationService grainDeliveryRegistrationService;

    /**
     * PC质检车牌号下拉
     * @param compId
     * @param warehouseName
     * @return
     */
    @Override
    public List<ShippingInformation> selectShippingInformation(String compId, String warehouseName) {
        List<ShippingInformation> shippingInformationList=new ArrayList<>();
        List<GrainDeliveryRegistration> grainDeliveryRegistrationList=grainDeliveryRegistrationService.selectList(new EntityWrapper<GrainDeliveryRegistration>()
                .eq("comp_id",compId)
                .eq("warehouse_name",warehouseName));
        if (CollectionUtils.isNotEmpty(grainDeliveryRegistrationList)){
            for(GrainDeliveryRegistration grainDeliveryRegistration:grainDeliveryRegistrationList){
                ShippingInformation shippingInformation=this.selectOne(new EntityWrapper<ShippingInformation>()
                .eq("grain_id",grainDeliveryRegistration.getId()));
                String[] carNo = shippingInformation.getNotCarNo().split(",");
                for (int i = 0; i < carNo.length; i++) {
                    ShippingInformation tmp = new ShippingInformation();
                    tmp.setCarNo(carNo[i]);
                    tmp.setIdentityId(shippingInformation.getIdentityId());
                    tmp.setShipperName(shippingInformation.getShipperName());
                    tmp.setShipperPhone(shippingInformation.getShipperPhone());
                    tmp.setSupplier(shippingInformation.getSupplier());
                    tmp.setCustomerNumberCard(shippingInformation.getCustomerNumberCard());
                    tmp.setValidityDate(grainDeliveryRegistration.getValidityDate());
                    shippingInformationList.add(tmp);
                }
            }
        }
        return shippingInformationList;
    }
}
