package com.fzm.mall.server.admin.distribution.model;

import com.alibaba.fastjson.JSONObject;
import com.fzm.mall.server.admin.distribution.entity.dto.ExpressItemDTO;
import com.fzm.mall.server.admin.distribution.mapper.ParcelCenterMapper;
import com.fzm.mall.server.admin.distribution.service.IExpressService;
import com.fzm.mall.server.admin.order.model.Delivery;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 物流中心-包裹中心
 * </p>
 *
 * @author wulixian
 * @since 2021-03-04
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class ParcelCenter implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 包裹单号
     */
    private String expressNo;

    /**
     * 用户编号
     */
    private String uid;

    /**
     * 提货订单编号
     */
    private String orderDeliveryId;

    /**
     * 商品编号
     */
    private String goodsId;

    /**
     * 商品数量
     */
    private Integer number;

    /**
     * 快递公司缩写
     */
    private String expressType;

    @ApiModelProperty(value = "快递公司名称")
    private String expressName;

    /**
     * 提货状态，0->待发货，1->已发货，2->已收货，3->已撤销，4->待退货，5->已退货
     */
    private Integer deliveryState;

    /**
     * 下单时间
     */
    private Long createTime;

    /**
     * 发货时间
     */
    private Long deliveryTime;

    /**
     * 快递物流详情
     */
    private String expressDetail;

    /**
     * 快递物流详情集合
     */
    private List<ExpressItemDTO> expressDetailList;

    /**
     * 订单商品规格
     */
    private List<ParcelCenterGoods> goodsList;

    /**
     * 收货人手机号
     */
    private String phone;

    public void listGoods(ParcelCenterMapper parcelCenterMapper, ParcelCenter parcelCenter) {
        List<ParcelCenterGoods> goodsList = parcelCenterMapper.listGoods(parcelCenter.getOrderDeliveryId());

        /**
         * 获取规格对应的属性值
         */
        List<String> skuIdList = goodsList.stream().map(ParcelCenterGoods::getSkuId).collect(Collectors.toList());

        if (CollectionUtils.isNotEmpty(skuIdList)) {
            List<ParcelCenterGoods> skuPropValList = parcelCenterMapper.listPropVal(skuIdList);
            Map<String, List<ParcelCenterGoods>> map = skuPropValList.stream().
                    collect(Collectors.groupingBy(ParcelCenterGoods::getSkuId));

            goodsList.forEach(good -> {
                List<ParcelCenterGoods> parcelCenterGoodList = map.get(good.getSkuId());
                List<String> propValList = parcelCenterGoodList.stream().map(ParcelCenterGoods::getPropVal)
                        .collect(Collectors.toList());
                good.setPropValList(propValList);
            });
        }

        setGoodsList(goodsList);
    }

    public void listExpressDetail(IExpressService expressService, Delivery delivery) {
        //只有处于“已发货”的提货单，按一定的时间间隔更新物流信息；其他状态说明物流信息已稳定并已持久化到本地，无需调用第三方接口查询
        if (getDeliveryState() == Delivery.DELIVERY_STATE_SENDED) {
            String expressDetail = expressService.readExpressDetail(delivery, getExpressType(), getExpressNo(), getPhone());
            setExpressDetail(expressDetail);
        }
        setExpressDetailList(JSONObject.parseArray(getExpressDetail(), ExpressItemDTO.class));
    }
}
