package thyyxxk.webserver.entity.clockin;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import thyyxxk.webserver.utils.SiUtil;

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

@Data
public class Clockin {
    private String starttime;
    private String endtime;
    private List<String> useridlist;

    // 数据库表
    private String userid;
    private String groupname;
    private String checkinType;
    private String exceptionType;
    private String checkinTime;
    private String checkinDate;
    private String checkinHour;
    private String locationTitle;
    private String locationDetail;
    private String notes;

    // flag, 判断是否信息科，默认否
    private boolean flag = false;

    // 需要从其他表获取的信息
    private String name;
    private String dept;

    public void setTimes(String starttime, String endtime) {
        this.starttime = starttime;
        this.endtime = endtime;
    }

    public void setAllInfo(JSONObject item) {
        setUserid(item.getString("userid"));
        setGroupname(item.getString("groupname"));
        setCheckinType(item.getString("checkin_type"));
        setExceptionType(item.getString("exception_type"));
        setCheckinTime(item.getString("checkin_time"));
        setLocationTitle(item.getString("location_title"));
        setLocationDetail(item.getString("location_detail"));
        setNotes(item.getString("notes"));
    }

    public void setAllInfoForItDept(JSONObject item, String starttime) {
        setUserid(item.getString("userid"));
        if (getUserid().equals("2482") || getUserid().equals("2727") || getUserid().equals("3210"))
            setGroupname("自由打卡");
        else setGroupname("后勤白班打卡");
        setCheckinType(item.getString("checkin_type"));
        setStarttime(starttime);
        setCheckinTime();
        setExceptionType("");
        setLocationTitle("沭阳铭和医院");
        setLocationDetail("湖南省长沙市开福区芙蓉北路529号");
        setNotes("");
    }

    public void setCheckinTime() {
        String suffix = mkSuffix(getCheckinType());
        this.checkinTime = getStarttime() + " " + suffix;
        setCheckinDate(getStarttime().replaceAll("-", "/"));
        setCheckinHour(suffix);
    }

    private String mkSuffix(String type) {
        if (type.equals("上班打卡")) {
            int random = new Random().nextInt(19) + 40;
            return "07:" + random;
        } else {
            int random = new Random().nextInt(49) + 10;
            Calendar calendar = Calendar.getInstance();
            if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
                return "12:" + random;
            return random < 35 ? "18:" + random : "17:" + random;
        }
    }

    public void setCheckinTime(String checkinTime) {
        if (checkinTime.contains("-")) {
            this.checkinTime = checkinTime;
        } else {
            long timeStamp = Long.parseLong(checkinTime) * 1000;
            DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            this.checkinTime = df.format(new Date(timeStamp));
            String[] datetime = this.checkinTime.split(" ");
            setCheckinDate(datetime[0]);
            setCheckinHour(datetime[1]);
            if (getExceptionType().equals("未打卡"))
                setCheckinHour("");
        }
    }
}
