package com.yiqi.order.domain.dto;

import cn.binarywang.wx.miniapp.bean.express.WxMaExpressDelivery;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yiqi.app.domain.AppUser;
import com.yiqi.app.domain.vo.AppUserInfoVO;
import com.yiqi.common.utils.bean.BeanUtils;
import com.yiqi.core.domain.GoodsInfo;
import com.yiqi.order.domain.OrderCloth;
import com.yiqi.order.domain.OrderClothDeliveryAddress;
import com.yiqi.order.domain.OrderGoods;
import com.yiqi.order.domain.OrderPurchaseGoods;
import com.yiqi.order.domain.vo.ExpressDeliveryVO;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

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

@Data
public class OrderThirdDeliveryAddDTO {

    @ApiModelProperty("订单编号")
    private String 	orderNo;

    @ApiModelProperty("小程序openId")
    private String openid;

    @ApiModelProperty("快递客户编码或者现付编码")
    private String bizId;

    @ApiModelProperty("快递公司ID")
    private String deliveryId;

    @ApiModelProperty("快递备注信息，比如\"易碎物品\"，不超过1024字节")
    private String customRemark;

    @ApiModelProperty("订单来源，0为小程序订单，2为App或H5订单，填2则不发送物流服务通知")
    private Integer addSource;

//    @ApiModelProperty("App或H5的appid，add_source=2时必填，需和开通了物流助手的小程序绑定同一open账号")
//    private String wxAppid;

    @ApiModelProperty("发件人信息")
    private OrderThirdDeliveryPersonDTO sender;

    @ApiModelProperty("收件人信息")
    private OrderThirdDeliveryPersonDTO receiver;

    @ApiModelProperty("包裹信息，将传递给快递公司")
    private OrderThirdDeliveryCargoDTO cargo;

    @ApiModelProperty("商品信息，会展示到物流服务通知和电子面单中")
    private List<OrderThirdDeliveryGoodsDTO> goodsList;

    @ApiModelProperty("服务类型")
    private ExpressDeliveryVO.ServiceType serviceType;

    @ApiModelProperty("顺丰必须传。 预期的上门揽件时间，0表示已事先约定取件时间；否则请传预期揽件时间戳，需大于当前时间，" +
            "收件员会在预期时间附近上门。例如expect_time为“1557989929”，表示希望收件员将在2019年05月16日14:58:49-15:58:49" +
            "内上门取货。说明：若选择 了预期揽件时间，请不要自己打单，由上门揽件的时候打印。如果是下顺丰散单，则必传此字段" +
            "，否则不会有收件员上门揽件。")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date expectTime;


    public static OrderThirdDeliveryAddDTO buildDeliveryOrderByGoodsOrder(OrderGoods orderGoods,
                                                                          OrderGoodsOutGoodsDTO orderGoodsOutGoodsDTO,
                                                                          List<GoodsInfo> goodsInfoList,
                                                                          AppUserInfoVO appUserInfo
                                                                          ){
        OrderThirdDeliveryAddDTO orderThirdDeliveryAddDTO = new OrderThirdDeliveryAddDTO();
        BeanUtils.copyProperties(orderGoodsOutGoodsDTO, orderThirdDeliveryAddDTO);
        orderThirdDeliveryAddDTO.setOrderNo(orderGoods.getOrderNo());
        if (StrUtil.isNotEmpty(appUserInfo.getWxOpenId())){
            orderThirdDeliveryAddDTO.setOpenid(appUserInfo.getWxOpenId());
            orderThirdDeliveryAddDTO.setAddSource(0);
        }else {
            orderThirdDeliveryAddDTO.setAddSource(2);
        }
        orderThirdDeliveryAddDTO.setSender(OrderThirdDeliveryPersonDTO.buildGoodsOrderPersonBySender());
        orderThirdDeliveryAddDTO.setReceiver(OrderThirdDeliveryPersonDTO.buildGoodsOrderPersonByReceiver(appUserInfo));
        orderThirdDeliveryAddDTO.setCargo(OrderThirdDeliveryCargoDTO.buildOrderThirdDeliveryCargo(orderGoodsOutGoodsDTO));

        List<OrderThirdDeliveryGoodsDTO>  goodsList = new ArrayList<>();
        for (GoodsInfo goodsInfo : goodsInfoList) {
            goodsList.add(OrderThirdDeliveryGoodsDTO.buildDeliveryGoodsInfo(goodsInfo));
        }
        orderThirdDeliveryAddDTO.setGoodsList(goodsList);

        ExpressDeliveryVO.ServiceType serviceType = new ExpressDeliveryVO.ServiceType();
//        serviceType.setServiceName(orderGoodsOutGoodsDTO.getServiceName());
//        serviceType.setServiceType(orderGoodsOutGoodsDTO.getServiceType());
        orderThirdDeliveryAddDTO.setServiceType(serviceType);
        return orderThirdDeliveryAddDTO;
    }

