/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workcommunicaterecord.service;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.workcommunicaterecord.dao.WorkCommunicateContentDao;
import com.jeeplus.modules.workcommunicaterecord.dao.WorkCommunicateRecordDao;
import com.jeeplus.modules.workcommunicaterecord.entity.WorkCommunicateContent;
import com.jeeplus.modules.workcommunicaterecord.entity.WorkCommunicateRecord;
import com.jeeplus.modules.workdevicerecord.entity.WorkDeviceRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 沟通记录Service
 * @author 马鹏波
 * @version 2018-06-06
 */
@Service
@Transactional(readOnly = true)
public class WorkCommunicateRecordService extends CrudService<WorkCommunicateRecordDao, WorkCommunicateRecord> {

	@Autowired
	private WorkCommunicateContentDao workCommunicateContentDao;
	@Autowired
	private WorkCommunicateRecordDao workCommunicateRecordDao;

	public WorkCommunicateRecord get(String id) {
		return super.get(id);
	}
	
	public List<WorkCommunicateRecord> findList(WorkCommunicateRecord workCommunicateRecord) {
		return super.findList(workCommunicateRecord);
	}
	
	public Page<WorkCommunicateRecord> findPage(Page<WorkCommunicateRecord> page, WorkCommunicateRecord workCommunicateRecord) {
		return super.findPage(page, workCommunicateRecord);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkCommunicateRecord workCommunicateRecord) {
		super.save(workCommunicateRecord);
		List<WorkCommunicateContent> workCommunicateContentList = workCommunicateRecord.getWorkCommunicateContentList();
		for(WorkCommunicateContent workCommunicateContent:workCommunicateContentList){
			if (WorkDeviceRecord.DEL_FLAG_NORMAL.equals(workCommunicateContent.getDelFlag())) {
				workCommunicateContent.setCommunicateId(workCommunicateRecord.getId());
				if (workCommunicateContent.getIsNewRecord()) {
					workCommunicateContent.preInsert();
					workCommunicateContentDao.insert(workCommunicateContent);
				} else {
					workCommunicateContent.preUpdate();
					workCommunicateContentDao.update(workCommunicateContent);
				}
			}else{
				workCommunicateContentDao.delete(workCommunicateContent);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkCommunicateRecord workCommunicateRecord) {
		super.delete(workCommunicateRecord);
	}

	public WorkCommunicateRecord findListByProId(WorkCommunicateRecord workCommunicateRecord){
		return workCommunicateRecordDao.findListByProId(workCommunicateRecord);
	}
	
	
}