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.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.persagy.calendar.mapper.WorkCalendarDictMapper;
import com.persagy.calendar.pojo.dto.WorkCalendarDict;
import com.persagy.calendar.service.IWorkCalendarDictService;
import com.persagy.common.utils.StringUtil;
import com.persagy.db.service.impl.SuperServiceImpl;

import cn.hutool.core.collection.CollectionUtil;

/**
 * @version 
 * @description 
 * @company persagy
 * @author zhangqiankun
 * @since 2021年3月4日:	上午9:48:36
 */
@Service
public class WorkCalendarDictServiceImpl extends SuperServiceImpl<WorkCalendarDictMapper, WorkCalendarDict> implements IWorkCalendarDictService {

	@Override
	public List<WorkCalendarDict> queryWorkCalendarDictList(String groupCode, String projectId, String calendarId, String dictType, String dictCode) {
		LambdaQueryWrapper<WorkCalendarDict> queryWrapper = new WorkCalendarDict.BuilderQueryWrapper().groupCodeEq(groupCode)
				.projectIdEq(projectId, null).calendarIdEq(calendarId, null).dictTypeEq(dictType).dictCodeEq(dictCode).builder();
		List<WorkCalendarDict> selectList = this.baseMapper.selectList(queryWrapper);
		return selectList;
	}
	
	@Override
	public int getDictTypeLevel(String groupCode, String dictType) {
		if (StringUtil.isBlank(dictType)) {
			return 2;	// 默认工作历级别
		}
		LambdaQueryWrapper<WorkCalendarDict> queryWrapper = new WorkCalendarDict.BuilderQueryWrapper().groupCodeEq(groupCode).dictTypeEq(dictType).builder();
		queryWrapper.select(WorkCalendarDict::getGroupCode, WorkCalendarDict::getProjectId, WorkCalendarDict::getCalendarId, WorkCalendarDict::getDictType);
		List<WorkCalendarDict> selectList = this.baseMapper.selectList(queryWrapper);
		if (CollectionUtil.isEmpty(selectList)) {
			return 2;	// 默认工作历级别
		}
		
		for (WorkCalendarDict workCalendarDict : selectList) {
			if ("0".equals(workCalendarDict.getProjectId()) && dictType.equals(workCalendarDict.getDictType())) {
				return 0; 	// 集团级别
			} else if ("0".equals(workCalendarDict.getCalendarId()) && dictType.equals(workCalendarDict.getDictType())) {
				return 1;	// 项目级别
			}
		}
		return 2;	// 默认工作历级别
	}
	
	@Override
	@Transactional
	public boolean batchCreateDictTypeByDelete(List<WorkCalendarDict> calendarDicts) {
		if (CollectionUtil.isEmpty(calendarDicts)) {
			return true;
		}
		for (WorkCalendarDict calendarDict : calendarDicts) {
			LambdaQueryWrapper<WorkCalendarDict> queryWrapper = new WorkCalendarDict.BuilderQueryWrapper()
					.groupCodeEq(calendarDict.getGroupCode()).dictTypeEq(calendarDict.getDictType()).builder();
			this.baseMapper.delete(queryWrapper);
		}
		
		Integer result = this.baseMapper.batchCreateCalendarDict(calendarDicts);
		return SqlHelper.retBool(result);
	}
	
	@Override
	@Transactional
	public boolean batchCreateCalendarDict(List<WorkCalendarDict> calendarDicts) {
		if (CollectionUtil.isEmpty(calendarDicts)) {
			return true;
		}
		Integer result = this.baseMapper.batchCreateCalendarDict(calendarDicts);
		return SqlHelper.retBool(result);
	}

}
