package thyyxxk.webserver.entity.clockin;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.Interval;
import org.joda.time.Period;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Data
@Slf4j
public class ClockinView {
    private String checkinDate; // 日期
    private String name;    // 姓名
    private String userid;  // 工号
    private String dept;    // 科室
    private Integer times = 0;  // 打卡次数
    private String worktime="";    // 工作时长（小时）
    private String checkinHourOn; // 上班打卡时间
    private String checkinAddrOn; // 上班打卡地点
    private String checkinStateOn;    // 上班打卡状态
    private String notesOn;    // 上班打卡备注
    private String checkinHourOff;    // 下班打卡时间
    private String checkinAddrOff;    // 下班打卡地点
    private String checkinStateOff;   // 下班打卡状态
    private String notesOff;   // 下班打卡备注

    public ClockinView(Clockin param) {
        this.checkinDate = param.getCheckinDate();
        this.name = param.getName();
        this.userid = param.getUserid();
        this.dept = param.getDept();
    }

    public void setInfo(Clockin param) {
        if (param.getCheckinType().equals("上班打卡")) {
            this.checkinHourOn = param.getCheckinHour();
            this.checkinAddrOn = param.getLocationTitle();
            this.checkinStateOn =
                    param.getExceptionType().equals("") ?
                            "正常" : param.getExceptionType();
            this.notesOn = param.getNotes();
            if (!getCheckinHourOn().equals("") && !getCheckinStateOn().equals("未打卡")) {
                times ++;
            }
        } else if (param.getCheckinType().equals("下班打卡")){
            this.checkinHourOff = param.getCheckinHour();
            this.checkinAddrOff = param.getLocationTitle();
            this.checkinStateOff =
                    param.getExceptionType().equals("") ?
                            "正常" : param.getExceptionType();
            this.notesOff = param.getNotes();
            if (!getCheckinHourOff().equals("") && !getCheckinStateOff().equals("未打卡")) {
                times ++;
            }
        }
        if (getWorktime().equals("")) {
            setWorktime(calculateWorktime());
        }
    }

    private String calculateWorktime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (getCheckinHourOn() == null || getCheckinHourOff() == null
        || getCheckinHourOn().equals("") || getCheckinHourOff().equals(""))
            return "";
        try {
            Date date1 = simpleDateFormat.parse("2020-01-01 " + getCheckinHourOn());
            Date date2 = simpleDateFormat.parse("2020-01-01 " + getCheckinHourOff());
            if (date2.before(date1))
                return "";
            return diff(date1,date2);
        } catch (ParseException e) {
            log.error("计算工作时长出错", e);
        }
        return "";
    }

    private String diff(Date date1, Date date2) {
        Interval interval = new Interval(date1.getTime(), date2.getTime());
        Period period = interval.toPeriod();
        int hour = period.getHours();
        double min = period.getMinutes();
        double suffix = min / 60;
        return String.format("%.1f 小时", hour+suffix);
    }


    public String getCheckinStateOn() {
        return null == checkinStateOn ? "未打卡" : (checkinStateOn).trim();
    }

    public String getCheckinStateOff() {
        return null == checkinStateOff ? "未打卡" : (checkinStateOff).trim();
    }
}
