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

import java.util.List;

import com.jeeplus.common.utils.Collections3;
import com.jeeplus.common.utils.MenuStatusEnum;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.projectrecord.entity.ProjectRecords;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.WorkattachmentService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workclientinfo.dao.WorkClientAttachmentDao;
import com.jeeplus.modules.workclientinfo.entity.WorkClientAttachment;
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.workperformance.entity.WorkPerformance;
import com.jeeplus.modules.workperformance.dao.WorkPerformanceDao;

/**
 * 公司业绩库Service
 * @author ssrh
 * @version 2018-08-20
 */
@Service
@Transactional(readOnly = true)
public class WorkPerformanceService extends CrudService<WorkPerformanceDao, WorkPerformance> {
	@Autowired
	private WorkattachmentService workattachmentService;
	@Autowired
	private WorkClientAttachmentDao workClientAttachmentDao;
	public WorkPerformance get(String id) {
		WorkPerformance workPerformance = super.get(id);
		//附件
		WorkClientAttachment workClientAttachment = new WorkClientAttachment();
		workClientAttachment.setAttachmentId(id);
		workClientAttachment.setAttachmentFlag("113");
		workPerformance.setWorkAttachments(workClientAttachmentDao.findList(workClientAttachment));
		return workPerformance;
	}
	
	public List<WorkPerformance> findList(WorkPerformance workPerformance) {
		return super.findList(workPerformance);
	}
	
	public Page<WorkPerformance> findPage(Page<WorkPerformance> page, WorkPerformance workPerformance) {
		//设置数据权限
		if(!UserUtils.getUser().isAdmin()) {
			String dataScopeSql = dataScopeFilterOR(workPerformance.getCurrentUser(), "o", "u", "s", MenuStatusEnum.WORK_PERFORMANCE.getValue());
			workPerformance.getSqlMap().put("dsf", dataScopeSql);
		}
		workPerformance.setPage(page);
		List<WorkPerformance> recordsList = findList(workPerformance);
		page.setList(recordsList);
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(WorkPerformance workPerformance) {
		super.save(workPerformance);
		//附件信息
		for (WorkClientAttachment workClientAttachment : workPerformance.getWorkAttachments()) {
			if (workClientAttachment.getId() == null) {
				continue;
			}
			if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())) {
				workClientAttachment.setAttachmentId(workPerformance.getId());
				workClientAttachment.setAttachmentFlag("113");
				workClientAttachment.setAttachmentUser(UserUtils.getUser().getId());
				if (StringUtils.isBlank(workClientAttachment.getId()) || "null".equals(workClientAttachment.getId())) {
					workClientAttachment.preInsert();
					workClientAttachmentDao.insert(workClientAttachment);
				} else {
					workClientAttachment.preUpdate();
					workClientAttachmentDao.update(workClientAttachment);
				}
			} else {
				workClientAttachmentDao.delete(workClientAttachment);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkPerformance workPerformance) {
		super.delete(workPerformance);
	}
	
	
	
	
}