Cron.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. <?php
  2. namespace Shop\Lib;
  3. use Dever;
  4. class Cron
  5. {
  6. # 定时更新银联账户余额信息
  7. public function yspay_account_api()
  8. {
  9. # 获取有银联商户号的门店
  10. $where = array();
  11. $mid = Dever::input('mid');
  12. if ($mid) {
  13. $where['mid'] = $mid;
  14. }
  15. $shop_id = Dever::input('shop_id');
  16. if ($shop_id) {
  17. $where['id'] = $shop_id;
  18. }
  19. $test = Dever::input('test');
  20. $shop = Dever::db('shop/info')->select($where);
  21. if (!$shop) {
  22. return 'error';
  23. }
  24. foreach ($shop as $k => $v) {
  25. if ($v['type'] == 10) {
  26. $v['mid'] = '89810007372106N';
  27. }
  28. if ($v['mid']) {
  29. $data = Dever::load('shop/yspay')->query_action($v['mid']);
  30. if ($test == 1) {
  31. print_r($data);die;
  32. }
  33. if ($data <= 0) {
  34. continue;
  35. }
  36. $update = array();
  37. $update['shop_id'] = $v['id'];
  38. $update['clear'] = true;
  39. $info = Dever::db('shop/yspay_account')->one($update);
  40. $update['cash'] = $data;
  41. $update['sdate'] = time();
  42. $update['mid'] = $v['mid'];
  43. if (!$info) {
  44. Dever::db('shop/yspay_account')->insert($update);
  45. } else {
  46. $update['where_id'] = $info['id'];
  47. Dever::db('shop/yspay_account')->update($update);
  48. }
  49. }
  50. }
  51. return 'ok';
  52. }
  53. # 定时获取银联提现信息
  54. public function yspay_tixian_api()
  55. {
  56. $num = Dever::input('num', 1);
  57. $start = Dever::input('start', date('Ymd', strtotime('-'.$num.' day')));
  58. $end = Dever::input('end', date('Ymd'));
  59. # 获取有银联商户号的门店
  60. $where = array();
  61. $mid = Dever::input('mid');
  62. if ($mid) {
  63. $where['mid'] = $mid;
  64. }
  65. $shop_id = Dever::input('shop_id');
  66. if ($shop_id) {
  67. $where['id'] = $shop_id;
  68. }
  69. $test = Dever::input('test');
  70. $shop = Dever::db('shop/info')->select($where);
  71. if (!$shop) {
  72. return 'error';
  73. }
  74. foreach ($shop as $k => $v) {
  75. if ($v['type'] == 10) {
  76. $v['mid'] = '89810007372106N';
  77. }
  78. if ($v['mid']) {
  79. $data = Dever::load('shop/yspay')->record_action($v['mid'], $start, $end);
  80. if ($data) {
  81. if ($test == 1) {
  82. print_r($data);die;
  83. }
  84. foreach ($data as $k1 => $v1) {
  85. $update = array();
  86. $update['shop_id'] = $v['id'];
  87. $update['type'] = $v1['trxTypeDtl'];
  88. $update['tdate'] = Dever::maketime($v1['trxTime']);
  89. $update['mid'] = $v1['mchntNo'];
  90. $update['cardNo'] = $v1['cardNo'];
  91. $update['order_num'] = $v1['sysOrderId'];
  92. $update['status'] = $v1['status'];
  93. $update['clear'] = true;
  94. $info = Dever::db('shop/yspay_tixian')->one($update);
  95. $update['cash'] = $v1['transAmt'];
  96. if (!$info) {
  97. Dever::db('shop/yspay_tixian')->insert($update);
  98. } else {
  99. $update['where_id'] = $info['id'];
  100. Dever::db('shop/yspay_tixian')->update($update);
  101. }
  102. $account_data = array();
  103. $account_data['shop_id'] = $v['id'];
  104. $account_data['clear'] = true;
  105. $account = Dever::db('shop/yspay_account')->one($account_data);
  106. $total = Dever::db('shop/yspay_tixian')->getTotal(array('shop_id' => $v['id'], 'status' => 4));
  107. if ($total) {
  108. $account_data['tx_cash'] = $total['cash'];
  109. }
  110. if (!$account) {
  111. $account_data['mid'] = $v1['mchntNo'];
  112. Dever::db('shop/yspay_account')->insert($account_data);
  113. } else {
  114. $account_data['where_id'] = $account['id'];
  115. Dever::db('shop/yspay_account')->update($account_data);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. return 'ok';
  122. }
  123. # 定时更新店铺经纬度
  124. public function geo_api()
  125. {
  126. Dever::config('base')->hook = true;
  127. $shop = Dever::db('shop/info')->select();
  128. if ($shop) {
  129. foreach ($shop as $k => $v) {
  130. $temp = explode(',', $v['area']);
  131. if (isset($temp[1]) && $v['address']) {
  132. $geo = Dever::load('shop/lib/info')->geo($temp[1], $v['address']);
  133. $update['lng'] = $geo[0];
  134. $update['lat'] = $geo[1];
  135. $update['map'] = $geo[2];
  136. $update['coord_address'] = $v['address'];
  137. $update['where_id'] = $v['id'];
  138. Dever::db('shop/info')->update($update);
  139. }
  140. }
  141. }
  142. return 'ok';
  143. }
  144. # 同步退款状态
  145. public function refund_status_api()
  146. {
  147. $order = Dever::db('shop/sell_order')->select();
  148. foreach ($order as $k => $v) {
  149. if ($v['refund_cash'] > 0) {
  150. if ($v['status'] == 6 || $v['status'] == 8) {
  151. $status = 4;
  152. } else {
  153. $status = 3;
  154. }
  155. } elseif ($v['refund_status'] == 2) {
  156. $status = 2;
  157. } else {
  158. $status = 1;
  159. }
  160. Dever::db('shop/sell_order')->update(array('where_id' => $v['id'], 'refund_status' => $status));
  161. }
  162. $order = Dever::db('shop/buy_order')->select();
  163. foreach ($order as $k => $v) {
  164. if ($v['refund_cash'] > 0) {
  165. if ($v['status'] == 6 || $v['status'] == 8) {
  166. $status = 4;
  167. } else {
  168. $status = 3;
  169. }
  170. } elseif ($v['refund_status'] == 2) {
  171. $status = 2;
  172. } else {
  173. $status = 1;
  174. }
  175. Dever::db('shop/buy_order')->update(array('where_id' => $v['id'], 'refund_status' => $status));
  176. }
  177. }
  178. # 获取7天后完成的订单 准备分账
  179. public function yspay_api()
  180. {
  181. $day = Dever::input('day', 7);
  182. $time = time();
  183. $where['status'] = '5,6';
  184. $where['withdraw'] = 1;
  185. $where['cdate'] = $time - $day*86400;
  186. $order = Dever::db('shop/sell_order')->getDataByTime($where);
  187. if ($order) {
  188. foreach ($order as $k => $v) {
  189. $this->yspayOne($v);
  190. }
  191. }
  192. }
  193. public function yspayOne($v)
  194. {
  195. $shop = Dever::db('shop/info')->find($v['shop_id']);
  196. if ($v['withdraw'] == 1 && $shop && $shop['mid'] && $shop['status'] == 1) {
  197. if ($v['refund_cash']) {
  198. $v['price'] -= $v['refund_cash'];
  199. $v['price'] = round($v['price'], 2);
  200. }
  201. if ($v['price'] > 0) {
  202. $cash = $v['price'] * 100;//之后还要加上礼品卡的钱
  203. $v['desc'] = '系统自动提现';
  204. $v['mtype'] = 1;
  205. Dever::load('shop/yspay')->action($shop['mid'], $v['order_num'], $v['id'], 2, $cash, $v);
  206. sleep(1);
  207. } else {
  208. Dever::db('shop/sell_order')->update(array('where_id' => $v['id'], 'withdraw' => 2));
  209. }
  210. }
  211. }
  212. public function setGoodsStatus_api()
  213. {
  214. $goods = Dever::db('goods/info')->select();
  215. if ($goods) {
  216. foreach ($goods as $k => $v) {
  217. Dever::db('shop/goods')->updates(array('option_goods_id' => $v['id'], 'set_status' => $v['status']));
  218. Dever::db('store/goods')->updates(array('option_goods_id' => $v['id'], 'set_status' => $v['status']));
  219. Dever::db('factory/goods')->updates(array('option_goods_id' => $v['id'], 'set_status' => $v['status']));
  220. }
  221. }
  222. }
  223. /**
  224. * 处理优惠券到期时间
  225. *
  226. * @return mixed
  227. */
  228. public function coupon_api()
  229. {
  230. $coupon = Dever::db('shop/user_coupon')->getAll(array('status' => 1, 'edate' => time() - 86400));
  231. if ($coupon && Dever::project('message')) {
  232. foreach ($coupon as $k => $v) {
  233. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  234. $msg = $coupon_info['name'] . ",要到期啦!";
  235. $msg_param['type'] = 2;//消息类型2是优惠券
  236. $msg_param['id'] = $v['id'];
  237. $msg_param['coupon_id'] = $v['coupon_id'];
  238. $msg_param = Dever::json_encode($msg_param);
  239. Dever::load('message/lib/data')->push(-1, $v['uid'], '优惠劵到期提醒', $msg, 4, 1, false, $msg_param);
  240. if (Dever::load('wechat_applet') && $v['uid'] && $v['uid'] > 0) {
  241. $user = Dever::db('passport/wechat')->one(array('uid' => $v['uid'], 'system_id' => 1, 'system_source' => 5));
  242. if ($user && $user['openid']) {
  243. $send = array
  244. (
  245. 'thing1' => array
  246. (
  247. 'value' => $coupon_info['name'],
  248. ),
  249. 'time2' => array
  250. (
  251. 'value' => date('Y-m-d H:i', $v['edate']),
  252. ),
  253. 'thing3' => array
  254. (
  255. 'value' => '您有一张优惠券即将到期,请尽快使用',
  256. ),
  257. );
  258. $send = Dever::json_encode($send);
  259. Dever::load('wechat_applet/subscribe')->sendOne('act_2', 1, $user['openid'], 'pages/my/coupon/coupon', $send, Dever::config('base')->wechat_applet);
  260. }
  261. }
  262. }
  263. }
  264. }
  265. /**
  266. * 处理待支付订单提醒
  267. *
  268. * @return mixed
  269. */
  270. public function order_api()
  271. {
  272. # 获取超过5分钟未支付的订单
  273. $time = time();
  274. $where['status'] = 1;
  275. $where['cdate'] = $time - 300;
  276. $order = Dever::db('shop/sell_order')->getDataByTime($where);
  277. if ($order && Dever::project('message')) {
  278. $config = Dever::db('shop/sell_order')->config;
  279. foreach ($order as $k => $v) {
  280. if ($time - $v['cdate'] >= 900) {
  281. Dever::load('shop/lib/sell')->set(1, 2)->cancel($v['uid'], $v['id'], 11);
  282. Dever::db('shop/sell_order')->update(array('where_id' => $v['id'], 'status' => 11, 'notice' => 2));
  283. } elseif ($v['notice'] == 1) {
  284. Dever::db('shop/sell_order')->update(array('where_id' => $v['id'], 'notice' => 2));
  285. $msg = "您有一笔待付款订单,请及时付款。\r\n订单15分钟内未付款自动取消~";
  286. $shop = Dever::db('shop/info')->one($v['shop_id']);
  287. $msg_param = array();
  288. $msg_param['type'] = 1;//消息类型1是订单消息
  289. $msg_param['id'] = $v['id'];
  290. $msg_param['name'] = $shop['name'];
  291. $msg_param = Dever::json_encode($msg_param);
  292. Dever::load('message/lib/data')->push(-1, $v['uid'], '订单待支付提醒', $msg, 2, 1, false, $msg_param);
  293. if (Dever::load('wechat_applet') && $v['uid'] && $v['uid'] > 0) {
  294. $user = Dever::db('passport/wechat')->one(array('uid' => $v['uid'], 'system_id' => 1, 'system_source' => 5));
  295. if ($user && $user['openid']) {
  296. $v['status_name'] = $config['status'][$v['status']];
  297. $send = array
  298. (
  299. 'character_string5' => array
  300. (
  301. 'value' => $v['order_num'],
  302. ),
  303. 'amount11' => array
  304. (
  305. 'value' => $v['price'] . '元',
  306. ),
  307. 'phrase13' => array
  308. (
  309. 'value' => $v['status_name'],
  310. ),
  311. 'thing12' => array
  312. (
  313. 'value' => '您有一笔待付款订单,请及时付款。',
  314. ),
  315. );
  316. $send = Dever::json_encode($send);
  317. Dever::load('wechat_applet/subscribe')->sendOne('order_pay', 1, $user['openid'], 'pages/app/order/order?id=' . $v['id'], $send, Dever::config('base')->wechat_applet);
  318. }
  319. }
  320. }
  321. }
  322. }
  323. }
  324. /**
  325. * 处理待支付订单提醒
  326. *
  327. * @return mixed
  328. */
  329. public function buy_order_api()
  330. {
  331. $time = time();
  332. $where['status'] = 1;
  333. $where['cdate'] = $time - 7200;
  334. $order = Dever::db('shop/buy_order')->getDataByTime($where);
  335. if ($order) {
  336. foreach ($order as $k => $v) {
  337. Dever::load('mshop/lib/buy')->set(1, 1)->cancel($v['type_id'], $v['id']);
  338. Dever::db('shop/buy_order')->update(array('where_id' => $v['id'], 'status' => 11));
  339. }
  340. }
  341. }
  342. /**
  343. * 处理销售数据 生成每天的销量统计
  344. *
  345. * @return mixed
  346. */
  347. public function sell_api()
  348. {
  349. $num = Dever::input('num', 1);
  350. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  351. $end = Dever::input('end', date('Y-m-d'));
  352. $where['status'] = '2,3,4,5,6';
  353. $start = Dever::maketime($start . ' 00:00:00');
  354. $end = Dever::maketime($end . ' 23:59:59');
  355. $day = intval(($end - $start)/86400);
  356. $shop = Dever::db('shop/info')->select();
  357. foreach ($shop as $k => $v) {
  358. $where['shop_id'] = $v['id'];
  359. for($i=0; $i<=$day; $i++) {
  360. $where['start'] = $start + 86400*$i;
  361. $where['end'] = $start + 86400*$i + 86399;
  362. $data = array();
  363. $data['shop_id'] = $v['id'];
  364. $data['day'] = $where['start'];
  365. $info = Dever::db('shop/sell_stat')->find($data);
  366. $cash = Dever::db('shop/sell_order')->getCashNum($where);
  367. $data['cash'] = round($cash['total'], 2);
  368. $data['order'] = Dever::db('shop/sell_order')->getOrderNum($where);
  369. if ($data['order'] > 0) {
  370. $goods = Dever::db('shop/sell_order')->getGoodsNum($where);
  371. $data['goods'] = $goods['total'];
  372. $data['area'] = $v['area'];
  373. $data['province'] = $v['province'];
  374. $data['city'] = $v['city'];
  375. $data['county'] = $v['county'];
  376. $data['town'] = $v['town'];
  377. if (!$info) {
  378. Dever::db('shop/sell_stat')->insert($data);
  379. } else {
  380. $data['where_id'] = $info['id'];
  381. Dever::db('shop/sell_stat')->update($data);
  382. }
  383. }
  384. }
  385. }
  386. }
  387. public function buy_api()
  388. {
  389. $num = Dever::input('num', 30);
  390. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  391. $end = Dever::input('end', date('Y-m-d'));
  392. $where['status'] = '2,3,4,5,6';
  393. $start = Dever::maketime($start . ' 00:00:00');
  394. $end = Dever::maketime($end . ' 23:59:59');
  395. $day = intval(($end - $start)/86400);
  396. $shop = Dever::db('shop/info')->select();
  397. foreach ($shop as $k => $v) {
  398. $where['type'] = 1;
  399. $where['type_id'] = $v['id'];
  400. for($i=0; $i<=$day; $i++) {
  401. $where['start'] = $start + 86400*$i;
  402. $where['end'] = $start + 86400*$i + 86399;
  403. $data = array();
  404. $data['shop_id'] = $v['id'];
  405. $data['day'] = $where['start'];
  406. $info = Dever::db('shop/buy_stat')->find($data);
  407. $cash = Dever::db('shop/buy_order_goods')->getCashNum($where);
  408. $data['cash'] = round($cash['total'], 2);
  409. $cash = Dever::db('shop/buy_order_goods')->getPCashNum($where);
  410. $data['p_cash'] = round($cash['total'], 2);
  411. $data['order'] = Dever::db('shop/buy_order')->getOrderNum($where);
  412. if ($data['order'] > 0) {
  413. $goods = Dever::db('shop/buy_order')->getGoodsNum($where);
  414. $data['goods'] = $goods['total'];
  415. $data['area'] = $v['area'];
  416. $data['province'] = $v['province'];
  417. $data['city'] = $v['city'];
  418. $data['county'] = $v['county'];
  419. $data['town'] = $v['town'];
  420. if (!$info) {
  421. Dever::db('shop/buy_stat')->insert($data);
  422. } else {
  423. $data['where_id'] = $info['id'];
  424. Dever::db('shop/buy_stat')->update($data);
  425. }
  426. }
  427. }
  428. }
  429. return 'ok';
  430. }
  431. /**
  432. * 门店数据统计
  433. *
  434. * @return mixed
  435. */
  436. public function shop_api()
  437. {
  438. $num = Dever::input('num', 1);
  439. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  440. $end = Dever::input('end', date('Y-m-d'));
  441. $start = Dever::maketime($start . ' 00:00:00');
  442. $end = Dever::maketime($end . ' 23:59:59');
  443. $day = intval(($end - $start)/86400);
  444. $shop = Dever::db('shop/info')->select();
  445. foreach ($shop as $k => $v) {
  446. $where = array();
  447. $where['status'] = '2,3,4,5,6';
  448. $where['shop_id'] = $v['id'];
  449. for($i=0; $i<=$day; $i++) {
  450. $where['start'] = $start + 86400*$i;
  451. $where['end'] = $start + 86400*$i + 86399;
  452. $data = array();
  453. $data['shop_id'] = $v['id'];
  454. $data['day'] = $where['start'];
  455. $info = Dever::db('shop/shop_stat')->find($data);
  456. $cash = Dever::db('shop/sell_order')->getCashNum($where);
  457. $data['sell_cash'] = round($cash['total'], 2);
  458. $data['sell_num'] = Dever::db('shop/sell_order')->getOrderNum($where);
  459. $where['type'] = 1;
  460. $where['type_id'] = $v['id'];
  461. $cash = Dever::db('shop/buy_order_goods')->getCashNum($where);
  462. $data['buy_cash'] = round($cash['total'], 2);
  463. $data['buy_num'] = Dever::db('shop/buy_order')->getOrderNum($where);
  464. $data['sl_num'] = 0;
  465. $data['area'] = $v['area'];
  466. $data['province'] = $v['province'];
  467. $data['city'] = $v['city'];
  468. $data['county'] = $v['county'];
  469. $data['town'] = $v['town'];
  470. if ($data['buy_num'] > 0) {
  471. if (!$info) {
  472. Dever::db('shop/shop_stat')->insert($data);
  473. } else {
  474. $data['where_id'] = $info['id'];
  475. Dever::db('shop/shop_stat')->update($data);
  476. }
  477. }
  478. }
  479. }
  480. }
  481. /**
  482. * 商品数据统计
  483. *
  484. * @return mixed
  485. */
  486. public function goods_api()
  487. {
  488. $num = Dever::input('num', 5);
  489. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  490. $end = Dever::input('end', date('Y-m-d'));
  491. $where['status'] = '2,3,4,5,6';
  492. $start = Dever::maketime($start . ' 00:00:00');
  493. $end = Dever::maketime($end . ' 23:59:59');
  494. $day = intval(($end - $start)/86400);
  495. for($i=0; $i<=$day; $i++) {
  496. $where['start'] = $start + 86400*$i;
  497. $where['end'] = $start + 86400*$i + 86399;
  498. $num = Dever::db('shop/sell_order_goods')->getNum($where);
  499. if ($num) {
  500. foreach ($num as $k => $v) {
  501. $data = array();
  502. $data['shop_id'] = $v['shop_id'];
  503. $data['goods_id'] = $v['goods_id'];
  504. $data['day'] = $where['start'];
  505. $info = Dever::db('shop/goods_stat')->find($data);
  506. $shop = Dever::db('shop/info')->find($data['shop_id']);
  507. if ($shop) {
  508. $data['area'] = $shop['area'];
  509. }
  510. $goods_info = Dever::db('goods/info')->find($data['goods_id']);
  511. if ($goods_info) {
  512. $data['category'] = $goods_info['category'];
  513. }
  514. $data['cash'] = round($v['cash'], 2);
  515. $data['num'] = round($v['num'], 2);
  516. if (!$info) {
  517. Dever::db('shop/goods_stat')->insert($data);
  518. } else {
  519. $data['where_id'] = $info['id'];
  520. Dever::db('shop/goods_stat')->update($data);
  521. }
  522. }
  523. }
  524. }
  525. return 'ok';
  526. }
  527. /**
  528. * 门店数据统计
  529. *
  530. * @return mixed
  531. */
  532. public function user_api()
  533. {
  534. $num = Dever::input('num', 1);
  535. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  536. $end = Dever::input('end', date('Y-m-d'));
  537. $start = Dever::maketime($start . ' 00:00:00');
  538. $end = Dever::maketime($end . ' 23:59:59');
  539. $day = intval(($end - $start)/86400);
  540. $shop = Dever::db('shop/info')->select();
  541. foreach ($shop as $k => $v) {
  542. $where = array();
  543. $where['status'] = '2,3,4,5,6';
  544. $where['shop_id'] = $v['id'];
  545. for($i=0; $i<=$day; $i++) {
  546. $where['start'] = $start + 86400*$i;
  547. $where['end'] = $start + 86400*$i + 86399;
  548. $data = array();
  549. $data['shop_id'] = $v['id'];
  550. $data['day'] = $where['start'];
  551. $info = Dever::db('shop/user_stat')->find($data);
  552. $total = Dever::db('shop/sell_order')->getUser($where);
  553. $data['total'] = count($total);
  554. foreach ($total as $k1 => $v1) {
  555. if ($v1['total'] <= 1) {
  556. unset($total[$k1]);
  557. }
  558. }
  559. $data['num'] = count($total);
  560. $data['order_num'] = Dever::db('shop/sell_order')->getOrderNum($where);
  561. if ($data['num'] > 0 || $data['order_num'] > 0) {
  562. $data['area'] = $v['area'];
  563. if (!$info) {
  564. Dever::db('shop/user_stat')->insert($data);
  565. } else {
  566. $data['where_id'] = $info['id'];
  567. Dever::db('shop/user_stat')->update($data);
  568. }
  569. }
  570. }
  571. }
  572. }
  573. /**
  574. * 用户排名统计
  575. *
  576. * @return mixed
  577. */
  578. public function user_rank_api()
  579. {
  580. $num = Dever::input('num', 1);
  581. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  582. $end = Dever::input('end', date('Y-m-d'));
  583. $start = Dever::maketime($start . ' 00:00:00');
  584. $end = Dever::maketime($end . ' 23:59:59');
  585. $day = intval(($end - $start)/86400);
  586. $w['start'] = $start;
  587. $w['end'] = $end;
  588. $w['status'] = '2,3,4,5,6';
  589. $data = Dever::db('shop/sell_order')->getAllByDate($w);
  590. if ($data) {
  591. $where = array();
  592. $result = array();
  593. foreach ($data as $k => $v) {
  594. $day = Dever::maketime(date('Y-m-d 00:00:00', $v['cdate']));
  595. $key = $v['shop_id'] . '_' . $day . '_' . $v['mobile'];
  596. if (!isset($result[$key])) {
  597. $result[$key] = array();
  598. $result[$key]['num'] = 0;
  599. $result[$key]['cash'] = 0;
  600. }
  601. $result[$key]['num'] += $v['num'];
  602. $result[$key]['cash'] = $v['total'];
  603. }
  604. if ($result) {
  605. foreach ($result as $k => $v) {
  606. $temp = explode('_', $k);
  607. $where['shop_id'] = $temp[0];
  608. $where['day'] = $temp[1];
  609. $where['mobile'] = $temp[2];
  610. $where['clear'] = true;
  611. $info = Dever::db('shop/user_rank_stat')->find($where);
  612. $up = array();
  613. if (!$info) {
  614. $up = $where;
  615. $shop = Dever::db('shop/info')->one($where['shop_id']);
  616. $up['area'] = $shop['area'];
  617. $up['num'] = $v['num'];
  618. $up['cash'] = $v['cash'];
  619. Dever::db('shop/user_rank_stat')->insert($up);
  620. } else {
  621. $up = array();
  622. $up['where_id'] = $info['id'];;
  623. $up['num'] = $v['num'];
  624. $up['cash'] = $v['cash'];
  625. Dever::db('shop/user_rank_stat')->update($up);
  626. }
  627. }
  628. }
  629. }
  630. }
  631. # 获取下单用户量
  632. public function order_user($where)
  633. {
  634. $where['status'] = '1,2,3,4,5,6';
  635. $user = Dever::db('shop/sell_order')->getUser($where);
  636. return $user;
  637. }
  638. # 处理月度对账数据 废弃,直接用结算单处理
  639. public function sell_month_api()
  640. {
  641. return;
  642. $num = Dever::input('num', -1);
  643. if ($num > 0) {
  644. $month = Dever::input('start', date('Y-m', strtotime('-'.$num.' month')));
  645. } else {
  646. $month = Dever::input('start', date('Y-m'));
  647. }
  648. $where['status'] = 2;
  649. $start = Dever::maketime($month . '-01 00:00:00');
  650. $end = Dever::maketime($month . '-31 23:59:59');
  651. $shop = Dever::db('shop/info')->select();
  652. foreach ($shop as $k => $v) {
  653. $where['shop_id'] = $v['id'];
  654. $where['start'] = $start;
  655. $where['end'] = $end;
  656. $data = array();
  657. $data['shop_id'] = $v['id'];
  658. $data['month'] = $start;
  659. $info = Dever::db('shop/sell_stat_month')->find($data);
  660. $data['cash'] = Dever::db('shop/sell_order')->getCashNum($where);
  661. $data['order'] = Dever::db('shop/sell_order')->getOrderNum($where);
  662. $data['goods'] = Dever::db('shop/sell_order')->getGoodsNum($where);
  663. if (!$info) {
  664. Dever::db('shop/sell_stat_month')->insert($data);
  665. } else {
  666. $data['where_id'] = $info['id'];
  667. Dever::db('shop/sell_stat_month')->update($data);
  668. }
  669. }
  670. }
  671. # 处理月度对账数据:门店采购 废弃,直接用结算单处理
  672. public function buy_month_api()
  673. {
  674. return;
  675. $num = Dever::input('num', -1);
  676. if ($num > 0) {
  677. $month = Dever::input('start', date('Y-m', strtotime('-'.$num.' month')));
  678. } else {
  679. $month = Dever::input('start', date('Y-m'));
  680. }
  681. $where['status'] = '5,6';
  682. $start = Dever::maketime($month . '-01 00:00:00');
  683. $end = Dever::maketime($month . '-31 23:59:59');
  684. $shop = Dever::db('shop/info')->select();
  685. foreach ($shop as $k => $v) {
  686. $where['type'] = 1;
  687. $where['type_id'] = $v['id'];
  688. $where['start'] = $start;
  689. $where['end'] = $end;
  690. $data = array();
  691. $data['shop_id'] = $v['id'];
  692. $data['month'] = $start;
  693. $info = Dever::db('shop/buy_stat_month')->find($data);
  694. $data['cash'] = Dever::db('shop/buy_order_goods')->getCashNum($where);
  695. $data['order'] = Dever::db('shop/buy_order')->getOrderNum($where);
  696. $data['goods'] = Dever::db('shop/buy_order')->getGoodsNum($where);
  697. if (!$info) {
  698. Dever::db('shop/buy_stat_month')->insert($data);
  699. } else {
  700. $data['where_id'] = $info['id'];
  701. Dever::db('shop/buy_stat_month')->update($data);
  702. }
  703. }
  704. }
  705. # 处理月度对账数据:仓库对账 废弃,直接用结算单处理
  706. public function store_month_api()
  707. {
  708. return;
  709. $num = Dever::input('num', -1);
  710. if ($num > 0) {
  711. $month = Dever::input('start', date('Y-m', strtotime('-'.$num.' month')));
  712. } else {
  713. $month = Dever::input('start', date('Y-m'));
  714. }
  715. $where['status'] = '5,6';
  716. $start = Dever::maketime($month . '-01 00:00:00');
  717. $end = Dever::maketime($month . '-31 23:59:59');
  718. $store = Dever::db('store/info')->select();
  719. foreach ($store as $k => $v) {
  720. $where['type'] = 2;
  721. $where['type_id'] = $v['id'];
  722. $where['start'] = $start;
  723. $where['end'] = $end;
  724. $data = array();
  725. $data['store_id'] = $v['id'];
  726. $data['month'] = $start;
  727. $info = Dever::db('shop/store_stat_month')->find($data);
  728. $data['cash'] = Dever::db('shop/buy_order_goods')->getCashNum($where);
  729. $data['order'] = Dever::db('shop/buy_order')->getOrderNum($where);
  730. $data['goods'] = Dever::db('shop/buy_order')->getGoodsNum($where);
  731. if (!$info) {
  732. Dever::db('shop/store_stat_month')->insert($data);
  733. } else {
  734. $data['where_id'] = $info['id'];
  735. Dever::db('shop/store_stat_month')->update($data);
  736. }
  737. }
  738. }
  739. # 处理月度对账数据:工厂对账 废弃,直接用结算单处理
  740. public function factory_month_api()
  741. {
  742. return;
  743. $num = Dever::input('num', -1);
  744. if ($num > 0) {
  745. $month = Dever::input('start', date('Y-m', strtotime('-'.$num.' month')));
  746. } else {
  747. $month = Dever::input('start', date('Y-m'));
  748. }
  749. $where['status'] = '5,6';
  750. $start = Dever::maketime($month . '-01 00:00:00');
  751. $end = Dever::maketime($month . '-31 23:59:59');
  752. $factory = Dever::db('factory/info')->select();
  753. foreach ($factory as $k => $v) {
  754. $where['source_type'] = 3;
  755. $where['source_id'] = $v['id'];
  756. $where['start'] = $start;
  757. $where['end'] = $end;
  758. $data = array();
  759. $data['factory_id'] = $v['id'];
  760. $data['month'] = $start;
  761. $info = Dever::db('shop/factory_stat_month')->find($data);
  762. $data['cash'] = Dever::db('shop/buy_order_goods')->getCashNum($where);
  763. $data['order'] = Dever::db('shop/buy_order')->getOrderNum($where);
  764. $data['goods'] = Dever::db('shop/buy_order')->getGoodsNum($where);
  765. if (!$info) {
  766. Dever::db('shop/factory_stat_month')->insert($data);
  767. } else {
  768. $data['where_id'] = $info['id'];
  769. Dever::db('shop/factory_stat_month')->update($data);
  770. }
  771. }
  772. }
  773. # 校正出厂价
  774. public function setPrice_api()
  775. {
  776. $data = Dever::db('shop/buy_order')->select();
  777. if ($data) {
  778. foreach ($data as $k => $v) {
  779. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $v['id']));
  780. if ($goods) {
  781. $p_price = 0;
  782. foreach ($goods as $v1) {
  783. $p_price += ($v1['p_price'] * $v1['num']);
  784. }
  785. Dever::db('shop/buy_order')->update(array('where_id' => $v['id'], 'p_price' => $p_price));
  786. }
  787. }
  788. }
  789. return 'ok';
  790. }
  791. #新增
  792. #平台商城订单表同步发货时间sell_order
  793. public function tongSellOrder_api()
  794. {
  795. $data = Dever::db('shop/sell_order')->state();
  796. foreach ($data as $k => $v) {
  797. if (isset($v['id']) && $v['id'] ) {
  798. $ps = Dever::db('shop/sell_order_ps')->find(array('order_id'=>$v['id']));
  799. $where['where_id'] = $v['id'];
  800. if (isset($ps) && $ps && isset($ps['cdate']) && $ps['cdate']) {
  801. $where['fadate'] = $ps['cdate'];
  802. Dever::db('shop/sell_order')->update($where);
  803. }
  804. }
  805. }
  806. }
  807. #门店采购发货时间
  808. public function tongBuyOrder_api()
  809. {
  810. $data = Dever::db('shop/buy_order')->state();
  811. foreach ($data as $k => $v) {
  812. if (isset($v['id']) && $v['id']) {
  813. $ps = Dever::db('shop/buy_order_ps')->find(array('order_id'=>$v['id']));
  814. $where['where_id'] = $v['id'];
  815. if (isset($ps) && $ps && isset($ps['cdate']) && $ps['cdate']) {
  816. $where['fadate'] = $ps['cdate'];
  817. Dever::db('shop/buy_order')->update($where);
  818. if ($v['parent_type'] == 2 && $v['parent_order_id'] > 0) {
  819. Dever::db('shop/sell_order')->update(array('where_id' => $v['parent_order_id'], 'fadate' => $ps['cdate']));
  820. $sell_order_ps = Dever::db('shop/sell_order_ps')->select(array('order_id'=>$v['parent_order_id']));
  821. if ($sell_order_ps) {
  822. foreach ($sell_order_ps as $k1 => $v1) {
  823. Dever::db('shop/sell_order_ps')->update(array('where_id' => $v1['id'], 'cdate' => $ps['cdate']));
  824. }
  825. }
  826. }
  827. }
  828. }
  829. }
  830. }
  831. # 处理配送服务商和配送时间
  832. public function upPs_api()
  833. {
  834. $where = array();
  835. $order_id = Dever::input('order_id');
  836. if ($order_id) {
  837. $where['order_id'] = $order_id;
  838. }
  839. /*
  840. $data = Dever::db('shop/buy_order_ps')->state($where);
  841. if ($data) {
  842. $update = array();
  843. foreach ($data as $k => $v) {
  844. $service = Dever::array_decode($v['service']);
  845. if ($service) {
  846. $update['service'] = array();
  847. foreach ($service as $k1 => $v1) {
  848. $update['service'][$k1] = $v1;
  849. if (isset($v1['service_id']) && $v1['service_id']) {
  850. $service_id = $v1['service_id'];
  851. $service_info = Dever::db('shop/service')->one($service_id);
  852. if ($service_info) {
  853. $service_new_info = Dever::db('shop/service')->getOne(array('name' => $service_info['name']));
  854. if ($service_new_info && $service_new_info['id'] != $service_id) {
  855. $update['service'][$k1]['service_id'] = $service_new_info['id'];
  856. Dever::db('shop/service')->update(array('where_id' => $service_info['id'], 'state' => 2));
  857. }
  858. }
  859. }
  860. }
  861. if ($update['service']) {
  862. $update['service'] = Dever::array_encode($update['service']);
  863. $update['where_id'] = $v['id'];
  864. Dever::db('shop/buy_order_ps')->update($update);
  865. }
  866. }
  867. }
  868. }
  869. if ($order_id) {
  870. return 'ok';
  871. }
  872. $data = Dever::db('shop/sell_order_ps')->state();
  873. if ($data) {
  874. $update = array();
  875. foreach ($data as $k => $v) {
  876. $service = Dever::array_decode($v['service']);
  877. if ($service) {
  878. $update['service'] = array();
  879. foreach ($service as $k1 => $v1) {
  880. $update['service'][$k1] = $v1;
  881. if (isset($v1['service_id']) && $v1['service_id']) {
  882. $service_id = $v1['service_id'];
  883. $service_info = Dever::db('shop/service')->one($service_id);
  884. if ($service_info) {
  885. $service_new_info = Dever::db('shop/service')->getOne(array('name' => $service_info['name']));
  886. if ($service_new_info && $service_new_info['id'] != $service_id) {
  887. $update['service'][$k1]['service_id'] = $service_new_info['id'];
  888. Dever::db('shop/service')->update(array('where_id' => $service_info['id'], 'state' => 2));
  889. }
  890. }
  891. }
  892. }
  893. if ($update['service']) {
  894. $update['service'] = Dever::array_encode($update['service']);
  895. $update['where_id'] = $v['id'];
  896. Dever::db('shop/sell_order_ps')->update($update);
  897. }
  898. }
  899. }
  900. }
  901. */
  902. $data = Dever::db('agent/dh_order_ps')->state();
  903. if ($data) {
  904. $update = array();
  905. foreach ($data as $k => $v) {
  906. $service = Dever::array_decode($v['service']);
  907. if ($service) {
  908. $update['service'] = array();
  909. foreach ($service as $k1 => $v1) {
  910. $update['service'][$k1] = $v1;
  911. if (isset($v1['service_id']) && $v1['service_id']) {
  912. $service_id = $v1['service_id'];
  913. $service_info = Dever::db('shop/service')->one($service_id);
  914. if ($service_info) {
  915. $service_new_info = Dever::db('shop/service')->getOne(array('name' => $service_info['name']));
  916. if ($service_new_info && $service_new_info['id'] != $service_id) {
  917. $update['service'][$k1]['service_id'] = $service_new_info['id'];
  918. Dever::db('shop/service')->update(array('where_id' => $service_info['id'], 'state' => 2));
  919. }
  920. }
  921. }
  922. }
  923. if ($update['service']) {
  924. $update['service'] = Dever::array_encode($update['service']);
  925. $update['where_id'] = $v['id'];
  926. Dever::db('agent/dh_order_ps')->update($update);
  927. }
  928. }
  929. }
  930. }
  931. return 'ok';
  932. }
  933. }