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

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.officeintroduce.entity.Officeintroduce;
import com.jeeplus.modules.projectreportnum.dao.ProjectReportNumDao;
import com.jeeplus.modules.projectreportnum.entity.ReportNum;
import com.jeeplus.modules.serialnum.service.SerialNumTplService;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(readOnly = true)
public class ProjectReportNumService extends CrudService<ProjectReportNumDao, ReportNum> {

	@Autowired
	private ProjectReportNumDao projectReportNumDao;
	@Autowired
	private SerialNumTplService serialNumTplService;


	public ReportNum get(String id) {
		return super.get(id);

	}

	public int countNum(String userId){
		return projectReportNumDao.countNum(userId);
	}
	
	public List<ReportNum> findList(ReportNum reportNum) {
		return super.findList(reportNum);
	}
	
	public Page<ReportNum> findPage(Page<ReportNum> page, ReportNum reportNum) {
		return super.findPage(page, reportNum);
	}

	public void updateNum(String num){
		projectReportNumDao.updateNum(num);
	}

	
	@Transactional(readOnly = false)
	public String  save(String type) {
		ReportNum reportNum = null;
		int count = projectReportNumDao.countNum(UserUtils.getUser().getId());
		if(count>=10){
			return "最多领取10个报告号";
		}
		if(type.equals("one")){
			String num = serialNumTplService.genSerialNum(UserUtils.getUser().getCompany(), "10");
			reportNum = new ReportNum();
			reportNum.setNum(num);
			reportNum.setState("0");
			super.save(reportNum);
		}else {
             for(int i=0;i<10-count;i++){
				 String num = serialNumTplService.genSerialNum(UserUtils.getUser().getCompany(), "10");
				 reportNum = new ReportNum();
				 reportNum.setNum(num);
				 reportNum.setState("0");
				 super.save(reportNum);
			 }
		}
        return "领取成功";
	}
	
	@Transactional(readOnly = false)
	public void delete(ReportNum reportNum) {
		super.delete(reportNum);
	}

	
	
}