package com.persagy.calendar.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.persagy.calendar.mapper.WorkCalendarDateMapper;
import com.persagy.calendar.pojo.dto.WorkCalendarDate;
import com.persagy.calendar.service.IWorkCalendarDateService;
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-03 13:54:22
 */
@Service
public class WorkCalendarDateServiceImpl extends SuperServiceImpl<WorkCalendarDateMapper, WorkCalendarDate> implements IWorkCalendarDateService {
	
	@Autowired
	private WorkCalendarDateMapper workCalendarDateMapper;
 	
	@Override
	@Transactional
	public boolean createWorkCalendarDate(WorkCalendarDate workCalendarDate) {
		Integer result = workCalendarDateMapper.insert(workCalendarDate);
		return SqlHelper.retBool(result);
	}
	
	@Override
	@Transactional
	public boolean updateWorkCalendarDate(WorkCalendarDate workCalendarDate) {
		Integer result = workCalendarDateMapper.updateById(workCalendarDate);
		return SqlHelper.retBool(result);
	}

	@Override
	@Transactional
	public boolean deleteWorkCalendarDate(String id) {
		Integer result = workCalendarDateMapper.deleteById(id);
		return SqlHelper.retBool(result);
	}

	@Override
	public boolean checkWorkDateValid(WorkCalendarDate calendarDate, Integer dictTypeLevel) {
		QueryWrapper<WorkCalendarDate> queryWrapper = new WorkCalendarDate.Builder().createQueryWrapper()
				.groupCodeEq(calendarDate.getGroupCode()).projectIdEq(calendarDate.getProjectId(), dictTypeLevel)
				.workDateEq(calendarDate.getWorkDate()).dictCodeEq(calendarDate.getDictCode())
				.dictTypeEq(calendarDate.getDictType()).customFlagEq(calendarDate.getCustomFlag())
				.builderQueryWrapper();
		WorkCalendarDate date = this.workCalendarDateMapper.selectOne(queryWrapper);
		if (date == null) {
			return true;
		}
		if (StringUtil.isNotBlank(calendarDate.getId()) && calendarDate.getId().equals(date.getId())) {
			return true;
		}
		return false;
	}

	@Override
	public boolean replaceIntoCalendarDate(WorkCalendarDate calendarDate) {
		Integer result = this.workCalendarDateMapper.replaceIntoCalendarDate(calendarDate);
		return SqlHelper.retBool(result);
	}

	@Override
	@Transactional
	public boolean batchCreateCalendarDate(List<WorkCalendarDate> datas) {
		if (CollectionUtil.isEmpty(datas)) {
			return true;
		}
		Integer result = this.workCalendarDateMapper.batchCreateCalendarDate(datas);
		return SqlHelper.retBool(result);
	}
	
}
