package com.yonge.cooleshow.biz.dal.dto.search;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.yonge.cooleshow.biz.dal.enums.PeriodEnum;
import com.yonge.toolset.base.page.QueryInfo;
import com.yonge.toolset.base.util.StringUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;

/**
 * @Author: liweifan
 * @Data: 2022-05-09 17:14:30
 */
@ApiModel(value = "UserOrderRefundSearch对象", description = "退款申请表查询对象")
public class UserOrderRefundSearch extends QueryInfo {
    private static final long serialVersionUID = 1L;

    @NotNull(message = "时间类型不能为空")
    @ApiModelProperty("时间类型 MONTH、月度  YEAR、年度")
    private PeriodEnum timeType;

    @ApiModelProperty("时间 yyyy | yyyy-MM")
    private String dateTime;
    @ApiModelProperty(hidden = true)
    private Long userId;
    /***
     * 开始时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(hidden = true)
    private LocalDateTime startTime;
    /***
     * 结束时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(hidden = true)
    private LocalDateTime endTime;


    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public PeriodEnum getTimeType() {
        return timeType;
    }

    public void setTimeType(PeriodEnum timeType) {
        this.timeType = timeType;
    }

    public String getDateTime() {
        return dateTime;
    }

    public void setDateTime(String dateTime) {
        this.dateTime = dateTime;
    }

    public LocalDateTime getStartTime() {
        if (null == startTime) {
            LocalDateTime firstDay;
            if (PeriodEnum.YEAR.equals(timeType)) {
                LocalDate paramDate;
                if (StringUtil.isEmpty(dateTime)) {
                    paramDate = LocalDate.now();
                } else {
                    paramDate = LocalDate.of(Integer.parseInt(dateTime), 1, 1);
                }
                firstDay = LocalDateTime.of(paramDate.with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
            } else {
                LocalDate paramDate;
                if (StringUtil.isEmpty(dateTime)) {
                    paramDate = LocalDate.now();
                } else {
                    String[] classDateSp = dateTime.split("-");
                    paramDate = LocalDate.of(Integer.parseInt(classDateSp[0]), Integer.parseInt(classDateSp[1]), 1);
                }
                firstDay = LocalDateTime.of(paramDate.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
            }
            return firstDay;
        }
        return startTime;
    }

    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }

    public LocalDateTime getEndTime() {
        if (null == endTime) {
            LocalDateTime lastDay;
            if (PeriodEnum.YEAR.equals(timeType)) {
                LocalDate paramDate;
                if (StringUtil.isEmpty(dateTime)) {
                    paramDate = LocalDate.now();
                } else {
                    paramDate = LocalDate.of(Integer.parseInt(dateTime), 1, 1);
                }
                lastDay = LocalDateTime.of(paramDate.with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
            } else {
                LocalDate paramDate;
                if (StringUtil.isEmpty(dateTime)) {
                    paramDate = LocalDate.now();
                } else {
                    String[] classDateSp = dateTime.split("-");
                    paramDate = LocalDate.of(Integer.parseInt(classDateSp[0]), Integer.parseInt(classDateSp[1]), 1);
                }
                lastDay = LocalDateTime.of(paramDate.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
            }
            return lastDay;
        }
        return endTime;
    }

    public void setEndTime(LocalDateTime endTime) {
        this.endTime = endTime;
    }
}