package com.fjhx.victoriatourist.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fjhx.purchase.entity.deliver.DeliverGoodsDetailsPo;
import com.fjhx.purchase.entity.deliver.DeliverGoodsPo;
import com.fjhx.purchase.entity.purchase.vo.PurchaseVo;
import com.fjhx.purchase.service.WdlyService;
import com.fjhx.victoriatourist.entity.abnormal.po.AbnormalInfo;
import com.fjhx.victoriatourist.entity.deliver.po.DeliverGoods;
import com.fjhx.victoriatourist.entity.deliver.po.DeliverGoodsDetails;
import com.fjhx.victoriatourist.entity.logistics.po.LogisticsInfos;
import com.fjhx.victoriatourist.service.abnormal.AbnormalInfoService;
import com.fjhx.victoriatourist.service.deliver.DeliverGoodsDetailsService;
import com.fjhx.victoriatourist.service.deliver.DeliverGoodsService;
import com.fjhx.victoriatourist.service.logistics.LogisticsInfosService;
import com.fjhx.victoriatourist.service.order.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class WdlyServiceImpl implements WdlyService {
    @Autowired
    DeliverGoodsService deliverGoodsService;
    @Autowired
    OrderInfoService orderInfoService;
    @Autowired
    DeliverGoodsDetailsService deliverGoodsDetailsService;
    @Autowired
    AbnormalInfoService abnormalInfoService;
    @Autowired
    private LogisticsInfosService logisticsInfosService;

    /**
     * 更新发货记录的状态为到货
     */
    @Override
    public void updateDeliverGoodsStatus(Long id) {
        DeliverGoods byId = deliverGoodsService.getById(id);
        byId.setStatus(1);
        deliverGoodsService.updateById(byId);
    }

    /**
     * 获取发货信息
     */
    @Override
    public DeliverGoodsPo getDeliverGoodsInfo(Long id) {
        DeliverGoods byId = deliverGoodsService.getById(id);
        DeliverGoodsPo deliverGoodsPo = BeanUtil.copyProperties(byId, DeliverGoodsPo.class);
        return deliverGoodsPo;
    }

    @Override
    public List<DeliverGoodsDetailsPo> getDeliverGoodsDetailsInfo(Long id) {
        List<DeliverGoodsDetails> list = deliverGoodsDetailsService.list(q -> q.eq(DeliverGoodsDetails::getDeliverGoodsId, id));
        List<DeliverGoodsDetailsPo> deliverGoodsDetailsPos = BeanUtil.copyToList(list, DeliverGoodsDetailsPo.class);
        return deliverGoodsDetailsPos;
    }

    @Override
    public void addAbnormalInfo(Long linkId, String linkCode, Long deliverGoodsId){
        LogisticsInfos logisticsInfos = logisticsInfosService.getOne(q -> q.eq(LogisticsInfos::getBusinessId, deliverGoodsId));

        AbnormalInfo abnormalInfo = new AbnormalInfo();

        //搜索物流单号是否已经存在 异常信息
        List<LogisticsInfos> logisticsInfosList = logisticsInfosService.list(q -> q.eq(LogisticsInfos::getCode, logisticsInfos.getCode()));
        if (ObjectUtil.isNotEmpty(logisticsInfosList)) {
            List<Long> ids = logisticsInfosList.stream().map(LogisticsInfos::getId).collect(Collectors.toList());
            AbnormalInfo oldAbnormalInfo = abnormalInfoService.getOne(q -> q
                    .in(AbnormalInfo::getLogisticsInfosId, ids)
                    .eq(AbnormalInfo::getType,40)
            );
            if (ObjectUtil.isNotEmpty(oldAbnormalInfo)) {
                abnormalInfo = oldAbnormalInfo;
            }
        }

        abnormalInfo.setStatus(0);
        abnormalInfo.setType(40);
        abnormalInfo.setLinkId(linkId);
        abnormalInfo.setLinkCode(linkCode);
        abnormalInfo.setTitle("到货数量与发货数量不一致");
        abnormalInfo.setLogisticsInfosId(logisticsInfos.getId());
        abnormalInfoService.saveOrUpdate(abnormalInfo);
    }

    @Override
    public PurchaseVo arrivalDetail(Long purchaseId){
       return  deliverGoodsService.arrivalDetail( purchaseId);
    }
}
