package com.ghy.web.pojo.vo;

import com.ghy.common.enums.OrderStatus;
import com.ghy.common.utils.DateUtils;
import com.ghy.common.utils.ObjectUtils;
import com.ghy.customer.domain.Customer;
import com.ghy.customer.domain.CustomerAddress;
import com.ghy.order.domain.OrderGoods;
import com.ghy.order.domain.OrderMaster;
import com.ghy.worker.domain.Worker;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;


/**
 * @author : 但星霖
 * @date : 2022-05-30 20:04
 * 订单列表返回数据vo
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class OrderDetailsResponseVo implements Serializable {

    /**
     * 序号
     */
    private Long id;

    /**
     * 商户ID
     */
    private Long deptId;

    /**
     * 订单编码
     */
    private String code;

    /**
     * 订单序号
     */
    private Long orderId;

    /**
     * 订单消费者信息。
     */
    private OrderCustomDetails customDetails;

    /**
     * 订单详细信息
     */
    private List<OrderDetails> detailsList;

    /**
     * 订单状态
     */
    private OrderState state;

    /**
     * 便签数据
     */
    private OrderMemo memo;

    /**
     * 订单流转信息，
     */
    private OrderCirculation circulation;

    /**
     * 商家信息
     */
    private OrderBusiness business;

    /**
     * 接单数据信息
     */
    private OrderReceiving receiving;

    /**
     * 邀请人相关。
     */
    private OrderInvitee invitee;

    /**
     * 邀请人相关。
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    public static class OrderInvitee{
        /**
         * 消费者id
         */
        private Long customerId;

        /**
         * 收件人姓名
         */
        private String name;

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

        public static OrderInvitee modelDataSupplement(Customer customer){
            OrderInvitee orderInvitee = new OrderInvitee();
            if(ObjectUtils.isNotNull(customer)){
                orderInvitee.setCustomerId(customer.getCustomerId());
                orderInvitee.setName(customer.getName());
                orderInvitee.setPhone(customer.getPhone());
            }else {
                orderInvitee.setName("--");
                orderInvitee.setPhone("--");
            }
            return orderInvitee;
        }
    }

    /**
     * 订单状态
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    public static class OrderState {

        /**
         * 订单状态
         */
        private Integer orderStatus;

        /**
         * 订单枚举
         */
        private OrderStatus orderStatusEnum;

        /**
         * 订单枚举翻译
         */
        private String orderStatusDesc;

        /**
         * 时间
         * 对应不同对状态展现不同对时间数据。
         * 发布时间/接单时间/完成时间
         * 格式为转移后对yyyy-mm-dd hh:ss:mm
         */
        private String receivingTime;

        /**
         * 修改上门时间次数
         * 单一字段展现。
         */
        private Integer doorFrequency;

        public static OrderState modelDataSupplement(OrderMaster orderMaster){
            OrderState orderState = new OrderState();
            orderState.setOrderStatus(orderMaster.getOrderStatus());
            if(ObjectUtils.isNotNull(orderMaster.getOrderStatus())){
                OrderStatus orderStatusByEnum = OrderStatus.parse(orderMaster.getOrderStatus());
                if(ObjectUtils.isNotNull(orderStatusByEnum)){
                    orderState.setOrderStatusEnum(orderStatusByEnum);
                    orderState.setOrderStatus(orderStatusByEnum.code());
                    orderState.setOrderStatusDesc(orderStatusByEnum.desc());
                }
            }
            if(ObjectUtils.isNull(orderState.getReceivingTime())){
                orderState.setReceivingTime("--");
            }else {
                orderState.setReceivingTime(orderState.getReceivingTime());
            }
            // todo 此处缺少上门次数字段。
            return orderState;
        }

    }

    /**
     * 便签数据
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    public static class OrderMemo {
        /**
         * 便签数据
         */
        private String memoStr;
    }


    /**
     * 流转信息
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    public static class OrderCirculation {
        /**
         * 流转信息文本数据
         */
        private String circulationStr;

        /**
         * 流转信息管理员Id
         */
        private String circulationUserId;
    }

    /**
     * 商家信息
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    public static class OrderBusiness {
        /**
         * 商家id
         */
        private String businessId;

        /**
         * 商家姓名
         */
        private String businessUserName;

        /**
         * 商家电话
         */
        private String businessUserPhone;
    }

    /**
     * 接单信息
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    public static class OrderReceiving {
        /**
         * 接单人用户Id
         */
        private Long receivingUserId;

        /**
         * 接单人用户姓名
         */
        private String receivingUserName;

        /**
         * 接单人用户电话
         */
        private String receivingUserPhone;

        /**
         * 操作人用户Id
         */
        private Long operationUserId;

        /**
         * 操作人用户姓名
         */
        private String operationUserName;

        /**
         * 操作人用户电话
         */
        private String operationUserPhone;

        public static OrderReceiving modelDataSupplement(Worker workerByReceiving, Worker workerByOperation){
            OrderReceiving orderReceiving = new OrderReceiving();
            // 接单人
            if(ObjectUtils.isNotNull(workerByReceiving)){
                orderReceiving.setReceivingUserId(workerByReceiving.getWorkerId());
                orderReceiving.setReceivingUserName(workerByReceiving.getName());
                orderReceiving.setReceivingUserPhone(workerByReceiving.getPhone());
            }
            // 操作人
            if(ObjectUtils.isNotNull(workerByOperation)){
                orderReceiving.setOperationUserId(workerByReceiving.getWorkerId());
                orderReceiving.setOperationUserName(workerByReceiving.getName());
                orderReceiving.setOperationUserPhone(workerByReceiving.getPhone());
            }
            return orderReceiving;
        }

    }

    /**
     * 订单详情
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    public static class OrderDetails {
        /**
         * 商品规格id
         */
        private Long goodsId;

        /**
         * 商品名称
         */
        private String goodsName;

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

        /**
         * 已服务数量
         */
        private Integer serverGoodsNum;

        /**
         * 完成时间
         */
        private String finishTime;

        /**
         * 子单总金额
         */
        private BigDecimal totalMoney;

        /**
         * 优惠金额
         */
        private BigDecimal discountMoney;

        /**
         * 实付金额
         */
        private BigDecimal payMoney;

        public static OrderDetails modelDataSupplement(OrderGoods orderGoods){
            OrderDetails orderDetails = new OrderDetails();
            if(ObjectUtils.isNotNull(orderGoods)){
                orderDetails.setGoodsId(orderGoods.getGoodsStandardId());
                orderDetails.setGoodsName(orderGoods.getGoodsName());
                orderDetails.setGoodsNum(orderGoods.getGoodsNum());
                orderDetails.setServerGoodsNum(orderGoods.getServerGoodsNum());
                if(ObjectUtils.isNotNull(orderGoods.getFinishTime())){
                    orderDetails.setFinishTime(DateUtils.getDateTimeFormat(orderGoods.getFinishTime()));
                }else {
                    orderDetails.setFinishTime("--");
                }
            }
            return orderDetails;
        }
    }


    /**
     * 订单消费者信息
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    public static class OrderCustomDetails {
        /**
         * 消费者id
         */
        private Long customerId;

        /**
         * 收件人姓名
         */
        private String name;

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

        /**
         * 省
         */
        private String provinceName;

        /**
         * 市
         */
        private String cityName;

        /**
         * 区
         */
        private String countryName;

        /**
         * 详细地址
         */
        private String address;

        public static OrderCustomDetails modelDataSupplement(CustomerAddress customerAddress){
            OrderCustomDetails orderCustomDetails = new OrderCustomDetails();
            if(ObjectUtils.isNotNull(customerAddress)){
                orderCustomDetails.setCustomerId(customerAddress.getCustomerId());
                orderCustomDetails.setAddress(customerAddress.getAddress());
                orderCustomDetails.setCityName(customerAddress.getCityName());
                orderCustomDetails.setPhone(customerAddress.getPhone());
                orderCustomDetails.setProvinceName(customerAddress.getProvinceName());
                orderCustomDetails.setName(customerAddress.getName());
                orderCustomDetails.setCountryName(customerAddress.getCountryName());
            }
            return orderCustomDetails;
        }
    }



}
