package com.persagy.calendar.pojo.dto;

import java.util.Set;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.persagy.calendar.constant.WorkCalendarConstant;
import com.persagy.common.annotation.IdGenerator;
import com.persagy.common.enums.IdGeneratorAlgorithm;
import com.persagy.common.utils.StringUtil;
import com.persagy.db.model.BaseEntity;

import cn.hutool.core.collection.CollectionUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 工作历作息时间表
 * 
 * @version 1.0.0
 * @company persagy
 * @author zhangqiankun
 * @date 2020-10-03 13:54:22
 */
@Getter
@Setter
@ToString
@TableName("work_calendar_date")
@IdGenerator(prefix = WorkCalendarConstant.WORK_CALENDAR_DATE_ID_PREFIX, algorithm = IdGeneratorAlgorithm.SNOW_STR)
public class WorkCalendarDate extends BaseEntity<WorkCalendarDate> {
	private static final long serialVersionUID = 2631725708285230622L;

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String groupCode; // 集团编码
	
	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String ruleId; // 规则ID

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String calendarId; // 工作历ID

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String projectId; // PROJECT_ID

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String calendarName; // 工作历名称

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String calendarDesc; // 工作历描述

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String dictType; // 字典类型

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String dictCode; // 字典数据的code
	
	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String workDateStart; // 新版YYYYMMDD，新版中和规则同步；老版YYYYMMDDHHMMSS,老版中和规则没有关系

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String workDateEnd; // 新版YYYYMMDD，新版中和规则同步；老版YYYYMMDDHHMMSS,老版中和规则没有关系

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String workDate; // 具体执行日期，新接口YYYYMMDD

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String workTime; // 具体时间段

	@TableField(fill = FieldFill.INSERT, updateStrategy = FieldStrategy.NOT_EMPTY)
	private String createTime; // 创建时间

	@TableField(fill = FieldFill.INSERT_UPDATE, updateStrategy = FieldStrategy.NOT_EMPTY)
	private String updateTime; // 更新时间

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String updateUser; // 操作用户ID

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String customFlag; // 0-非自定义；1-自定义

	public static class Builder {

		private QueryWrapper<WorkCalendarDate> queryWrapper = null;

		private UpdateWrapper<WorkCalendarDate> updateWrapper = null;

		public Builder createQueryWrapper() {
			queryWrapper = new QueryWrapper<WorkCalendarDate>();
			return this;
		}

		public Builder createUpdateWrapper() {
			updateWrapper = new UpdateWrapper<WorkCalendarDate>();
			return this;
		}

		public Builder idEq(String id) {
			if (StringUtil.isNotBlank(id)) {
				if (updateWrapper != null) {
					updateWrapper.eq("ID", id);
				} else {
					queryWrapper.eq("ID", id);
				}
			}
			return this;
		}
		
		public Builder groupCodeEq(String groupCode) {
			if (StringUtil.isNotBlank(groupCode)) {
				if (updateWrapper != null) {
					updateWrapper.eq("GROUP_CODE", groupCode);
				} else {
					queryWrapper.eq("GROUP_CODE", groupCode);
				}
			}
			return this;
		}

		public Builder ruleIdEq(String ruleId) {
			if (StringUtil.isNotBlank(ruleId)) {
				if (updateWrapper != null) {
					updateWrapper.eq("RULE_ID", ruleId);
				} else {
					queryWrapper.eq("RULE_ID", ruleId);
				}
			}
			return this;
		}
		
		/**
		 * 
		 * @param projectId
		 * @param dictTypeLevel 当为null，或者不为0时，才会生效 
		 * @return
		 */
		public Builder projectIdEq(String projectId, Integer dictTypeLevel) {
			if (StringUtil.isNotBlank(projectId) && (dictTypeLevel == null || dictTypeLevel != 0)) {
				if (updateWrapper != null) {
					updateWrapper.eq("PROJECT_ID", projectId);
				} else {
					queryWrapper.eq("PROJECT_ID", projectId);
				}
				
			}
			return this;
		}
		
		/**
		 * @param calendarId
		 * @param dictTypeLevel 当为2 或者null时，才会生效
		 * @return
		 */
		public Builder calendarIdEq(String calendarId, Integer dictTypeLevel) {
			if (StringUtil.isNotBlank(calendarId) && (dictTypeLevel == null || dictTypeLevel == 2)) {
				if (updateWrapper != null) {
					updateWrapper.eq("CALENDAR_ID", calendarId);
				} else {
					queryWrapper.eq("CALENDAR_ID", calendarId);
				}
			}
			return this;
		}
		
		public Builder calendarIdOrZore(String calendarId) {
			if (StringUtil.isNotBlank(calendarId)) {
				if (updateWrapper != null) {
					updateWrapper.and(wrapper -> wrapper
						.eq("CALENDAR_ID", calendarId)
						.or()
						.eq("CALENDAR_ID", "0"));
				} else {
					queryWrapper.and(wrapper -> wrapper
							.eq("CALENDAR_ID", calendarId)
							.or()
							.eq("CALENDAR_ID", "0"));
				}
			}
			return this;
		}

