package cn.lihongjie.coal.empMonthAttendance.entity;

import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import cn.lihongjie.coal.employee.entity.EmployeeEntity;

import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;

import lombok.Data;

import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Comment;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@Data
@Entity

@jakarta.persistence.Table(indexes = @jakarta.persistence.Index(name ="idx_emp_month_attendance_org_id", columnList = "organization_id"))

public class EmpMonthAttendanceEntity extends OrgCommonEntity {

    @ManyToOne private EmployeeEntity employee;

    @Comment("年月")
    private LocalDate yearMonth;

    @Comment("是否全勤")
    private Boolean fullAttendance;

    @Comment("是否满勤")
    private Boolean fullWork;

    @Comment("应出勤天数")
    private Double shouldAttendanceDays;

    @Comment("实际出勤天数")
    private Double actualAttendanceDays;

    @Comment("加班天数")
    private Double overtimeDays;

    @Comment("请假天数")
    private Double leaveDays;

    /** 后面这些不重要,放到最后 */
    @Comment("事假天数")
    private Double personalLeaveDays;

    @Comment("病假天数")
    private Double sickLeaveDays;

    @Comment("婚假天数")
    private Double marriageLeaveDays;

    @Comment("产假天数")
    private Double maternityLeaveDays;

    @Comment("陪产假天数")
    private Double paternityLeaveDays;

    @Comment("丧假天数")
    private Double funeralLeaveDays;

    @Comment("年假天数")
    private Double annualLeaveDays;

    @Comment("迟到次数")
    private Integer lateTimes;

    @Comment("迟到分钟数")
    private Integer lateMinutes;

    @Comment("早退次数")
    private Integer earlyTimes;

    @Comment("早退分钟数")
    private Integer earlyMinutes;

    @Comment("旷工次数")
    private Integer absenteeismTimes;

    @Comment("归档状态")
    @ColumnDefault("'0'")
    private String archiveStatus = "0";

    @Override
    public void prePersist() {
        super.prePersist();
        this.setDefaultVal();
    }

    @Override
    public void preUpdate() {
        super.preUpdate();
        this.setDefaultVal();
    }


    public Map<String, Object> toCtxMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("monthAttendanceId", this.getId());
        map.put("fullAttendance", fullAttendance);
        map.put("fullWork", fullWork);
        map.put("shouldAttendanceDays", shouldAttendanceDays);
        map.put("actualAttendanceDays", actualAttendanceDays);
        map.put("overtimeDays", overtimeDays);
        map.put("leaveDays", leaveDays);
        map.put("personalLeaveDays", personalLeaveDays);
        map.put("sickLeaveDays", sickLeaveDays);
        map.put("marriageLeaveDays", marriageLeaveDays);
        map.put("maternityLeaveDays", maternityLeaveDays);
        map.put("paternityLeaveDays", paternityLeaveDays);
        map.put("funeralLeaveDays", funeralLeaveDays);
        map.put("annualLeaveDays", annualLeaveDays);
        map.put("lateTimes", lateTimes);
        map.put("lateMinutes", lateMinutes);
        map.put("earlyTimes", earlyTimes);
        map.put("earlyMinutes", earlyMinutes);
        map.put("absenteeismTimes", absenteeismTimes);
        return map;
    }


    public void setDefaultVal() {


        if (this.yearMonth != null) {
            this.yearMonth = LocalDate.of(this.yearMonth.getYear(), this.yearMonth.getMonth(), 1);
        }

        if (this.fullAttendance == null) {
            this.fullAttendance = false;
        }

        if (this.fullWork == null) {
            this.fullWork = false;
        }

        if (this.shouldAttendanceDays == null) {
            this.shouldAttendanceDays = 0.0;
        }

        if (this.actualAttendanceDays == null) {
            this.actualAttendanceDays = 0.0;
        }

        if (this.overtimeDays == null) {
            this.overtimeDays = 0.0;
        }

        if (this.leaveDays == null) {
            this.leaveDays = 0.0;
        }

        if (this.personalLeaveDays == null) {
            this.personalLeaveDays = 0.0;
        }

        if (this.sickLeaveDays == null) {
            this.sickLeaveDays = 0.0;
        }

        if (this.marriageLeaveDays == null) {
            this.marriageLeaveDays = 0.0;
        }

        if (this.maternityLeaveDays == null) {
            this.maternityLeaveDays = 0.0;
        }

        if (this.paternityLeaveDays == null) {
            this.paternityLeaveDays = 0.0;
        }

        if (this.funeralLeaveDays == null) {
            this.funeralLeaveDays = 0.0;
        }

        if (this.annualLeaveDays == null) {
            this.annualLeaveDays = 0.0;
        }

        if (this.lateTimes == null) {
            this.lateTimes = 0;
        }

        if (this.lateMinutes == null) {
            this.lateMinutes = 0;
        }

        if (this.earlyTimes == null) {
            this.earlyTimes = 0;
        }

        if (this.earlyMinutes == null) {
            this.earlyMinutes = 0;
        }

        if (this.absenteeismTimes == null) {
            this.absenteeismTimes = 0;
        }
    }
}