package com.hzs.sale.deliver.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SaOrderFieldConstants;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.sale.deliver.SaDeliverItems;
import com.hzs.sale.deliver.mapper.SaDeliverItemsMapper;
import com.hzs.sale.deliver.service.ISaDeliverItemsService;
import com.hzs.sale.deliver.vo.DeliverBarCodeVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 发货单明细表 服务实现类
 *
 * @author hzs
 * @since 2022-08-31
 */
@Service
public class SaDeliverItemsServiceImpl extends ServiceImpl<SaDeliverItemsMapper, SaDeliverItems> implements ISaDeliverItemsService {

    @Override
    public int insertBatch(List<SaDeliverItems> list) {
        return baseMapper.insertBatch(list);
    }

    @Override
    public List<DeliverBarCodeVO> deliverCodeData(List<Long> idList, List<Long> orderIdList, Integer pkCountry, Integer bind) {
        return baseMapper.deliverCodeData(idList, orderIdList, pkCountry, bind);
    }

    @Override
    public List<SaDeliverItems> queryDeliverItemsByOrder(Long pkOrder,
                                                         List<Long> pkOrderItemList,
                                                         List<Integer> orderStatusList,
                                                         Integer pkCountry) {
        return baseMapper.queryDeliverItemsByOrder(pkOrder, pkOrderItemList, orderStatusList, pkCountry);
    }

    @Override
    public Boolean validateOrderBindBarCode(String orderCode, List<Long> itemIdList) {
        List<SaDeliverItems> saDeliverItems = baseMapper.querySaDeliverItemByOrder(orderCode, itemIdList);
        if(CollectionUtil.isNotEmpty(saDeliverItems)){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Long queryPkMemberByDeliverItemsId(Long pkId) {
        return baseMapper.queryPkMemberByDeliverItemsId(pkId);
    }

    @Override
    public void deleteSaDeliverItems(Long pkOrder, List<Long> pkOrderItemsList) {
        UpdateWrapper<SaDeliverItems> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(SystemFieldConstants.DEL_FLAG, EYesNo.NO.getIntValue());
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, DateUtils.currentDateTime());
        updateWrapper.eq(SaOrderFieldConstants.PK_ORDER, pkOrder);
        if(pkOrderItemsList != null && pkOrderItemsList.size() >0){
            updateWrapper.in(SaOrderFieldConstants.PK_ORDER_ITEMS, pkOrderItemsList);
        }
        update(updateWrapper);
    }
}
