package com.yonge.cooleshow.biz.dal.queryInfo;

import com.alibaba.fastjson.JSON;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.coupon.CouponCategoryEnum;
import com.yonge.cooleshow.biz.dal.enums.coupon.CouponTypeEnum;
import com.yonge.cooleshow.biz.dal.enums.coupon.CouponUseStateEnum;
import com.yonge.cooleshow.biz.dal.enums.coupon.CouponValidTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 优惠券查询条件
 * Created by Eric.Shang on 2022/9/2.
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CouponInfoQuery implements Serializable {

    // 优惠券ID
    private Long couponId;
    // 优惠券名称
    private String name;
    // 客户端类型
    private ClientEnum clientType;
    // 优惠券类型
    private CouponTypeEnum couponType;
    // 可用品类
    private CouponCategoryEnum category;
    // 优惠券状态
    private Integer status;
    // 当前时间
    private Long timestamp;
    // 优惠券时间类型
    private CouponValidTypeEnum validType;

    public static CouponInfoQuery from(String recv) {

        return JSON.parseObject(recv, CouponInfoQuery.class);
    }

    /**
     * 优惠券发放数统计
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class IssueStatQuery implements Serializable {

        // 当前时间
        private Long timestamp;

        // 使用状态
        private CouponUseStateEnum useState;

        // 状态排除
        private List<CouponUseStateEnum> excludeUseState;

        // 发送用户
        private List<Long> userIds;
    }

    /**
     * 优惠券状态统计
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CouponStateStatQuery implements Serializable {

        // 客户端类型
        private ClientEnum clientType;

        // 优惠券类型
        private CouponTypeEnum couponType;

        // 类型分组
        private Boolean groupByState;

        // 当前时间
        private Long timestamp;

        // 订单类型
        private CouponUseStateEnum userState;

        public String jsonString() {
            return JSON.toJSONString(this);
        }

        public static CouponStateStatQuery from(String recv) {

            return JSON.parseObject(recv, CouponStateStatQuery.class);
        }


        public CouponStateStatQuery couponType(String couponType) {
            if (CouponTypeEnum.match(couponType)) {
                this.couponType = CouponTypeEnum.valueOf(couponType);
            }
            return this;
        }


        public CouponStateStatQuery groupByState(Boolean groupByState) {
            this.groupByState = groupByState;
            return this;
        }

        public CouponStateStatQuery timestamp(Long timestamp) {
            this.timestamp = timestamp;
            return this;
        }

        public CouponStateStatQuery userState(CouponUseStateEnum userState) {
            this.userState = userState;
            return this;
        }
    }

    /**
     * 用户发放优惠券信息
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CouponOrderQuery implements Serializable {

        // 客户端类型
        private ClientEnum clientType;

        // 优惠券类型
        private List<CouponCategoryEnum> couponTypes;

        // 优惠券发放ID
        private List<Long> issueIds;

        // 可用状态
        private CouponUseStateEnum useState;

        // 当前时间
        private Long timestamp;

        // 订单编号
        private String orderNo;

        // 订单支付金额
        private Double amount;

        public static CouponOrderQuery from(String recv) {
            return JSON.parseObject(recv, CouponOrderQuery.class);
        }

        public Double getAmount() {
            return Optional.ofNullable(amount).orElse(0D);
        }

        public List<CouponCategoryEnum> getCouponTypes() {

            return Optional.ofNullable(couponTypes).filter(CollectionUtils::isNotEmpty).orElse(null);
        }


        public CouponOrderQuery issueIds(String issueIds) {
            if (StringUtils.isNotEmpty(issueIds)) {
                this.issueIds = Arrays.stream(issueIds.split(",")).map(Long::parseLong).collect(Collectors.toList());
            }
            return this;
        }
    }
}
