admin
2022-04-16 04f09e52ffd4681bdfd85e51acd3da0d1280c3d3
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package com.yeshi.buwan.controller.api;
 
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.ks.goldcorn.exception.GoldAppException;
import com.ks.goldcorn.exception.GoldTradeException;
import com.ks.goldcorn.exception.GoldUserException;
import com.ks.goldcorn.pojo.DO.GoldCornGetSource;
import com.ks.goldcorn.pojo.DO.GoldCornRecord;
import com.ks.goldcorn.pojo.DO.GoldCornRecordCountMap;
import com.ks.goldcorn.pojo.Query.GoldCornRecordQuery;
import com.yeshi.buwan.domain.goldcorn.CodeCornGetSourceType;
import com.yeshi.buwan.domain.goldcorn.GoldCornTaskActionDetail;
import com.yeshi.buwan.domain.jump.JumpDetail;
import com.yeshi.buwan.domain.jump.JumpTypeEnum;
import com.yeshi.buwan.domain.system.DetailSystem;
import com.yeshi.buwan.domain.system.DetailSystemConfig;
import com.yeshi.buwan.dto.goldcorn.SignInGoldCornDateData;
import com.yeshi.buwan.exception.goldcorn.SignInException;
import com.yeshi.buwan.exception.user.LoginUserException;
import com.yeshi.buwan.service.imp.DetailSystemConfigService;
import com.yeshi.buwan.service.inter.goldcorn.GoldCornTaskActionDetailService;
import com.yeshi.buwan.service.inter.goldcorn.SignInService;
import com.yeshi.buwan.service.inter.system.SystemConfigService;
import com.yeshi.buwan.service.manager.GoldCornManager;
import com.yeshi.buwan.util.Constant;
import com.yeshi.buwan.util.JsonUtil;
import com.yeshi.buwan.util.JsonUtilV2;
import com.yeshi.buwan.util.TimeUtil;
import com.yeshi.buwan.vo.AcceptData;
import com.yeshi.buwan.vo.goldcorn.GoldCornCouponVideoVO;
import com.yeshi.buwan.vo.goldcorn.GoldCornRecordVO;
import com.yeshi.buwan.vo.goldcorn.GoldCornTaskVO;
import com.yeshi.buwan.vo.goldcorn.SignInInfoVO;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import javax.annotation.Resource;
import java.util.*;
 
@Controller
@RequestMapping("api/v2/goldcorn")
public class GoldCornController {
 
    Logger logger = LoggerFactory.getLogger(GoldCornController.class);
 
 
    @Resource
    private GoldCornManager goldCornManager;
 
    @Resource
    private SignInService signInService;
 
    @Resource
    private SystemConfigService systemConfigService;
 
    @Resource
    private GoldCornTaskActionDetailService goldCornTaskActionDetailService;
 
