package com.persagy.calendar.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.persagy.calendar.constant.WorkCalendarConstant;
import com.persagy.calendar.mapper.WorkCalendarRuleMapper;
import com.persagy.calendar.pojo.dto.WorkCalendarRule;
import com.persagy.calendar.pojo.dto.WorkCalendarRule.Builder;
import com.persagy.calendar.service.IWorkCalendarRuleService;
import com.persagy.common.utils.StringUtil;
import com.persagy.db.service.impl.SuperServiceImpl;

import cn.hutool.core.collection.CollectionUtil;

/**
 * 工作历规则表
 * 
 * @version 1.0.0
 * @company persagy 
 * @author zhangqiankun
 * @date 2020-10-02 18:26:14
 */
@Service
public class WorkCalendarRuleServiceImpl extends SuperServiceImpl<WorkCalendarRuleMapper, WorkCalendarRule> implements IWorkCalendarRuleService {
	
	@Override
	public List<WorkCalendarRule> queryRuleByCalendarId(String groupCode, String calendarId, String projectId, Integer dictTypeLevel) {
    	QueryWrapper<WorkCalendarRule> queryWrapper = new WorkCalendarRule.Builder().createQueryWrapper().groupCodeEq(groupCode)
    			.calendarIdEq(calendarId, dictTypeLevel).projectIdEq(projectId, dictTypeLevel)
    			.delFlagEq(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR).builderQueryWrapper();
		return this.baseMapper.selectList(queryWrapper);
	}
	
	@Override
	public String getCalendarRule(WorkCalendarRule workCalendarRule, Integer dictTypeLevel) {
		QueryWrapper<WorkCalendarRule> queryWrapper = new WorkCalendarRule.Builder().createQueryWrapper().groupCodeEq(workCalendarRule.getGroupCode())
    			.projectIdEq(workCalendarRule.getProjectId(), dictTypeLevel).dictTypeEq(workCalendarRule.getDictType())
    			.dictCodeEq(workCalendarRule.getDictCode()).delFlagEq(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR)
    			.builderQueryWrapper();
		List<WorkCalendarRule> selectList = this.baseMapper.selectList(queryWrapper);
		if (CollectionUtil.isEmpty(selectList)) {
			return null;
		}
		for (WorkCalendarRule rule : selectList) {
			if (workCalendarRule.getValue().equals(rule.getValue())) {
				return rule.getId();
			}
		}
		return null;
	}
	
	@Override
	public WorkCalendarRule getById(String id) {
    	QueryWrapper<WorkCalendarRule> queryWrapper = new WorkCalendarRule.Builder().createQueryWrapper().idEq(id)
    			.delFlagEq(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR).builderQueryWrapper();
		return this.baseMapper.selectOne(queryWrapper);
	}
	
	@Override
	@Transactional
	public boolean createWorkCalendarRule(WorkCalendarRule workCalendarRule) {
		Integer result = this.baseMapper.insert(workCalendarRule);
		return SqlHelper.retBool(result);
	}
	
	@Override
	@Transactional
	public boolean updateWorkCalendarRule(WorkCalendarRule workCalendarRule) {
		Integer result = this.baseMapper.updateById(workCalendarRule);
		return SqlHelper.retBool(result);
	}
	
	@Override
	@Transactional
	public boolean deleteWorkCalendarRule(WorkCalendarRule workCalendarRule, Integer dictTypeLevel) {
		workCalendarRule.setDelFlag(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_DEL);
		
		Integer result = null;
		if (StringUtil.isNotBlank(workCalendarRule.getId())) {
			result = this.baseMapper.updateById(workCalendarRule);
		} else {
	    	UpdateWrapper<WorkCalendarRule> updateWrapper = new WorkCalendarRule.Builder().createUpdateWrapper().groupCodeEq(workCalendarRule.getGroupCode())
	    			.projectIdEq(workCalendarRule.getProjectId(), dictTypeLevel).calendarIdEq(workCalendarRule.getCalendarId(), dictTypeLevel)
	    			.delFlagEq(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR).builderUpdateWrapper();
			result = this.baseMapper.update(workCalendarRule, updateWrapper);
		}
		return SqlHelper.retBool(result);
	}
	
	@Override
	@Transactional
	public boolean removeWorkCalendarRule(String groupCode, String projectId, String id, String calendarId, String dictType, Integer dictTypeLevel) {
		Integer result = null;
		UpdateWrapper<WorkCalendarRule> updateWrapper = null;
		Builder builder = new WorkCalendarRule.Builder().createUpdateWrapper();
		if (StringUtil.isNotBlank(id)) {
			updateWrapper = builder.idEq(id).delFlagEq(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR).builderUpdateWrapper();
			result = this.baseMapper.delete(updateWrapper);
		} else {
	    	updateWrapper = builder.groupCodeEq(groupCode).projectIdEq(projectId, dictTypeLevel).calendarIdEq(calendarId, dictTypeLevel)
	    			.dictTypeEq(dictType).delFlagEq(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR).builderUpdateWrapper();
			result = this.baseMapper.delete(updateWrapper);
		}
		
		return SqlHelper.retBool(result);
	}

	@Override
	public boolean checkDictRuleValid(String groupCode, String projectId, String calendarId, String dictType, String dictCode, String id, Integer dictTypeLevel) {
		QueryWrapper<WorkCalendarRule> queryWrapper = new WorkCalendarRule.Builder().createQueryWrapper().groupCodeEq(groupCode)
    			.projectIdEq(projectId, dictTypeLevel).calendarIdEq(calendarId, dictTypeLevel).dictTypeEq(dictType).dictCodeEq(dictCode)
    			.delFlagEq(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR).builderQueryWrapper();
		WorkCalendarRule calendarRule = this.baseMapper.selectOne(queryWrapper);
		if (calendarRule == null) {
			return true;
		}
		if (StringUtil.isNotBlank(id) && id.equals(calendarRule.getId())) {
			return true;
		}
		return false;
	}

}
