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

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.workhandbill.dao.WorkHandBillDao;
import com.jeeplus.modules.workhandbill.dao.WorkHandBillDetailDao;
import com.jeeplus.modules.workhandbill.entity.WorkHandBill;
import com.jeeplus.modules.workhandbill.entity.WorkHandBillDetail;
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 WorkHandBillService extends CrudService<WorkHandBillDao, WorkHandBill> {

	@Autowired
	private WorkHandBillDetailDao workHandBillDetailDao;
	@Autowired
	private WorkHandBillDao workHandBillDao;

	public WorkHandBill get(String id) {
		return super.get(id);
	}
	
	public List<WorkHandBill> findList(WorkHandBill workHandBill) {
		return super.findList(workHandBill);
	}
	
	public Page<WorkHandBill> findPage(Page<WorkHandBill> page, WorkHandBill workHandBill) {
		return super.findPage(page, workHandBill);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkHandBill workHandBill) {
		super.save(workHandBill);
		List<WorkHandBillDetail> workHandBillDetailList = workHandBill.getWorkHandBillDetailList();
			for (WorkHandBillDetail workHandBillDetail : workHandBillDetailList) {
				if (WorkHandBillDetail.DEL_FLAG_NORMAL.equals(workHandBillDetail.getDelFlag())) {
					workHandBillDetail.setBillId(workHandBill.getId());
					if (workHandBillDetail.getIsNewRecord()) {
						workHandBillDetail.preInsert();
						workHandBillDetailDao.insert(workHandBillDetail);
					} else {
						workHandBillDetail.preUpdate();
						workHandBillDetailDao.update(workHandBillDetail);
					}
				}else{
					workHandBillDetailDao.delete(workHandBillDetail);
				}
			}
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkHandBill workHandBill) {
		super.delete(workHandBill);
	}
	
	public int deleteByProId(WorkHandBill workHandBill){
		return workHandBillDao.deleteByProId(workHandBill);
	}

	public WorkHandBill findListByProjectId(WorkHandBill workHandBill){
		return workHandBillDao.findListByProjectId(workHandBill);
	}
}