johnswang
2021-04-19 b6cfc585d1a31d71ad2f393b09076304f65589a1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.yc.sdk.shopping.action.jiazhuang.task;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.yc.action.BaseAction;
import com.yc.entity.DataSourceEntity;
import com.yc.multiData.MultiDataSource;
import com.yc.multiData.SpObserver;
import com.yc.sdk.shopping.entity.PanicBuyingEntity;
 
@Controller
@RequestMapping("/shopping/panicBuyingTaskTest.do")
public class PanicBuyingTaskTest  extends BaseAction {
    @Autowired
    PanicBuyingTaskIfc panicBuyingTaskIfc ;
    @RequestMapping(params = "m=startCron")
    public void getAddressList(HttpServletRequest request, HttpServletResponse response) {
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        String docCode = request.getParameter("doccode");
        try {
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ;
            SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源
            PanicBuyingEntity panicBuyingEntity = new PanicBuyingEntity();
            panicBuyingEntity.setDocCode(docCode);
            panicBuyingTaskIfc.addStatusTask(dataSourceEntity, panicBuyingEntity);
            json.addProperty("state", "success");
            this.printJson(response, json.toString());
            return;
        }catch(DataAccessException e ) {
            errJson.addProperty("warning", e.getCause() != null ?e.getCause().getMessage():e.getMessage());
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        }catch(Exception e){
            errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        }finally {
            SpObserver.setDBtoInstance();
        }    
    }
    
    
    @RequestMapping(params = "m=getTaskList")
    public void getTaskList(HttpServletRequest request, HttpServletResponse response) {
        JsonObject json = new JsonObject();
        JsonObject errJson = new JsonObject();
        try {
            
            JsonArray jsonArray = new JsonArray();
            PanicBuyingTask.task.forEach((taskName, future) -> {
                //System.out.println(key + ":" + value);
                jsonArray.add(taskName);
            });
            json.add("list", jsonArray);
            json.addProperty("state", "success");
            this.printJson(response, json.toString());
            return;
        }catch(DataAccessException e ) {
            errJson.addProperty("warning", e.getCause() != null ?e.getCause().getMessage():e.getMessage());
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        }catch(Exception e){
            errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage(): e.getMessage()));
            json.add("error", errJson);
            this.printJson(response, json.toString());
            return;
        }
    }
}