		public Builder calendarNameEq(String calendarName) {
			if (StringUtil.isNotBlank(calendarName)) {
				if (updateWrapper != null) {
					updateWrapper.eq("CALENDAR_NAME", calendarName);
				} else {
					queryWrapper.eq("CALENDAR_NAME", calendarName);
				}
			}
			return this;
		}
		
		public Builder calendarNameLike(String calendarName) {
			if (StringUtil.isNotBlank(calendarName)) {
				if (updateWrapper != null) {
					updateWrapper.like("CALENDAR_NAME", calendarName);
				} else {
					queryWrapper.like("CALENDAR_NAME", calendarName);
				}
			}
			return this;
		}

		public Builder calendarDescEq(String calendarDesc) {
			if (StringUtil.isNotBlank(calendarDesc)) {
				if (updateWrapper != null) {
					updateWrapper.eq("CALENDAR_DESC", calendarDesc);
				} else {
					queryWrapper.eq("CALENDAR_DESC", calendarDesc);
				}
			}
			return this;
		}

		public Builder dictTypeEq(String dictType) {
			if (StringUtil.isNotBlank(dictType)) {
				if (updateWrapper != null) {
					updateWrapper.eq("DICT_TYPE", dictType);
				} else {
					queryWrapper.eq("DICT_TYPE", dictType);
				}
			}
			return this;
		}
		
		public Builder dictTypeIn(Set<String> dictType) {
			if (CollectionUtil.isNotEmpty(dictType)) {
				if (updateWrapper != null) {
					updateWrapper.in("DICT_TYPE", dictType);
				} else {
					queryWrapper.in("DICT_TYPE", dictType);
				}
			}
			return this;
		}

		public Builder dictCodeEq(String dictCode) {
			if (StringUtil.isNotBlank(dictCode)) {
				if (updateWrapper != null) {
					updateWrapper.eq("DICT_CODE", dictCode);
				} else {
					queryWrapper.eq("DICT_CODE", dictCode);
				}
			}
			return this;
		}
		
		public Builder dictCodeIn(Set<String> dictCode) {
			if (CollectionUtil.isNotEmpty(dictCode)) {
				if (updateWrapper != null) {
					updateWrapper.in("DICT_CODE", dictCode);
				} else {
					queryWrapper.in("DICT_CODE", dictCode);
				}
			}
			return this;
		}
		
		public Builder dictCodeLikeRight(String dictCode) {
			if (StringUtil.isNotBlank(dictCode)) {
				if (updateWrapper != null) {
					updateWrapper.likeRight("DICT_CODE", dictCode);
				} else {
					queryWrapper.likeRight("DICT_CODE", dictCode);
				}
			}
			return this;
		}

		public Builder workDateStartEq(String workDateStart) {
			if (StringUtil.isNotBlank(workDateStart)) {
				if (updateWrapper != null) {
					updateWrapper.eq("WORK_DATE_START", workDateStart);
				} else {
					queryWrapper.eq("WORK_DATE_START", workDateStart);
				}
			}
			return this;
		}

		public Builder workDateStartGe(String workDateStart) {
			if (StringUtil.isNotBlank(workDateStart)) {
				if (updateWrapper != null) {
					updateWrapper.ge("WORK_DATE_START", workDateStart);
				} else {
					queryWrapper.ge("WORK_DATE_START", workDateStart);
				}
			}
			return this;
		}
		
		public Builder workDateStartLe(String workDateStart) {
			if (StringUtil.isNotBlank(workDateStart)) {
				if (updateWrapper != null) {
					updateWrapper.le("WORK_DATE_START", workDateStart);
				} else {
					queryWrapper.le("WORK_DATE_START", workDateStart);
				}
			}
			return this;
		}
		
		public Builder workDateEndEq(String workDateEnd) {
			if (StringUtil.isNotBlank(workDateEnd)) {
				if (updateWrapper != null) {
					updateWrapper.eq("WORK_DATE_END", workDateEnd);
				} else {
					queryWrapper.eq("WORK_DATE_END", workDateEnd);
				}
			}
			return this;
		}
		
		public Builder workDateEndLe(String workDateEnd) {
			if (StringUtil.isNotBlank(workDateEnd)) {
				if (updateWrapper != null) {
					updateWrapper.le("WORK_DATE_END", workDateEnd);
				} else {
					queryWrapper.le("WORK_DATE_END", workDateEnd);
				}
			}
			return this;
		}

		public Builder workDateEq(String workDate) {
			if (StringUtil.isNotBlank(workDate)) {
				if (updateWrapper != null) {
					updateWrapper.eq("WORK_DATE", workDate);
				} else {
					queryWrapper.eq("WORK_DATE", workDate);
				}
			}
			return this;
		}
		
