33import com .fasterxml .jackson .core .JsonProcessingException ;
44import com .fasterxml .jackson .databind .DeserializationFeature ;
55import com .fasterxml .jackson .databind .ObjectMapper ;
6- import datart .core .base .PageInfo ;
76import datart .core .base .consts .AttachmentType ;
87import datart .core .base .consts .FileOwner ;
9- import datart .core .base . exception . Exceptions ;
10- import datart .core .common .* ;
11- import datart .core .data . provider . Dataframe ;
8+ import datart .core .common . Application ;
9+ import datart .core .common .FileUtils ;
10+ import datart .core .common . UUIDGenerator ;
1211import datart .core .entity .Folder ;
1312import datart .core .entity .Schedule ;
1413import datart .core .entity .ScheduleLog ;
1514import datart .core .entity .User ;
16- import datart .core .entity .poi .POISettings ;
1715import datart .core .mappers .ext .ScheduleLogMapperExt ;
1816import datart .core .mappers .ext .ScheduleMapperExt ;
1917import datart .core .mappers .ext .UserMapperExt ;
2422import datart .server .base .dto .DatachartDetail ;
2523import datart .server .base .dto .ScheduleJobConfig ;
2624import datart .server .base .params .DownloadCreateParam ;
27- import datart .server .base .params .ShareCreateParam ;
28- import datart .server .base .params .ShareToken ;
2925import datart .server .base .params .ViewExecuteParam ;
30- import datart .server .common .PoiConvertUtils ;
31- import datart .server .service .DataProviderService ;
26+ import datart .server .common .JsParserUtils ;
27+ import datart .server .service .AttachmentService ;
3228import datart .server .service .FolderService ;
3329import datart .server .service .ShareService ;
3430import datart .server .service .VizService ;
3531import lombok .extern .slf4j .Slf4j ;
3632import org .apache .commons .lang3 .StringUtils ;
37- import org .apache .commons .lang3 .time .DateUtils ;
38- import org .apache .poi .ss .usermodel .Workbook ;
3933import org .quartz .Job ;
4034import org .quartz .JobExecutionContext ;
4135import org .springframework .util .CollectionUtils ;
4236
43- import javax .script .Invocable ;
44- import javax .script .ScriptException ;
4537import java .io .Closeable ;
4638import java .io .File ;
4739import java .io .IOException ;
@@ -56,8 +48,6 @@ public abstract class ScheduleJob implements Job, Closeable {
5648
5749 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
5850
59- public Invocable parser ;
60-
6151 static {
6252 OBJECT_MAPPER .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
6353 }
@@ -128,29 +118,49 @@ public void doGetData() throws Exception {
128118 return ;
129119 }
130120
121+ String path = FileUtils .concatPath (FileOwner .SCHEDULE .getPath (), schedule .getId ());
122+
131123 FolderService folderService = Application .getBean (FolderService .class );
132124
133125 for (ScheduleJobConfig .VizContent vizContent : config .getVizContents ()) {
134126 Folder folder = folderService .retrieve (vizContent .getVizId ());
135127 DownloadCreateParam downloadCreateParam ;
136128 if (ResourceType .DATACHART .name ().equals (folder .getRelType ())) {
137129 DatachartDetail datachart = vizService .getDatachart (folder .getRelId ());
138- downloadCreateParam = parseExecuteParam ("chart" , OBJECT_MAPPER .writeValueAsString (datachart ));
130+ downloadCreateParam = JsParserUtils . parseExecuteParam ("chart" , OBJECT_MAPPER .writeValueAsString (datachart ));
139131 } else {
140132 DashboardDetail dashboard = vizService .getDashboard (folder .getRelId ());
141- downloadCreateParam = parseExecuteParam ("board" , OBJECT_MAPPER .writeValueAsString (dashboard ));
133+ downloadCreateParam = JsParserUtils . parseExecuteParam ("board" , OBJECT_MAPPER .writeValueAsString (dashboard ));
142134 }
143- if (config .getAttachments ().contains (AttachmentType .EXCEL )) {
144- downloadExcel (downloadCreateParam );
135+
136+ if (config .getImageWidth () != null ) {
137+ downloadCreateParam .setImageWidth (config .getImageWidth ());
145138 }
146139
147- if (config .getAttachments ().contains (AttachmentType .IMAGE )) {
148- downloadImage (ResourceType .valueOf (folder .getRelType ()), folder .getRelId (), config .getImageWidth ());
140+ for (AttachmentType type : config .getAttachments ()) {
141+ setVizId (downloadCreateParam , folder , type );
142+ String beanName = type .name ().toLowerCase () + "AttachmentService" ;
143+ AttachmentService attachmentService = Application .getBean (beanName , AttachmentService .class );
144+ attachments .add (attachmentService .getFile (downloadCreateParam , path , downloadCreateParam .getFileName ()));
149145 }
150146 }
151147
152148 }
153149
150+ private void setVizId (DownloadCreateParam downloadCreateParam , Folder folder , AttachmentType attachmentType ) {
151+ if (ResourceType .DATACHART .name ().equals (folder .getRelType ())
152+ && !CollectionUtils .isEmpty (downloadCreateParam .getDownloadParams ())
153+ && downloadCreateParam .getDownloadParams ().size ()==1 ) {
154+ ViewExecuteParam viewExecuteParam = downloadCreateParam .getDownloadParams ().get (0 );
155+ if (attachmentType .equals (AttachmentType .EXCEL )) {
156+ viewExecuteParam .setVizId (folder .getRelId ());
157+ viewExecuteParam .setVizType (ResourceType .DATACHART );
158+ } else {
159+ viewExecuteParam .setVizId (folder .getId ());
160+ }
161+ }
162+ }
163+
154164 public abstract void doSend () throws Exception ;
155165
156166 private ScheduleJobConfig parseConfig (Schedule schedule ) throws JsonProcessingException {
@@ -171,64 +181,6 @@ private void insertLog(Date start, Date end, String scheduleId, int status, Stri
171181 scheduleLogMapper .insert (scheduleLog );
172182 }
173183
174- private void downloadExcel (DownloadCreateParam downloadParams ) throws Exception {
175- DataProviderService dataProviderService = Application .getBean (DataProviderService .class );
176- Workbook workbook = POIUtils .createEmpty ();
177- for (int i = 0 ; i < downloadParams .getDownloadParams ().size (); i ++) {
178- ViewExecuteParam viewExecuteParam = downloadParams .getDownloadParams ().get (i );
179- viewExecuteParam .setPageInfo (PageInfo .builder ().pageNo (1 )
180- .pageSize (Integer .MAX_VALUE ).build ());
181- Dataframe dataframe = dataProviderService .execute (downloadParams .getDownloadParams ().get (i ));
182- String chartConfigStr = vizService .getChartConfigByVizId (viewExecuteParam .getVizId (), viewExecuteParam .getVizType ());
183- POISettings poiSettings = PoiConvertUtils .covertToPoiSetting (chartConfigStr , dataframe );
184- String sheetName = StringUtils .isNotBlank (viewExecuteParam .getVizName ()) ? viewExecuteParam .getVizName () : "Sheet" +i ;
185- POIUtils .withSheet (workbook , sheetName , dataframe , poiSettings );
186- }
187- File tempFile = File .createTempFile (UUIDGenerator .generate (), ".xlsx" );
188- POIUtils .save (workbook , tempFile .getPath (), true );
189- attachments .add (tempFile );
190- }
191-
192- private void downloadImage (ResourceType vizType , String vizId , int imageWidth ) throws Exception {
193-
194- ShareCreateParam shareCreateParam = new ShareCreateParam ();
195- shareCreateParam .setVizId (vizId );
196- shareCreateParam .setVizType (vizType );
197- shareCreateParam .setExpiryDate (DateUtils .addHours (new Date (), 1 ));
198- ShareToken share = shareService .createShare (schedule .getCreateBy (), shareCreateParam );
199-
200- String url = Application .getWebRootURL () + "/share?eager=true&token=" ;
201-
202- log .info ("image url {} " , url );
203-
204- String path = FileUtils .concatPath (Application .getFileBasePath (), FileOwner .SCHEDULE .getPath (), schedule .getId ());
205-
206- File file = WebUtils .screenShot2File (url , path , imageWidth );
207-
208- attachments .add (file );
209-
210- }
211-
212- private DownloadCreateParam parseExecuteParam (String type , String json ) throws ScriptException , NoSuchMethodException , JsonProcessingException {
213- Invocable parser = getParser ();
214- if (parser == null ) {
215- Exceptions .msg ("param parser load error" );
216- }
217- Object result = parser .invokeFunction ("getQueryData" , type , json );
218- return OBJECT_MAPPER .readValue (result .toString (), DownloadCreateParam .class );
219- }
220-
221- private synchronized Invocable getParser () {
222- if (parser == null ) {
223- try {
224- parser = JavascriptUtils .load ("javascript/parser.js" );
225- } catch (Exception e ) {
226- Exceptions .e (e );
227- }
228- }
229- return parser ;
230- }
231-
232184 @ Override
233185 public void close () throws IOException {
234186 try {
0 commit comments