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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.*;
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.model.ParcelCenterGoods;
import com.fzm.mall.server.admin.distribution.service.IExpressService;
import com.fzm.mall.server.admin.order.service.IDeliveryLogService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * <p>
 * 用户提货表
 * </p>
 *
 * @author wulixian
 * @since 2021-03-18
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("order_delivery")
@ApiModel(value = "Delivery对象", description = "用户提货表")
public class Delivery implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 提货状态，0->待发货
     */
    public static final Integer DELIVERY_STATE_TO_SEND = 0;

    /**
     * 提货状态，1->已发货
     */
    public static final Integer DELIVERY_STATE_SENDED = 1;

    /**
     * 提货状态，4->待退货
     */
    public static final Integer DELIVERY_STATE_TO_REFUND = 4;

    /**
     * 提货状态，5->已退货
     */
    public static final Integer DELIVERY_STATE_REFUNDED = 5;

    /**
     * 提货状态，6->交易结束
     */
    public static final Integer DELIVERY_STATE_CLOSED = 6;

    /**
     * 提货状态，7->商家驳回
     */
    public static final Integer DELIVERY_STATE_REFUSED = 7;

    /**
     * 提货状态，8->退货待签收
     */
    public static final Integer DELIVERY_STATE_RETURN_TO_BE_DESIGNED = 8;

    @ApiModelProperty(value = "主键自增编号")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "提货订单编号")
    private String orderDeliveryId;

    @ApiModelProperty(value = "用户编号")
    private String uid;

    @ApiModelProperty(value = "卖家编号")
    private String merchantId;

    @ApiModelProperty(value = "商品编号")
    private String goodsId;

    @ApiModelProperty(value = "商品名称")
    private String name;

    @ApiModelProperty(value = "币种名称")
    private String coin;

    @ApiModelProperty(value = "数量")
    private Integer number;

    @ApiModelProperty(value = "收货人名称")
    private String uname;

    @ApiModelProperty(value = "收人手机号")
    private String phone;

    @ApiModelProperty(value = "收货地址")
    private String address;

    @ApiModelProperty(value = "提货备注")
    private String note;

    @ApiModelProperty(value = "提货状态，0->待发货，1->已发货，2->已收货，3->已撤销，4->待退货，5->已退货，6->交易结束")
    private Integer deliveryState;

    @ApiModelProperty(value = "快递公司缩写")
    private String expressType;

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

    @ApiModelProperty(value = "快递单号")
    private String expressNo;

    @ApiModelProperty(value = "快递物流详情")
    private String expressDetail;

    @ApiModelProperty(value = "发货时间")
    private Long deliveryTime;

    @ApiModelProperty(value = "完成时间")
    private Long finishTime;

    @ApiModelProperty(value = "是否删除，0->否，1->是")
    private Boolean isDelete;

    @ApiModelProperty(value = "买家评价状态，0->未评价，1->已评价，2->已追评")
    private Boolean buyerState;

    @ApiModelProperty(value = "卖家评价状态，0->未评价，1->已评价")
    private Boolean sellerState;

    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    private Long createTime;

    @ApiModelProperty(value = "更改时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateTime;

    /**
     * 订单商品规格
     */
    @TableField(exist = false)
    private List<ParcelCenterGoods> goodsList;

    /**
     * 提货订餐操作记录
     */
    @TableField(exist = false)
    private List<DeliveryLog> logList;

    @TableField(exist = false)
    @ApiModelProperty(value = "快递物流详情集合")
    private List<ExpressItemDTO> expressDetailList;

    @ApiModelProperty(value = "退货理由")
    @TableField(exist = false)
    private String returnReason;

    @ApiModelProperty(value = "退货理由-图片")
    @TableField(exist = false)
    private String returnImg;

    @ApiModelProperty(value = "退货理由-图片集合")
    @TableField(exist = false)
    private List<String> returnImgList;

    public void listGoods(ParcelCenterMapper parcelCenterMapper, String orderDeliveryId) {
        List<ParcelCenterGoods> goodsList = parcelCenterMapper.listGoods(orderDeliveryId);

        /**
         * 获取规格对应的属性值
         */
        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);
    }

    /**
     * 获取提货单的操作记录
     *
     * @param deliveryLogService
     * @param orderDeliveryId
     */
    public void listLogs(IDeliveryLogService deliveryLogService, String orderDeliveryId) {

        List<DeliveryLog> list = deliveryLogService.listAll(orderDeliveryId);
        setLogList(list);
    }

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

    public void listReturnImg() {
        returnImgList = JSON.parseArray(returnImg, String.class);
    }
}
