package com.fjhx.victoriatourist.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.fjhx.purchase.entity.deliver.DeliverGoodsDetailsPo;
import com.fjhx.purchase.entity.deliver.DeliverGoodsPo;
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.service.abnormal.AbnormalInfoService;
import com.fjhx.victoriatourist.service.deliver.DeliverGoodsDetailsService;
import com.fjhx.victoriatourist.service.deliver.DeliverGoodsService;
import com.fjhx.victoriatourist.service.order.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

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

    /**
     * 更新发货记录的状态为到货
     */
    @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){
        AbnormalInfo abnormalInfo = new AbnormalInfo();
        abnormalInfo.setStatus(0);
        abnormalInfo.setId(IdWorker.getId());
        abnormalInfo.setType(40);
        abnormalInfo.setLinkId(linkId);
        abnormalInfo.setLinkCode(linkCode);
        abnormalInfo.setTitle("到货数量与发货数量不一致");
        abnormalInfoService.save(abnormalInfo);
    }
}
