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.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 cn.hutool.core.collection.CollectionUtil;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 工作历规则表
 * 
 * @version 1.0.0
 * @company persagy
 * @author zhangqiankun
 * @date 2020-10-02 18:26:14
 */
@Getter
@Setter
@ToString
@EqualsAndHashCode(callSuper = false)
@TableName(value = "work_calendar_rule", autoResultMap = true)
@IdGenerator(prefix = WorkCalendarConstant.WORK_CALENDAR_RULE_ID_PREFIX, algorithm = IdGeneratorAlgorithm.SNOW_STR)
public class WorkCalendarRule extends BaseEntity<WorkCalendarRule> {
	private static final long serialVersionUID = -6758862747401349143L;

	@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(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String dictType; // 字典类型

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String dictCode; // 字典数据的code

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String ruleDateStart; // 规则生效的开始日期YYYYMMDD

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String ruleDateEnd; // 规则生效的结束日期YYYYMMDD

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String ruleTime; // 规则生效的具体时间点HHMMSS

	@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 delFlag; // 0-正常；1-删除

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
	private String ruleFlag; // 0-新接口规则；1-老接口规则，方便后续解析

	@TableField(updateStrategy = FieldStrategy.NOT_EMPTY, typeHandler = FastjsonTypeHandler.class)
	private Set<String> value; // 当存多时间段时，或按周、月等解析时，日期时间点存入此字段内，假如 ruleTime 存在，已 ruleTime 为准

	public static class Builder {

		private QueryWrapper<WorkCalendarRule> queryWrapper = null;

		private UpdateWrapper<WorkCalendarRule> updateWrapper = null;

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

		public Builder createUpdateWrapper() {
			updateWrapper = new UpdateWrapper<WorkCalendarRule>();
			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;
		}
		
		/**
		 * 
		 * @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 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;
		}
		
		/**
		 * start[ruleDateStart~ruleDateEnd] OR end[ruleDateStart~ruleDateEnd]
		 */
		public Builder ruleDateBetween(String start, String end) {
			if (StringUtil.isNotBlank(start) && StringUtil.isNotBlank(end)) {
				if (updateWrapper != null) {
					updateWrapper.and(wrapper -> wrapper
							.ge("RULE_DATE_END", end)
							.le("RULE_DATE_START", end)
							.or()
							.le("RULE_DATE_END", end)
							.le("RULE_DATE_END", start));
				} else {
					queryWrapper.and(wrapper -> wrapper
							.ge("RULE_DATE_END", end)
							.le("RULE_DATE_START", end)
							.or()
							.le("RULE_DATE_END", end)
							.ge("RULE_DATE_END", start));
				}
			}
			return this;
		}

		public Builder ruleDateStartEq(String ruleDateStart) {
			if (StringUtil.isNotBlank(ruleDateStart)) {
				if (updateWrapper != null) {
					updateWrapper.eq("RULE_DATE_START", ruleDateStart);
				} else {
					queryWrapper.eq("RULE_DATE_START", ruleDateStart);
				}
			}
			return this;
		}

		public Builder ruleDateStartBetween(String start, String end) {
			if (StringUtil.isNotBlank(start) && StringUtil.isNotBlank(end)) {
				if (updateWrapper != null) {
					updateWrapper.between("RULE_DATE_START", start, end);
				} else {
					queryWrapper.between("RULE_DATE_START", start, end);
				}
			}
			return this;
		}
		
		public Builder ruleDateEndEq(String ruleDateEnd) {
			if (StringUtil.isNotBlank(ruleDateEnd)) {
				if (updateWrapper != null) {
					updateWrapper.eq("RULE_DATE_END", ruleDateEnd);
				} else {
					queryWrapper.eq("RULE_DATE_END", ruleDateEnd);
				}
			}
			return this;
		}
		
		public Builder ruleDateEndBetween(String start, String end) {
			if (StringUtil.isNotBlank(start) && StringUtil.isNotBlank(end)) {
				if (updateWrapper != null) {
					updateWrapper.between("RULE_DATE_END", start, end);
				} else {
					queryWrapper.between("RULE_DATE_END", start, end);
				}
			}
			return this;
		}

		public Builder ruleTimeEq(String ruleTime) {
			if (StringUtil.isNotBlank(ruleTime)) {
				if (updateWrapper != null) {
					updateWrapper.eq("RULE_TIME", ruleTime);
				} else {
					queryWrapper.eq("RULE_TIME", ruleTime);
				}
			}
			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 delFlagEq(String delFlag) {
			if (StringUtil.isNotBlank(delFlag)) {
				if (updateWrapper != null) {
					updateWrapper.eq("DEL_FLAG", delFlag);
				} else {
					queryWrapper.eq("DEL_FLAG", delFlag);
				}
			}
			return this;
		}

		public Builder ruleFlagEq(String ruleFlag) {
			if (StringUtil.isNotBlank(ruleFlag)) {
				if (updateWrapper != null) {
					updateWrapper.eq("RULE_FLAG", ruleFlag);
				} else {
					queryWrapper.eq("RULE_FLAG", ruleFlag);
				}
			}
			return this;
		}

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

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