|  | @@ -5,13 +5,11 @@ import java.io.IOException;
 | 
	
		
			
				|  |  |  import java.math.BigDecimal;
 | 
	
		
			
				|  |  |  import java.text.DateFormat;
 | 
	
		
			
				|  |  |  import java.text.SimpleDateFormat;
 | 
	
		
			
				|  |  | -import java.util.Date;
 | 
	
		
			
				|  |  | -import java.util.HashMap;
 | 
	
		
			
				|  |  | -import java.util.List;
 | 
	
		
			
				|  |  | -import java.util.Map;
 | 
	
		
			
				|  |  | +import java.util.*;
 | 
	
		
			
				|  |  |  import java.util.Map.Entry;
 | 
	
		
			
				|  |  |  import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.dao.StudentDao;
 | 
	
		
			
				|  |  |  import org.apache.commons.io.FileUtils;
 | 
	
		
			
				|  |  |  import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.InitializingBean;
 | 
	
	
		
			
				|  | @@ -63,6 +61,9 @@ import com.ym.mec.util.date.DateUtil;
 | 
	
		
			
				|  |  |  import com.ym.mec.util.freemarker.FreemarkerTemplateEngine;
 | 
	
		
			
				|  |  |  import com.ym.mec.util.money.MoneyUtil;
 | 
	
		
			
				|  |  |  import com.ym.mec.util.pdf.PDFUtil;
 | 
	
		
			
				|  |  | +import org.springframework.transaction.annotation.Isolation;
 | 
	
		
			
				|  |  | +import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | +import org.springframework.util.CollectionUtils;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  @Service
 | 
	
		
			
				|  |  |  public class ContractServiceImpl implements ContractService, InitializingBean {
 | 
	
	
		
			
				|  | @@ -116,6 +117,9 @@ public class ContractServiceImpl implements ContractService, InitializingBean {
 | 
	
		
			
				|  |  |  	@Autowired
 | 
	
		
			
				|  |  |  	private MusicGroupService musicGroupService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +	@Autowired
 | 
	
		
			
				|  |  | +	private StudentDao studentDao;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  	@Value("${contract.baseDir:/var/pdf}")
 | 
	
		
			
				|  |  |  	private String contractBaseDir;
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -285,6 +289,10 @@ public class ContractServiceImpl implements ContractService, InitializingBean {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	@Override
 | 
	
		
			
				|  |  |  	public boolean transferMusicGroupCoursesContract(Integer userId, String musicGroupId) {
 | 
	
		
			
				|  |  | +		if(true){
 | 
	
		
			
				|  |  | +			return transferProduceContract(userId, musicGroupId);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  		SysUserTsign sysUserTsign = sysUserTsignService.get(userId);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  		if (sysUserTsign == null) {
 | 
	
	
		
			
				|  | @@ -477,6 +485,10 @@ public class ContractServiceImpl implements ContractService, InitializingBean {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	@Override
 | 
	
		
			
				|  |  |  	public boolean transferVipGroupCoursesContract(Integer userId, Long vipGroupId) {
 | 
	
		
			
				|  |  | +		if(true){
 | 
	
		
			
				|  |  | +			return transferProduceContract(userId, null);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  		SysUserTsign sysUserTsign = sysUserTsignService.get(userId);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  		if (sysUserTsign == null) {
 | 
	
	
		
			
				|  | @@ -616,6 +628,10 @@ public class ContractServiceImpl implements ContractService, InitializingBean {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	@Override
 | 
	
		
			
				|  |  |  	public boolean transferGoodsContract(Integer userId, String musicGroupId, String goodsIds, KitGroupPurchaseTypeEnum kitGroupPurchaseTypeEnum) {
 | 
	
		
			
				|  |  | +		if(true){
 | 
	
		
			
				|  |  | +			return transferProduceContract(userId, musicGroupId);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  		SysUserTsign sysUserTsign = sysUserTsignService.get(userId);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  		if (sysUserTsign == null) {
 | 
	
	
		
			
				|  | @@ -847,6 +863,10 @@ public class ContractServiceImpl implements ContractService, InitializingBean {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	@Override
 | 
	
		
			
				|  |  |  	public boolean transferPracticeCoursesContract(Integer userId, int courseSectionNum, Date startDate, Date endDate, BigDecimal fee) {
 | 
	
		
			
				|  |  | +		if(true){
 | 
	
		
			
				|  |  | +			return transferProduceContract(userId, null);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  		SysUserTsign sysUserTsign = sysUserTsignService.get(userId);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  		if (sysUserTsign == null) {
 | 
	
	
		
			
				|  | @@ -920,4 +940,148 @@ public class ContractServiceImpl implements ContractService, InitializingBean {
 | 
	
		
			
				|  |  |  		return true;
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +	@Override
 | 
	
		
			
				|  |  | +	@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
 | 
	
		
			
				|  |  | +	public boolean transferProduceContract(Integer userId,String musicGroupId) {
 | 
	
		
			
				|  |  | +		SysUser user = studentDao.lockUserReturnInfo(userId);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		List<SysUserContracts> userContracts = sysUserContractsService.getUserContractWithType(userId, ContractType.PRODUCT);
 | 
	
		
			
				|  |  | +		if(!CollectionUtils.isEmpty(userContracts)){
 | 
	
		
			
				|  |  | +			return true;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		SysUserTsign sysUserTsign = sysUserTsignService.get(userId);
 | 
	
		
			
				|  |  | +		if (sysUserTsign == null) {
 | 
	
		
			
				|  |  | +			if (user == null) {
 | 
	
		
			
				|  |  | +				throw new BizException("用户信息查询失败");
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			sysUserTsign = register(userId, user.getRealName(), user.getIdCardNo(), user.getPhone());
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		Date date = new Date();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 合成freemarker
 | 
	
		
			
				|  |  | +		String srcPdfPath = contractBaseDir + "/product/" + dateFormat1.format(date) + "/" + userId + ".pdf";
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		File srcFile = new File(srcPdfPath);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		File debtFile = new File(srcFile.getParent());
 | 
	
		
			
				|  |  | +		if (!debtFile.exists()) {
 | 
	
		
			
				|  |  | +			debtFile.mkdirs();
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		FreemarkerTemplateEngine templateEngine = FreemarkerTemplateEngine.getInstance();
 | 
	
		
			
				|  |  | +		templateEngine.setClassForTemplateLoading(ContractServiceImpl.class, "/config/contracts/");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		Map<String, Object> params = new HashMap<String, Object>();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 查询参数信息
 | 
	
		
			
				|  |  | +		StudentInfo studentInfo = studentRegistrationService.queryStudentInfo(userId);
 | 
	
		
			
				|  |  | +		params.put("studentInfo", studentInfo);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		params.put("isShowVisualSeal", false);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		if(StringUtils.isBlank(musicGroupId)){
 | 
	
		
			
				|  |  | +			params.put("ownershipType", "OWN");
 | 
	
		
			
				|  |  | +		}else{
 | 
	
		
			
				|  |  | +			MusicGroup musicGroup = musicGroupService.get(musicGroupId);
 | 
	
		
			
				|  |  | +			if (musicGroup == null) {
 | 
	
		
			
				|  |  | +				throw new BizException("乐团信息没找到");
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			params.put("ownershipType", musicGroup.getOwnershipType().name());
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		templateEngine.render(params, "product.ftl", srcFile);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 生成借款协议PDF
 | 
	
		
			
				|  |  | +		try {
 | 
	
		
			
				|  |  | +			PDFUtil.renderToPDFByData(ContractServiceImpl.class.getResource("/").getFile(), FileUtils.readFileToString(srcFile), srcPdfPath, "simsun.ttc");
 | 
	
		
			
				|  |  | +		} catch (IOException e) {
 | 
	
		
			
				|  |  | +			throw new BizException("生成产品协议失败", e);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		String organCode = "";
 | 
	
		
			
				|  |  | +		if (OwnershipType.OWN.name().equals(params.get("ownershipType"))){
 | 
	
		
			
				|  |  | +			organCode = "91440300326364429H";
 | 
	
		
			
				|  |  | +		} else {
 | 
	
		
			
				|  |  | +			organCode = "91420106333619290A";
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		SysUserTsign organTsign = sysUserTsignService.queryByCardNo(organCode);
 | 
	
		
			
				|  |  | +		if (organTsign == null) {
 | 
	
		
			
				|  |  | +			throw new BizException("甲方未创建签章");
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		eSealPlugin.organSign(organTsign.getSealData(), srcPdfPath, srcPdfPath);
 | 
	
		
			
				|  |  | +		if (sysUserTsign != null) {
 | 
	
		
			
				|  |  | +			eSealPlugin.userSign(sysUserTsign.getAccountId(), sysUserTsign.getSealData(), srcPdfPath, srcPdfPath);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 上传到oss
 | 
	
		
			
				|  |  | +		String dateStrOss = dateFormatOss.format(date);
 | 
	
		
			
				|  |  | +		dateStrOss = "product/" + dateStrOss + "/" + DateUtil.getHour(date);
 | 
	
		
			
				|  |  | +		String pdfFilePath = storagePluginContext.uploadFile(KS3StoragePlugin.PLUGIN_NAME, dateStrOss, srcFile);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		SysUserContracts sysUserContracts = new SysUserContracts();
 | 
	
		
			
				|  |  | +		sysUserContracts.setCreateTime(date);
 | 
	
		
			
				|  |  | +		sysUserContracts.setType(ContractType.PRODUCT);
 | 
	
		
			
				|  |  | +		sysUserContracts.setUrl(pdfFilePath);
 | 
	
		
			
				|  |  | +		sysUserContracts.setUserId(userId);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		sysUserContractsService.insert(sysUserContracts);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		FileUtils.deleteQuietly(srcFile);
 | 
	
		
			
				|  |  | +		return true;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	@Override
 | 
	
		
			
				|  |  | +	@Transactional(isolation = Isolation.READ_COMMITTED)
 | 
	
		
			
				|  |  | +	public Map<String, Object> queryProductContract(Integer userId,String musicGroupId) {
 | 
	
		
			
				|  |  | +		// 合成freemarker
 | 
	
		
			
				|  |  | +		String srcPdfPath = contractBaseDir + "/product/" + userId + ".pdf";
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		File srcFile = new File(srcPdfPath);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		File debtFile = new File(srcFile.getParent());
 | 
	
		
			
				|  |  | +		if (!debtFile.exists()) {
 | 
	
		
			
				|  |  | +			debtFile.mkdirs();
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		FreemarkerTemplateEngine templateEngine = FreemarkerTemplateEngine.getInstance();
 | 
	
		
			
				|  |  | +		templateEngine.setClassForTemplateLoading(ContractServiceImpl.class, "/config/contracts/");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		Map<String, Object> params = new HashMap<String, Object>();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		StudentInfo studentInfo = studentRegistrationService.queryStudentInfo(userId);
 | 
	
		
			
				|  |  | +		params.put("studentInfo", studentInfo);
 | 
	
		
			
				|  |  | +		params.put("isShowVisualSeal", true);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		if(StringUtils.isBlank(musicGroupId)){
 | 
	
		
			
				|  |  | +			params.put("ownershipType", "OWN");
 | 
	
		
			
				|  |  | +		}else{
 | 
	
		
			
				|  |  | +			MusicGroup musicGroup = musicGroupService.get(musicGroupId);
 | 
	
		
			
				|  |  | +			if (musicGroup == null) {
 | 
	
		
			
				|  |  | +				throw new BizException("乐团信息没找到");
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			params.put("ownershipType", musicGroup.getOwnershipType().name());
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		templateEngine.render(params, "product.ftl", srcFile);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		String html = "";
 | 
	
		
			
				|  |  | +		try {
 | 
	
		
			
				|  |  | +			html = FileUtils.readFileToString(srcFile);
 | 
	
		
			
				|  |  | +		} catch (IOException e) {
 | 
	
		
			
				|  |  | +			throw new BizException("读取产品协议出错", e);
 | 
	
		
			
				|  |  | +		} finally {
 | 
	
		
			
				|  |  | +			FileUtils.deleteQuietly(srcFile);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		List<SysUserContracts> userContractWithType = sysUserContractsService.getUserContractWithType(userId, ContractType.PRODUCT);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		Map<String, Object> result = new HashMap<>();
 | 
	
		
			
				|  |  | +		result.put("exists", !CollectionUtils.isEmpty(userContractWithType));
 | 
	
		
			
				|  |  | +		result.put("productContract", html);
 | 
	
		
			
				|  |  | +		return result;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  }
 |