package com.persagy.calendar.service.impl;

import java.util.List;
import java.util.Set;

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.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.persagy.calendar.constant.WorkCalendarConstant;
import com.persagy.calendar.mapper.WorkCalendarDateMapper;
import com.persagy.calendar.mapper.WorkCalendarMapper;
import com.persagy.calendar.mapper.WorkCalendarObjectMapper;
import com.persagy.calendar.pojo.dto.WorkCalendar;
import com.persagy.calendar.pojo.dto.WorkCalendarDate;
import com.persagy.calendar.pojo.dto.WorkCalendarObject;
import com.persagy.calendar.service.IWorkCalendarService;
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 11:33:00
 */
@Service
public class WorkCalendarServiceImpl extends SuperServiceImpl<WorkCalendarMapper, WorkCalendar> implements IWorkCalendarService {
	
	@Autowired
	private WorkCalendarMapper workCalendarMapper;
	
	@Autowired
	private WorkCalendarDateMapper workCalendarDateMapper;
	
	@Autowired
	private WorkCalendarObjectMapper workCalendarObjectMapper;
	
	@Override
	public List<WorkCalendar> queryCalendarObjectList(WorkCalendar calendar) {
		return this.workCalendarMapper.queryCalendarObjectList(calendar);
	}
	
	@Override
	public WorkCalendar getById(String id, String groupCode, String projectId) {
    	QueryWrapper<WorkCalendar> queryWrapper = new WorkCalendar.Builder().createQueryWrapper().idEq(id).projectIdEq(projectId)
    			.groupCodeEq(groupCode).delFlagEq(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR).builderQueryWrapper();
		return workCalendarMapper.selectOne(queryWrapper);
	}
	
	@Override
	public boolean checkProjetcIdAndName(String id, String projectId, String groupCode, String calendarName) {
		QueryWrapper<WorkCalendar> queryWrapper = new WorkCalendar.Builder().createQueryWrapper().groupCodeEq(groupCode)
				.projectIdEq(projectId).calendarNameEq(calendarName).delFlagEq(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR)
				.builderQueryWrapper();
		WorkCalendar workCalendar = workCalendarMapper.selectOne(queryWrapper);
		// 数据不存在允许，允许被插入或更新
		if (workCalendar == null) {
			return true;
		}
		
		// 数据存在，且是更新情况时
		if (StringUtil.isNotBlank(id) && workCalendar != null) {
			if (id.equals(workCalendar.getId())) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	@Transactional
	public boolean createWorkCalendar(WorkCalendar workCalendar, Set<String> objIds) {
		Integer result = workCalendarMapper.insert(workCalendar);
		boolean retBool = SqlHelper.retBool(result);
		if (retBool && CollectionUtil.isNotEmpty(objIds)) {
			// 插入对象与工作历的关联表中
			WorkCalendarObject object = new WorkCalendarObject();
			object.setGroupCode(workCalendar.getGroupCode());
			object.setCalendarId(workCalendar.getId());
			object.setCreateTime(workCalendar.getCreateTime());
			object.setUpdateUser(workCalendar.getUpdateUser());
			object.setProjectId(workCalendar.getProjectId());
			this.workCalendarObjectMapper.batchInsertObjects(object, objIds);
		}
		return retBool;
	}
	
	@Override
	@Transactional
	public boolean updateWorkCalendar(WorkCalendar workCalendar) {
		Integer result = workCalendarMapper.updateById(workCalendar);
		boolean retBool = SqlHelper.retBool(result);
		if (retBool) {
			// 修改date表中的name和desc属性
			WorkCalendarDate calendarDate = new WorkCalendarDate();
			calendarDate.setCalendarName(workCalendar.getCalendarName());
			calendarDate.setCalendarDesc(workCalendar.getCalendarDesc());
			calendarDate.setUpdateTime(workCalendar.getUpdateTime());
			UpdateWrapper<WorkCalendarDate> updateWrapper = new WorkCalendarDate.Builder().createUpdateWrapper()
					.calendarIdEq(workCalendar.getId(), null).projectIdEq(workCalendar.getProjectId(), null).builderUpdateWrapper();
			this.workCalendarDateMapper.update(calendarDate, updateWrapper);
		}
		return SqlHelper.retBool(result);
	}
	
	@Override
	@Transactional
	public boolean deleteWorkCalendar(String id, String updateUser) {
		WorkCalendar workCalendar = new WorkCalendar();
		workCalendar.setId(id);
		workCalendar.setUpdateUser(updateUser);
		workCalendar.setDelFlag(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_DEL);
		Integer result = workCalendarMapper.updateById(workCalendar);
		return SqlHelper.retBool(result);
	}

}
