package com.sinosoft.microservice.product.manage.basicinfo.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxytiandi.encrypt.springboot.annotation.DecryptIgnore;
import com.cxytiandi.encrypt.springboot.annotation.EncryptIgnore;
import com.sinosoft.microservice.product.manage.basicinfo.mapper.PdPdInfoMapper;
import com.sinosoft.microservice.product.manage.basicinfo.mapper.PdProductCopyInfoMapper;
import com.sinosoft.microservice.product.manage.basicinfo.mapper.PdtempPdVerRlsMapper;
import com.sinosoft.microservice.product.manage.basicinfo.service.PdtempPdInfoService;
import com.sinosoft.microservice.product.manage.basicinfo.service.PdtempPdVerRlsService;
import com.sinosoft.microservice.product.manage.basicinfo.vo.PdProductCopyInfoVo;
import com.sinosoft.microservice.product.manage.config.common.sqlexecute.ExeSql;
import com.sinosoft.microservice.product.manage.config.dto.entry.BaseRequest;
import com.sinosoft.microservice.product.manage.config.utils.FileHttpUtil;
import com.sinosoft.microservice.product.manage.config.utils.PageHandler;
import com.sinosoft.microservice.product.manage.domain.basicinfo.PdPdInfo;
import com.sinosoft.microservice.product.manage.domain.basicinfo.PdtempPdAddAuditInfo;
import com.sinosoft.microservice.product.manage.domain.basicinfo.PdtempPdInfo;
import com.sinosoft.microservice.product.manage.domain.basicinfo.PdtempPdVerRls;
import com.sinosoft.microservice.product.manage.workflow.service.WorkFlowService;
import com.sinosoft.microservice.product.manage.workflow.vo.ProductReleaseInfoVo;
import com.sinosoft.microservice.product.manage.workflow.vo.TaskVo;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
*
* 服务实现类
*
*
* @author chenganyuan
* @since 2020-05-12
*/
@Service
public class PdtempPdVerRlsServiceImpl extends ServiceImpl implements PdtempPdVerRlsService {
@Autowired
private PdtempPdVerRlsMapper mapper;
@Autowired
private ExeSql exeSql;
@Resource
private WorkFlowService workFlowService;
@Resource
private PdProductCopyInfoMapper copyMapper;
@Resource
private SqlSession sqlSession;
@Resource
private PdtempPdInfoService pdtempPdInfoService;
@Resource
private PdPdInfoMapper pdPdInfoMapper;
private List mFileList;
private File zipFile;
@Override
public Long savePdtempPdVerRls(PdtempPdVerRls entity) {
if (entity.getId() != null) {
entity.updateById();
} else {
entity.insert();
}
return entity.getId();
}
public Long updatePdtempPdVerRls(PdtempPdVerRls entity) {
return savePdtempPdVerRls(entity);
}
@Override
public void selectPdtempPdVerRlssByPage(Page page, QueryWrapper queryWrapper) {
baseMapper.selectPage(page, queryWrapper);
}
@Override
public void deletePdtempPdVerRlsById(Long id) {
baseMapper.deleteById(id);
}
@Override
public PdtempPdVerRls findPdtempPdVerRlsById(Long id) {
return baseMapper.selectById(id);
}
//生成脚本文件入口方法
@Override
@EncryptIgnore
@Transactional(rollbackFor = Exception.class)
public PdtempPdVerRls releInfo(HttpServletResponse response, PdtempPdVerRls input, int num) {
try {
String pdCode = input.getPdCode();
String pdEdition = input.getPdEdition();
PdtempPdInfo pdtempPdInfo = pdtempPdInfoService.findPdtempPdInfoByPdCodeAndPdEdition(pdCode, pdEdition);
/*生成脚本*/
String sqlFileName = createSqlFile(pdCode,pdEdition,input,num);
/* if (num == 2){
downloadFile(response,sqlFileName);
}*/
/*temp 转数据 到 pd*/
copyDataFromTempToPd(pdtempPdInfo.getId(),pdtempPdInfo);
/* 保存发布记录 */
savePdtempPdVerRls(input);
} catch (Exception e) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
e.printStackTrace();
//throw new RuntimeException(); // 会自动回滚
}
return input;
}
/* 生成脚本文件 */
/*1. 查询对应字段数据*/
public String createSqlFile(String pdCode, String pdEdition, PdtempPdVerRls input, int num) {
String sqlFileName = "";
try {
String fileName = baseMapper.getFileName();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HHmmss");
String date = formatter.format(new Date());
String path = "D:\\file\\template";
File file = new File(path);
//判断文件夹是否存在
if (!file.exists()) {
file.getParentFile().mkdirs();
}
file = new File(path+"\\"+fileName+"_"+pdCode+"_"+date+".sql");// 脚本名称
input.setPdReleFilePath(path); // 存储文件生成路径
if (num == 2){
sqlFileName = file.getPath();
input.setPdReleFile(fileName + "_" + pdCode + "_" + date + ".sql"); //存储文件名称
}else if(num == 1){
mFileList = new ArrayList();
mFileList.add(file);
String zipFileName = fileName+"_"+date;
input.setPdReleFile(zipFileName+".zip"); //存储zip文件名称
zipFile = new File(path+"\\"+zipFileName+".zip"); //压缩包名称
}
file.createNewFile();// 创建新文件
//准备好一个输出的对象,通过对象多态性,进行实例化
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
StringBuffer sColumn = new StringBuffer();
StringBuffer sCondition = new StringBuffer();
StringBuffer insSql = new StringBuffer();
StringBuffer delSql = new StringBuffer();
/* 获取所有要生成的表 */
List otherTableNames = baseMapper.getOtherTableNames();
for (int i = 0; i < otherTableNames.size(); i++) {
//将StringBuffer清空
sColumn.setLength(0);
sCondition.setLength(0);
insSql.setLength(0);
delSql.setLength(0);
String othertablename = otherTableNames.get(i).toString();
//开始拼接sql
sColumn.append("select ");
sCondition.append(" where ");
insSql.append("insert into "+othertablename+" ( ");
delSql.append("delete from "+othertablename+" where ( ");
List