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

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.workdevicerecord.dao.WorkDeviceRecordDao;
import com.jeeplus.modules.workdevicerecord.dao.WorkDeviceXjRecordDao;
import com.jeeplus.modules.workdevicerecord.entity.WorkDeviceRecord;
import com.jeeplus.modules.workdevicerecord.entity.WorkDeviceXjRecord;
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-05
 */
@Service
@Transactional(readOnly = true)
public class WorkDeviceXjRecordService extends CrudService<WorkDeviceXjRecordDao, WorkDeviceXjRecord> {

	@Autowired
	private WorkDeviceRecordDao workDeviceRecordDao;
	@Autowired
	private WorkDeviceXjRecordDao workDeviceXjRecordDao;

	public WorkDeviceXjRecord get(String id) {
		return super.get(id);
	}
	
	public List<WorkDeviceXjRecord> findList(WorkDeviceXjRecord workDeviceXjRecord) {
		return super.findList(workDeviceXjRecord);
	}
	
	public Page<WorkDeviceXjRecord> findPage(Page<WorkDeviceXjRecord> page, WorkDeviceXjRecord workDeviceXjRecord) {
		return super.findPage(page, workDeviceXjRecord);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkDeviceXjRecord workDeviceXjRecord) {
		super.save(workDeviceXjRecord);
		List<WorkDeviceRecord> workDeviceRecordList = workDeviceXjRecord.getWorkDeviceRecordList();
		for(WorkDeviceRecord workDeviceRecord:workDeviceRecordList){
			if (WorkDeviceRecord.DEL_FLAG_NORMAL.equals(workDeviceRecord.getDelFlag())) {
				workDeviceRecord.setDeviceXjId(workDeviceXjRecord.getId());
				if (workDeviceRecord.getIsNewRecord()) {
					workDeviceRecord.preInsert();
					workDeviceRecordDao.insert(workDeviceRecord);
				} else {
					workDeviceRecord.preUpdate();
					workDeviceRecordDao.update(workDeviceRecord);
				}
			}else{
				workDeviceRecordDao.delete(workDeviceRecord);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkDeviceXjRecord workDeviceXjRecord) {
		super.delete(workDeviceXjRecord);
	}


	public WorkDeviceXjRecord findListByXjId(WorkDeviceXjRecord workDeviceXjRecord){
		return workDeviceXjRecordDao.findListByXjId(workDeviceXjRecord);
	}
	
}