package com.zcloud.fire.check.domain.model;

import com.jjb.saas.framework.domain.model.BaseE;
import com.zcloud.fire.check.domain.enums.ListManagerCheckPeriodEnum;
import lombok.Data;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * web-domain
 *
 * @Author zhaokai
 * @Date 2025-11-28 10:32:08
 */
@Data
public class FireCheckListE extends BaseE {
    //主键
    private Long id;
    //业务主键id
    private String fireCheckId;
    //企业id
    private Long corpinfoId;
    //检查表名字
    private String fireCheckName;
    //检查表类型
    private String fireCheckType;
    //检查表类型名称
    private String fireCheckTypeName;
//    //负责部门
//    private Long departmentId;
//    //负责人
//    private Long userId;
    //禁用1启用0
    private Integer state;
    //检查照片上传方式（1相机照相，2相册获取）
    private Integer photoUploadMethod;
    //乐观锁
    private Integer version;
    //创建人
    private Long createId;
    //创建人姓名
    private String createName;
    //创建时间
    private LocalDateTime createTime;
    //更新人
    private Long updateId;
    //修改人名称
    private String updateName;
    //更新时间
    private LocalDateTime updateTime;
    //描述
    private String remarks;
    //是否删除
    private String deleteEnum;
    //租户ID
    private Long tenantId;
    //机构ID
    private Long orgId;
    //环境
    private String env;
    //周期开始时间
    private LocalDateTime periodStart;
    //周期结束时间
    private LocalDateTime periodEnd;

