package com.yonge.cooleshow.admin.io.request.coupon;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yonge.cooleshow.biz.dal.enums.MK;
import com.yonge.cooleshow.biz.dal.enums.coupon.CouponInventoryEnum;
import com.yonge.cooleshow.common.enums.EStatus;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.QueryInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * 优惠券库存量
 * Created by Eric.Shang on 2022/9/5.
 */
public class CouponInventoryVO {


    /**
     * 优惠券库存调整分页请求信息
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel(value = "优惠券库存调整分页请求", description = "优惠券库存调整分页")
    public static class PageRequest extends QueryInfo {

        @ApiModelProperty("优惠券ID")
        private Long couponId;

        @ApiModelProperty("操作人")
        private Long userId;

        @ApiModelProperty("用户昵称或名称匹配")
        private String keyword;

        @ApiModelProperty("数据类型: ADDITION(添加) REDUCE(减少)")
        private CouponInventoryEnum dataType;

        @ApiModelProperty("开始时间, yyyy-MM-dd HH:mm:ss")
        @DateTimeFormat(pattern = MK.TIME_PATTERN)
        @JsonFormat(pattern = MK.TIME_PATTERN, timezone = MK.TIME_ZONE)
        private Date startQueryTime;

        @ApiModelProperty("结束时间, yyyy-MM-dd HH:mm:ss")
        @DateTimeFormat(pattern = MK.TIME_PATTERN)
        @JsonFormat(pattern = MK.TIME_PATTERN, timezone = MK.TIME_ZONE)
        private Date endQueryTime;

        @ApiModelProperty(value = "结束时间", hidden = true)
        private Long startTime;
        @ApiModelProperty(value = "结束时间", hidden = true)
        private Long endTime;

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

        public boolean invalidRequestParam() {

            // 优惠券ID不能为空
            return Optional.ofNullable(getCouponId()).orElse(0L) <= 0;
        }

        public Long getUserId() {

            if (Optional.ofNullable(this.userId).orElse(0L) > 0) {
                return this.userId;
            }

            return null;
        }

        public Long getStartTime() {
            if (Objects.nonNull(getStartQueryTime())) {
                return new DateTime(getStartQueryTime()).getMillis() / 1000;
            }
            return startTime;
        }

        public Long getEndTime() {
            if (Objects.nonNull(getEndQueryTime())) {
                return new DateTime(getEndQueryTime()).getMillis() / 1000;
            }
            return endTime;
        }

        public String getKeyword() {
            if (StringUtils.isNotEmpty(this.keyword)) {
                return keyword;
            }
            return null;
        }
    }

    /**
     * 库存量调整分页数据
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class PageInfo implements Serializable {

        @ApiModelProperty("发放ID")
        private Long id;

        @ApiModelProperty("用户ID")
        private Long userId;

        @ApiModelProperty("操作人")
        private String username;

        @ApiModelProperty("优惠券ID")
        private Long couponId;

        @ApiModelProperty("数据类型: ADDITION(添加) REDUCE(减少)")
        private CouponInventoryEnum dataType;

        @ApiModelProperty("变化量")
        private Integer number;

        @ApiModelProperty("备注")
        private String remark;

        @ApiModelProperty("创建时间")
        @JsonFormat(pattern = MK.TIME_PATTERN, timezone = MK.TIME_ZONE)
        private Date createdTime;

    }

    /**
     * 库存量调整记录
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class InventoryInfo implements Serializable {

        @ApiModelProperty(value = "发放ID", hidden = true)
        private Long id;

        @ApiModelProperty(value = "用户ID", hidden = true)
        private Long userId;

        @ApiModelProperty("优惠券ID")
        private Long couponId;

        @ApiModelProperty("数据类型: ADDITION(添加) REDUCE(减少)")
        private CouponInventoryEnum dataType;

        @ApiModelProperty("变化量")
        private Integer number;

        @ApiModelProperty(value = "备注", hidden = true)
        private String remark;

        @ApiModelProperty(value = "启/禁状态", hidden = true)
        private Integer status;

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

        public boolean invalidRequestParam(Long userId) {

            if (Optional.ofNullable(getCouponId()).orElse(0L) <= 0) {
                throw new BizException("优惠券ID不能为空");
            }

            if (Objects.isNull(getDataType())) {
                throw new BizException("类型不能为空");
            }

            if (Optional.ofNullable(getNumber()).orElse(0) <= 0) {
                throw new BizException("变化量不能为空");
            }

            // 设置保存参数
            this.userId(userId).initSaveParam();

            return false;
        }

        public InventoryInfo initSaveParam() {

            return this.remark("").status(EStatus.ENABLE.getValue());
        }


        public InventoryInfo id(Long id) {
            this.id = id;
            return this;
        }

        public InventoryInfo userId(Long userId) {
            this.userId = userId;
            return this;
        }

        public InventoryInfo remark(String remark) {
            this.remark = remark;
            return this;
        }

        public InventoryInfo status(Integer status) {
            this.status = status;
            return this;
        }
    }

    /**
     * 库存量调整用户分页数据
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class PageUserInfo implements Serializable {

        @ApiModelProperty("用户ID")
        private Long userId;

        @ApiModelProperty("操作人")
        private String username;

        @ApiModelProperty("优惠券ID")
        private Long couponId;

    }
}
