package com.heyu.api.data.service.impl.vv;

import com.heyu.api.data.dao.vv.VvPackageDao;
import com.heyu.api.data.dao.vv.VvTradeOrderLineStatusLogDao;
import com.heyu.api.data.entity.vv.VvPackageEntity;
import com.heyu.api.data.entity.vv.VvTradeOrderEntity;
import com.heyu.api.data.entity.vv.VvTradeOrderLineEntity;
import com.heyu.api.data.entity.vv.VvTradeOrderLineStatusLogEntity;
import com.heyu.api.data.enums.OrderStatusEnums;
import com.heyu.api.data.enums.ReverseStatusEnums;
import com.heyu.api.data.kuaidi100.*;
import com.heyu.api.data.service.impl.AbstractOrderAction;
import com.heyu.api.data.service.vv.VvTradeOrderConvertService;
import com.heyu.api.data.utils.CollectionUtils;
import com.heyu.api.data.utils.SanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.MDC;

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

@Service
public class VvTradeOrderConvertServiceImpl implements VvTradeOrderConvertService {

    @Autowired
    private VvPackageDao vvPackageDao;

    @Autowired
    private VvTradeOrderLineStatusLogDao vvTradeOrderLineStatusLogDao;


    @Override
    public List<VVOrderListResp> convertTradeOrderLineResp(List<VvTradeOrderEntity> vvTradeOrderEntities,
                                                           List<VvTradeOrderLineEntity> list,boolean isApp) {
        List<String> trackNumbers = SanUtils.list2listFilterNull(list, VvTradeOrderLineEntity::getTrackNumber);
        List<VvPackageEntity> packageEntities = vvPackageDao.selectVvPackageByTrackNumbers(trackNumbers);
        Map<String, VvPackageEntity> vvPackageEntityMap = SanUtils.groupByFiled2Map(packageEntities, VvPackageEntity::getTrackNumber);

        Map<Long,List<VvTradeOrderLineEntity>> groupByTradeOrderIdMap = SanUtils.groupByFiled2MapList(list,VvTradeOrderLineEntity::getTradeOrderId );
        List<VVOrderListResp> vvOrderListResps = new ArrayList<>();
        for (VvTradeOrderEntity vvTradeOrderEntity : vvTradeOrderEntities) {
            VVOrderListResp vvOrderListResp = new VVOrderListResp();
            BeanUtils.copyProperties(vvTradeOrderEntity, vvOrderListResp);

            List<VvTradeOrderLineEntity> vvTradeOrderLineEntityList = groupByTradeOrderIdMap.get(vvTradeOrderEntity.getId());
            Map<String, List<VvTradeOrderLineEntity>> groupByTrackNumberMap = SanUtils.groupByFiled2MapList(vvTradeOrderLineEntityList, VvTradeOrderLineEntity::getTrackNumber);

            List<AdminPackageDTO> packageList = new ArrayList<>();
            for (Map.Entry<String, List<VvTradeOrderLineEntity>> vvTradeOrderLineTrackNumber : groupByTrackNumberMap.entrySet()) {
                List<VvTradeOrderLineEntity> vvTradeOrderLineTrackNumberList = vvTradeOrderLineTrackNumber.getValue();
                String trackNumber = vvTradeOrderLineTrackNumber.getKey();
                List<VvTradeOrderLineDO> vvTradeOrderLineDOList  = SanUtils.copy2List(vvTradeOrderLineTrackNumberList,VvTradeOrderLineDO.class);
                List<VvTradeOrderLineDO> mergeTradeOrderLine = SanUtils.groupMerge2List(
                        VvTradeOrderLineEntity::getSkuId,
                        vvTradeOrderLineDOList,
                        VvTradeOrderLineEntity::getPromotionPrice,
                        VvTradeOrderLineEntity::getSalePrice,
                        VvTradeOrderLineEntity::getNum,
                        VvTradeOrderLineEntity::getOriginPrice,
                        VvTradeOrderLineEntity::getShippingAmount,
                        VvTradeOrderLineEntity::getRefundAmount,
                        VvTradeOrderLineEntity::getRefundCount,
                        VvTradeOrderLineEntity::getProfitAmount,
                        VvTradeOrderLineEntity::getId
                );
                AdminPackageDTO adminPackageDTO = new AdminPackageDTO();
                VvPackageEntity vvPackage = vvPackageEntityMap.get(trackNumber);
                if(vvPackage != null){
                    BeanUtils.copyProperties(vvPackage,adminPackageDTO);
                }
                adminPackageDTO.setVvTradeOrderLineDOList(mergeTradeOrderLine);
                List<ActionDTO> orderActionList = AbstractOrderAction.getOrderActionDTO(mergeTradeOrderLine.get(0), isApp);
                adminPackageDTO.setOrderActionList(orderActionList);
                packageList.add(adminPackageDTO);

            }
            vvOrderListResp.setPackageList(packageList);
            vvOrderListResps.add(vvOrderListResp);
        }
        return vvOrderListResps;
    }