    public static OrderThirdDeliveryAddDTO buildDeliveryOrderByPurchaseGoodsOrder(OrderPurchaseGoods orderPurchaseGoods,
                                                                                  OrderGoodsOutGoodsDTO orderGoodsOutGoodsDTO,
                                                                                  List<GoodsInfo> goodsInfoList,
                                                                                  DeliveryOrgInfoDTO deliveryOrgInfoBySender,
                                                                                  DeliveryOrgInfoDTO deliveryOrgInfoByReceiver){
        OrderThirdDeliveryAddDTO orderThirdDeliveryAddDTO = new OrderThirdDeliveryAddDTO();
        BeanUtils.copyProperties(orderGoodsOutGoodsDTO, orderThirdDeliveryAddDTO);
        orderThirdDeliveryAddDTO.setOrderNo(orderPurchaseGoods.getOrderNo());
        orderThirdDeliveryAddDTO.setAddSource(2);
        orderThirdDeliveryAddDTO.setSender(OrderThirdDeliveryPersonDTO.buildPurchaseGoodsOrderPersonBySender(deliveryOrgInfoBySender));
        orderThirdDeliveryAddDTO.setReceiver(OrderThirdDeliveryPersonDTO.buildPurchaseGoodsOrderPersonByReceiver(deliveryOrgInfoByReceiver));
        orderThirdDeliveryAddDTO.setCargo(OrderThirdDeliveryCargoDTO.buildOrderThirdDeliveryCargo(orderGoodsOutGoodsDTO));
        List<OrderThirdDeliveryGoodsDTO>  goodsList = new ArrayList<>();
        for (GoodsInfo goodsInfo : goodsInfoList) {
            goodsList.add(OrderThirdDeliveryGoodsDTO.buildDeliveryGoodsInfo(goodsInfo));
        }
        orderThirdDeliveryAddDTO.setGoodsList(goodsList);
        ExpressDeliveryVO.ServiceType serviceType = new ExpressDeliveryVO.ServiceType();
//        serviceType.setServiceName(orderGoodsOutGoodsDTO.getServiceName());
//        serviceType.setServiceType(orderGoodsOutGoodsDTO.getServiceType());
        orderThirdDeliveryAddDTO.setServiceType(serviceType);
        return orderThirdDeliveryAddDTO;
    }

    public static OrderThirdDeliveryAddDTO buildDeliveryOrderByClothOrder(OrderCloth orderCloth,
                                                                          OrderClothDeliveryAddress orderClothDeliveryAddress,
                                                                          OrderClothThirdDeliverySendDTO orderClothThirdDeliverySendDTO,
                                                                          DeliveryOrgInfoDTO deliveryOrgInfoBySender,
                                                                          AppUserInfoVO appUserInfoVO){
        OrderThirdDeliveryAddDTO orderThirdDeliveryAddDTO = new OrderThirdDeliveryAddDTO();
        BeanUtils.copyProperties(orderClothThirdDeliverySendDTO, orderThirdDeliveryAddDTO);
        orderThirdDeliveryAddDTO.setOrderNo(orderClothDeliveryAddress.getThirdDeliveryOrderNo());
        if (StrUtil.isNotEmpty(appUserInfoVO.getWxOpenId())){
            orderThirdDeliveryAddDTO.setOpenid(appUserInfoVO.getWxOpenId());
            orderThirdDeliveryAddDTO.setAddSource(0);
        }else {
            orderThirdDeliveryAddDTO.setAddSource(2);
        }
        orderThirdDeliveryAddDTO.setSender(OrderThirdDeliveryPersonDTO.buildPurchaseGoodsOrderPersonBySender(deliveryOrgInfoBySender));
        orderThirdDeliveryAddDTO.setReceiver(OrderThirdDeliveryPersonDTO.buildGoodsOrderPersonByReceiver(appUserInfoVO));
        OrderThirdDeliveryCargoDTO orderThirdDeliveryCargoDTO = new OrderThirdDeliveryCargoDTO();
        BeanUtils.copyProperties(orderClothThirdDeliverySendDTO, orderThirdDeliveryCargoDTO);
        orderThirdDeliveryAddDTO.setCargo(orderThirdDeliveryCargoDTO);
        List<OrderThirdDeliveryGoodsDTO>  goodsList = new ArrayList<>();
        goodsList.add(OrderThirdDeliveryGoodsDTO.buildClothGoodsInfo(orderCloth));
        orderThirdDeliveryAddDTO.setGoodsList(goodsList);
        ExpressDeliveryVO.ServiceType serviceType = new ExpressDeliveryVO.ServiceType();
        serviceType.setServiceName(orderClothThirdDeliverySendDTO.getServiceName());
        serviceType.setServiceType(orderClothThirdDeliverySendDTO.getServiceType());
        orderThirdDeliveryAddDTO.setServiceType(serviceType);
        return orderThirdDeliveryAddDTO;
    }
}
