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

import java.util.List;

import com.jeeplus.common.utils.MenuStatusEnum;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.workeffectdetail.dao.WorkEffectDetailDao;
import com.jeeplus.modules.workeffectdetail.entity.WorkEffectDetail;
import com.jeeplus.modules.workeffectdetail.service.WorkEffectDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.workeffect.entity.WorkEffect;
import com.jeeplus.modules.workeffect.dao.WorkEffectDao;

/**
 * 绩效Service
 * @author ssrh
 * @version 2018-09-11
 */
@Service
@Transactional(readOnly = true)
public class WorkEffectService extends CrudService<WorkEffectDao, WorkEffect> {
	@Autowired
	private WorkEffectDetailService workEffectDetailService;
	@Autowired
	private WorkEffectDetailDao workEffectDetailDao;

	public WorkEffect get(String id) {
		WorkEffect workEffect = super.get(id);
		if(workEffect!=null){
			//明细
			WorkEffectDetail workEffectDetail = new WorkEffectDetail();
			workEffectDetail.setEffectId(id);
			List<WorkEffectDetail> list = workEffectDetailService.findList(workEffectDetail);
			workEffect.setEffectDetailList(list);
		}
		return workEffect;

	}
	
	public List<WorkEffect> findList(WorkEffect workEffect) {
		return super.findList(workEffect);
	}
	
	public Page<WorkEffect> findPage(Page<WorkEffect> page, WorkEffect workEffect) {
		workEffect.getSqlMap().put("dsf", dataScopeFilter(workEffect.getCurrentUser(), "o", "u","s", MenuStatusEnum.WORK_EFFECT.getValue()));
		workEffect.setPage(page);
		page.setList(findList(workEffect));
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(WorkEffect workEffect) {
		super.save(workEffect);
		for (WorkEffectDetail workEffectDetail : workEffect.getEffectDetailList()){
			if (workEffectDetail.getId() == null){
				continue;
			}
			if (workEffectDetail.DEL_FLAG_NORMAL.equals(workEffectDetail.getDelFlag())){
				workEffectDetail.setEffectId(workEffect.getId());
				if (StringUtils.isBlank(workEffectDetail.getId())){
					//workEffectDetail.preInsert();
					//workEffectDetailDao.insert(workEffectDetail);
				}else{
					workEffectDetail.preUpdate();
					//修改金额
					workEffectDetailDao.updateMoney(workEffectDetail);
				}
			}else{
				//workEffectDetailDao.delete(workEffectDetail);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkEffect workEffect) {
		super.delete(workEffect);
		//删除子表
		if(workEffect!=null &&StringUtils.isNotBlank(workEffect.getId())){
			workEffectDetailDao.deleteByEffectId(workEffect.getId());
		}
	}
	@Transactional(readOnly = false)
	public void updateEffectNum(WorkEffect workEffect) {
		dao.updateEffectNum(workEffect);
	}
	@Transactional(readOnly = false)
	public void updateMoney(WorkEffect workEffect) {
		dao.updateMoney(workEffect);
	}
}