Cron.php 15 KB

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