    @Resource
    private DetailSystemConfigService configService;
 
 
    /**
     * 挣金币
     *
     * @param acceptData
     * @param code
     * @param loginUid
     * @param param1
     * @param param2
     * @return
     */
    @RequestMapping("makeGoldCorn")
    @ResponseBody
    public String makeGoldCorn(AcceptData acceptData, String code, String loginUid, String param1, String param2) {
        try {
            GoldCornGetSource source = goldCornManager.getGoldCornGetSource(CodeCornGetSourceType.valueOf(code));
            goldCornManager.addGoldCorn(loginUid, CodeCornGetSourceType.valueOf(code), null, source.getSourceName(), "");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("goldCorn", source.getGoldCorn());
            return JsonUtil.loadTrueJson(jsonObject.toString());
        } catch (GoldUserException e) {
            return JsonUtil.loadFalseJson(e.getMessage());
        } catch (GoldAppException e) {
            return JsonUtil.loadFalseJson(e.getMessage());
        } catch (GoldTradeException e) {
            return JsonUtil.loadFalseJson(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("添加影视豆出错", e);
            return JsonUtil.loadFalseJson("影视豆添加失败");
        }
 
    }
 
 
    @RequestMapping("canDoTask")
    @ResponseBody
    public String canDoTask(AcceptData acceptData, String loginUid, String code, String param2) {
 
        long time = goldCornManager.getNextDoTaskTime(CodeCornGetSourceType.valueOf(code), loginUid);
        if (time <= 0) {
            return JsonUtil.loadTrueJson("");
        } else {
            long span = time - System.currentTimeMillis();
            int ss = (int) (span / 1000);
            int h = ss / 3600;
            int m = ss % 3600 / 60;
            int s = ss % 60;
            String st = "";
            if (h > 0) {
                st += h + "小时";
            }
            if (m != 0 || h > 0)
                st += m + "分钟";
            if (s > 0 && st.length() == 0)
                st += s + "秒";
            return JsonUtil.loadFalseJson(String.format("请%s后再试", st));
        }
 
    }
 
 
    @RequestMapping("signIn")
    @ResponseBody
    public String signIn(AcceptData acceptData, String loginUid) {
        try {
            int goldCorn = signInService.signIn(loginUid);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("goldCorn", goldCorn);
            return JsonUtil.loadTrueJson(jsonObject.toString());
        } catch (LoginUserException e) {
            return JsonUtil.loadFalseJson("签到出错,请稍后再试");
        } catch (SignInException e) {
            e.printStackTrace();
            logger.error("签到出错:", e);
            return JsonUtil.loadFalseJson(e.getMessage());
        }
 
    }
 
    @RequestMapping("getSignInData")
    @ResponseBody
    public String getSignInData(AcceptData acceptData, String loginUid) {
        try {
            SignInInfoVO signInInfo = new SignInInfoVO();
            List<SignInGoldCornDateData> recordList = signInService.getContinueSignInList(loginUid);
            signInInfo.setDateList(recordList);
            signInInfo.setSignDay(signInService.getContinueSignDayCount(loginUid));
            signInInfo.setBalance((int) goldCornManager.getBalance(loginUid));
            SignInInfoVO.SignInConfig config = new SignInInfoVO.SignInConfig();
            config.setRecordUrl(systemConfigService.getConfigValueByKeyCache("goldCornRecordUrl"));
            config.setRuleUrl(systemConfigService.getConfigValueByKeyCache("goldCornRuleUrl"));
 
            String action = systemConfigService.getConfigValueByKeyCache("useGoldCornAction");
            JSONObject actionJSON = JSONObject.fromObject(action);
            Gson gson = new Gson();
            config.setUserJump(gson.fromJson(actionJSON.optJSONObject("jumpDetail").toString(), JumpDetail.class));
            config.setUserJumpParams(actionJSON.optString("params"));
 
            signInInfo.setSignInConfig(config);
            signInInfo.setCanSignIn(!signInService.isTodaySignIned(loginUid, new Date()));
            return JsonUtil.loadTrueJson(new Gson().toJson(signInInfo));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取签到信息异常:", e);
        }
        return JsonUtil.loadFalseJson("服务器内部错误");
    }
 
 
    /**
     * 获取金币记录
     *
     * @param acceptData
     * @param loginUid
     * @param page
     * @return
     */
    @RequestMapping("getRecordList")
    @ResponseBody
    public String getRecordList(AcceptData acceptData, String loginUid, int page) {
        GoldCornRecordQuery recordQuery = new GoldCornRecordQuery();
        recordQuery.setPageSize(Constant.pageCount);
        recordQuery.setPage(page);
        try {
            List<GoldCornRecord> list = goldCornManager.getRecordList(recordQuery, loginUid);
            List<GoldCornRecordVO> voList = new ArrayList<>();
            for (GoldCornRecord record : list) {
                voList.add(new GoldCornRecordVO(record.getTitle(), record.getDesc(), TimeUtil.getGernalTime(record.getCreateTime().getTime(), "yyyy.MM.dd HH:mm"), record.getType() == GoldCornRecord.TYPE_CONSUME ? 0 - record.getGoldCorn() : record.getGoldCorn()));
            }
            long count = goldCornManager.getRecordCount(recordQuery, loginUid);
            JSONObject data = new JSONObject();
            data.put("list", new Gson().toJson(voList));
            data.put("count", count);
            return JsonUtilV2.loadTrueJson(data.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonUtil.loadFalseJson("服务器内部错误");
    }
 
    @RequestMapping("getCouponVideoList")
    @ResponseBody
    public String getCouponVideoList(AcceptData acceptData) {
        List<GoldCornCouponVideoVO> voList = new ArrayList<>();
        JumpDetail jumpDetail = new JumpDetail();
        jumpDetail.setType(JumpTypeEnum.web);
 
        JSONObject params = new JSONObject();
        params.put("url", "https://www.baidu.com");
 
        voList.add(new GoldCornCouponVideoVO("https://r1.ykimg.com/058400005FA60E6A14187C0791817503?x-oss-process=image/resize,w_290/interlace,1/quality,Q_80", jumpDetail, params.toString().replace("\"", "\\" + "\"")));
        voList.add(new GoldCornCouponVideoVO("https://r1.ykimg.com/058400005F2BA59B6AEB660E907A2B99?x-oss-process=image/resize,w_290/interlace,1/quality,Q_80", jumpDetail, params.toString().replace("\"", "\\" + "\"")));
        voList.add(new GoldCornCouponVideoVO("https://r1.ykimg.com/052700005DC9247B1B769182B50A8C21?x-oss-process=image/resize,w_290/interlace,1/quality,Q_80", jumpDetail, params.toString().replace("\"", "\\" + "\"")));
//        if (voList.size() < 3)
        voList.clear();
        JSONObject data = new JSONObject();
        data.put("list", new Gson().toJson(voList));
        data.put("count", voList.size());
        data.put("exchangeUrl", "http://www.baidu.com");
        return JsonUtil.loadTrueJson(data.toString());
    }
 
    @RequestMapping("getTaskList")
    @ResponseBody
    public String getTaskList(AcceptData acceptData, String loginUid) {
        List<GoldCornTaskVO> voList = new ArrayList<>();
        try {
            //登录
            List<GoldCornGetSource> list1 = goldCornManager.listGetCornSource(1, 20);
 
            List<GoldCornGetSource> list = new ArrayList<>();
            list.addAll(list1);
 
            //删除华为渠道的看激励视频任务
            if (acceptData.getChannel().equalsIgnoreCase("huawei")) {
                if (list != null && list.size() > 0)
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).getSourceCode().equalsIgnoreCase(CodeCornGetSourceType.watchRewardAd.name())) {
                            //华为是否正在上线
                            DetailSystemConfig onLiningVersionInfo = configService.getConfigByKey("ad_hw_online_version_info", new DetailSystem("44"), 1);
                            JSONObject onLiningVersionInfoJson = JSONObject.fromObject(onLiningVersionInfo.getValue());
                            int onLiningVersionCode = onLiningVersionInfoJson.optInt("versionCode");
                            if (acceptData.getVersion() >= onLiningVersionCode) {
                                list.remove(i);
                                break;
                            }
                        }
                    }
            }
 
 
            List<String> sourceCodes = new ArrayList<>();
            for (GoldCornGetSource source : list) {
                sourceCodes.add(source.getSourceCode());
            }
 
            List<GoldCornTaskActionDetail> details = goldCornTaskActionDetailService.list(sourceCodes);
            Map<String, GoldCornTaskActionDetail> detailMap = Maps.uniqueIndex(details.iterator(), new Function<GoldCornTaskActionDetail, String>() {
                @Override
                public String apply(GoldCornTaskActionDetail detail) {
                    return detail.getSourceCode();
                }
            });
 
 
            //加载已完成数据
 
            Date maxCreateTime = new Date();
            Date minCreateTime = new Date(TimeUtil.convertGernalTime(TimeUtil.getGernalTime(maxCreateTime.getTime(), "yyyyMMdd"), "yyyyMMdd"));
 
            List<GoldCornRecordCountMap> mapList = goldCornManager.countRecordByGetSource(loginUid, sourceCodes, minCreateTime, maxCreateTime);
            Map<String, GoldCornRecordCountMap> map = new HashMap<>();
            if (mapList != null)
                for (GoldCornRecordCountMap m : mapList) {
                    map.put(m.getSourceCode(), m);
                }
            String[] hiddens = new String[]{CodeCornGetSourceType.elme.name(), CodeCornGetSourceType.meituan.name()};
            List<String> hiddenList = Arrays.asList(hiddens);
            for (GoldCornGetSource source : list) {
 
                GoldCornTaskVO vo = GoldCornTaskVO.create(source);
                //详细的信息
                GoldCornTaskActionDetail detail = detailMap.get(source.getSourceCode());
                if (detail != null) {
                    vo.setJumpDetail(detail.getJumpDetail());
                    vo.setParams(detail.getParams());
                    vo.setActionName(detail.getActionName());
                }
 
                if (!hiddenList.contains(source.getSourceCode())) {
                    if (map.get(source.getSourceCode()) == null) {
                        vo.setDesc(source.getSourceDesc() + "," + String.format("今日已完成%s/%s", 0, source.getMaxDayCount()));
                    } else {
                        vo.setEnable(map.get(source.getSourceCode()).getCount() < source.getMaxDayCount());
                        vo.setDesc(source.getSourceDesc() + "," + String.format("今日已完成%s/%s", map.get(source.getSourceCode()).getCount(), source.getMaxDayCount()));
                    }
                } else {
                    vo.setGoldCorn("实付款*" + source.getGoldCorn());
                }
 
                voList.add(vo);
            }
 
 
        } catch (Exception e) {
 
        }
 
        JSONObject data = new JSONObject();
        data.put("list", new Gson().toJson(voList));
        data.put("count", voList.size());
        return JsonUtil.loadTrueJson(data.toString());
    }
 
 
}