    @Override
    public int addOrderLineStatusLogList(List<Long> tradeOrderLineIdList, Object status,
                                         Long changeStatusUserId,String changeStatusUser, String... reason) {
        for (Long tradeOrderLineId : tradeOrderLineIdList) {
            addOrderLineStatusLog(tradeOrderLineId, status,changeStatusUserId, changeStatusUser, reason);
        }
        return tradeOrderLineIdList.size();
    }


    @Override
    public int addOrderLineStatusLog(Long tradeOrderLineId, Object status, Long changeStatusUserId, String changeStatusUser, String... reason) {
        VvTradeOrderLineStatusLogEntity entity = new VvTradeOrderLineStatusLogEntity();
        entity.setTradeOrderLineId(tradeOrderLineId);
        if (status instanceof OrderStatusEnums) {
            entity.setStatus(((OrderStatusEnums) status).getStatus());
            entity.setStatusDesc(((OrderStatusEnums) status).getDesc());
            entity.setType("正向");
        } else if (status instanceof ReverseStatusEnums) {
            entity.setStatus(((ReverseStatusEnums) status).getStatus());
            entity.setStatusDesc(((ReverseStatusEnums) status).getDesc());
            entity.setType("逆向");
        }
        entity.setChangeStatusUser(changeStatusUser);
        entity.setChangeStatusUserId(changeStatusUserId);
        if (reason != null && reason.length > 0) {
            entity.setReason(reason[0]);
            if (reason.length > 1) {
                entity.setFeatures(reason[1]);
            }
        }
        entity.setTraceId(MDC.get("traceId"));
        vvTradeOrderLineStatusLogDao.insertVvTradeOrderLineStatusLog(entity);
        return 1;
    }

    @Override
    public List<AppPackageDTO> buildAppPackageDTO(List<VVOrderListResp> vvOrderListResps) {
        List<AppPackageDTO> appPackageDTOList = new ArrayList<>();
        for (VVOrderListResp vvOrderListResp : vvOrderListResps) {
            VvTradeOrderEntity vvTradeOrderEntity = new VvTradeOrderEntity();
            BeanUtils.copyProperties(vvOrderListResp, vvTradeOrderEntity);
            if (CollectionUtils.isNotEmpty(vvOrderListResp.getPackageList())) {
                for (AdminPackageDTO adminPackageDTO : vvOrderListResp.getPackageList()) {
                    AppPackageDTO appPackageDTO = new AppPackageDTO();

                    appPackageDTO.setVvTradeOrderLineDOList(adminPackageDTO.getVvTradeOrderLineDOList());
                    appPackageDTO.setOrderActionList(adminPackageDTO.getOrderActionList());
                    appPackageDTO.setTradeOrderEntity(vvTradeOrderEntity);

                    VvPackageEntity vvPackageEntity = new VvPackageEntity();
                    BeanUtils.copyProperties(adminPackageDTO, vvPackageEntity);
                    appPackageDTO.setVvPackageEntity(vvPackageEntity);

                    appPackageDTOList.add(appPackageDTO);
                }
            }
        }
        return appPackageDTOList;
    }

}
