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

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.yonge.cooleshow.common.enums.EActivationCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * 机构激活码
 * 2023-07-21 17:32:49
 */
@ApiModel(value = "TenantActivationCodeWrapper对象", description = "机构激活码查询对象")
public class TenantActivationCodeWrapper {

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel(" TenantActivationCodeQuery-机构激活码")
    public static class TenantActivationCodeQuery implements QueryInfo {

        @ApiModelProperty("当前页")
        private Integer page;

        @ApiModelProperty("分页行数")
        private Integer rows;

        @ApiModelProperty("关键字匹配")
        private String keyword;

        @ApiModelProperty("机构ID")
        private Long tenantId;

        @ApiModelProperty("机构小组ID")
        private Long tenantGroupId;

        @ApiModelProperty("声部ID")
        private String subjectId;

        @ApiModelProperty("专辑购买ID")
        private Long tenantAlbumPurchaseId;

        @ApiModelProperty("发送状态")
        private EActivationCode sendStatus;

        @ApiModelProperty("激活状态")
        private Boolean activationStatus;

        @ApiModelProperty("激活手机号码")
        private String activationPhone;

        @ApiModelProperty("激活时间计算到月 yyyy-MM")
        private String activationTime;

        @ApiModelProperty("激活开始")
        private Date activationStartTime;

        @ApiModelProperty("激活结束")
        private Date activationEndTime;

        @ApiModelProperty("订单号 订单详情专用")
        private String orderNo;

        @ApiModelProperty("激活码")
        private String activationCode;

        public String getKeyword() {
            return Optional.ofNullable(keyword).filter(StringUtils::isNotBlank).orElse(null);
        }

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

        public static TenantActivationCodeQuery from(String json) {
            return JSON.parseObject(json, TenantActivationCodeQuery.class);
        }
    }

    @Data
    @ApiModel(" TenantActivationCode-机构激活码")
    public static class TenantActivationCode {

        @ApiModelProperty("主键ID")
        private Long id;

        @ApiModelProperty("机构ID")
        private Long tenantId;

        @ApiModelProperty("机构专辑ID")
        private Long tenantAlbumId;

        @ApiModelProperty("周期")
        private Integer purchaseCycle;

        @ApiModelProperty("机构专辑采购ID")
        private Long tenantAlbumPurchaseId;

        @ApiModelProperty("激活码")
        private String activationCode;

        @ApiModelProperty("发送状态(WAIT,SEND)")
        private String sendStatus;

        @ApiModelProperty("激活状态")
        private Boolean activationStatus;

        @ApiModelProperty("激活手机号")
        private String activationPhone;

        @ApiModelProperty("激活时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
        private Date activationTime;

        @ApiModelProperty("激活用户ID")
        private Long activationUserId;

        @ApiModelProperty("激活用户名称")
        private String activationUserName;

        @ApiModelProperty("激活用户头像")
        private String activationUserAvatar;

        @ApiModelProperty("更新时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
        private Date updateTime;

        @ApiModelProperty("创建时间")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
        private Date createTime;

        @ApiModelProperty("激活数量")
        private Integer activeQuantity;


        @ApiModelProperty("购买数量")
        private Integer  purchaseQuantity;

        @ApiModelProperty("发放时间")
        private Date sendTime;

        @ApiModelProperty("采购周期类型 DAY 日 MONTH 月 QUARTERLY 季度 YEAR_HALF 半年 YEAR 年")
        private String purchaseType;

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

        public static TenantActivationCode from(String json) {
            return JSON.parseObject(json, TenantActivationCode.class);
        }
    }

    @Data
    public static class ImportTemplate {

        @ExcelProperty(value = "激活码")
        private String code;

        @ExcelProperty(value = "手机号")
        private String phone;

        private static final String PHONE_REG = "^(13\\d|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18\\d|19[0-35-9])" +
                "\\d{8}$";

        public List<String> checkIsIllegal() {
            List<String> errMsg = new ArrayList<>();
            if (StringUtils.isEmpty(code)) {
                errMsg.add("激活码不能为空");
            }
//            if (StringUtils.isNotEmpty(phone) && !Pattern.matches(PHONE_REG, phone.trim())) {
//                errMsg.add("手机号格式错误");
//            }
            return errMsg;
        }


    }

    @Data
    public static class ExportTemplate {

        @ExcelProperty(value = "激活码")
        private String activationCode;

        @ExcelProperty(value = "手机号")
        private String activationPhone;

    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel(" ExportFile-学生浏览埋点统计下载")
    public static class ExportFile {

        @ApiModelProperty("文件名称")
        private String fileName;

        @ApiModelProperty("下载地址")
        private String downloadPath;
    }
}