@ -1,23 +1,37 @@
package cn.estsh.i3plus.ext.mes.apiservice.controller.base ;
import cn.estsh.i3plus.ext.mes.api.base.IMesEnumExtService ;
import cn.estsh.i3plus.ext.mes.api.busi.report.IMesYfReportService ;
import cn.estsh.i3plus.ext.mes.apiservice.utils.MesException ;
import cn.estsh.i3plus.ext.mes.pojo.constant.MesCommonConstant ;
import cn.estsh.i3plus.platform.common.tool.ExcelTool ;
import cn.estsh.i3plus.pojo.base.bean.ListPager ;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil ;
import cn.estsh.i3plus.pojo.mes.bean.MesProduceSn ;
import cn.estsh.i3plus.pojo.mes.bean.MesProductionRecord ;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil ;
import cn.estsh.i3plus.pojo.model.common.ExportDataModel ;
import cn.estsh.impp.framework.base.controller.MesBaseController ;
import cn.estsh.impp.framework.boot.auth.AuthUtil ;
import cn.estsh.impp.framework.boot.exception.ImppBusiException ;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder ;
import cn.estsh.impp.framework.boot.util.RedisCacheTool ;
import cn.estsh.impp.framework.boot.util.ResultBean ;
import cn.estsh.impp.framework.boot.util.ValidatorBean ;
import com.alibaba.fastjson.JSON ;
import io.swagger.annotations.Api ;
import io.swagger.annotations.ApiOperation ;
import org.apache.commons.collections.CollectionUtils ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.web.bind.annotation.GetMapping ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RequestParam ;
import org.springframework.web.bind.annotation.RestController ;
import org.springframework.web.bind.annotation.* ;
import javax.persistence.EntityManager ;
import javax.servlet.http.HttpServletResponse ;
import java.io.* ;
import java.net.URLEncoder ;
import java.util.LinkedHashMap ;
import java.util.Map ;
import java.util.Objects ;
/ * *
* @Description :
@ -35,6 +49,12 @@ public class BaseExtDataController extends MesBaseController {
@Autowired
private IMesEnumExtService enumService ;
@Autowired
private IMesYfReportService mesYfReportService ;
@Autowired
private EntityManager entityManager ;
@GetMapping ( value = "/enumlist" )
@ApiOperation ( value = "获取MES系统所有枚举" )
public ResultBean enumlist ( @RequestParam ( name = "enumName" ) String enumName ) {
@ -81,5 +101,78 @@ public class BaseExtDataController extends MesBaseController {
}
}
@ApiOperation ( value = "导出" , notes = "导出" )
@PostMapping ( "/data-export" )
public ResultBean doDataExport ( @RequestBody ExportDataModel exportDataModel , HttpServletResponse response ) {
try {
ValidatorBean . beginValid ( exportDataModel )
. notNull ( "exportCol" , exportDataModel . getExportCol ( ) )
. notNull ( "selectWhere" , exportDataModel . getSelectWhere ( ) )
. notNull ( "exportMode" , exportDataModel . getExportMode ( ) ) ;
File excelFile = null ;
ExcelTool excelTool = new ExcelTool ( this . entityManager , RedisCacheTool . getImppRedis ( ) ) ;
Class mesClass = null ;
ListPager listPager = null ;
switch ( MesExtEnumUtil . DATA_EXPORT . valueOfCode ( exportDataModel . getExportMode ( ) ) ) {
case MES_PRODUCE_SN_DATA_EXPORT :
mesClass = MesProduceSn . class ;
//查询数据
listPager = mesYfReportService . queryMesProduceSn ( JSON . parseObject ( exportDataModel . getSelectWhere ( ) , MesProduceSn . class ) , null ) ; break ;
case MES_PRODUCTION_RECORD_DATA_EXPORT :
mesClass = MesProductionRecord . class ;
//查询数据
listPager = mesYfReportService . queryMesProductionRecord ( JSON . parseObject ( exportDataModel . getSelectWhere ( ) , MesProductionRecord . class ) , null ) ; break ;
default :
break ;
}
if ( Objects . isNull ( listPager ) | | CollectionUtils . isEmpty ( listPager . getObjectList ( ) ) ) {
MesException . throwMesBusiException ( "未查询到有效数据" ) ;
}
LinkedHashMap < String , String > exportCol = ( LinkedHashMap ) JSON . parseObject ( exportDataModel . getExportCol ( ) , LinkedHashMap . class ) ;
excelFile = new File ( System . getProperty ( "java.io.tmpdir" ) + File . separator + mesClass . getSimpleName ( ) + ".xls" ) ;
excelFile . createNewFile ( ) ;
excelTool . exportData ( excelFile , listPager . getObjectList ( ) , mesClass , exportCol ) ;
response . setContentType ( "application/vnd.ms-excel;charset=UTF-8" ) ;
response . setCharacterEncoding ( "UTF-8" ) ;
response . setHeader ( "Content-Disposition" , "attachment;fileName=" + URLEncoder . encode ( excelFile . getName ( ) , "UTF-8" ) ) ;
response . flushBuffer ( ) ;
BufferedInputStream bis = null ;
try {
bis = new BufferedInputStream ( new FileInputStream ( excelFile ) ) ;
OutputStream os = response . getOutputStream ( ) ;
byte [ ] buffer = new byte [ 1024 ] ;
for ( int i = bis . read ( buffer ) ; i ! = - 1 ; i = bis . read ( buffer ) ) {
os . write ( buffer , 0 , i ) ;
}
} catch ( Exception var28 ) {
var28 . printStackTrace ( ) ;
} finally {
if ( bis ! = null ) {
try {
bis . close ( ) ;
excelFile . delete ( ) ;
} catch ( IOException var27 ) {
var27 . printStackTrace ( ) ;
}
}
}
return ResultBean . success ( "导出成功" ) . setCode ( ResourceEnumUtil . MESSAGE . SUCCESS . getCode ( ) ) ;
} catch ( ImppBusiException var30 ) {
return ResultBean . fail ( var30 ) ;
} catch ( Exception var31 ) {
return ImppExceptionBuilder . newInstance ( ) . buildExceptionResult ( var31 ) ;
}
}
}