You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
4.4 KiB
128 lines
4.4 KiB
package web.controller;
|
|
|
|
import core.thrift.FactoryPool;
|
|
import core.thrift.MyAsyncMethodCallback;
|
|
import core.thrift.comment.QueryComment;
|
|
import db.form.DBAction;
|
|
import db.model.bilibili.DataModel;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.hibernate.criterion.DetachedCriteria;
|
|
import org.hibernate.criterion.MatchMode;
|
|
import org.hibernate.criterion.Property;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.ui.Model;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import web.html.data.DataTable;
|
|
import web.model.DataModelForm;
|
|
import web.service.DataService;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.nio.charset.Charset;
|
|
import java.util.EnumSet;
|
|
import java.util.List;
|
|
|
|
@Controller
|
|
@RequestMapping("/data")
|
|
public class DataController extends TableController<DataModel, DataModelForm, DataService,DataTable<DataModel>> {
|
|
|
|
private final String regionJson = IOUtils.toString(DataController.class.getResourceAsStream("/region.json"), Charset.forName("UTF-8"));
|
|
|
|
private static final String key = "regionJson";
|
|
|
|
@Resource
|
|
private FactoryPool factoryPool;
|
|
|
|
public DataController() throws IOException {
|
|
super();
|
|
}
|
|
|
|
|
|
@RequestMapping("start/{size}")
|
|
public void start(@PathVariable Integer size) {
|
|
stop();
|
|
service.start(size);
|
|
}
|
|
|
|
@ResponseBody
|
|
@RequestMapping(value = "count/{cid}")
|
|
public Integer count(@PathVariable int cid) {
|
|
try {
|
|
MyAsyncMethodCallback<Integer> result=new MyAsyncMethodCallback<>();
|
|
factoryPool.doExecute(QueryComment.Client.class, (client, aBoolean) ->result.setResult(client.commentSum(cid)),result);
|
|
return result.getResult();
|
|
} catch (Exception e) {
|
|
log.error(e);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@RequestMapping("download")
|
|
public ResponseEntity export(DataModelForm form) throws Exception {
|
|
if (StringUtils.isNotEmpty(form.getFileName()) && form.getCids() != null && !form.getCids().isEmpty()) {
|
|
HttpHeaders headers = new HttpHeaders();
|
|
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
|
headers.setContentDispositionFormData("attachment", form.getFileName() + ".zip");
|
|
MyAsyncMethodCallback<String> call = new MyAsyncMethodCallback<>();
|
|
// TTransportUtil.doExecute(client -> client.download(form.getCids(), form.getFileName(), call),QueryComment.AsyncClient.class);
|
|
String filePath = call.getResult();
|
|
if (filePath != null) {
|
|
return new ResponseEntity<>(FileUtils.readFileToByteArray(new File(filePath)), headers, HttpStatus.CREATED);
|
|
} else {
|
|
return new ResponseEntity<>("弹幕下载失败", HttpStatus.INTERNAL_SERVER_ERROR);
|
|
}
|
|
} else {
|
|
return new ResponseEntity("参数异常", HttpStatus.BAD_REQUEST);
|
|
}
|
|
}
|
|
|
|
@RequestMapping("stop")
|
|
public void stop() {
|
|
service.stop();
|
|
}
|
|
|
|
@Override
|
|
public boolean checkAction(DBAction action) {
|
|
return !EnumSet.of(DBAction.D).contains(action);
|
|
}
|
|
|
|
@Override
|
|
protected DetachedCriteria getDetachedCriteria(DataModelForm queryCommand) {
|
|
DetachedCriteria criteria = super.getDetachedCriteria(queryCommand);
|
|
if (StringUtils.isNotEmpty(queryCommand.getTitle())) {
|
|
criteria.add(Property.forName("title").like(queryCommand.getTitle(), MatchMode.ANYWHERE));
|
|
}
|
|
|
|
return criteria;
|
|
}
|
|
|
|
@Override
|
|
protected List find(DataModelForm command, Integer firstResult, Integer maxResults) {
|
|
return service.find(command, firstResult, maxResults);
|
|
}
|
|
|
|
@Override
|
|
protected long rowCount(DataModelForm command) {
|
|
return service.rowCount(command);
|
|
}
|
|
|
|
|
|
@Override
|
|
protected void setModel(Model model) {
|
|
model.addAttribute(key, regionJson);
|
|
}
|
|
|
|
@Override
|
|
protected DataTable<DataModel> getNewTable() {
|
|
return new DataTable<>();
|
|
}
|
|
}
|
|
|