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

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.MenuStatusEnum;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.projectapproval.dao.ProjectApprovalDao;
import com.jeeplus.modules.projectapproval.entity.ProjectApproval;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectReportRecord;
import com.jeeplus.modules.projectrecord.entity.ProjectRecords;
import com.jeeplus.modules.serialnum.service.SerialNumTplService;
import com.jeeplus.modules.sys.dao.WorkattachmentDao;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.entity.Workattachment;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workreimbursement.entity.WorkReimbursement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 项目报批Service
 */
@Service
@Transactional(readOnly = true)
public class ProjectApprovalService extends CrudService<ProjectApprovalDao, ProjectApproval> {
	@Autowired
	private WorkattachmentDao workattachmentDao;
	@Autowired
	private SerialNumTplService serialNumTplService;

	public ProjectApproval get(String id) {
		ProjectApproval projectBasedData = super.get(id);
		if(projectBasedData!=null) {
            Workattachment workattachment = new Workattachment();
            workattachment.setAttachmentId(id);
            workattachment.setAttachmentFlag("84");
            projectBasedData.setWorkAttachments(workattachmentDao.findList(workattachment));
        }
		return projectBasedData;
	}


	public List<ProjectApproval> findList(ProjectApproval projectBasedData) {
		return super.findList(projectBasedData);
	}
	
	public Page<ProjectApproval> findPage(Page<ProjectApproval> page, ProjectApproval projectBasedData) {
		projectBasedData.getSqlMap().put("dsf", dataScopeFilter(UserUtils.getUser(), "o", "u","s", MenuStatusEnum.WORK_OVERTIME.getValue()));
		return super.findPage(page, projectBasedData);
	}
	
	@Transactional(readOnly = false)
	public void save(ProjectApproval projectBasedData) {
		User user = UserUtils.getUser();
//		if (StringUtils.isBlank(projectBasedData.getNum())) {
//			projectBasedData.setNum(serialNumTplService.genSerialNum(user.getCompany(), WorkReimbursement.SERIAL_BIZCODE));
//		}
		super.save(projectBasedData);
		saveWorkattachments(projectBasedData);
	}

	@Transactional(readOnly = false)
	public void saveWorkattachments(ProjectApproval projectBasedData) {
		List<Workattachment> workattachments = projectBasedData.getWorkAttachments();
		if (workattachments!=null && workattachments.size()!=0){
			for (Workattachment workattachment:workattachments){
				if (workattachment.getId() == null) {
					continue;
				}
				if (workattachment.DEL_FLAG_NORMAL.equals(workattachment.getDelFlag())) {
					workattachment.setAttachmentId(projectBasedData.getId());
					workattachment.setAttachmentFlag("84");
					workattachment.setAttachmentUser(UserUtils.getUser().getId());
					if (StringUtils.isBlank(workattachment.getId()) || "null".equals(workattachment.getId())) {
						workattachment.preInsert();
						workattachmentDao.insert(workattachment);
					} else {
						workattachment.preUpdate();
						workattachmentDao.update(workattachment);
					}
				} else {
					workattachmentDao.delete(workattachment);
				}
			}
		}

	}


}