9706388757dc5972629b6ac9b7b1c7048e8b1878.svn-base 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package com.chinacreator.process.service;
  2. import com.chinacreator.process.bean.TencentBillChannel;
  3. import com.chinacreator.process.bean.TencentBillLResult;
  4. import com.chinacreator.process.bean.TencentBillLog;
  5. import com.chinacreator.process.bean.TencentBillOrder;
  6. import com.chinacreator.process.dao.TencentBillDao;
  7. import com.chinacreator.process.util.ftp.FtpUtil;
  8. import com.chinacreator.process.util.ftp.SFtpUtil;
  9. import org.apache.log4j.Logger;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Component;
  12. import java.io.BufferedWriter;
  13. import java.io.File;
  14. import java.io.FileWriter;
  15. import java.text.SimpleDateFormat;
  16. import java.util.ArrayList;
  17. import java.util.Calendar;
  18. import java.util.Date;
  19. import java.util.List;
  20. @Component
  21. public class TencentBillService
  22. {
  23. private static Logger logger;
  24. private static SimpleDateFormat sdf;
  25. @Autowired
  26. private TencentBillDao dao;
  27. static {
  28. TencentBillService.logger = Logger.getLogger((Class)TencentBillService.class);
  29. TencentBillService.sdf = new SimpleDateFormat("yyyyMMdd");
  30. }
  31. public void Bill(final TencentBillChannel ch, final String billtype, final String datestr) {
  32. if ("1".equals(billtype)) {
  33. if (datestr == null || "".equals(datestr.trim()) || datestr.trim().length() != 8) {
  34. TencentBillService.logger.info((Object)">>>\u5bf9\u8d26\u8d26\u671f\u5f02\u5e38");
  35. return;
  36. }
  37. }
  38. else {
  39. if (!"2".equals(billtype)) {
  40. TencentBillService.logger.info((Object)">>>\u5bf9\u8d26\u7c7b\u578b\u5f02\u5e38");
  41. return;
  42. }
  43. if (datestr == null || "".equals(datestr.trim()) || datestr.trim().length() != 6) {
  44. TencentBillService.logger.info((Object)">>>\u5bf9\u8d26\u8d26\u671f\u5f02\u5e38");
  45. return;
  46. }
  47. }
  48. if ("1".equals(billtype) || "2".equals(billtype)) {
  49. TencentBillLog log = this.dao.existBillLog(datestr, billtype, ch.getCpid(), ch.getSpid());
  50. if (log == null) {
  51. final TencentBillLResult flagFiles = this.createFile(ch, billtype, datestr);
  52. final String filenames = flagFiles.getFilenames();
  53. final String filepath = flagFiles.getFilepath();
  54. String billstatus = flagFiles.isResult() ? "0" : "1";
  55. String code = "";
  56. String info = "";
  57. if ("0".equals(billstatus)) {
  58. final TencentBillLResult flag = this.ftpFile(ch, filepath, filenames, datestr);
  59. code = flag.getCode();
  60. info = flag.getInfo();
  61. if (!flag.isResult()) {
  62. billstatus = "2";
  63. }
  64. }
  65. log = new TencentBillLog();
  66. log.setId(String.valueOf(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())) + String.valueOf((int)((Math.random() * 9.0 + 1.0) * 1000.0)));
  67. log.setBilldate(datestr);
  68. log.setBilltype(billtype);
  69. log.setCpid(ch.getCpid());
  70. log.setSpid(ch.getSpid());
  71. log.setDatacounts(flagFiles.getCounts());
  72. log.setFilecounts(flagFiles.getFiles());
  73. log.setFiles(filenames);
  74. log.setFilepath(filepath);
  75. log.setResultcode(code);
  76. log.setErrorinfo(info);
  77. log.setStatus(billstatus);
  78. this.dao.saveBillLog(log);
  79. }
  80. }
  81. }
  82. public void BillRetry3(final TencentBillChannel ch, final TencentBillLog log) {
  83. if (log == null) {
  84. return;
  85. }
  86. final String billtype = log.getBilltype();
  87. final String datestr = log.getBilldate();
  88. if ("1".equals(billtype)) {
  89. if (datestr == null || "".equals(datestr.trim()) || datestr.trim().length() != 8) {
  90. TencentBillService.logger.info((Object)">>>\u5bf9\u8d26\u8d26\u671f\u5f02\u5e38");
  91. return;
  92. }
  93. }
  94. else {
  95. if (!"2".equals(billtype)) {
  96. TencentBillService.logger.info((Object)">>>\u5bf9\u8d26\u7c7b\u578b\u5f02\u5e38");
  97. return;
  98. }
  99. if (datestr == null || "".equals(datestr.trim()) || datestr.trim().length() != 6) {
  100. TencentBillService.logger.info((Object)">>>\u5bf9\u8d26\u8d26\u671f\u5f02\u5e38");
  101. return;
  102. }
  103. }
  104. if ("1".equals(billtype) || "2".equals(billtype)) {
  105. final TencentBillLResult flagFiles = this.createFile(ch, billtype, datestr);
  106. final String filenames = flagFiles.getFilenames();
  107. final String filepath = flagFiles.getFilepath();
  108. String billstatus = flagFiles.isResult() ? "0" : "1";
  109. String code = "";
  110. String info = "";
  111. if ("0".equals(billstatus)) {
  112. final TencentBillLResult flag = this.ftpFile(ch, filepath, filenames, datestr);
  113. code = flag.getCode();
  114. info = flag.getInfo();
  115. if (!flag.isResult()) {
  116. billstatus = "2";
  117. }
  118. }
  119. final TencentBillLog nlog = new TencentBillLog();
  120. nlog.setId(String.valueOf(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())) + String.valueOf((int)((Math.random() * 9.0 + 1.0) * 1000.0)));
  121. nlog.setBilldate(datestr);
  122. nlog.setBilltype(billtype);
  123. nlog.setCpid(ch.getCpid());
  124. nlog.setSpid(ch.getSpid());
  125. nlog.setDatacounts(flagFiles.getCounts());
  126. nlog.setFilecounts(flagFiles.getFiles());
  127. nlog.setFiles(filenames);
  128. nlog.setFilepath(filepath);
  129. nlog.setResultcode(code);
  130. nlog.setErrorinfo(info);
  131. nlog.setStatus(billstatus);
  132. this.dao.saveBillLog(nlog);
  133. this.dao.updatetBillLog(log.getId());
  134. }
  135. }
  136. public void BillRetry4(final TencentBillChannel ch, final TencentBillLog log) {
  137. if (log == null) {
  138. return;
  139. }
  140. final String billtype = log.getBilltype();
  141. final String datestr = log.getBilldate();
  142. if ("1".equals(billtype)) {
  143. if (datestr == null || "".equals(datestr.trim()) || datestr.trim().length() != 8) {
  144. TencentBillService.logger.info((Object)">>>\u5bf9\u8d26\u8d26\u671f\u5f02\u5e38");
  145. return;
  146. }
  147. }
  148. else {
  149. if (!"2".equals(billtype)) {
  150. TencentBillService.logger.info((Object)">>>\u5bf9\u8d26\u7c7b\u578b\u5f02\u5e38");
  151. return;
  152. }
  153. if (datestr == null || "".equals(datestr.trim()) || datestr.trim().length() != 6) {
  154. TencentBillService.logger.info((Object)">>>\u5bf9\u8d26\u8d26\u671f\u5f02\u5e38");
  155. return;
  156. }
  157. }
  158. if (("1".equals(billtype) || "2".equals(billtype)) && ("0".equals(log.getStatus()) || "1".equals(log.getStatus()))) {
  159. String billstatus = "0";
  160. String code = "";
  161. String info = "";
  162. final TencentBillLResult flag = this.ftpFile(ch, log.getFilepath(), log.getFiles(), datestr);
  163. code = flag.getCode();
  164. info = flag.getInfo();
  165. if (!flag.isResult()) {
  166. billstatus = "2";
  167. }
  168. this.dao.updatetBillLogFtp(log.getId(), billstatus, code, info);
  169. }
  170. }
  171. public TencentBillLResult createFile(final TencentBillChannel ch, final String billtype, final String datestr) {
  172. final TencentBillLResult res = new TencentBillLResult();
  173. res.setResult(true);
  174. final String FILE_PATH = ch.getFilepath();
  175. final int FILE_SISE = ch.getFilesize();
  176. final int PAGE_SISE = ch.getPagesize();
  177. String filenames = "";
  178. final String filepath = String.valueOf(FILE_PATH) + ch.getCpid() + "_" + ch.getSpid() + "/" + datestr.substring(0, 6) + "/";
  179. final File filedir = new File(filepath);
  180. if (!filedir.exists()) {
  181. filedir.mkdirs();
  182. }
  183. int counts = 0;
  184. int files = 0;
  185. try {
  186. if ("1".equals(billtype)) {
  187. counts = this.countForDay(ch.getCpid(), ch.getSpid(), datestr);
  188. }
  189. else {
  190. counts = this.countForMonth(ch.getCpid(), ch.getSpid(), datestr);
  191. }
  192. TencentBillService.logger.info((Object)(">>>\u8bb0\u5f55\u603b\u6570" + counts));
  193. if (counts > 0) {
  194. files = ((counts % FILE_SISE != 0) ? 1 : 0) + counts / FILE_SISE;
  195. for (int k = 1; k <= files; ++k) {
  196. final String sortno = "0000" + k;
  197. final String filename = String.valueOf(datestr) + "_" + ch.getCpid() + "_" + sortno.substring(sortno.length() - 4, sortno.length()) + ".req";
  198. filenames = String.valueOf((filenames.length() >= 1) ? new StringBuilder(String.valueOf(filenames)).append(",").toString() : "") + filename;
  199. final File file = new File(String.valueOf(filepath) + filename);
  200. if (file.exists()) {
  201. file.renameTo(new File(String.valueOf(filepath) + filename + "." + new Date().getTime()));
  202. }
  203. file.createNewFile();
  204. TencentBillService.logger.info((Object)(">>>\u751f\u6210\u6587\u4ef6\uff1a" + filename));
  205. int filecount = 0;
  206. if (counts - (k - 1) * FILE_SISE >= FILE_SISE) {
  207. filecount = FILE_SISE;
  208. }
  209. else {
  210. filecount = counts - (k - 1) * FILE_SISE;
  211. }
  212. final BufferedWriter out = new BufferedWriter(new FileWriter(file));
  213. out.write(String.valueOf(counts) + "\r\n");
  214. if (filecount > 0) {
  215. for (int pages = ((filecount % PAGE_SISE != 0) ? 1 : 0) + filecount / PAGE_SISE, i = 1; i <= pages; ++i) {
  216. List<TencentBillOrder> bills = new ArrayList<TencentBillOrder>();
  217. if ("1".equals(billtype)) {
  218. bills = this.pageQueryForDay(ch.getCpid(), ch.getSpid(), datestr, i + (k - 1) * FILE_SISE / PAGE_SISE, PAGE_SISE);
  219. }
  220. else {
  221. bills = this.pageQueryForMonth(ch.getCpid(), ch.getSpid(), datestr, i + (k - 1) * FILE_SISE / PAGE_SISE, PAGE_SISE);
  222. }
  223. if (bills != null && bills.size() > 0) {
  224. int y = (k - 1) * FILE_SISE + (i - 1) * PAGE_SISE;
  225. for (final TencentBillOrder bo : bills) {
  226. ++y;
  227. final StringBuffer sb = new StringBuffer();
  228. sb.append(y).append("|");
  229. sb.append((bo.getOrderid() == null) ? "" : bo.getOrderid()).append("|");
  230. sb.append((bo.getUserid() == null) ? "" : bo.getUserid()).append("|");
  231. sb.append((bo.getBusitype() == null) ? "" : bo.getBusitype()).append("|");
  232. sb.append((bo.getOptime() == null) ? "" : bo.getOptime()).append("|");
  233. sb.append((bo.getResultcode() == null) ? "" : bo.getResultcode()).append("|");
  234. sb.append((bo.getResultinfo() == null) ? "" : bo.getResultinfo());
  235. sb.append("\r\n");
  236. out.write(sb.toString());
  237. }
  238. }
  239. }
  240. }
  241. out.flush();
  242. out.close();
  243. }
  244. }
  245. else {
  246. final String filename2 = filenames = String.valueOf(datestr) + "_" + ch.getCpid() + "_0001.req";
  247. files = 1;
  248. final File file2 = new File(String.valueOf(filepath) + filename2);
  249. if (file2.exists()) {
  250. file2.renameTo(new File(String.valueOf(filepath) + filename2 + "." + new Date().getTime()));
  251. }
  252. file2.createNewFile();
  253. TencentBillService.logger.info((Object)(">>>\u751f\u6210\u6587\u4ef6\uff1a" + filename2));
  254. final BufferedWriter out2 = new BufferedWriter(new FileWriter(file2));
  255. out2.write(String.valueOf(counts) + "\r\n");
  256. out2.flush();
  257. out2.close();
  258. }
  259. }
  260. catch (Exception e) {
  261. TencentBillService.logger.info((Object)">>>\u5bf9\u8d26\u6587\u4ef6\u751f\u6210\u5931\u8d25");
  262. res.setResult(false);
  263. }
  264. res.setCounts(counts);
  265. res.setFiles(files);
  266. res.setFilepath(filepath);
  267. res.setFilenames(filenames);
  268. return res;
  269. }
  270. public TencentBillLResult ftpFile(final TencentBillChannel ch, final String filepath, final String filenames, final String datestr) {
  271. final TencentBillLResult res = new TencentBillLResult();
  272. boolean flag = false;
  273. String code = "";
  274. String info = "";
  275. TencentBillService.logger.info((Object)(">>>\u76ee\u5f55\uff1a" + filepath));
  276. TencentBillService.logger.info((Object)(">>>\u6587\u4ef6\u5217\u8868\uff1a" + filenames));
  277. if ("ftp".equals(ch.getFtptype().toLowerCase())) {
  278. final FtpUtil util = new FtpUtil();
  279. String ftppath = ch.getFtpdir();
  280. if (ftppath != null || !"".equals(ftppath)) {
  281. ftppath = ftppath.replace("{yyyyMM}", datestr.substring(0, 6));
  282. }
  283. flag = util.UpBillFiles(ch.getFtpurl(), ch.getFtpuser(), ch.getFtppwd(), ch.getFtpport(), ftppath, filepath, filenames);
  284. code = util.getErrorcode();
  285. info = util.getErrorinfo();
  286. }
  287. else if ("sftp".equals(ch.getFtptype().toLowerCase())) {
  288. final SFtpUtil util2 = new SFtpUtil();
  289. String ftppath = ch.getFtpdir();
  290. if (ftppath != null || !"".equals(ftppath)) {
  291. ftppath = ftppath.replace("{yyyyMM}", datestr.substring(0, 6));
  292. }
  293. flag = util2.UpBillFiles(ch.getFtpurl(), ch.getFtpuser(), ch.getFtppwd(), ch.getFtpport(), ftppath, filepath, filenames);
  294. code = util2.getErrorcode();
  295. info = util2.getErrorinfo();
  296. }
  297. else {
  298. code = "9999";
  299. info = "FTP\u914d\u7f6e\u5f02\u5e38";
  300. }
  301. res.setResult(flag);
  302. res.setCode(code);
  303. res.setInfo(info);
  304. return res;
  305. }
  306. public List<TencentBillOrder> pageQueryForDay(final String cpid, final String spid, final String datestr, final int pageno, final int pagesize) {
  307. try {
  308. final Date date = TencentBillService.sdf.parse(datestr);
  309. final String starttime = TencentBillService.sdf.format(date);
  310. final Calendar cal = Calendar.getInstance();
  311. cal.setTime(date);
  312. cal.add(5, 1);
  313. final String endtime = TencentBillService.sdf.format(cal.getTime());
  314. return this.dao.pageQuery(cpid, spid, starttime, endtime, pageno, pagesize);
  315. }
  316. catch (Exception e) {
  317. e.printStackTrace();
  318. return null;
  319. }
  320. }
  321. public int countForDay(final String cpid, final String spid, final String datestr) {
  322. try {
  323. final Date date = TencentBillService.sdf.parse(datestr);
  324. final String starttime = TencentBillService.sdf.format(date);
  325. final Calendar cal = Calendar.getInstance();
  326. cal.setTime(date);
  327. cal.add(5, 1);
  328. final String endtime = TencentBillService.sdf.format(cal.getTime());
  329. return this.dao.counts(cpid, spid, starttime, endtime);
  330. }
  331. catch (Exception e) {
  332. e.printStackTrace();
  333. return 0;
  334. }
  335. }
  336. public List<TencentBillOrder> pageQueryForMonth(final String cpid, final String spid, final String monthstr, final int pageno, final int pagesize) {
  337. try {
  338. final Date date = TencentBillService.sdf.parse(String.valueOf(monthstr) + "01");
  339. final Calendar cal = Calendar.getInstance();
  340. cal.setTime(date);
  341. cal.add(2, 1);
  342. final String endtime = TencentBillService.sdf.format(cal.getTime());
  343. return this.dao.pageQuery(cpid, spid, null, endtime, pageno, pagesize);
  344. }
  345. catch (Exception e) {
  346. e.printStackTrace();
  347. return null;
  348. }
  349. }
  350. public int countForMonth(final String cpid, final String spid, final String monthstr) {
  351. try {
  352. final Date date = TencentBillService.sdf.parse(String.valueOf(monthstr) + "01");
  353. final Calendar cal = Calendar.getInstance();
  354. cal.setTime(date);
  355. cal.add(2, 1);
  356. final String endtime = TencentBillService.sdf.format(cal.getTime());
  357. return this.dao.counts(cpid, spid, null, endtime);
  358. }
  359. catch (Exception e) {
  360. e.printStackTrace();
  361. return 0;
  362. }
  363. }
  364. public static void main(final String[] args) {
  365. }
  366. }