        //获取当前日期的所有区间日期
        public DateTimeE getCheckRecordInterval(LocalDateTime today) {
            DateTimeE  dateTimeE = new DateTimeE();
            if(today == null){
                today = LocalDateTime.now();
            }
            LocalDate todayDate = today.toLocalDate();
            dateTimeE.setToDay(todayDate);
            //每日
            dateTimeE.setDayStartTime(todayDate.atStartOfDay());
            dateTimeE.setDayEndTime(todayDate.atTime(23, 59, 59));
            //每周
            LocalDate monday = todayDate.with(java.time.temporal.TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            LocalDate sunday = todayDate.with(java.time.temporal.TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
            dateTimeE.setWeeklyStartTime(monday.atStartOfDay());
            dateTimeE.setWeeklyEndTime(sunday.atTime(23, 59, 59));
            //每旬
            // 每旬：每月1-10日、11-20日、21-月末为一个周期
            int dayOfMonth = todayDate.getDayOfMonth();
            if (dayOfMonth <= 10) {
                LocalDateTime tenDayStart = todayDate.withDayOfMonth(1).atStartOfDay();
                LocalDateTime tenDayEnd = todayDate.withDayOfMonth(10).atTime(23, 59, 59);
                dateTimeE.setTenDaysStartTime(tenDayStart);
                dateTimeE.setTenDaysEndTime(tenDayEnd);
            } else if (dayOfMonth <= 20) {
                LocalDateTime tenDayStart = todayDate.withDayOfMonth(11).atStartOfDay();
                LocalDateTime tenDayEnd = todayDate.withDayOfMonth(20).atTime(23, 59, 59);
                dateTimeE.setTenDaysStartTime(tenDayStart);
                dateTimeE.setTenDaysEndTime(tenDayEnd);
            } else {
                LocalDateTime tenDayStart = todayDate.withDayOfMonth(21).atStartOfDay();
                LocalDateTime tenDayEnd = todayDate.withDayOfMonth(todayDate.lengthOfMonth()).atTime(23, 59, 59);
                dateTimeE.setTenDaysStartTime(tenDayStart);
                dateTimeE.setTenDaysEndTime(tenDayEnd);
            }
            // 每月：月初到月末
            LocalDateTime monthStart = todayDate.withDayOfMonth(1).atStartOfDay();
            LocalDateTime monthEnd = todayDate.withDayOfMonth(todayDate.lengthOfMonth()).atTime(23, 59, 59);
            dateTimeE.setMonthlyStartTime(monthStart);
            dateTimeE.setMonthlyEndTime(monthEnd);

            // 每季：每季度初到季度末
            int month = ((todayDate.getMonthValue() - 1) / 3) * 3 + 1;
            LocalDateTime quarterStart = todayDate.withMonth(month).withDayOfMonth(1).atStartOfDay();
            LocalDate quarterEndDate = todayDate.withMonth(Math.min(month + 2, 12)).withDayOfMonth(1).plusMonths(1).minusDays(1);
            LocalDateTime quarterEnd = quarterEndDate.atTime(23, 59, 59);
            dateTimeE.setQuarterlyStartTime(quarterStart);
            dateTimeE.setQuarterlyEndTime(quarterEnd);

            // 半年：1-6月或7-12月
            if (todayDate.getMonthValue() <= 6) {
                LocalDateTime halfYearStart = todayDate.withMonth(1).withDayOfMonth(1).atStartOfDay();
                LocalDateTime halfYearEnd = todayDate.withMonth(6).withDayOfMonth(30).atTime(23, 59, 59);

                dateTimeE.setHalfYearStartTime(halfYearStart);
                dateTimeE.setHalfYearEndTime(halfYearEnd);
            } else {
                LocalDateTime halfYearStart = todayDate.withMonth(7).withDayOfMonth(1).atStartOfDay();
                LocalDateTime halfYearEnd = todayDate.withMonth(12).withDayOfMonth(31).atTime(23, 59, 59);
                dateTimeE.setHalfYearStartTime(halfYearStart);
                dateTimeE.setHalfYearEndTime(halfYearEnd);
            }
            // 每年：年初到年末
            // 每年：年初到年末
            LocalDateTime yearStart = todayDate.withMonth(1).withDayOfMonth(1).atStartOfDay();
            LocalDateTime yearEnd = todayDate.withMonth(12).withDayOfMonth(31).atTime(23, 59, 59);
//        checkRecordInterval.put("fireCheckPeriod0006", new DateTimeE(yearStart, yearEnd));
            dateTimeE.setYearlyStartTime(yearStart);
            dateTimeE.setYearlyEndTime(yearEnd);
            // 半月：1-15日或16-月末
            if (todayDate.getDayOfMonth() <= 15) {
                LocalDateTime halfMonthStart = todayDate.withDayOfMonth(1).atStartOfDay();
                LocalDateTime halfMonthEnd = todayDate.withDayOfMonth(15).atTime(23, 59, 59);
                dateTimeE.setHalfMonthStartTime(halfMonthStart);
                dateTimeE.setHalfMonthEndTime(halfMonthEnd);
            } else {
                LocalDateTime halfMonthStart = todayDate.withDayOfMonth(16).atStartOfDay();
                LocalDateTime halfMonthEnd = todayDate.withDayOfMonth(todayDate.lengthOfMonth()).atTime(23, 59, 59);
                dateTimeE.setHalfMonthStartTime(halfMonthStart);
                dateTimeE.setHalfMonthEndTime(halfMonthEnd);
            }

            return dateTimeE;
        }

    public void getCheckRecordPeriod(String checkPeriod, LocalDateTime checkTimen) {
        ListManagerCheckPeriodEnum byCode = ListManagerCheckPeriodEnum.getByCode(checkPeriod);
        //时间为传入检查时间来计算周期时间

        LocalDateTime today = checkTimen;
        LocalDate todayDate = today.toLocalDate();

        switch (byCode) {
            case DAILY:
                // 每日：当天00:00:00到23:59:59
                this.periodStart = todayDate.atStartOfDay();
                this.periodEnd = todayDate.atTime(23, 59, 59);
                break;
            case WEEKLY:
                // 每周：本周一00:00:00到周日23:59:59
                LocalDate monday = todayDate.with(java.time.temporal.TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
                LocalDate sunday = todayDate.with(java.time.temporal.TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
                this.periodStart = monday.atStartOfDay();
                this.periodEnd = sunday.atTime(23, 59, 59);
                break;
            case TEN_DAYS:
                // 每旬：每月1-10日、11-20日、21-月末为一个周期
                int dayOfMonth = todayDate.getDayOfMonth();
                if (dayOfMonth <= 10) {
                    this.periodStart = todayDate.withDayOfMonth(1).atStartOfDay();
                    this.periodEnd = todayDate.withDayOfMonth(10).atTime(23, 59, 59);
                } else if (dayOfMonth <= 20) {
                    this.periodStart = todayDate.withDayOfMonth(11).atStartOfDay();
                    this.periodEnd = todayDate.withDayOfMonth(20).atTime(23, 59, 59);
                } else {
                    this.periodStart = todayDate.withDayOfMonth(21).atStartOfDay();
                    this.periodEnd = todayDate.withDayOfMonth(todayDate.lengthOfMonth()).atTime(23, 59, 59);
                }
                break;
            case MONTHLY:
                // 每月：月初到月末
                this.periodStart = todayDate.withDayOfMonth(1).atStartOfDay();
                this.periodEnd = todayDate.withDayOfMonth(todayDate.lengthOfMonth()).atTime(23, 59, 59);
                break;
            case QUARTERLY:
                // 每季：每季度初到季度末
                int month = ((todayDate.getMonthValue() - 1) / 3) * 3 + 1;
                this.periodStart = todayDate.withMonth(month).withDayOfMonth(1).atStartOfDay();
                LocalDate quarterEndDate = todayDate.withMonth(Math.min(month + 2, 12)).withDayOfMonth(1).plusMonths(1).minusDays(1);
                this.periodEnd = quarterEndDate.atTime(23, 59, 59);
                break;
            case HALF_YEAR:
                // 半年：1-6月或7-12月
                if (todayDate.getMonthValue() <= 6) {
                    this.periodStart = todayDate.withMonth(1).withDayOfMonth(1).atStartOfDay();
                    this.periodEnd = todayDate.withMonth(6).withDayOfMonth(30).atTime(23, 59, 59);
                } else {
                    this.periodStart = todayDate.withMonth(7).withDayOfMonth(1).atStartOfDay();
                    this.periodEnd = todayDate.withMonth(12).withDayOfMonth(31).atTime(23, 59, 59);
                }
                break;
            case YEARLY:
                // 每年：年初到年末
                this.periodStart = todayDate.withMonth(1).withDayOfMonth(1).atStartOfDay();
                this.periodEnd = todayDate.withMonth(12).withDayOfMonth(31).atTime(23, 59, 59);
                break;
            case HALF_MONTH:
                // 半月：1-15日或16-月末
                if (todayDate.getDayOfMonth() <= 15) {
                    this.periodStart = todayDate.withDayOfMonth(1).atStartOfDay();
                    this.periodEnd = todayDate.withDayOfMonth(15).atTime(23, 59, 59);
                } else {
                    this.periodStart = todayDate.withDayOfMonth(16).atStartOfDay();
                    this.periodEnd = todayDate.withDayOfMonth(todayDate.lengthOfMonth()).atTime(23, 59, 59);
                }
                break;
            case WEEKLY_N_TIMES:
                // 每周：本周一00:00:00到周日23:59:59
                LocalDate mondayN = todayDate.with(java.time.temporal.TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
                LocalDate sundayN = todayDate.with(java.time.temporal.TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
                this.periodStart = mondayN.atStartOfDay();
                this.periodEnd = sundayN.atTime(23, 59, 59);
                break;
        }
    }
}