		public Builder workDateGe(String workDate) {
			if (StringUtil.isNotBlank(workDate)) {
				if (updateWrapper != null) {
					updateWrapper.ge("WORK_DATE", workDate);
				} else {
					queryWrapper.ge("WORK_DATE", workDate);
				}
			}
			return this;
		}
		
		public Builder workDateLe(String workDate) {
			if (StringUtil.isNotBlank(workDate)) {
				if (updateWrapper != null) {
					updateWrapper.le("WORK_DATE", workDate);
				} else {
					queryWrapper.le("WORK_DATE", workDate);
				}
			}
			return this;
		}

		public Builder workTimeEq(String workTime) {
			if (StringUtil.isNotBlank(workTime)) {
				if (updateWrapper != null) {
					updateWrapper.eq("WORK_TIME", workTime);
				} else {
					queryWrapper.eq("WORK_TIME", workTime);
				}
			}
			return this;
		}

		public Builder createTimeEq(String createTime) {
			if (StringUtil.isNotBlank(createTime)) {
				if (updateWrapper != null) {
					updateWrapper.eq("CREATE_TIME", createTime);
				} else {
					queryWrapper.eq("CREATE_TIME", createTime);
				}
			}
			return this;
		}

		public Builder updateTimeEq(String updateTime) {
			if (StringUtil.isNotBlank(updateTime)) {
				if (updateWrapper != null) {
					updateWrapper.eq("UPDATE_TIME", updateTime);
				} else {
					queryWrapper.eq("UPDATE_TIME", updateTime);
				}
			}
			return this;
		}

		public Builder updateUserEq(String updateUser) {
			if (StringUtil.isNotBlank(updateUser)) {
				if (updateWrapper != null) {
					updateWrapper.eq("UPDATE_USER", updateUser);
				} else {
					queryWrapper.eq("UPDATE_USER", updateUser);
				}
			}
			return this;
		}

		public Builder customFlagEq(String customFlag) {
			if (StringUtil.isNotBlank(customFlag)) {
				if (updateWrapper != null) {
					updateWrapper.eq("CUSTOM_FLAG", customFlag);
				} else {
					queryWrapper.eq("CUSTOM_FLAG", customFlag);
				}
			}
			return this;
		}

		public QueryWrapper<WorkCalendarDate> builderQueryWrapper() {
			return queryWrapper;
		}

		public UpdateWrapper<WorkCalendarDate> builderUpdateWrapper() {
			return updateWrapper;
		}
	}

	/**
	 * 调用之前转换一下项目ID和工作历ID，按照字典类型级别
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		
		if (getClass() != obj.getClass()) {
			return false;
		}
		
		WorkCalendarDate other = (WorkCalendarDate) obj;
		if (StringUtil.isBlank(calendarId)) {
			if (StringUtil.isNotBlank(other.calendarId)) {
				return false;
			}
		} else if (!calendarId.equals(other.calendarId == null ? "" : other.calendarId)) {
			return false;
		}
		
		if (StringUtil.isBlank(dictCode)) {
			if (StringUtil.isNotBlank(other.dictCode)) {
				return false;
			}
		} else if (!dictCode.equals(other.dictCode == null ? "" : other.dictCode)) {
			return false;
		}
		
		if (StringUtil.isBlank(dictType)) {
			if (StringUtil.isNotBlank(other.dictType)) {
				return false;
			}
		} else if (!dictType.equals(other.dictType == null ? "" : other.dictType)) {
			return false;
		}

		if (StringUtil.isBlank(groupCode)) {
			if (StringUtil.isNotBlank(other.groupCode)) {
				return false;
			}
		} else if (!groupCode.equals(other.groupCode == null ? "" : other.groupCode)) {
			return false;
		}

		if (StringUtil.isBlank(projectId)) {
			if (StringUtil.isNotBlank(other.projectId)) {
				return false;
			}
		} else if (!projectId.equals(other.projectId == null ? "" : other.projectId)) {
			return false;
		}
		
		if (StringUtil.isBlank(workDate)) {
			if (StringUtil.isNotBlank(other.workDate)) {
				return false;
			}
		} else if (!workDate.equals(other.workDate == null ? "" : other.workDate)) {
			return false;
		}
		
		return true;
	}

	/**
	 * 调用之前转换一下项目ID和工作历ID，按照字典类型级别
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((StringUtil.isBlank(calendarId)) ? 0 : calendarId.hashCode());
		result = prime * result + ((StringUtil.isBlank(dictCode)) ? 0 : dictCode.hashCode());
		result = prime * result + ((StringUtil.isBlank(dictType)) ? 0 : dictType.hashCode());
		result = prime * result + ((StringUtil.isBlank(groupCode)) ? 0 : groupCode.hashCode());
		result = prime * result + ((StringUtil.isBlank(projectId)) ? 0 : projectId.hashCode());
		result = prime * result + ((StringUtil.isBlank(workDate)) ? 0 : workDate.hashCode());
		return result;
	}
	
}
