package cn.momia.api.course.dto;

import cn.momia.common.util.TimeUtil;
import com.alibaba.fastjson.annotation.JSONField;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CourseSku {
    public static final CourseSku NOT_EXIST_COURSE_SKU = new CourseSku();

    private static final DateFormat MONTH_DATE_FORMAT = new SimpleDateFormat("MM月dd日");
    private static final DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm");

    private long id;
    private long courseId;
    private Date startTime;
    private Date endTime;
    @JSONField(serialize = false) private Date deadline;
    @JSONField(serialize = false) private int unlockedStock;
    @JSONField(serialize = false) private int placeId;
    @JSONField(serialize = false) private int adult;
    @JSONField(serialize = false) private int child;
    @JSONField(serialize = false) private int status;

    private CourseSkuPlace place;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getCourseId() {
        return courseId;
    }

    public void setCourseId(long courseId) {
        this.courseId = courseId;
    }

    public Date getStartTime() {
        return startTime;
    }

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

    public Date getEndTime() {
        return endTime;
    }

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

    public Date getDeadline() {
        return deadline;
    }

    public void setDeadline(Date deadline) {
        this.deadline = deadline;
    }

    public int getUnlockedStock() {
        return unlockedStock;
    }

    public void setUnlockedStock(int unlockedStock) {
        this.unlockedStock = unlockedStock;
    }

    public int getPlaceId() {
        return placeId;
    }

    public void setPlaceId(int placeId) {
        this.placeId = placeId;
    }

    public int getAdult() {
        return adult;
    }

    public void setAdult(int adult) {
        this.adult = adult;
    }

    public int getChild() {
        return child;
    }

    public void setChild(int child) {
        this.child = child;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public CourseSkuPlace getPlace() {
        return place;
    }

    public void setPlace(CourseSkuPlace place) {
        this.place = place;
    }

    public boolean exists() {
        return id > 0;
    }

    @JSONField(serialize = false)
    public int getJoinCount() {
        return adult + child;
    }

    @JSONField(serialize = false)
    public boolean isAvaliable(Date now) {
        return status == 1 && deadline.after(now);
    }

    public String getTime() {
        if (TimeUtil.isSameDay(startTime, endTime)) {
            return TIME_FORMAT.format(startTime) + " - " + TIME_FORMAT.format(endTime);
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startTime);
            calendar.add(Calendar.DATE, 1);
            Date nextDay = calendar.getTime();
            if (TimeUtil.isSameDay(nextDay, endTime)) {
                return TIME_FORMAT.format(startTime) + " - 次日 " + TIME_FORMAT.format(endTime);
            } else {
                return TIME_FORMAT.format(startTime) + " - " + MONTH_DATE_FORMAT.format(endTime) + " " + TIME_FORMAT.format(endTime);
            }
        }

    }

    public String getScheduler() {
        if (TimeUtil.isSameDay(startTime, endTime)) {
            return MONTH_DATE_FORMAT.format(startTime) + " " + TimeUtil.getWeekDay(startTime) + " " + TIME_FORMAT.format(startTime);
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startTime);
            calendar.add(Calendar.DATE, 1);
            Date nextDay = calendar.getTime();
            if (TimeUtil.isSameDay(nextDay, endTime)) {
                return MONTH_DATE_FORMAT.format(startTime) + " " + TIME_FORMAT.format(startTime) + " - 次日 " + TIME_FORMAT.format(endTime);
            } else {
                return MONTH_DATE_FORMAT.format(startTime) + " " + TIME_FORMAT.format(startTime) + " - " + MONTH_DATE_FORMAT.format(endTime) + " " + TIME_FORMAT.format(endTime);
            }
        }
    }

    public int getStock() {
        return unlockedStock;
    }

    public void setStock(int stock) {
        this.unlockedStock = stock;
    }
}

@@ -3822,591 +3822,95 @@
     
-Calendar calendar = Calendar.getInstance();%0A            calendar.setTime(startTime);%0A            calendar.add(Calendar.DATE, 1);%0A            Date nextDay = calendar.getTime();%0A            if (TimeUtil.isSameDay(nextDay, endTime)) %7B%0A                return MONTH_DATE_FORMAT.format(startTime) + %22 %22 + TIME_FORMAT.format(startTime) + %22 - %E6%AC%A1%E6%97%A5 %22 + TIME_FORMAT.format(endTime);%0A            %7D else %7B%0A                return MONTH_DATE_FORMAT.format(startTime) + %22 %22 + TIME_FORMAT.format(startTime) + %22 - %22 + MONTH_DATE_FORMAT.format(endTime) + %22 %22 + TIME_FORMAT.format(endTime);%0A            %7D
+return MONTH_DATE_FORMAT.format(startTime) + %22 - %22 + MONTH_DATE_FORMAT.format(endTime);
 %0A   

简化CourseSku 的Scheduler格式