Cron.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. namespace Shop\Lib;
  3. use Dever;
  4. class Cron
  5. {
  6. /**
  7. * 处理优惠券到期时间
  8. *
  9. * @return mixed
  10. */
  11. public function coupon_api()
  12. {
  13. $coupon = Dever::db('shop/user_coupon')->getAll(array('status' => 1, 'edate' => time() - 86400));
  14. if ($coupon && Dever::project('message')) {
  15. foreach ($coupon as $k => $v) {
  16. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  17. $msg = $coupon_info['name'] . ",要到期啦!";
  18. $msg_param['type'] = 2;//消息类型2是优惠券
  19. $msg_param['id'] = $v['id'];
  20. $msg_param['coupon_id'] = $v['coupon_id'];
  21. $msg_param = Dever::json_encode($msg_param);
  22. Dever::load('message/lib/data')->push(-1, $v['uid'], '优惠劵到期提醒', $msg, 2, 1, false, $msg_param);
  23. if (Dever::load('wechat_applet') && $v['uid'] && $v['uid'] > 0) {
  24. $user = Dever::db('passport/wechat')->one(array('uid' => $v['uid'], 'system_id' => 1, 'system_source' => 5));
  25. if ($user && $user['openid']) {
  26. $send = array
  27. (
  28. 'thing1' => array
  29. (
  30. 'value' => $coupon_info['name'],
  31. ),
  32. 'time2' => array
  33. (
  34. 'value' => date('Y-m-d H:i', $v['edate']),
  35. ),
  36. 'thing3' => array
  37. (
  38. 'value' => '您有一张优惠券即将到期,请尽快使用',
  39. ),
  40. );
  41. $send = Dever::json_encode($send);
  42. Dever::load('wechat_applet/subscribe')->sendOne('act_2', 1, $user['openid'], 'pages/my/coupon/coupon', $send, Dever::config('base')->wechat_applet);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. /**
  49. * 处理待支付订单提醒
  50. *
  51. * @return mixed
  52. */
  53. public function order_api()
  54. {
  55. # 获取超过5分钟未支付的订单
  56. $time = time();
  57. $where['status'] = 1;
  58. $where['cdate'] = $time + 300;
  59. $order = Dever::db('shop/sell_order')->getDataByTime($where);
  60. if ($order && Dever::project('message')) {
  61. $config = Dever::db('shop/sell_order')->config;
  62. foreach ($order as $k => $v) {
  63. if ($time - $v['cdate'] >= 900) {
  64. Dever::db('shop/sell_order')->update(array('where_id' => $v['id'], 'status' => 11));
  65. } else {
  66. $msg = "您有一笔待付款订单,请及时付款。\r\n订单15分钟内未付款自动取消~";
  67. $shop = Dever::db('shop/info')->one($v['shop_id']);
  68. $msg_param['type'] = 1;//消息类型1是订单消息
  69. $msg_param['id'] = $v['id'];
  70. $msg_param['name'] = $shop['name'];
  71. $msg_param = Dever::json_encode($msg_param);
  72. Dever::load('message/lib/data')->push(-1, $v['uid'], '订单待支付提醒', $msg, 2, 1, false, $msg_param);
  73. if (Dever::load('wechat_applet') && $v['uid'] && $v['uid'] > 0) {
  74. $user = Dever::db('passport/wechat')->one(array('uid' => $v['uid'], 'system_id' => 1, 'system_source' => 5));
  75. if ($user && $user['openid']) {
  76. $v['status_name'] = $config['status'][$v['status']];
  77. $send = array
  78. (
  79. 'character_string5' => array
  80. (
  81. 'value' => $v['order_num'],
  82. ),
  83. 'amount11' => array
  84. (
  85. 'value' => $v['price'] . '元',
  86. ),
  87. 'phrase13' => array
  88. (
  89. 'value' => $v['status_name'],
  90. ),
  91. 'thing12' => array
  92. (
  93. 'value' => '您有一笔待付款订单,请及时付款。',
  94. ),
  95. );
  96. $send = Dever::json_encode($send);
  97. Dever::load('wechat_applet/subscribe')->sendOne('order_pay', 1, $user['openid'], 'pages/app/order/order?id=' . $v['id'], $send, Dever::config('base')->wechat_applet);
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * 处理销售数据 生成每天的销量统计
  106. *
  107. * @return mixed
  108. */
  109. public function sell_api()
  110. {
  111. $num = Dever::input('num', 1);
  112. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  113. $end = Dever::input('end', date('Y-m-d'));
  114. $where['status'] = '1,2,3,4,5,6';
  115. $start = Dever::maketime($start . ' 00:00:00');
  116. $end = Dever::maketime($end . ' 23:59:59');
  117. $day = intval(($end - $start)/86400);
  118. $shop = Dever::db('shop/info')->select();
  119. foreach ($shop as $k => $v) {
  120. $where['shop_id'] = $v['id'];
  121. for($i=0; $i<=$day; $i++) {
  122. $where['start'] = $start + 86400*$i;
  123. $where['end'] = $start + 86400*$i + 86399;
  124. $data = array();
  125. $data['shop_id'] = $v['id'];
  126. $data['day'] = $where['start'];
  127. $info = Dever::db('shop/sell_stat')->find($data);
  128. $cash = Dever::db('shop/sell_order')->getCashNum($where);
  129. $data['cash'] = $cash['total'];
  130. $data['order'] = Dever::db('shop/sell_order')->getOrderNum($where);
  131. $goods = Dever::db('shop/sell_order')->getGoodsNum($where);
  132. $data['goods'] = $goods['total'];
  133. $data['area'] = $v['area'];
  134. $data['province'] = $v['province'];
  135. $data['city'] = $v['city'];
  136. $data['county'] = $v['county'];
  137. $data['town'] = $v['town'];
  138. if (!$info) {
  139. Dever::db('shop/sell_stat')->insert($data);
  140. } else {
  141. $data['where_id'] = $info['id'];
  142. Dever::db('shop/sell_stat')->update($data);
  143. }
  144. }
  145. }
  146. }
  147. public function buy_api()
  148. {
  149. $num = Dever::input('num', 1);
  150. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  151. $end = Dever::input('end', date('Y-m-d'));
  152. $where['status'] = '1,2,3,4,5,6';
  153. $start = Dever::maketime($start . ' 00:00:00');
  154. $end = Dever::maketime($end . ' 23:59:59');
  155. $day = intval(($end - $start)/86400);
  156. $shop = Dever::db('shop/info')->select();
  157. foreach ($shop as $k => $v) {
  158. $where['shop_id'] = $v['id'];
  159. for($i=0; $i<=$day; $i++) {
  160. $where['start'] = $start + 86400*$i;
  161. //$where['end'] = $start + 86400*$i + 86399;
  162. $data = array();
  163. $data['type'] = 1;
  164. $data['type_id'] = $v['id'];
  165. $data['day'] = $where['start'];
  166. $info = Dever::db('shop/buy_stat')->find($data);
  167. $cash = Dever::db('shop/buy_order')->getCashNum($where);
  168. $data['cash'] = $cash['total'];
  169. $cash = Dever::db('shop/buy_order')->getPCashNum($where);
  170. $data['p_cash'] = $cash['total'];
  171. $data['order'] = Dever::db('shop/buy_order')->getOrderNum($where);
  172. $goods = Dever::db('shop/buy_order')->getGoodsNum($where);
  173. $data['goods'] = $goods['total'];
  174. $data['area'] = $v['area'];
  175. $data['province'] = $v['province'];
  176. $data['city'] = $v['city'];
  177. $data['county'] = $v['county'];
  178. $data['town'] = $v['town'];
  179. if (!$info) {
  180. Dever::db('shop/buy_stat')->insert($data);
  181. } else {
  182. $data['where_id'] = $info['id'];
  183. Dever::db('shop/buy_stat')->update($data);
  184. }
  185. }
  186. }
  187. }
  188. # 处理月度对账数据 废弃,直接用结算单处理
  189. public function sell_month_api()
  190. {
  191. return;
  192. $num = Dever::input('num', -1);
  193. if ($num > 0) {
  194. $month = Dever::input('start', date('Y-m', strtotime('-'.$num.' month')));
  195. } else {
  196. $month = Dever::input('start', date('Y-m'));
  197. }
  198. $where['status'] = 2;
  199. $start = Dever::maketime($month . '-01 00:00:00');
  200. $end = Dever::maketime($month . '-31 23:59:59');
  201. $shop = Dever::db('shop/info')->select();
  202. foreach ($shop as $k => $v) {
  203. $where['shop_id'] = $v['id'];
  204. $where['start'] = $start;
  205. $where['end'] = $end;
  206. $data = array();
  207. $data['shop_id'] = $v['id'];
  208. $data['month'] = $start;
  209. $info = Dever::db('shop/sell_stat_month')->find($data);
  210. $data['cash'] = Dever::db('shop/sell_order')->getCashNum($where);
  211. $data['order'] = Dever::db('shop/sell_order')->getOrderNum($where);
  212. $data['goods'] = Dever::db('shop/sell_order')->getGoodsNum($where);
  213. if (!$info) {
  214. Dever::db('shop/sell_stat_month')->insert($data);
  215. } else {
  216. $data['where_id'] = $info['id'];
  217. Dever::db('shop/sell_stat_month')->update($data);
  218. }
  219. }
  220. }
  221. # 处理月度对账数据:门店采购 废弃,直接用结算单处理
  222. public function buy_month_api()
  223. {
  224. return;
  225. $num = Dever::input('num', -1);
  226. if ($num > 0) {
  227. $month = Dever::input('start', date('Y-m', strtotime('-'.$num.' month')));
  228. } else {
  229. $month = Dever::input('start', date('Y-m'));
  230. }
  231. $where['status'] = '5,6';
  232. $start = Dever::maketime($month . '-01 00:00:00');
  233. $end = Dever::maketime($month . '-31 23:59:59');
  234. $shop = Dever::db('shop/info')->select();
  235. foreach ($shop as $k => $v) {
  236. $where['type'] = 1;
  237. $where['type_id'] = $v['id'];
  238. $where['start'] = $start;
  239. $where['end'] = $end;
  240. $data = array();
  241. $data['shop_id'] = $v['id'];
  242. $data['month'] = $start;
  243. $info = Dever::db('shop/buy_stat_month')->find($data);
  244. $data['cash'] = Dever::db('shop/buy_order')->getCashNum($where);
  245. $data['order'] = Dever::db('shop/buy_order')->getOrderNum($where);
  246. $data['goods'] = Dever::db('shop/buy_order')->getGoodsNum($where);
  247. if (!$info) {
  248. Dever::db('shop/buy_stat_month')->insert($data);
  249. } else {
  250. $data['where_id'] = $info['id'];
  251. Dever::db('shop/buy_stat_month')->update($data);
  252. }
  253. }
  254. }
  255. # 处理月度对账数据:仓库对账 废弃,直接用结算单处理
  256. public function store_month_api()
  257. {
  258. return;
  259. $num = Dever::input('num', -1);
  260. if ($num > 0) {
  261. $month = Dever::input('start', date('Y-m', strtotime('-'.$num.' month')));
  262. } else {
  263. $month = Dever::input('start', date('Y-m'));
  264. }
  265. $where['status'] = '5,6';
  266. $start = Dever::maketime($month . '-01 00:00:00');
  267. $end = Dever::maketime($month . '-31 23:59:59');
  268. $store = Dever::db('store/info')->select();
  269. foreach ($store as $k => $v) {
  270. $where['type'] = 2;
  271. $where['type_id'] = $v['id'];
  272. $where['start'] = $start;
  273. $where['end'] = $end;
  274. $data = array();
  275. $data['store_id'] = $v['id'];
  276. $data['month'] = $start;
  277. $info = Dever::db('shop/store_stat_month')->find($data);
  278. $data['cash'] = Dever::db('shop/buy_order')->getCashNum($where);
  279. $data['order'] = Dever::db('shop/buy_order')->getOrderNum($where);
  280. $data['goods'] = Dever::db('shop/buy_order')->getGoodsNum($where);
  281. if (!$info) {
  282. Dever::db('shop/store_stat_month')->insert($data);
  283. } else {
  284. $data['where_id'] = $info['id'];
  285. Dever::db('shop/store_stat_month')->update($data);
  286. }
  287. }
  288. }
  289. # 处理月度对账数据:工厂对账 废弃,直接用结算单处理
  290. public function factory_month_api()
  291. {
  292. return;
  293. $num = Dever::input('num', -1);
  294. if ($num > 0) {
  295. $month = Dever::input('start', date('Y-m', strtotime('-'.$num.' month')));
  296. } else {
  297. $month = Dever::input('start', date('Y-m'));
  298. }
  299. $where['status'] = '5,6';
  300. $start = Dever::maketime($month . '-01 00:00:00');
  301. $end = Dever::maketime($month . '-31 23:59:59');
  302. $factory = Dever::db('factory/info')->select();
  303. foreach ($factory as $k => $v) {
  304. $where['source_type'] = 3;
  305. $where['source_id'] = $v['id'];
  306. $where['start'] = $start;
  307. $where['end'] = $end;
  308. $data = array();
  309. $data['factory_id'] = $v['id'];
  310. $data['month'] = $start;
  311. $info = Dever::db('shop/factory_stat_month')->find($data);
  312. $data['cash'] = Dever::db('shop/buy_order')->getCashNum($where);
  313. $data['order'] = Dever::db('shop/buy_order')->getOrderNum($where);
  314. $data['goods'] = Dever::db('shop/buy_order')->getGoodsNum($where);
  315. if (!$info) {
  316. Dever::db('shop/factory_stat_month')->insert($data);
  317. } else {
  318. $data['where_id'] = $info['id'];
  319. Dever::db('shop/factory_stat_month')->update($data);
  320. }
  321. }
  322. }
  323. }