package com.yn.bftl.thirdparty.modules.u8.service.impl;

import com.yn.bftl.common.modules.company.entity.ErpAccountSet;
import com.yn.bftl.common.modules.inventory.entity.Warehouse;
import com.yn.bftl.common.modules.order.entity.Order;
import com.yn.bftl.common.modules.order.entity.OrderItem;
import com.yn.bftl.common.modules.order.entity.Shipping;
import com.yn.bftl.common.modules.order.entity.ShippingItem;
import com.yn.bftl.common.modules.order.enums.ShippingStatus;
import com.yn.bftl.common.modules.order.enums.SyncState;
import com.yn.bftl.thirdparty.common.exception.YnceErrorException;
import com.yn.bftl.thirdparty.common.repository.*;
import com.yn.bftl.thirdparty.modules.business.dto.U8ShippingDTO;
import com.yn.bftl.thirdparty.modules.business.dto.U8ShippingDetailDTO;
import com.yn.bftl.thirdparty.modules.u8.service.U8ShippingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Slf4j
public class U8ShippingServiceImpl implements U8ShippingService {

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private OrderItemRepository orderItemRepository;

    @Autowired
    private WarehouseRepository warehouseRepository;

    @Autowired
    private ShippingRepository shippingRepository;

    @Autowired
    private ErpAccountSetRepository erpAccountSetRepository;

    @Transactional
    public void saveU8SynData(U8ShippingDTO shippingSynDataDTO) {
        Order order = orderRepository.findFirstBySn(shippingSynDataDTO.getSn());
        if (order == null) {
            log.error("订单号不存在");
            throw new YnceErrorException("订单号不存在");
        }
        ErpAccountSet erpAccountSet = erpAccountSetRepository.findFirstByCode(shippingSynDataDTO.getErpCode());
        if (erpAccountSet == null) {
            log.error("账套号不存在");
            throw new YnceErrorException("账套号不存在");
        }
        Shipping shipping = new Shipping();
        BeanUtils.copyProperties(shippingSynDataDTO, shipping);
        shipping.setOrders(order);
        shipping.setErpAccountSet(erpAccountSet);
        List<ShippingItem> shippingItemList = new ArrayList<>();
        for (U8ShippingDetailDTO u8ShippingDetailDTO: shippingSynDataDTO.getDetailList()) {
            OrderItem orderItem = orderItemRepository.findById(Long.parseLong(u8ShippingDetailDTO.getOrderItemId())).orElse(null);
            if (orderItem == null) {
                log.error("订单项不存在");
                throw new YnceErrorException("订单项不存在");
            }
            ShippingItem shippingItem = new ShippingItem();
            BeanUtils.copyProperties(u8ShippingDetailDTO, shippingItem);
            Warehouse warehouse = warehouseRepository.findByCode(u8ShippingDetailDTO.getCode());
            if (warehouse == null) {
                log.error("仓库不存在");
                throw new YnceErrorException("仓库不存在");
            }
            shippingItem.setOutWarehouse(warehouse);
            shippingItem.setShipping(shipping);
            shippingItemList.add(shippingItem);
        }
        shipping.setShippingItem(shippingItemList);
        shipping.setUpdateState(SyncState.SYNCED);
        shipping.setDeleteState(SyncState.UNSYNCED);
        shippingRepository.save(shipping);
        // 更新订单状态
        order.setShippingStatus(ShippingStatus.SHIPPED);
        orderRepository.saveAndFlush(order);
        log.error("====================u8同步成功");
    }

    public void unShipping(String shippingSn, String erpCode) {
        ErpAccountSet erpAccountSet = erpAccountSetRepository.findFirstByCode(erpCode);
        if (erpAccountSet == null) {
            log.error("账套号不存在");
            throw new YnceErrorException("账套号不存在");
        }
        Shipping shipping = shippingRepository.findFirstByShippingSnAndErpAccountSet(shippingSn, erpAccountSet);
        if (shipping == null) {
            log.error("发货单不存在");
            throw new YnceErrorException("发货单不存在");
        }

        shipping.setDeleteState(SyncState.SYNCED);
        shipping.setUpdateState(SyncState.UNSYNCED);
        shippingRepository.saveAndFlush(shipping);
    }
}
