package com.palmnest.service.washcar.manager.impl;

import cn.jiguang.common.utils.StringUtils;
import com.palmnest.dao.washcar.ProjectSetDao;
import com.palmnest.model.washcar.ProjectSet;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.washcar.manager.ProjectSetManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 项目设置(设置名额)
 *
 * @author longmeizhen
 */
@Slf4j
@Service("projectSetManager")
public class ProjectSetManagerImpl extends GenericManagerImpl<ProjectSet, Long> implements ProjectSetManager {


	private ProjectSetDao projectSetDao;

	@Autowired
	public void setProjectSetDao(ProjectSetDao projectSetDao) {
		this.projectSetDao = projectSetDao;
		this.dao = projectSetDao;
	}

	@Transactional
	public ProjectSet saveQuota(String orgId, Long id, Integer quota, String userId) throws Exception {

		try {
			if (id == null || id == 0) {
				ProjectSet proSet = new ProjectSet();
				proSet.setCreateTime(new Date());
				proSet.setCreator(userId);
				proSet.setIsEnabled("1");
				proSet.setProjectId(orgId);
				proSet.setQuota(quota);
				proSet.setRemark("");
				proSet = projectSetDao.save(proSet);
				return proSet;
			} else {
				ProjectSet proSet = projectSetDao.get(id);
				proSet.setQuota(quota);
				proSet.setProjectId(orgId);
				proSet.setModifier(userId);
				proSet.setModifyTime(new Date());
				projectSetDao.updateObj(proSet);
				return proSet;
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			throw new Exception(e);
		}
	}

	@Override
	public ProjectSet fingByProjectId(String projectId) throws Exception {
		ProjectSet proSet = new ProjectSet();
		if (StringUtils.isNotEmpty(projectId)) {
			proSet = projectSetDao.fingByProjectId(projectId);
		} else {
			if (projectSetDao.getAll() != null && projectSetDao.getAll().size() > 0) {
				proSet = projectSetDao.getAll().get(0);
			}
		}
		return proSet;
	}

}
