package com.fjhx.victoriatourist.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fjhx.area.utils.CustomizeAreaUtil;
import com.fjhx.victoriatourist.entity.abnormal.po.AbnormalInfo;
import com.fjhx.victoriatourist.entity.group.dto.GroupRecordDto;
import com.fjhx.victoriatourist.entity.group.po.GroupRecordDetails;
import com.fjhx.victoriatourist.entity.jd.dto.JdOrderDto;
import com.fjhx.victoriatourist.entity.jd.po.JdOrderDetails;
import com.fjhx.victoriatourist.entity.logistics.po.LogisticsInfos;
import com.fjhx.victoriatourist.entity.purchase.po.PurchaseBack;
import com.fjhx.victoriatourist.entity.purchase.vo.PurchaseBackVo;
import com.fjhx.victoriatourist.entity.quality.po.QualityDetails;
import com.fjhx.victoriatourist.service.abnormal.AbnormalInfoService;
import com.fjhx.victoriatourist.service.group.GroupRecordDetailsService;
import com.fjhx.victoriatourist.service.jd.JdOrderService;
import com.fjhx.victoriatourist.service.logistics.LogisticsInfosService;
import com.fjhx.victoriatourist.service.order.OrderInfoService;
import com.fjhx.victoriatourist.service.purchase.PurchaseBackService;
import com.fjhx.victoriatourist.service.quality.QualityDetailsService;
import com.fjhx.wms.entity.QualityDetailsPo;
import com.fjhx.wms.entity.stock.dto.StockWaitDto;
import com.fjhx.wms.entity.stock.po.StockWait;
import com.fjhx.wms.entity.stock.po.StockWaitDetails;
import com.fjhx.wms.service.WmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WmsServiceImpl implements WmsService {
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private AbnormalInfoService abnormalInfoService;
    @Autowired
    private PurchaseBackService purchaseBackService;
    @Autowired
    private QualityDetailsService qualityDetailsService;
    @Autowired
    private GroupRecordDetailsService groupRecordDetailsService;
    @Autowired
    private JdOrderService jdOrderService;
    @Autowired
    private LogisticsInfosService logisticsInfosService;

//    /**
//     * 销售订单出库
//     */
//    @Override
//    public void outbound(StockWaitDetailsDto stockWaitDetailsDto) {
//        orderInfoService.outbound(stockWaitDetailsDto);
//    }

    @Override
    public void outbounds(StockWaitDto stockWaitDto) {
        orderInfoService.outbounds(stockWaitDto);
    }

    @Override
    public void addEndInStockAbnormal(StockWait stockWait, Long purchaseId) {
        String swJsonStr = stockWait.getVictoriatouristJson();
        JSONObject swJson = ObjectUtil.isEmpty(swJsonStr) ? new JSONObject() : JSONObject.parseObject(swJsonStr);
        Long deliverGoodsId = swJson.getLong("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,50)
            );
            if (ObjectUtil.isNotEmpty(oldAbnormalInfo)) {
                abnormalInfo = oldAbnormalInfo;
            }
        }

        abnormalInfo.setStatus(0);
        abnormalInfo.setType(50);//结束入库
        abnormalInfo.setLinkId(stockWait.getId());
        abnormalInfo.setLinkCode(stockWait.getBusinessCode());
        abnormalInfo.setTitle("入库数量差异");
        abnormalInfo.setPurchaseId(purchaseId);
        abnormalInfoService.saveOrUpdate(abnormalInfo);
    }

    @Override
    public Object getPurchaseBackInfo(Long id) {
        PurchaseBack purchaseBack = purchaseBackService.getById(id);
        PurchaseBackVo purchaseBackVo = BeanUtil.copyProperties(purchaseBack, PurchaseBackVo.class);
        // 赋值国省市
        CustomizeAreaUtil.setAreaName(purchaseBackVo);
        return purchaseBackVo;
    }

    @Override
    public List<QualityDetailsPo> getQualityDetailsList(List<Long> arrivalStockRecordsDetailsIds) {
        List<QualityDetails> list = qualityDetailsService.list(q -> q.in(QualityDetails::getArrivalStockRecordsDetailsId, arrivalStockRecordsDetailsIds));
        List<QualityDetailsPo> qualityDetailsPos = BeanUtil.copyToList(list, QualityDetailsPo.class);
        return qualityDetailsPos;
    }

    @Override
    public void autoCombination(Long productId, BigDecimal quantity, Long warehouseId) {
        GroupRecordDto dto = new GroupRecordDto();
        dto.setType(1);
        dto.setGroupWarehouseId(warehouseId);
        dto.setProductWarehouseId(warehouseId);
        dto.setRemark("直接出库系统自动组合");

        GroupRecordDetails groupRecordDetails = new GroupRecordDetails();
        groupRecordDetails.setProductId(productId);
        groupRecordDetails.setGroupNum(quantity);

        dto.setGroupRecordDetailsList(Arrays.asList(groupRecordDetails));

        //调用组合接口自动组合产品
        groupRecordDetailsService.add(dto);
    }

    @Override
    public void jdOrderOutbound(StockWaitDto stockWaitDto) {
        JdOrderDto jdOrderDto = new JdOrderDto();
        jdOrderDto.setId(stockWaitDto.getBusinessId());
        jdOrderDto.setInWarehouseId(stockWaitDto.getInWarehouseId());
        jdOrderDto.setOutWarehouseId(stockWaitDto.getOutWarehouseId());
        jdOrderDto.setReservationCode(stockWaitDto.getReservationCode());
        jdOrderDto.setRemark(stockWaitDto.getRemark());
        jdOrderDto.setLogisticsCompanyCode(stockWaitDto.getLogisticsCompanyCode());
        jdOrderDto.setLogisticsCode(stockWaitDto.getLogisticsCode());

        List<StockWaitDetails> stockWaitDetailsList = stockWaitDto.getStockWaitDetailsList();
        for (StockWaitDetails stockWaitDetails : stockWaitDetailsList) {
            JdOrderDetails jdOrderDetails = new JdOrderDetails();
            jdOrderDetails.setId(stockWaitDetails.getBusinessDetailsId());
            jdOrderDetails.setProductId(stockWaitDetails.getProductId());
            jdOrderDetails.setQuantity(stockWaitDetails.getQuantity());
        }
        jdOrderService.outbound(jdOrderDto);
    }

    @Override
    public void purchaseBackOutbound(StockWaitDto stockWaitDto) {
        purchaseBackService.backOuts(stockWaitDto);
    }
}
