package com.persagy.calendar.pojo.dto;

import java.util.Date;
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.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
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 lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * @version 
 * @description 
 * @company persagy
 * @author zhangqiankun
 * @since 2021年3月2日:	下午4:57:22
 */
@Getter
@Setter
@ToString
@TableName(value = "work_calendar_label", autoResultMap = true)
@IdGenerator(prefix = WorkCalendarConstant.WORK_CALENDAR_LABEL_ID_PREFIX, algorithm = IdGeneratorAlgorithm.SNOW_STR)
public class WorkCalendarLabel extends BaseEntity<WorkCalendarLabel> {
	private static final long serialVersionUID = 6895232648769300864L;

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String groupCode; // 集团编码
	
	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String calendarId; // 工作历ID

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

	@TableField(exist = false)
	private Integer labelMonth; 	// 标签所属日期
	
	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String labelDate; 	// 标签所属日期

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String dictType;
	
	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String dictCode;
	
	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String diyLabel;	// 是否自定义，0-非自定义，1-自定义
	
	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY, typeHandler = FastjsonTypeHandler.class)
	private Set<String> diyValue;
	
	@TableField(exist = false)
	private String diyValueStr;
	
	@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(exist = false)
	private String labelDateStart; 		// 标签所属开始日期
	
	@TableField(exist = false)
	private String labelDateEnd; 		// 标签所属结束日期

	public static class Builder {

		private QueryWrapper<WorkCalendarLabel> queryWrapper = null;

		private UpdateWrapper<WorkCalendarLabel> updateWrapper = null;

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

		public Builder createUpdateWrapper() {
			updateWrapper = new UpdateWrapper<WorkCalendarLabel>();
			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;
		}
		
		/**
		 * 
		 * @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 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 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 diyLabelEq(String diyLabel) {
			if (StringUtil.isNotBlank(diyLabel)) {
				if (updateWrapper != null) {
					updateWrapper.eq("DIY_LABEL", diyLabel);
				} else {
					queryWrapper.eq("DIY_LABEL", diyLabel);
				}
			}
			return this;
		}
		
		public Builder labelDateEq(String labelDate) {
			if (StringUtil.isNotBlank(labelDate)) {
				if (updateWrapper != null) {
					updateWrapper.eq("LABEL_DATE", labelDate);
				} else {
					queryWrapper.eq("LABEL_DATE", labelDate);
				}
			}
			return this;
		}
		
		public Builder labelDateGe(String labelDate) {
			if (StringUtil.isNotBlank(labelDate)) {
				if (updateWrapper != null) {
					updateWrapper.ge("LABEL_DATE", labelDate);
				} else {
					queryWrapper.ge("LABEL_DATE", labelDate);
				}
			}
			return this;
		}
		
		public Builder labelDateLe(String labelDate) {
			if (StringUtil.isNotBlank(labelDate)) {
				if (updateWrapper != null) {
					updateWrapper.le("LABEL_DATE", labelDate);
				} else {
					queryWrapper.le("LABEL_DATE", labelDate);
				}
			}
			return this;
		}
		
		public Builder createTimeEq(Date createTime) {
			if (null != createTime) {
				if (updateWrapper != null) {
					updateWrapper.eq("CREATE_TIME", createTime);
				} else {
					queryWrapper.eq("CREATE_TIME", createTime);
				}
			}
			return this;
		}
		
		public Builder updateTimeEq(Date updateTime) {
			if (null != 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 QueryWrapper<WorkCalendarLabel> builderQueryWrapper() {
			return queryWrapper;
		}

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

	// 不要加labelDate字段
	@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(diyLabel)) ? 0 : diyLabel.hashCode());
		result = prime * result + ((StringUtil.isBlank(groupCode)) ? 0 : groupCode.hashCode());
		result = prime * result + ((StringUtil.isBlank(projectId)) ? 0 : projectId.hashCode());
		return result;
	}

	// 不要加labelDate字段
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		
		if (getClass() != obj.getClass()) {
			return false;
		}
		
		WorkCalendarLabel other = (WorkCalendarLabel) obj;
		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(calendarId)) {
			if (StringUtil.isNotBlank(other.calendarId)) {
				return false;
			}
		} else if (!calendarId.equals(other.calendarId == null ? "" : other.calendarId)) {
			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(dictCode)) {
			if (StringUtil.isNotBlank(other.dictCode)) {
				return false;
			}
		} else if (!dictCode.equals(other.dictCode == null ? "" : other.dictCode)) {
			return false;
		}
		
		if (StringUtil.isBlank(diyLabel)) {
			if (StringUtil.isNotBlank(other.diyLabel)) {
				return false;
			}
		} else if (!diyLabel.equals(other.diyLabel == null ? "" : other.diyLabel)) {
			return false;
		}
		
		return true;
	}
	
}
