Date.php 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  1. <?php
  2. namespace app\crontab\controller;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. * DSMall多用户商城
  7. * ============================================================================
  8. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  9. * 网站地址: http://www.csdeshang.com
  10. * ----------------------------------------------------------------------------
  11. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  12. * 不允许对程序代码以任何形式任何目的的再发布。
  13. * ============================================================================
  14. * 定时器
  15. */
  16. class Date extends BaseCron {
  17. /**
  18. * 该文件中所有任务执行频率,默认1天,单位:秒
  19. * @var int
  20. */
  21. const EXE_TIMES = 86400;
  22. /**
  23. * 优惠券即将到期提醒时间,单位:天
  24. * @var int
  25. */
  26. const VOUCHER_INTERVAL = 5;
  27. /**
  28. * 兑换码即将到期提醒时间,单位:天
  29. * @var int
  30. */
  31. const VR_CODE_INTERVAL = 5;
  32. /**
  33. * 订单结束后可评论时间,15天,60*60*24*15
  34. * @var int
  35. */
  36. const ORDER_EVALUATE_TIME = 1296000;
  37. /**
  38. * 每次到货通知消息数量
  39. * @var int
  40. */
  41. const ARRIVAL_NOTICE_NUM = 100;
  42. /**
  43. * 默认方法
  44. */
  45. public function index() {
  46. //小程序直播商品状态查询
  47. $this->_minipro_live_goods_state();
  48. //小程序直播商品删除
  49. $this->_minipro_live_goods_delete();
  50. //小程序直播商品创建
  51. $this->_minipro_live_goods_add();
  52. //小程序直播商品更新
  53. $this->_minipro_live_goods_update();
  54. //订单超期后不允许评价
  55. $this->_order_eval_expire_update();
  56. //未付款订单超期自动关闭
  57. $this->_order_timeout_cancel();
  58. //增加会员积分和经验值
  59. $this->_add_points();
  60. //订单自动完成
  61. $this->_order_auto_complete();
  62. //更新订单扩展表收货人所在省份ID
  63. $this->_order_reciver_provinceid_update();
  64. //更新退款申请超时处理
  65. model('trade')->editRefundConfirm();
  66. //店铺到期关闭
  67. $this->_store_auto_close();
  68. //生成店铺结算单
  69. $this->_creat_bill();
  70. //代金券即将过期提醒
  71. $this->_voucher_will_expire();
  72. //虚拟兑换码即将过期提醒
  73. $this->_vr_code_will_expire();
  74. //更新商品访问量
  75. $this->_goods_click_update();
  76. //更新商品促销到期状态
  77. $this->_goods_promotion_state_update();
  78. //商品到货通知提醒
  79. $this->_arrival_notice();
  80. //更新浏览量
  81. $this->_goods_browse_update();
  82. //缓存订单及订单商品相关数据
  83. $this->_order_goods_cache();
  84. //会员相关数据统计
  85. $this->_member_stat();
  86. }
  87. /*
  88. * 小程序直播商品创建(调用额度:500次/一天)
  89. */
  90. function _minipro_live_goods_add() {
  91. if (intval(config('ds_config.live_type')) != 1) {
  92. return;
  93. }
  94. $goods_list = Db::name('goodscommon')->alias('goodscommon')->join('minipro_live_goods minipro_live_goods', 'minipro_live_goods.goods_commonid=goodscommon.goods_commonid', 'LEFT')->field('goodscommon.gc_id,goodscommon.gc_id_1,goodscommon.gc_id_2,goodscommon.gc_id_3,goodscommon.gc_name,goodscommon.goods_name,goodscommon.goods_image,goodscommon.goods_price,goodscommon.goods_commonid,goodscommon.store_id,goodscommon.store_name')->where(array(array('minipro_live_goods.goods_commonid', 'null')))->limit(100)->select()->toArray();
  95. $wechat_model = model('wechat');
  96. $wechat_model->getOneWxconfig();
  97. $accessToken = $wechat_model->getAccessToken('miniprogram', 0);
  98. if ($wechat_model->error_code) {
  99. $this->log('获取小程序AccessToken失败' . $wechat_model->error_message);
  100. return;
  101. }
  102. $minipro_live_goods_model = model('minipro_live_goods');
  103. foreach ($goods_list as $goods_info) {
  104. $goods_id = Db::name('goods')->where(array(array('goods_commonid', '=', $goods_info['goods_commonid'])))->order('goods_id asc')->value('goods_id');
  105. if (!$goods_id) {
  106. $this->log('商品不存在');
  107. continue;
  108. }
  109. //上传临时素材
  110. $goods_image = str_replace(UPLOAD_SITE_URL, BASE_UPLOAD_PATH, goods_thumb($goods_info));
  111. $image_path = pathinfo($goods_image, PATHINFO_DIRNAME);
  112. $image_name = pathinfo($goods_image, PATHINFO_BASENAME);
  113. if (!strstr($image_name, '_240')) {
  114. create_albumpic_thumb($image_path, $image_name);
  115. $goods_image = str_ireplace('.', '_240.', $image_name);
  116. }
  117. $res = $wechat_model->uploadMedia('image', $goods_image, "image/jpeg", $image_name);
  118. if (!$res['code']) {
  119. $this->log('上传临时素材失败' . $res['msg']);
  120. continue;
  121. }
  122. $coverImgUrl = $res['data'];
  123. $data = array(
  124. 'goodsInfo' => array(
  125. 'coverImgUrl' => $coverImgUrl,
  126. 'name' => mb_substr($goods_info['goods_name'], 0, 14), //商品名称,最长14个汉字
  127. 'priceType' => 1,
  128. 'price' => $goods_info['goods_price'],
  129. 'url' => 'pages/index/index?ret_url=' . urlencode(config('ds_config.h5_site_url') . '/pages/home/goodsdetail/Goodsdetail?goods_id=' . $goods_id)
  130. )
  131. );
  132. $res = http_request('https://api.weixin.qq.com/wxaapi/broadcast/goods/add?access_token=' . $accessToken, 'POST', $data);
  133. $res = json_decode($res, true);
  134. if (!$res || $res['errcode']) {
  135. $this->log(isset($res['errmsg']) ? $res['errmsg'] : '添加商品出错,错误码:' . $res['errcode']);
  136. continue;
  137. }
  138. $minipro_live_goods_model->addMiniproLiveGoods(array(
  139. 'store_id' => $goods_info['store_id'],
  140. 'store_name' => $goods_info['store_name'],
  141. 'goods_id' => $goods_id,
  142. 'goods_commonid' => $goods_info['goods_commonid'],
  143. 'goods_name' => $goods_info['goods_name'],
  144. 'goods_price' => $goods_info['goods_price'],
  145. 'goods_image' => $goods_info['goods_image'],
  146. 'minipro_live_goods_add_time' => TIMESTAMP,
  147. 'minipro_live_goods_state' => 0,
  148. 'minipro_live_goods_result_id' => $res['goodsId'],
  149. 'minipro_live_goods_verify_id' => $res['auditId'],
  150. ));
  151. }
  152. }
  153. /*
  154. * 小程序直播商品删除(调用额度:1000次/一天)
  155. */
  156. function _minipro_live_goods_delete() {
  157. if (intval(config('ds_config.live_type')) != 1) {
  158. return;
  159. }
  160. $wechat_model = model('wechat');
  161. $wechat_model->getOneWxconfig();
  162. $accessToken = $wechat_model->getAccessToken('miniprogram', 0);
  163. if ($wechat_model->error_code) {
  164. $this->log('获取小程序AccessToken失败' . $wechat_model->error_message);
  165. return;
  166. }
  167. $minipro_live_goods_model = model('minipro_live_goods');
  168. //商品不存在的删除,审核中的、已审核的商品名称、商品图片不一致的删除
  169. $minipro_live_goods_result_ids = Db::name('minipro_live_goods')->alias('minipro_live_goods')->join('goodscommon goodscommon', 'minipro_live_goods.goods_commonid=goodscommon.goods_commonid', 'LEFT')->where(array(array('minipro_live_goods_close', '=', 0), array('goodscommon.goods_commonid', 'null')))->whereOr(function($query) {
  170. $query->where(array(array('minipro_live_goods_state', '<>', 0)))->where(function ($query) {
  171. $query->whereOr(array(array('goodscommon.goods_name', '<>', Db::raw('minipro_live_goods.goods_name'))))->whereOr(array(array('goodscommon.goods_image', '<>', Db::raw('minipro_live_goods.goods_image'))));
  172. });
  173. })->limit(100)->column('minipro_live_goods_result_id');
  174. foreach ($minipro_live_goods_result_ids as $minipro_live_goods_result_id) {
  175. $data = array(
  176. 'goodsId' => $minipro_live_goods_result_id
  177. );
  178. $res = http_request('https://api.weixin.qq.com/wxaapi/broadcast/goods/delete?access_token=' . $accessToken, 'POST', $data);
  179. $res = json_decode($res, true);
  180. if (!$res || $res['errcode']) {
  181. $this->log(isset($res['errmsg']) ? $res['errmsg'] : '删除商品出错,错误码:' . $res['errcode']);
  182. continue;
  183. }
  184. $minipro_live_goods_model->delMiniproLiveGoods(array(array('minipro_live_goods_result_id', '=', $minipro_live_goods_result_id)));
  185. }
  186. //审核中的、已审核的商品ID不存在的删除
  187. $subQuery = Db::name('goods')->field('goods_id')->where('1=1')->buildSql();
  188. $minipro_live_goods_result_ids = Db::name('minipro_live_goods')->where(array(array('minipro_live_goods_close', '=', 0), array('minipro_live_goods_state', '<>', 0), array('goods_id', 'not in', Db::raw($subQuery))))->limit(100)->column('minipro_live_goods_result_id');
  189. foreach ($minipro_live_goods_result_ids as $minipro_live_goods_result_id) {
  190. $data = array(
  191. 'goodsId' => $minipro_live_goods_result_id
  192. );
  193. $res = http_request('https://api.weixin.qq.com/wxaapi/broadcast/goods/delete?access_token=' . $accessToken, 'POST', $data);
  194. $res = json_decode($res, true);
  195. if (!$res || $res['errcode']) {
  196. $this->log(isset($res['errmsg']) ? $res['errmsg'] : '删除商品出错,错误码:' . $res['errcode']);
  197. continue;
  198. }
  199. $minipro_live_goods_model->delMiniproLiveGoods(array(array('minipro_live_goods_result_id', '=', $minipro_live_goods_result_id)));
  200. }
  201. }
  202. /*
  203. * 小程序直播商品更新(调用额度:1000次/一天)
  204. */
  205. function _minipro_live_goods_update() {
  206. if (intval(config('ds_config.live_type')) != 1) {
  207. return;
  208. }
  209. $wechat_model = model('wechat');
  210. $wechat_model->getOneWxconfig();
  211. $accessToken = $wechat_model->getAccessToken('miniprogram', 0);
  212. if ($wechat_model->error_code) {
  213. $this->log('获取小程序AccessToken失败' . $wechat_model->error_message);
  214. return;
  215. }
  216. $minipro_live_goods_model = model('minipro_live_goods');
  217. //未审核的商品、已审核的商品价格不一致的更新
  218. $goods_list = Db::name('minipro_live_goods')->alias('minipro_live_goods')->field('minipro_live_goods_result_id,goodscommon.goods_name,goodscommon.goods_image,minipro_live_goods.goods_image as goods_image2,goodscommon.goods_price,goodscommon.goods_commonid,goodscommon.store_id,goodscommon.store_name')->join('goodscommon goodscommon', 'minipro_live_goods.goods_commonid=goodscommon.goods_commonid')->where(array(array('minipro_live_goods_close', '=', 0)))->where(function ($query) {
  219. $query->where(array(array('minipro_live_goods_state', '=', 2)))->where(array(array('goodscommon.goods_price', '<>', Db::raw('minipro_live_goods.goods_price'))));
  220. })->whereOr(function ($query) {
  221. $query->where(array(array('minipro_live_goods_state', '=', 0)))->where(function ($query) {
  222. $query->whereOr(array(array('goodscommon.goods_price', '<>', Db::raw('minipro_live_goods.goods_price'))))->whereOr(array(array('goodscommon.goods_name', '<>', Db::raw('minipro_live_goods.goods_name'))))->whereOr(array(array('goodscommon.goods_image', '<>', Db::raw('minipro_live_goods.goods_image'))));
  223. });
  224. })->limit(100)->select()->toArray();
  225. foreach ($goods_list as $goods_info) {
  226. $goods_data = array(
  227. 'goodsId' => $goods_info['minipro_live_goods_result_id'],
  228. 'priceType' => 1,
  229. );
  230. if ($goods_info['minipro_live_goods_state'] == 2) {
  231. $goods_data['price'] = $goods_info['goods_price'];
  232. }
  233. if ($goods_info['minipro_live_goods_state'] == 0) {
  234. if ($goods_info['goods_image'] != $goods_info['goods_image2']) {
  235. //上传临时素材
  236. $goods_image = str_replace(UPLOAD_SITE_URL, BASE_UPLOAD_PATH, goods_thumb($goods_info));
  237. $image_path = pathinfo($goods_image, PATHINFO_DIRNAME);
  238. $image_name = pathinfo($goods_image, PATHINFO_BASENAME);
  239. if (!strstr($image_name, '_240')) {
  240. create_albumpic_thumb($image_path, $image_name);
  241. $goods_image = str_ireplace('.', '_240.', $image_name);
  242. }
  243. $res = $wechat_model->uploadMedia('image', $goods_image, "image/jpeg", $image_name);
  244. if (!$res['code']) {
  245. $this->log('上传临时素材失败' . $res['msg']);
  246. continue;
  247. }
  248. $coverImgUrl = $res['data'];
  249. $goods_data['coverImgUrl'] = $coverImgUrl;
  250. }
  251. $goods_data['name'] = mb_substr($goods_info['goods_name'], 0, 14); //商品名称,最长14个汉字
  252. $goods_data['price'] = $goods_info['goods_price'];
  253. }
  254. $data = array(
  255. 'goodsInfo' => $goods_data
  256. );
  257. $res = http_request('https://api.weixin.qq.com/wxaapi/broadcast/goods/update?access_token=' . $accessToken, 'POST', $data);
  258. $res = json_decode($res, true);
  259. if (!$res || $res['errcode']) {
  260. $this->log(isset($res['errmsg']) ? $res['errmsg'] : '更新商品出错,错误码:' . $res['errcode']);
  261. continue;
  262. }
  263. $minipro_live_goods_model->editMiniproLiveGoods(array(
  264. 'goods_name' => $goods_info['goods_name'],
  265. 'goods_price' => $goods_info['goods_price'],
  266. 'goods_image' => $goods_info['goods_image'],
  267. ), array(array('minipro_live_goods_result_id', '=', $goods_info['minipro_live_goods_result_id'])));
  268. }
  269. //未审核的商品ID不存在的更新
  270. $subQuery = Db::name('goods')->field('goods_id')->where('1=1')->buildSql();
  271. $goods_list = Db::name('minipro_live_goods')->where(array(array('minipro_live_goods_close', '=', 0), array('minipro_live_goods_state', '=', 0), array('goods_id', 'not in', Db::raw($subQuery))))->limit(100)->select()->toArray();
  272. foreach ($goods_list as $goods_info) {
  273. $goods_id = Db::name('goods')->where(array(array('goods_commonid', '=', $goods_info['goods_commonid'])))->order('goods_id asc')->value('goods_id');
  274. if (!$goods_id) {
  275. $this->log('商品不存在');
  276. continue;
  277. }
  278. $goods_data = array(
  279. 'goodsId' => $goods_info['minipro_live_goods_result_id'],
  280. 'url' => 'pages/index/index?ret_url=' . urlencode(config('ds_config.h5_site_url') . '/pages/home/goodsdetail/Goodsdetail?goods_id=' . $goods_id)
  281. );
  282. $data = array(
  283. 'goodsInfo' => $goods_data
  284. );
  285. $res = http_request('https://api.weixin.qq.com/wxaapi/broadcast/goods/update?access_token=' . $accessToken, 'POST', $data);
  286. $res = json_decode($res, true);
  287. if (!$res || $res['errcode']) {
  288. $this->log(isset($res['errmsg']) ? $res['errmsg'] : '更新商品出错,错误码:' . $res['errcode']);
  289. continue;
  290. }
  291. $minipro_live_goods_model->editMiniproLiveGoods(array(
  292. 'goods_id' => $goods_id,
  293. ), array(array('minipro_live_goods_result_id', '=', $goods_info['minipro_live_goods_result_id'])));
  294. }
  295. }
  296. /*
  297. * 小程序直播商品状态查询(调用额度:1000次/一天)
  298. */
  299. function _minipro_live_goods_state() {
  300. if (intval(config('ds_config.live_type')) != 1) {
  301. return;
  302. }
  303. //审核时间为1~7天
  304. $wechat_model = model('wechat');
  305. $wechat_model->getOneWxconfig();
  306. $accessToken = $wechat_model->getAccessToken('miniprogram', 0);
  307. if ($wechat_model->error_code) {
  308. $this->log('获取小程序AccessToken失败' . $wechat_model->error_message);
  309. return;
  310. }
  311. $ids = Db::name('minipro_live_goods')->where(array(array('minipro_live_goods_close', '=', 0), array('minipro_live_goods_state', '=', 0)))->limit(200)->column('minipro_live_goods_result_id');
  312. if (!empty($ids)) {
  313. $data = array(
  314. 'goods_ids' => $ids
  315. );
  316. $res = http_request('https://api.weixin.qq.com/wxa/business/getgoodswarehouse?access_token=' . $accessToken, 'POST', json_encode($data));
  317. $res = json_decode($res, true);
  318. if (!$res || $res['errcode']) {
  319. $this->log('小程序直播商品状态查询失败' . (isset($res['errmsg']) ? $res['errmsg'] : ',错误码:' . $res['errcode']));
  320. return;
  321. }
  322. $minipro_live_goods_model = model('minipro_live_goods');
  323. foreach ($res['goods'] as $val) {
  324. if (in_array($val['audit_status'], array(2, 3))) {
  325. $minipro_live_goods_model->editMiniproLiveGoods(array('minipro_live_goods_state' => $val['audit_status']), array(array('minipro_live_goods_result_id', '=', $val['goods_id'])));
  326. }
  327. }
  328. }
  329. }
  330. /*
  331. * 店铺到期关闭
  332. */
  333. function _store_auto_close() {
  334. $store_model = model('store');
  335. $condition = array();
  336. $condition[] = array('store_state', '=', 1);
  337. $condition[] = array('store_endtime', '>', 0);
  338. $condition[] = array('store_endtime', '<', TIMESTAMP);
  339. $store_ids = Db::name('store')->where($condition)->limit(100)->column('store_id');
  340. $goods_model = model('goods');
  341. foreach ($store_ids as $store_id) {
  342. Db::startTrans();
  343. try {
  344. $store_model->editStore(array('store_state' => 0, 'store_recommend' => 0), array(array('store_id', '=', $store_id)));
  345. //根据店铺状态修改该店铺所有商品状态
  346. $goods_model->editProducesOffline(array(array('store_id', '=', $store_id)));
  347. } catch (\Exception $e) {
  348. Db::rollback();
  349. $this->log('到期店铺关闭失败[店铺ID:' . $store_id . ']' . $e->getMessage());
  350. }
  351. Db::commit();
  352. }
  353. }
  354. /*
  355. * 生成店铺结算单
  356. */
  357. public function _creat_bill() {
  358. //搜索商家结算日期小于当前时间减结算周期的所有商家,每次100个
  359. $store_ids = Db::name('store')->where(array(array('store_bill_time', '<', strtotime(date('Y-m-d 0:0:0')) - intval(config('ds_config.store_bill_cycle')) * 86400)))->limit(100)->column('store_id');
  360. $storemoneylog_mod = model('storemoneylog');
  361. $orderinviter_model = model('orderinviter');
  362. $bill_model = model('bill');
  363. $trade_model = model('trade');
  364. $order_refund_day = intval($trade_model->getMaxDay('order_refund'));
  365. $order_refund_time = strtotime(date('Y-m-d 0:0:0')) - $order_refund_day * 86400;
  366. $vr_order_refund_day = intval(config('ds_config.code_invalid_refund'));
  367. $vr_order_refund_time = strtotime(date('Y-m-d 0:0:0')) - $vr_order_refund_day * 86400;
  368. foreach ($store_ids as $store_id) {
  369. Db::startTrans();
  370. $store_info = Db::name('store')->where('store_id', $store_id)->field('store_id,store_name,seller_name,is_platform_store,store_bill_time')->lock(true)->find();
  371. if ($store_info) {
  372. try {
  373. $data = array();
  374. //如果店铺没有结算过,则查询与结算单相关项目的最小时间作为结算单开始时间
  375. if (!$store_info['store_bill_time']) {
  376. $ob_startdate = 0;
  377. $storecost_time = Db::name('storecost')->where(array(array('storecost_store_id', '=', $store_id), array('storecost_state', '=', 0), array('storecost_time', '<', strtotime(date('Y-m-d 0:0:0')) - intval(config('ds_config.store_bill_cycle')) * 86400)))->order('storecost_time asc')->lock(true)->value('storecost_time');
  378. //取结算时间和退款过期时间的较小者
  379. $temp_time = $order_refund_time;
  380. if ($temp_time > (strtotime(date('Y-m-d 0:0:0')) - intval(config('ds_config.store_bill_cycle')) * 86400)) {
  381. $temp_time = strtotime(date('Y-m-d 0:0:0')) - intval(config('ds_config.store_bill_cycle')) * 86400;
  382. }
  383. $order_time = Db::name('order')->where(array(array('store_id', '=', $store_id), array('order_state', '=', ORDER_STATE_SUCCESS), array('delay_time', '<', $temp_time), array('payment_code', '<>', 'offline'), array('ob_no', '=', 0)))->order('add_time asc')->lock(true)->value('add_time');
  384. //不允许退款且超过使用期限的、允许退款且超过可退款期限、允许退款并且全部完成退款的虚拟订单
  385. $vr_order_time = Db::name('vrorder')->where(array(array('store_id', '=', $store_id), array('order_state', '=', ORDER_STATE_SUCCESS), array('finnshed_time', '<', strtotime(date('Y-m-d 0:0:0')) - intval(config('ds_config.store_bill_cycle')) * 86400), array('ob_no', '=', 0)))->where('((vr_invalid_refund=0) || (vr_invalid_refund=1 AND vr_indate<' . $vr_order_refund_time . ') || (vr_invalid_refund=1 AND refund_state=2))')->order('add_time asc')->lock(true)->value('add_time');
  386. $ob_startdate = (!$ob_startdate || ($ob_startdate && $storecost_time && $storecost_time < $ob_startdate)) ? $storecost_time : $ob_startdate;
  387. $ob_startdate = (!$ob_startdate || ($ob_startdate && $order_time && $order_time < $ob_startdate)) ? $order_time : $ob_startdate;
  388. $ob_startdate = (!$ob_startdate || ($ob_startdate && $vr_order_time && $vr_order_time < $ob_startdate)) ? $vr_order_time : $ob_startdate;
  389. } else {
  390. $ob_startdate = $store_info['store_bill_time'];
  391. }
  392. if ($ob_startdate) {
  393. //零点开始
  394. $ob_startdate = strtotime(date('Y-m-d 0:0:0', $ob_startdate));
  395. $ob_enddate = $ob_startdate + intval(config('ds_config.store_bill_cycle')) * 86400;
  396. //店铺促销活动费用
  397. $storecost_sum = Db::name('storecost')->where(array(array('storecost_store_id', '=', $store_id), array('storecost_state', '=', 0), array('storecost_time', '<', $ob_enddate)))->field('SUM(storecost_price) AS ob_store_cost_totals,COUNT(*) AS count')->lock(true)->find();
  398. //取结算时间和退款过期时间的较小者
  399. $temp_time = $order_refund_time;
  400. if ($temp_time > $ob_enddate) {
  401. $temp_time = $ob_enddate;
  402. }
  403. $order_ids = Db::name('order')->where(array(array('store_id', '=', $store_id), array('order_state', '=', ORDER_STATE_SUCCESS), array('delay_time', '<', $temp_time), array('payment_code', '<>', 'offline'), array('ob_no', '=', 0)))->lock(true)->column('order_id');
  404. $vr_order_ids = Db::name('vrorder')->where(array(array('store_id', '=', $store_id), array('order_state', '=', ORDER_STATE_SUCCESS), array('finnshed_time', '<', $temp_time), array('ob_no', '=', 0)))->where('((vr_invalid_refund=0) || (vr_invalid_refund=1 AND vr_indate<' . $vr_order_refund_time . ') || (vr_invalid_refund=1 AND refund_state=2))')->lock(true)->column('order_id');
  405. if ($storecost_sum && $storecost_sum['count']) {
  406. $data['ob_store_cost_totals'] = $storecost_sum['ob_store_cost_totals'];
  407. }
  408. if ($order_ids) {
  409. //查看相关订单是否有未完成的退款
  410. $r_order_ids = Db::name('refundreturn')->where('order_id', 'in', $order_ids)->where('refund_state', 'not in', [3, 4])->lock(true)->column('order_id');
  411. if ($r_order_ids) {
  412. $order_ids = array_diff($order_ids, $r_order_ids);
  413. }
  414. //如果全部为待处理退款的订单,则不生成结算单,等退款完成再处理
  415. if ($order_ids) {
  416. //结算订单金额,结算运费,结算退单金额
  417. $order_sum = Db::name('order')->where('order_id', 'in', $order_ids)->field('SUM(order_amount) AS ob_order_totals,SUM(shipping_fee) AS ob_shipping_totals,SUM(refund_amount) AS ob_order_return_totals')->lock(true)->find();
  418. //结算平台代金券金额
  419. $ob_mall_voucher_totals = Db::name('ordercommon')->where('order_id','in',$order_ids)->field('SUM(mallvoucher_price) AS ob_mall_voucher_totals')->lock(true)->find();
  420. //结算佣金金额
  421. $ordergoods_sum = Db::name('ordergoods')->where('order_id', 'in', $order_ids)->field('SUM(ROUND(goods_pay_price*commis_rate/100,2)) AS ob_commis_totals')->lock(true)->find();
  422. //结算退还佣金,部分退款的
  423. $refundreturn_sum_1 = Db::name('refundreturn')->alias('refundreturn')->join('ordergoods ordergoods', 'refundreturn.order_goods_id = ordergoods.rec_id')->where('refundreturn.order_id', 'in', $order_ids)->where('refundreturn.refund_state', 3)->where('refundreturn.seller_state', 2)->where('refundreturn.order_goods_id', '>', 0)->field('SUM(ROUND(refundreturn.refund_amount*ordergoods.commis_rate/100,2)) AS ob_commis_return_totals')->lock(true)->find();
  424. $refundreturn_sum_2 = array();
  425. $refundreturn_rec_ids_2 = Db::name('refundreturn')->where('order_id', 'in', $order_ids)->where('refund_state', 3)->where('seller_state', 2)->where('order_goods_id', 0)->lock(true)->column('order_id');
  426. if ($refundreturn_rec_ids_2) {
  427. $refundreturn_sum_2 = Db::name('ordergoods')->where('order_id', 'in', $refundreturn_rec_ids_2)->field('SUM(ROUND(goods_pay_price*commis_rate/100,2)) AS ob_commis_return_totals')->lock(true)->find();
  428. }
  429. foreach ($order_ids as $order_id) {
  430. $orderinviter_model->giveMoney($order_id, 0);
  431. }
  432. //分销的佣金
  433. $orderinviter_sum = Db::name('orderinviter')->where('orderinviter_order_id', 'in', $order_ids)->where('orderinviter_valid', 1)->where('orderinviter_order_type', 0)->field('SUM(orderinviter_money) AS ob_inviter_totals')->lock(true)->find();
  434. $data['ob_order_totals'] = $order_sum['ob_order_totals'];
  435. $data['ob_shipping_totals'] = $order_sum['ob_shipping_totals'];
  436. $data['ob_order_return_totals'] = $order_sum['ob_order_return_totals'];
  437. $data['ob_commis_totals'] = $ordergoods_sum['ob_commis_totals'];
  438. $data['ob_commis_return_totals'] = bcadd(((!empty($refundreturn_sum_1)) ? $refundreturn_sum_1['ob_commis_return_totals'] : 0), ((!empty($refundreturn_sum_2)) ? $refundreturn_sum_2['ob_commis_return_totals'] : 0), 2);
  439. $data['ob_inviter_totals'] = (!empty($orderinviter_sum)) ? $orderinviter_sum['ob_inviter_totals'] : 0;
  440. }
  441. }
  442. if ($vr_order_ids) {
  443. foreach ($vr_order_ids as $order_id) {
  444. $orderinviter_model->giveMoney($order_id, 1);
  445. }
  446. //分销的佣金
  447. $orderinviter_sum = Db::name('orderinviter')->where('orderinviter_order_id', 'in', $vr_order_ids)->where('orderinviter_valid', 1)->where('orderinviter_order_type', 1)->field('SUM(orderinviter_money) AS ob_vr_inviter_totals')->lock(true)->find();
  448. $vr_order_sum = Db::name('vrorder')->where('order_id', 'in', $vr_order_ids)->field('SUM(order_amount) AS ob_vr_order_totals,SUM(ROUND(order_amount*commis_rate/100,2)) AS ob_vr_commis_totals,SUM(refund_amount) AS ob_vr_order_return_totals,SUM(ROUND(refund_amount*commis_rate/100,2)) AS ob_vr_commis_return_totals')->lock(true)->find();
  449. $data['ob_vr_order_totals'] = $vr_order_sum['ob_vr_order_totals'];
  450. $data['ob_vr_commis_totals'] = $vr_order_sum['ob_vr_commis_totals'];
  451. $data['ob_vr_order_return_totals'] = $vr_order_sum['ob_vr_order_return_totals'];
  452. $data['ob_vr_commis_return_totals'] = $vr_order_sum['ob_vr_commis_return_totals'];
  453. $data['ob_vr_inviter_totals'] = (!empty($orderinviter_sum)) ? $orderinviter_sum['ob_vr_inviter_totals'] : 0;
  454. }
  455. if (!empty($data)) {
  456. $data['ob_createdate'] = TIMESTAMP;
  457. $data['ob_startdate'] = $ob_startdate;
  458. $data['ob_enddate'] = $ob_enddate;
  459. $data['ob_store_id'] = $store_id;
  460. $data['ob_store_name'] = $store_info['store_name']; //平台自营店铺不需要增加店铺资金
  461. $data['ob_state'] = ($store_info['is_platform_store'] == 1) ? 4 : 1;
  462. $data['ob_mall_voucher_totals'] = 0;
  463. if (!isset($data['ob_order_totals'])) {
  464. $data['ob_order_totals'] = 0;
  465. }
  466. if (!isset($data['ob_store_cost_totals'])) {
  467. $data['ob_store_cost_totals'] = 0;
  468. }
  469. if (!isset($data['ob_inviter_totals'])) {
  470. $data['ob_inviter_totals'] = 0;
  471. }
  472. if (!isset($data['ob_order_return_totals'])) {
  473. $data['ob_order_return_totals'] = 0;
  474. }
  475. if (!isset($data['ob_commis_totals'])) {
  476. $data['ob_commis_totals'] = 0;
  477. }
  478. if (!isset($data['ob_commis_return_totals'])) {
  479. $data['ob_commis_return_totals'] = 0;
  480. }
  481. if (!isset($data['ob_vr_order_totals'])) {
  482. $data['ob_vr_order_totals'] = 0;
  483. }
  484. if (!isset($data['ob_vr_order_return_totals'])) {
  485. $data['ob_vr_order_return_totals'] = 0;
  486. }
  487. if (!isset($data['ob_vr_commis_totals'])) {
  488. $data['ob_vr_commis_totals'] = 0;
  489. }
  490. if (!isset($data['ob_vr_commis_return_totals'])) {
  491. $data['ob_vr_commis_return_totals'] = 0;
  492. }
  493. if (!isset($data['ob_vr_inviter_totals'])) {
  494. $data['ob_vr_inviter_totals'] = 0;
  495. }
  496. if(!isset($ob_mall_voucher_totals) && !isset($vr_order_sum['ob_vr_mall_voucher_totals'])){
  497. $data['ob_mall_voucher_totals'] = 0;
  498. }else{
  499. $data['ob_mall_voucher_totals'] = bcadd($ob_mall_voucher_totals['ob_mall_voucher_totals'], $vr_order_sum['ob_vr_mall_voucher_totals'],2);
  500. }
  501. //计算实际可得金额
  502. $data['ob_result_totals'] = round($data['ob_order_totals'] - $data['ob_store_cost_totals'] - $data['ob_inviter_totals'] - $data['ob_order_return_totals'] - $data['ob_commis_totals'] + $data['ob_commis_return_totals'] + $data['ob_vr_order_totals'] - $data['ob_vr_order_return_totals'] - $data['ob_vr_commis_totals'] + $data['ob_vr_commis_return_totals'] - $data['ob_vr_inviter_totals'] + $data['ob_mall_voucher_totals'], 2);
  503. //插入到结算账单
  504. $ob_no = $bill_model->addOrderbill($data);
  505. if (!$ob_no) {
  506. throw new \think\Exception('新增结算单失败', 10006);
  507. }
  508. if ($order_ids) {
  509. //订单更新结算单号
  510. if (!Db::name('order')->where('order_id', 'in', $order_ids)->update(array('ob_no' => $ob_no))) {
  511. throw new \think\Exception('更新实物订单结算单号失败', 10006);
  512. }
  513. }
  514. if ($vr_order_ids) {
  515. //订单更新结算单号
  516. if (!Db::name('vrorder')->where('order_id', 'in', $vr_order_ids)->update(array('ob_no' => $ob_no))) {
  517. throw new \think\Exception('更新虚拟订单结算单号失败', 10006);
  518. }
  519. }
  520. }
  521. //店铺活动改成已结算
  522. if (!empty($storecost_sum) && $storecost_sum['count'] && !Db::name('storecost')->where(array(array('storecost_store_id', '=', $store_id), array('storecost_state', '=', 0), array('storecost_time', '<', $ob_enddate)))->update(array('storecost_state' => 1))) {
  523. throw new \think\Exception('更新店铺活动结算单号失败', 10006);
  524. }
  525. } else {
  526. $ob_enddate = strtotime(date('Y-m-d 0:0:0')) - intval(config('ds_config.store_bill_cycle')) * 86400;
  527. }
  528. //更新店铺结算时间
  529. if (!Db::name('store')->where('store_id', $store_id)->update(array('store_bill_time' => $ob_enddate))) {
  530. throw new \think\Exception('更新店铺结算时间失败', 10006);
  531. }
  532. } catch (\Exception $e) {
  533. Db::rollback();
  534. $this->log('[店铺名称:' . $store_info['store_name'] . ']' . $e->getMessage());
  535. }
  536. }
  537. Db::commit();
  538. }
  539. //如果还有未更新结算日期的店铺,则重定向继续生产结算单
  540. if (Db::name('store')->where(array(array('store_bill_time', '<', strtotime(date('Y-m-d 0:0:0')) - intval(config('ds_config.store_bill_cycle')) * 86400)))->count() > 0) {
  541. $this->redirect('date/index');
  542. }
  543. }
  544. /**
  545. * 未付款订单超期自动关闭
  546. */
  547. private function _order_timeout_cancel() {
  548. //实物订单超期未支付系统自动关闭
  549. $_break = false;
  550. $chain_order_model = model('chain_order');
  551. $order_model = model('order');
  552. $logic_order = model('order', 'logic');
  553. $condition = array();
  554. $condition[] = array('order_state', '=', ORDER_STATE_NEW);
  555. $condition[] = array('add_time', '<', TIMESTAMP - config('ds_config.order_auto_cancel_day') * self::EXE_TIMES);
  556. //分批,每批处理100个订单,最多处理5W个订单
  557. for ($i = 0; $i < 500; $i++) {
  558. if ($_break) {
  559. break;
  560. }
  561. $order_list = $order_model->getOrderList($condition, '', '*', '', 100);
  562. if (empty($order_list))
  563. break;
  564. foreach ($order_list as $order_info) {
  565. try{
  566. Db::startTrans();
  567. $logic_order->changeOrderStateCancel($order_info, 'system', '系统', '超期未支付系统自动关闭订单', true, false);
  568. } catch (\Exception $e) {
  569. Db::rollback();
  570. $this->log('实物订单超期未支付关闭失败SN:' . $order_info['order_sn']);
  571. $_break = true;
  572. break;
  573. }
  574. Db::commit();
  575. }
  576. }
  577. //虚拟订单超期未支付系统自动关闭
  578. $_break = false;
  579. $vrorder_model = model('vrorder');
  580. $logic_vrorder = model('vrorder', 'logic');
  581. $condition = array();
  582. $condition[] = array('order_state', '=', ORDER_STATE_NEW);
  583. $condition[] = array('add_time', '<', TIMESTAMP - config('ds_config.order_auto_cancel_day') * self::EXE_TIMES);
  584. //分批,每批处理100个订单,最多处理5W个订单
  585. for ($i = 0; $i < 500; $i++) {
  586. if ($_break) {
  587. break;
  588. }
  589. $order_list = $vrorder_model->getVrorderList($condition, '', '*', '', 100);
  590. if (empty($order_list))
  591. break;
  592. foreach ($order_list as $order_info) {
  593. $result = $logic_vrorder->changeOrderStateCancel($order_info, 'system', '超期未支付系统自动关闭订单', false);
  594. }
  595. if (!$result['code']) {
  596. $this->log('虚拟订单超期未支付关闭失败SN:' . $order_info['order_sn']);
  597. $_break = true;
  598. break;
  599. }
  600. }
  601. }
  602. /**
  603. * 订单自动完成
  604. */
  605. private function _order_auto_complete() {
  606. //虚拟订单过使用期自动完成
  607. $_break = false;
  608. $vrorder_model = model('vrorder');
  609. $logic_vrorder = model('vrorder', 'logic');
  610. $condition = array();
  611. $condition[] = array('order_state', '=', ORDER_STATE_PAY);
  612. $condition[] = array('vr_indate', '<', TIMESTAMP);
  613. //分批,每批处理100个订单,最多处理5W个订单
  614. for ($i = 0; $i < 500; $i++) {
  615. if ($_break) {
  616. break;
  617. }
  618. $order_list = $vrorder_model->getVrorderList($condition, '', 'order_id,order_sn', 'vr_indate asc', 100);
  619. if (empty($order_list))
  620. break;
  621. foreach ($order_list as $order_info) {
  622. $result = $logic_vrorder->changeOrderStateSuccess($order_info['order_id']);
  623. if (!$result['code']) {
  624. $this->log('虚拟订单过使用期自动完成失败SN:' . $order_info['order_sn']);
  625. $_break = true;
  626. break;
  627. }
  628. }
  629. }
  630. //实物订单发货后,超期自动收货完成
  631. $_break = false;
  632. $order_model = model('order');
  633. $logic_order = model('order', 'logic');
  634. $condition = array();
  635. $condition[] = array('order_state', '=', ORDER_STATE_SEND);
  636. $condition[] = array('lock_state', '=', 0);
  637. $condition[] = array('delay_time', '<', TIMESTAMP - config('ds_config.order_auto_receive_day') * 86400);
  638. //分批,每批处理100个订单,最多处理5W个订单
  639. for ($i = 0; $i < 500; $i++) {
  640. if ($_break) {
  641. break;
  642. }
  643. $order_list = $order_model->getOrderList($condition, '', '*', 'delay_time asc', 100);
  644. if (empty($order_list))
  645. break;
  646. foreach ($order_list as $order_info) {
  647. $result = $logic_order->changeOrderStateReceive($order_info, 'system', '系统', '超期未收货系统自动完成订单');
  648. if (!$result['code']) {
  649. $this->log('实物订单超期未收货自动完成订单失败SN:' . $order_info['order_sn']);
  650. $_break = true;
  651. break;
  652. }
  653. }
  654. }
  655. }
  656. /**
  657. * 更新订单扩展表中收货人所在省份ID
  658. */
  659. private function _order_reciver_provinceid_update() {
  660. $order_model = model('order');
  661. $area_model = model('area');
  662. //每次最多处理5W个订单
  663. $condition = array();
  664. $condition[] = array('reciver_province_id', '=', 0);
  665. $condition[] = array('reciver_city_id', '<>', 0);
  666. for ($i = 0; $i < 500; $i++) {
  667. $order_list = $order_model->getOrdercommonList($condition, 'reciver_city_id', 'order_id desc', 100);
  668. if (!empty($order_list)) {
  669. $city_ids = array();
  670. foreach ($order_list as $v) {
  671. if (!in_array($v['reciver_city_id'], $city_ids)) {
  672. $city_ids[] = $v['reciver_city_id'];
  673. }
  674. }
  675. $area_list = $area_model->getAreaList(array(array('area_id', 'in', $city_ids)), 'area_parent_id,area_id');
  676. if (!empty($area_list)) {
  677. foreach ($area_list as $v) {
  678. $update = $order_model->editOrdercommon(array('reciver_province_id' => $v['area_parent_id']), array('reciver_city_id' => $v['area_id']));
  679. if (!$update) {
  680. $this->log('更新订单扩展表中收货人所在省份ID失败');
  681. break;
  682. }
  683. }
  684. }
  685. } else {
  686. break;
  687. }
  688. }
  689. }
  690. /**
  691. * 增加会员积分和经验值
  692. */
  693. private function _add_points() {
  694. return;
  695. $points_model = model('points');
  696. $exppoints_model = model('exppoints');
  697. //24小时之内登录的会员送积分和经验值,每次最多处理5W个会员
  698. $member_model = model('member');
  699. $condition = array();
  700. $condition[] = array('member_logintime', '>', TIMESTAMP - self::EXE_TIMES);
  701. for ($i = 0; $i < 50000; $i = $i + 100) {
  702. $member_list = $member_model->getMemberList($condition, 'member_name,member_id', 0, '', "{$i},100");
  703. if (!empty($member_list)) {
  704. foreach ($member_list as $member_info) {
  705. if (config('ds_config.points_isuse')) {
  706. $points_model->savePointslog('login', array('pl_memberid' => $member_info['member_id'], 'pl_membername' => $member_info['member_name']), true);
  707. }
  708. $exppoints_model->saveExppointslog('login', array('explog_memberid' => $member_info['member_id'], 'explog_membername' => $member_info['member_name']), true);
  709. }
  710. } else {
  711. break;
  712. }
  713. }
  714. //24小时之内注册的会员送积分,每次最多处理5W个会员
  715. if (config('ds_config.points_isuse')) {
  716. $condition = array();
  717. $condition[] = array('member_addtime', '>', TIMESTAMP - self::EXE_TIMES);
  718. for ($i = 0; $i < 50000; $i = $i + 100) {
  719. $member_list = $member_model->getMemberList($condition, 'member_name,member_id', 0, 'member_id desc', "{$i},100");
  720. if (!empty($member_list)) {
  721. foreach ($member_list as $member_info) {
  722. $points_model->savePointslog('regist', array('pl_memberid' => $member_info['member_id'], 'pl_membername' => $member_info['member_name']), true);
  723. }
  724. } else {
  725. break;
  726. }
  727. }
  728. }
  729. //24小时之内完成了实物订单送积分和经验值,每次最多处理5W个订单
  730. $order_model = model('order');
  731. $condition = array();
  732. $condition[] = array('finnshed_time', '>', TIMESTAMP - self::EXE_TIMES);
  733. for ($i = 0; $i < 50000; $i = $i + 100) {
  734. $order_list = $order_model->getOrderList($condition, '', 'buyer_name,buyer_id,order_amount,order_sn,order_id', '', "{$i},100");
  735. if (!empty($order_list)) {
  736. foreach ($order_list as $order_info) {
  737. if (config('ds_config.points_isuse')) {
  738. $points_model->savePointslog('order', array('pl_memberid' => $order_info['buyer_id'], 'pl_membername' => $order_info['buyer_name'], 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'], 'order_id' => $order_info['order_id']), true);
  739. }
  740. $exppoints_model->saveExppointslog('order', array('explog_memberid' => $order_info['buyer_id'], 'explog_membername' => $order_info['buyer_name'], 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'], 'order_id' => $order_info['order_id']), true);
  741. }
  742. } else {
  743. break;
  744. }
  745. }
  746. //24小时之内完成了实物订单送积分和经验值,每次最多处理5W个订单
  747. $vrorder_model = model('vrorder');
  748. $condition = array();
  749. $condition[] = array('finnshed_time', '>', TIMESTAMP - self::EXE_TIMES);
  750. for ($i = 0; $i < 50000; $i = $i + 100) {
  751. $order_list = $vrorder_model->getVrorderList($condition, '', 'buyer_name,buyer_id,order_amount,order_sn,order_id', '', "{$i},100");
  752. if (!empty($order_list)) {
  753. foreach ($order_list as $order_info) {
  754. if (config('ds_config.points_isuse')) {
  755. $points_model->savePointslog('order', array('pl_memberid' => $order_info['buyer_id'], 'pl_membername' => $order_info['buyer_name'], 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'], 'order_id' => $order_info['order_id']), true);
  756. }
  757. $exppoints_model->saveExppointslog('order', array('explog_memberid' => $order_info['buyer_id'], 'explog_membername' => $order_info['buyer_name'], 'orderprice' => $order_info['order_amount'], 'order_sn' => $order_info['order_sn'], 'order_id' => $order_info['order_id']), true);
  758. }
  759. } else {
  760. break;
  761. }
  762. }
  763. }
  764. /**
  765. * 代金券即将过期提醒
  766. */
  767. private function _voucher_will_expire() {
  768. $time_start = mktime(0, 0, 0, date("m"), date("d") + self::VOUCHER_INTERVAL, date("Y"));
  769. $time_stop = $time_start + self::EXE_TIMES - 1;
  770. $where = array();
  771. $where[] = array('voucher_enddate', '>=', $time_start);
  772. $where[] = array('voucher_enddate', '<=', $time_stop);
  773. $list = model('voucher')->getVoucherUnusedList($where);
  774. if (!empty($list)) {
  775. foreach ($list as $val) {
  776. $param = array();
  777. $param['code'] = 'voucher_will_expire';
  778. $param['member_id'] = $val['voucher_owner_id'];
  779. $param['ali_param'] = array(
  780. 'indate' => date('Y-m-d H:i:s', $val['voucher_enddate']),
  781. );
  782. $param['ten_param'] = array(
  783. date('Y-m-d H:i:s', $val['voucher_enddate']),
  784. );
  785. $param['param'] = array_merge($param['ali_param'], array(
  786. 'voucher_url' => HOME_SITE_URL .'/Membervoucher/index'
  787. ));
  788. $param['weixin_param'] = array(
  789. 'url' => config('ds_config.h5_site_url') . '/pages/member/voucher/VoucherList',
  790. 'data' => array(
  791. "keyword1" => array(
  792. "value" => $val['voucher_code'],
  793. "color" => "#333"
  794. ),
  795. "keyword2" => array(
  796. "value" => date('Y-m-d', $val['voucher_startdate']) . '~' . date('Y-m-d', $val['voucher_enddate']),
  797. "color" => "#333"
  798. )
  799. ),
  800. );
  801. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'sendMemberMsg','cron_value'=>serialize($param)));
  802. }
  803. }
  804. }
  805. /**
  806. * 虚拟兑换码即将过期提醒
  807. */
  808. private function _vr_code_will_expire() {
  809. $time_start = mktime(0, 0, 0, date("m"), date("d") + self::VR_CODE_INTERVAL, date("Y"));
  810. $time_stop = $time_start + self::EXE_TIMES - 1;
  811. $where = array();
  812. $where[] = array('vr_indate', '>=', $time_start);
  813. $where[] = array('vr_indate', '<=', $time_stop);
  814. $list = model('vrorder')->getCodeUnusedList($where);
  815. if (!empty($list)) {
  816. foreach ($list as $val) {
  817. $param = array();
  818. $param['code'] = 'vr_code_will_expire';
  819. $param['member_id'] = $val['buyer_id'];
  820. $param['ali_param'] = array(
  821. 'indate' => date('Y-m-d H:i:s', $val['vr_indate']),
  822. );
  823. $param['ten_param'] = array(
  824. date('Y-m-d H:i:s', $val['vr_indate']),
  825. );
  826. $param['param'] = array_merge($param['ali_param'], array(
  827. 'vr_order_url' => HOME_SITE_URL .'/Membervrorder/index'
  828. ));
  829. $vrorder = model('vrorder')->getVrorderInfo(array('order_id' => $val['order_id']));
  830. $param['weixin_param'] = array(
  831. 'url' => config('ds_config.h5_site_url') . '/pages/member/vrorder/OrderDetail?order_id=' . $val['order_id'],
  832. 'data' => array(
  833. "keyword1" => array(
  834. "value" => (!empty($vrorder)) ? $vrorder['goods_name'] : '',
  835. "color" => "#333"
  836. ),
  837. "keyword2" => array(
  838. "value" => date('Y-m-d', $val['vr_indate']),
  839. "color" => "#333"
  840. )
  841. ),
  842. );
  843. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'sendMemberMsg','cron_value'=>serialize($param)));
  844. }
  845. }
  846. }
  847. /**
  848. * 订单超期后不允许评价
  849. */
  850. private function _order_eval_expire_update() {
  851. //实物订单超期未评价自动更新状态,每次最多更新1000个订单
  852. $order_model = model('order');
  853. $condition = array();
  854. $condition[] = array('order_state', '=', ORDER_STATE_SUCCESS);
  855. $condition[] = array('evaluation_state', '=', 0);
  856. $condition[] = array('finnshed_time', '<', TIMESTAMP - self::ORDER_EVALUATE_TIME);
  857. $update = array();
  858. $update['evaluation_state'] = 2;
  859. $update = $order_model->editOrder($update, $condition, 1000);
  860. if (!$update) {
  861. $this->log('更新实物订单超期不能评价失败');
  862. }
  863. //虚拟订单超期未评价自动更新状态,每次最多更新1000个订单
  864. $vrorder_model = model('vrorder');
  865. $condition = array();
  866. $condition[] = array('order_state', '=', ORDER_STATE_SUCCESS);
  867. $condition[] = array('evaluation_state', '=', 0);
  868. $condition[] = array('use_state', '=', 1);
  869. $condition[] = array('finnshed_time', '<', TIMESTAMP - self::ORDER_EVALUATE_TIME);
  870. $update = array();
  871. $update['evaluation_state'] = 2;
  872. $update = $vrorder_model->editVrorder($update, $condition, 1000);
  873. if (!$update) {
  874. $this->log('更新虚拟订单超期不能评价失败');
  875. }
  876. }
  877. /**
  878. * 更新商品访问量(redis)
  879. */
  880. private function _goods_click_update() {
  881. $data = rcache('updateRedisDate', 'goodsClick');
  882. if (is_array($data)) {
  883. foreach ($data as $key => $val) {
  884. model('goods')->editGoodsById(array('goods_click' => Db::raw('goods_click+' . $val)), $key);
  885. }
  886. }
  887. dcache('updateRedisDate', 'goodsClick');
  888. }
  889. /**
  890. * 更新商品促销到期状态(目前只有满即送)
  891. */
  892. private function _goods_promotion_state_update() {
  893. //满即送过期
  894. model('pmansong')->editExpireMansong();
  895. }
  896. /**
  897. * 商品到货通知提醒
  898. */
  899. private function _arrival_notice() {
  900. $arrivalnotice_model = model('arrivalnotice');
  901. $count = $arrivalnotice_model->getArrivalNoticeCount(array());
  902. $times = ceil($count / self::ARRIVAL_NOTICE_NUM);
  903. if ($times == 0)
  904. return false;
  905. for ($i = 0; $i <= $times; $i++) {
  906. $notice_list = $arrivalnotice_model->getArrivalNoticeList(array(), '*', $i . ',' . self::ARRIVAL_NOTICE_NUM);
  907. if (empty($notice_list))
  908. continue;
  909. // 查询商品是否已经上架
  910. $goodsid_array = array();
  911. foreach ($notice_list as $val) {
  912. $goodsid_array[] = $val['goods_id'];
  913. }
  914. $goodsid_array = array_unique($goodsid_array);
  915. $condition = array();
  916. $condition[] = array('goods_id', 'in', $goodsid_array);
  917. $condition[] = array('goods_storage', '>', 0);
  918. $goods_list = model('goods')->getGoodsOnlineList($condition, 'goods_id');
  919. if (empty($goods_list))
  920. continue;
  921. // 需要通知到货的商品
  922. $goodsid_array = array();
  923. $storage_array = array();
  924. foreach ($goods_list as $val) {
  925. $goodsid_array[] = $val['goods_id'];
  926. $storage_array[$val['goods_id']] = $val['goods_storage'];
  927. }
  928. // 根据商品id重新查询需要通知的列表
  929. $notice_list = $arrivalnotice_model->getArrivalNoticeList(array(array('goods_id', 'in', $goodsid_array)), '*');
  930. if (empty($notice_list))
  931. continue;
  932. foreach ($notice_list as $val) {
  933. $param = array();
  934. $param['code'] = 'arrival_notice';
  935. $param['member_id'] = $val['member_id'];
  936. $param['ali_param'] = array(
  937. 'goods_name' => $val['goods_name'],
  938. );
  939. $param['ten_param'] = array(
  940. $val['goods_name'],
  941. );
  942. $param['param'] = array_merge(array(
  943. 'goods_name' => $val['goods_name'],
  944. 'goods_url' => HOME_SITE_URL .'/Goods/index?goods_id='.$val['goods_id']
  945. ));
  946. $param['number'] = array('mobile' => $val['arrivalnotice_mobile'], 'email' => $val['arrivalnotice_email']);
  947. $param['weixin_param'] = array(
  948. 'url' => config('ds_config.h5_site_url') . '/pages/home/goodsdetail/Goodsdetail?goods_id=' . $val['goods_id'],
  949. 'data' => array(
  950. "keyword1" => array(
  951. "value" => $val['goods_name'],
  952. "color" => "#333"
  953. ),
  954. "keyword2" => array(
  955. "value" => isset($storage_array[$val['goods_id']]) ? $storage_array[$val['goods_id']] : '99',
  956. "color" => "#333"
  957. ),
  958. "keyword3" => array(
  959. "value" => date('Y-m-d'),
  960. "color" => "#333"
  961. )
  962. ),
  963. );
  964. model('cron')->addCron(array('cron_exetime'=>TIMESTAMP,'cron_type'=>'sendMemberMsg','cron_value'=>serialize($param)));
  965. }
  966. // 清楚发送成功的数据
  967. $arrivalnotice_model->editArrivalNotice(['arrivalnotice_state' => 2, 'arrivalnotice_time' => TIMESTAMP], array(array('goods_id', 'in', $goodsid_array)));
  968. }
  969. }
  970. /**
  971. * 将缓存中的浏览记录存入数据库中,并删除30天前的浏览历史
  972. */
  973. private function _goods_browse_update() {
  974. $goodsbrowse_model = model('goodsbrowse');
  975. //将cache中的记录存入数据库
  976. //如果浏览记录已经存入了缓存中,则将其整理到数据库中
  977. //上次更新缓存的时间
  978. $latest_record = $goodsbrowse_model->getOneGoodsbrowse(array(), '', 'goodsbrowse_time desc');
  979. $starttime = ($t = intval($latest_record['goodsbrowse_time'])) ? $t : 0;
  980. $monthago = strtotime(date('Y-m-d', TIMESTAMP)) - 86400 * 30;
  981. $member_model = model('member');
  982. //查询会员信息总条数
  983. $countnum = $member_model->getMemberCount(array());
  984. $eachnum = 100;
  985. for ($i = 0; $i < $countnum; $i += $eachnum) {//每次查询100条
  986. $member_list = $member_model->getMemberList(array(), '*', 0, 'member_id asc', "$i,$eachnum");
  987. foreach ((array) $member_list as $k => $v) {
  988. $insert_arr = array();
  989. $goodsid_arr = array();
  990. //生成缓存的键值
  991. $hash_key = $v['member_id'];
  992. $browse_goodsid = rcache($hash_key, 'goodsbrowse');
  993. if ($browse_goodsid) {
  994. //删除缓存中多余的浏览历史记录,仅保留最近的30条浏览历史,先取出最近30条浏览历史的商品ID
  995. $cachegoodsid_arr = $browse_goodsid['goodsid'] ? unserialize($browse_goodsid['goodsid']) : array();
  996. unset($browse_goodsid['goodsid']);
  997. if ($cachegoodsid_arr) {
  998. $cachegoodsid_arr = array_slice($cachegoodsid_arr, -30, 30, true);
  999. }
  1000. //处理存入数据库的浏览历史缓存信息
  1001. $_cache = rcache($hash_key, 'goodsbrowse');
  1002. foreach ((array) $_cache as $c_k => $c_v) {
  1003. $c_v = unserialize($c_v);
  1004. if (isset($c_v['goodsbrowse_time']) && $c_v['goodsbrowse_time'] >= $starttime) {//如果 缓存中的数据未更新到数据库中(即添加时间大于上次更新到数据库中的数据时间)则将数据更新到数据库中
  1005. $tmp_arr = array();
  1006. $tmp_arr['goods_id'] = $c_v['goods_id'];
  1007. $tmp_arr['member_id'] = $v['member_id'];
  1008. $tmp_arr['goodsbrowse_time'] = $c_v['goodsbrowse_time'];
  1009. $tmp_arr['gc_id'] = $c_v['gc_id'];
  1010. $tmp_arr['gc_id_1'] = $c_v['gc_id_1'];
  1011. $tmp_arr['gc_id_2'] = $c_v['gc_id_2'];
  1012. $tmp_arr['gc_id_3'] = $c_v['gc_id_3'];
  1013. $insert_arr[] = $tmp_arr;
  1014. $goodsid_arr[] = $c_v['goods_id'];
  1015. }
  1016. //除了最近的30条浏览历史之外多余的浏览历史记录或者30天之前的浏览历史从缓存中删除
  1017. if (!in_array($c_v['goods_id'], $cachegoodsid_arr) || $c_v['goodsbrowse_time'] < $monthago) {
  1018. unset($_cache[$c_k]);
  1019. }
  1020. }
  1021. //删除已经存在的该商品浏览记录
  1022. if ($goodsid_arr) {
  1023. $condition = array();
  1024. $condition[] = array('member_id', '=', $v['member_id']);
  1025. $condition[] = array('goods_id', 'in', $goodsid_arr);
  1026. $goodsbrowse_model->delGoodsbrowse($condition);
  1027. }
  1028. //将缓存中的浏览历史存入数据库
  1029. if ($insert_arr) {
  1030. $goodsbrowse_model->addGoodsbrowseAll($insert_arr);
  1031. }
  1032. //重新赋值浏览历史缓存
  1033. dcache($hash_key, 'goodsbrowse');
  1034. $_cache['goodsid'] = serialize($cachegoodsid_arr);
  1035. wcache($hash_key, $_cache, 'goodsbrowse');
  1036. }
  1037. }
  1038. }
  1039. //删除30天前的浏览历史
  1040. $goodsbrowse_model->delGoodsbrowse(array(array('goodsbrowse_time', '<', $monthago)));
  1041. }
  1042. /**
  1043. * 缓存订单及订单商品相关数据
  1044. */
  1045. private function _order_goods_cache() {
  1046. //查询最后统计的记录
  1047. $latest_record = Db::name('statordergoods')->order('stat_updatetime desc,rec_id desc')->find();
  1048. $stime = 0;
  1049. if ($latest_record) {
  1050. $start_time = strtotime(date('Y-m-d', $latest_record['stat_updatetime']));
  1051. } else {
  1052. $start_time = strtotime(date('Y-m-d', strtotime(config('ds_config.setup_date')))); //从系统的安装时间开始统计
  1053. }
  1054. for ($stime = $start_time; $stime < TIMESTAMP; $stime = $stime + 86400) {
  1055. $etime = $stime + 86400 - 1;
  1056. //避免重复统计,开始时间必须大于最后一条记录的记录时间
  1057. $search_stime = $latest_record['stat_updatetime'] > $stime ? $latest_record['stat_updatetime'] : $stime;
  1058. //统计一天的数据,如果结束时间大于当前时间,则结束时间为当前时间,避免因为查询时间的延迟造成数据遗落
  1059. $search_etime = ($t = ($stime + 86400 - 1)) > TIMESTAMP ? TIMESTAMP : ($stime + 86400 - 1);
  1060. //查询时间段内新订单或者更新过的订单,在缓存表中需要将新订单和更新过的订单进行重新缓存
  1061. $where = array();
  1062. $where[] = array('log_time', 'between', array($search_stime, $search_etime));
  1063. //查询记录总条数
  1064. $countnum_arr = Db::name('orderlog')->field('COUNT(DISTINCT order_id) as countnum')->where($where)->find();
  1065. $countnum = intval($countnum_arr['countnum']);
  1066. for ($i = 0; $i < $countnum; $i += 100) {//每次查询100条
  1067. $orderlog_list = array();
  1068. $orderlog_list = Db::name('orderlog')->field('DISTINCT order_id')->where($where)->limit($i . ',100')->select()->toArray();
  1069. if ($orderlog_list) {
  1070. //店铺ID数组
  1071. $storeid_arr = array();
  1072. //商品ID数组
  1073. $goodsid_arr = array();
  1074. //商品公共表ID数组
  1075. $goods_commonid_arr = array();
  1076. //订单ID数组
  1077. $orderid_arr = array();
  1078. //整理需要缓存的订单ID
  1079. foreach ((array) $orderlog_list as $k => $v) {
  1080. $orderid_arr[] = $v['order_id'];
  1081. }
  1082. unset($orderlog_list);
  1083. //查询订单数据
  1084. $field = 'order_id,order_sn,store_id,buyer_id,buyer_name,add_time,payment_code,order_amount,shipping_fee,evaluation_state,order_state,refund_state,refund_amount,order_from';
  1085. $order_list_tmp = Db::name('order')->field($field)->where('order_id', 'in', $orderid_arr)->select()->toArray();
  1086. $order_list = array();
  1087. foreach ((array) $order_list_tmp as $k => $v) {
  1088. //判读订单是否计入统计(在线支付订单已支付或者经过退款的取消订单或者货到付款订单订单已成功)
  1089. $v['order_isvalid'] = 0;
  1090. if ($v['payment_code'] != 'offline' && $v['order_state'] != ORDER_STATE_NEW && $v['order_state'] != ORDER_STATE_CANCEL) {//在线支付并且已支付并且未取消
  1091. $v['order_isvalid'] = 1;
  1092. } elseif ($v['order_state'] == ORDER_STATE_CANCEL && $v['refund_state'] != 0) {//经过退款的取消订单
  1093. $v['order_isvalid'] = 1;
  1094. } elseif ($v['payment_code'] == 'offline' && $v['order_state'] == ORDER_STATE_SUCCESS) {//货到付款订单,订单成功之后才计入统计
  1095. $v['order_isvalid'] = 1;
  1096. }
  1097. $order_list[$v['order_id']] = $v;
  1098. $storeid_arr[] = $v['store_id'];
  1099. }
  1100. unset($order_list_tmp);
  1101. //查询订单扩展数据
  1102. $field = 'order_id,reciver_province_id';
  1103. $order_common_list_tmp = Db::name('ordercommon')->field($field)->where('order_id', 'in', $orderid_arr)->select()->toArray();
  1104. $order_common_list = array();
  1105. foreach ((array) $order_common_list_tmp as $k => $v) {
  1106. $order_common_list[$v['order_id']] = $v;
  1107. }
  1108. unset($order_common_list_tmp);
  1109. //查询店铺信息
  1110. $field = 'store_id,store_name,grade_id,storeclass_id';
  1111. $store_list_tmp = Db::name('store')->field($field)->where('store_id', 'in', $storeid_arr)->select()->toArray();
  1112. $store_list = array();
  1113. foreach ((array) $store_list_tmp as $k => $v) {
  1114. $store_list[$v['store_id']] = $v;
  1115. }
  1116. unset($store_list_tmp);
  1117. //查询订单商品
  1118. $field = 'rec_id,order_id,goods_id,goods_name,goods_price,goods_num,goods_image,goods_pay_price,store_id,buyer_id,goods_type,promotions_id,commis_rate,gc_id';
  1119. $ordergoods_list = Db::name('ordergoods')->field($field)->where('order_id', 'in', $orderid_arr)->select()->toArray();
  1120. foreach ((array) $ordergoods_list as $k => $v) {
  1121. $goodsid_arr[] = $v['goods_id'];
  1122. }
  1123. //查询商品信息
  1124. $field = 'goods_id,goods_commonid,goods_price,goods_serial,gc_id,gc_id_1,gc_id_2,gc_id_3,goods_image';
  1125. $goods_list_tmp = Db::name('goods')->field($field)->where('goods_id', 'in', $goodsid_arr)->select()->toArray();
  1126. foreach ((array) $goods_list_tmp as $k => $v) {
  1127. $goods_commonid_arr[] = $v['goods_commonid'];
  1128. }
  1129. //查询商品公共信息
  1130. $field = 'goods_commonid,goods_name,brand_id,brand_name';
  1131. $goods_common_list_tmp = Db::name('goodscommon')->field($field)->where('goods_commonid', 'in', $goods_commonid_arr)->select()->toArray();
  1132. $goods_common_list = array();
  1133. foreach ((array) $goods_common_list_tmp as $k => $v) {
  1134. $goods_common_list[$v['goods_commonid']] = $v;
  1135. }
  1136. unset($goods_common_list_tmp);
  1137. //处理商品数组
  1138. $goods_list = array();
  1139. foreach ((array) $goods_list_tmp as $k => $v) {
  1140. $v['goods_commonname'] = $goods_common_list[$v['goods_commonid']]['goods_name'];
  1141. $v['brand_id'] = $goods_common_list[$v['goods_commonid']]['brand_id'];
  1142. $v['brand_name'] = $goods_common_list[$v['goods_commonid']]['brand_name'];
  1143. $goods_list[$v['goods_id']] = $v;
  1144. }
  1145. unset($goods_list_tmp);
  1146. //查询订单缓存是否存在,存在则删除
  1147. Db::name('statordergoods')->where('order_id', 'in', $orderid_arr)->delete();
  1148. //查询订单缓存是否存在,存在则删除
  1149. Db::name('statorder')->where('order_id', 'in', $orderid_arr)->delete();
  1150. //整理新增数据
  1151. $ordergoods_insert_arr = array();
  1152. foreach ((array) $ordergoods_list as $k => $v) {
  1153. $tmp = array();
  1154. $tmp['rec_id'] = $v['rec_id'];
  1155. $tmp['stat_updatetime'] = $search_etime;
  1156. $tmp['order_id'] = $v['order_id'];
  1157. $tmp['order_sn'] = $order_list[$v['order_id']]['order_sn'];
  1158. $tmp['order_add_time'] = $order_list[$v['order_id']]['add_time'];
  1159. $tmp['payment_code'] = $order_list[$v['order_id']]['payment_code'];
  1160. $tmp['order_amount'] = $order_list[$v['order_id']]['order_amount'];
  1161. $tmp['shipping_fee'] = $order_list[$v['order_id']]['shipping_fee'];
  1162. $tmp['evaluation_state'] = (string) $order_list[$v['order_id']]['evaluation_state'];
  1163. $tmp['order_state'] = (string) $order_list[$v['order_id']]['order_state'];
  1164. $tmp['refund_state'] = $order_list[$v['order_id']]['refund_state'];
  1165. $tmp['refund_amount'] = $order_list[$v['order_id']]['refund_amount'];
  1166. $tmp['order_from'] = $order_list[$v['order_id']]['order_from'];
  1167. $tmp['order_isvalid'] = $order_list[$v['order_id']]['order_isvalid'];
  1168. $tmp['reciver_province_id'] = $order_common_list[$v['order_id']]['reciver_province_id'];
  1169. $tmp['store_id'] = $v['store_id'];
  1170. $tmp['store_name'] = $store_list[$v['store_id']]['store_name'];
  1171. $tmp['grade_id'] = $store_list[$v['store_id']]['grade_id'];
  1172. $tmp['storeclass_id'] = $store_list[$v['store_id']]['storeclass_id'];
  1173. $tmp['buyer_id'] = $order_list[$v['order_id']]['buyer_id'];
  1174. $tmp['buyer_name'] = $order_list[$v['order_id']]['buyer_name'];
  1175. $tmp['goods_id'] = $v['goods_id'];
  1176. $tmp['goods_name'] = $v['goods_name'];
  1177. $tmp['goods_commonid'] = intval($goods_list[$v['goods_id']]['goods_commonid']);
  1178. $tmp['goods_commonname'] = ($t = $goods_list[$v['goods_id']]['goods_commonname']) ? $t : '';
  1179. $tmp['gc_id'] = intval($goods_list[$v['goods_id']]['gc_id']);
  1180. $tmp['gc_parentid_1'] = intval($goods_list[$v['goods_id']]['gc_id_1']);
  1181. $tmp['gc_parentid_2'] = intval($goods_list[$v['goods_id']]['gc_id_2']);
  1182. $tmp['gc_parentid_3'] = intval($goods_list[$v['goods_id']]['gc_id_3']);
  1183. $tmp['brand_id'] = intval($goods_list[$v['goods_id']]['brand_id']);
  1184. $tmp['brand_name'] = ($t = $goods_list[$v['goods_id']]['brand_name']) ? $t : '';
  1185. $tmp['goods_serial'] = ($t = $goods_list[$v['goods_id']]['goods_serial']) ? $t : '';
  1186. $tmp['goods_price'] = $v['goods_price'];
  1187. $tmp['goods_num'] = $v['goods_num'];
  1188. $tmp['goods_image'] = $goods_list[$v['goods_id']]['goods_image'];
  1189. $tmp['goods_pay_price'] = $v['goods_pay_price'];
  1190. $tmp['goods_type'] = $v['goods_type'];
  1191. $tmp['promotions_id'] = $v['promotions_id'];
  1192. $tmp['commis_rate'] = $v['commis_rate'];
  1193. $ordergoods_insert_arr[] = $tmp;
  1194. }
  1195. Db::name('statordergoods')->insertAll($ordergoods_insert_arr);
  1196. $order_insert_arr = array();
  1197. foreach ((array) $order_list as $k => $v) {
  1198. $tmp = array();
  1199. $tmp['order_id'] = $v['order_id'];
  1200. $tmp['order_sn'] = $v['order_sn'];
  1201. $tmp['order_add_time'] = $v['add_time'];
  1202. $tmp['payment_code'] = $v['payment_code'];
  1203. $tmp['order_amount'] = $v['order_amount'];
  1204. $tmp['shipping_fee'] = $v['shipping_fee'];
  1205. $tmp['evaluation_state'] = (string) $v['evaluation_state'];
  1206. $tmp['order_state'] = (string) $v['order_state'];
  1207. $tmp['refund_state'] = $v['refund_state'];
  1208. $tmp['refund_amount'] = $v['refund_amount'];
  1209. $tmp['order_from'] = $v['order_from'];
  1210. $tmp['order_isvalid'] = $v['order_isvalid'];
  1211. $tmp['reciver_province_id'] = $order_common_list[$v['order_id']]['reciver_province_id'];
  1212. $tmp['store_id'] = $v['store_id'];
  1213. $tmp['store_name'] = $store_list[$v['store_id']]['store_name'];
  1214. $tmp['grade_id'] = $store_list[$v['store_id']]['grade_id'];
  1215. $tmp['storeclass_id'] = $store_list[$v['store_id']]['storeclass_id'];
  1216. $tmp['buyer_id'] = $v['buyer_id'];
  1217. $tmp['buyer_name'] = $v['buyer_name'];
  1218. $order_insert_arr[] = $tmp;
  1219. }
  1220. Db::name('statorder')->insertAll($order_insert_arr);
  1221. }
  1222. }
  1223. }
  1224. }
  1225. /**
  1226. * 会员相关数据统计
  1227. */
  1228. private function _member_stat() {
  1229. $stat_model = model('stat');
  1230. //查询最后统计的记录
  1231. $latest_record = $stat_model->getOneStatmember(array(), '', 'statm_id desc');
  1232. $stime = 0;
  1233. if ($latest_record) {
  1234. $start_time = strtotime(date('Y-m-d', $latest_record['statm_updatetime']));
  1235. } else {
  1236. $start_time = strtotime(date('Y-m-d', strtotime(config('ds_config.setup_date')))); //从系统的安装时间开始统计
  1237. }
  1238. $j = 1;
  1239. for ($stime = $start_time; $stime < TIMESTAMP; $stime = $stime + 86400) {
  1240. //数据库更新数据数组
  1241. $insert_arr = array();
  1242. $update_arr = array();
  1243. $etime = $stime + 86400 - 1;
  1244. //避免重复统计,开始时间必须大于最后一条记录的记录时间
  1245. $search_stime = $latest_record['statm_updatetime'] > $stime ? $latest_record['statm_updatetime'] : $stime;
  1246. //统计一天的数据,如果结束时间大于当前时间,则结束时间为当前时间,避免因为查询时间的延迟造成数据遗落
  1247. $search_etime = ($t = ($stime + 86400 - 1)) > TIMESTAMP ? TIMESTAMP : ($stime + 86400 - 1);
  1248. //统计订单下单量和下单金额
  1249. $field = ' order.order_id,add_time,buyer_id,buyer_name,order_amount';
  1250. $where = array();
  1251. $where[] = array('order.order_state', '<>', ORDER_STATE_NEW); //去除未支付订单
  1252. $where[] = array('order.refund_state', '<>', "0"); //没有参与退款的取消订单,不记录到统计中
  1253. $where[] = array('orderlog.log_time', 'between', array($search_stime, $search_etime)); //按照订单付款的操作时间统计
  1254. //货到付款当交易成功进入统计,非货到付款当付款后进入统计
  1255. $where = "(order.payment_code='offline' and orderlog.log_orderstate = '" . ORDER_STATE_SUCCESS . "') or (order.payment_code<>'offline' and orderlog.log_orderstate = '" . ORDER_STATE_PAY . "' )";
  1256. $orderlist_tmp = $stat_model->statByOrderLog($where, $field, 0, 0, 'order_id'); //此处由于底层的限制,仅能查询1000条,如果日下单量大于1000,则需要limit的支持
  1257. $order_list = array();
  1258. $orderid_list = array();
  1259. foreach ((array) $orderlist_tmp as $k => $v) {
  1260. $addtime = strtotime(date('Y-m-d', $v['add_time']));
  1261. if ($addtime != $stime) {//订单如果隔天支付的话,需要进行统计数据更新
  1262. $update_arr[$addtime][$v['buyer_id']]['statm_membername'] = $v['buyer_name'];
  1263. $update_arr[$addtime][$v['buyer_id']]['statm_ordernum'] = intval($update_arr[$addtime][$v['buyer_id']]['statm_ordernum']) + 1;
  1264. $update_arr[$addtime][$v['buyer_id']]['statm_orderamount'] = floatval($update_arr[$addtime][$v['buyer_id']]['statm_orderamount']) + (($t = floatval($v['order_amount'])) > 0 ? $t : 0);
  1265. } else {
  1266. $order_list[$v['buyer_id']]['buyer_name'] = $v['buyer_name'];
  1267. $order_list[$v['buyer_id']]['ordernum'] = intval($order_list[$v['buyer_id']]['ordernum']) + 1;
  1268. $order_list[$v['buyer_id']]['orderamount'] = floatval($order_list[$v['buyer_id']]['orderamount']) + (($t = floatval($v['order_amount'])) > 0 ? $t : 0);
  1269. }
  1270. //记录订单ID数组
  1271. $orderid_list[] = $v['order_id'];
  1272. }
  1273. //统计下单商品件数
  1274. if ($orderid_list) {
  1275. $field = ' add_time,order.buyer_id,order.buyer_name,goods_num ';
  1276. $where = array();
  1277. $where[] = array('order.order_id', 'in', $orderid_list);
  1278. $ordergoods_tmp = $stat_model->statByOrderGoods($where, $field, 0, 0, 'order.order_id');
  1279. $ordergoods_list = array();
  1280. foreach ((array) $ordergoods_tmp as $k => $v) {
  1281. $addtime = strtotime(date('Y-m-d', $v['add_time']));
  1282. if ($addtime != $stime) {//订单如果隔天支付的话,需要进行统计数据更新
  1283. $update_arr[$addtime][$v['buyer_id']]['statm_goodsnum'] = intval($update_arr[$addtime][$v['buyer_id']]['statm_goodsnum']) + (($t = floatval($v['goods_num'])) > 0 ? $t : 0);
  1284. } else {
  1285. $ordergoods_list[$v['buyer_id']]['goodsnum'] = $ordergoods_list[$v['buyer_id']]['goodsnum'] + (($t = floatval($v['goods_num'])) > 0 ? $t : 0);
  1286. }
  1287. }
  1288. }
  1289. //统计的预存款记录
  1290. $field = ' lg_member_id,lg_member_name,lg_av_amount as predincrease, lg_av_amount as predreduce ';
  1291. $where = array();
  1292. $where[] = array('lg_addtime', 'between', array($stime, $etime));
  1293. $predeposit_tmp = $stat_model->getPredepositInfo($where, $field, 0, 'lg_member_id', 0, 'lg_member_id');
  1294. $predeposit_list = array();
  1295. foreach ((array) $predeposit_tmp as $k => $v) {
  1296. $predeposit_list[$v['lg_member_id']] = $v;
  1297. }
  1298. //统计的积分记录
  1299. $field = ' pl_memberid,pl_membername,pl_points as pointsincrease, pl_points as pointsreduce ';
  1300. $where = array();
  1301. $where[] = array('pl_addtime', 'between', array($stime, $etime));
  1302. $points_tmp = $stat_model->statByPointslog($where, $field, 0, 0, '', 'pl_memberid');
  1303. $points_list = array();
  1304. foreach ((array) $points_tmp as $k => $v) {
  1305. $points_list[$v['pl_memberid']] = $v;
  1306. }
  1307. //处理需要更新的数据
  1308. foreach ((array) $update_arr as $k => $v) {
  1309. foreach ($v as $m_k => $m_v) {
  1310. //查询记录是否存在
  1311. $statmember_info = $stat_model->getOneStatmember(array('statm_time' => $k, 'statm_memberid' => $m_k));
  1312. if ($statmember_info) {
  1313. $m_v['statm_ordernum'] = intval($statmember_info['statm_ordernum']) + $m_v['statm_ordernum'];
  1314. $m_v['statm_orderamount'] = floatval($statmember_info['statm_ordernum']) + $m_v['statm_orderamount'];
  1315. $m_v['statm_updatetime'] = $search_etime;
  1316. $stat_model->editStatmember(array('statm_time' => $k, 'statm_memberid' => $m_k), $m_v);
  1317. } else {
  1318. $tmp = array();
  1319. $tmp['statm_memberid'] = $m_k;
  1320. $tmp['statm_membername'] = $m_v['statm_membername'];
  1321. $tmp['statm_time'] = $k;
  1322. $tmp['statm_updatetime'] = $search_etime;
  1323. $tmp['statm_ordernum'] = ($t = intval($m_v['statm_ordernum'])) > 0 ? $t : 0;
  1324. $tmp['statm_orderamount'] = ($t = floatval($m_v['statm_orderamount'])) > 0 ? $t : 0;
  1325. $tmp['statm_goodsnum'] = ($t = intval($m_v['statm_goodsnum'])) ? $t : 0;
  1326. $tmp['statm_predincrease'] = 0;
  1327. $tmp['statm_predreduce'] = 0;
  1328. $tmp['statm_pointsincrease'] = 0;
  1329. $tmp['statm_pointsreduce'] = 0;
  1330. $insert_arr[] = $tmp;
  1331. }
  1332. unset($statmember_info);
  1333. }
  1334. }
  1335. //处理获得所有会员ID数组
  1336. $memberidarr_order = isset($order_list) ? array_keys($order_list) : array();
  1337. $memberidarr_ordergoods = isset($ordergoods_list) ? array_keys($ordergoods_list) : array();
  1338. $memberidarr_predeposit = isset($predeposit_list) ? array_keys($predeposit_list) : array();
  1339. $memberidarr_points = isset($points_list) ? array_keys($points_list) : array();
  1340. $memberid_arr = array_merge($memberidarr_order, $memberidarr_ordergoods, $memberidarr_predeposit, $memberidarr_points);
  1341. //查询会员信息
  1342. $memberid_list = model('member')->getMemberList(array(array('member_id', 'in', $memberid_arr)), '', 0);
  1343. //查询记录是否存在
  1344. $statmemberlist_tmp = $stat_model->statByStatmember(array('statm_time' => $stime));
  1345. $statmemberlist = array();
  1346. foreach ((array) $statmemberlist_tmp as $k => $v) {
  1347. $statmemberlist[$v['statm_memberid']] = $v;
  1348. }
  1349. foreach ((array) $memberid_list as $k => $v) {
  1350. $tmp = array();
  1351. $tmp['statm_memberid'] = $v['member_id'];
  1352. $tmp['statm_membername'] = $v['member_name'];
  1353. $tmp['statm_time'] = $stime;
  1354. $tmp['statm_updatetime'] = $search_etime;
  1355. //因为记录可能已经存在,所以加上之前的统计记录
  1356. $statmemberlist[$tmp['statm_memberid']] = isset($statmemberlist[$tmp['statm_memberid']]) ? $statmemberlist[$tmp['statm_memberid']] : 0;
  1357. $order_list[$tmp['statm_memberid']] = isset($order_list[$tmp['statm_memberid']]) ? $order_list[$tmp['statm_memberid']] : 0;
  1358. $ordergoods_list[$tmp['statm_memberid']] = isset($ordergoods_list[$tmp['statm_memberid']]) ? $ordergoods_list[$tmp['statm_memberid']] : 0;
  1359. $predeposit_list[$tmp['statm_memberid']] = isset($predeposit_list[$tmp['statm_memberid']]) ? $predeposit_list[$tmp['statm_memberid']] : 0;
  1360. $tmp['statm_ordernum'] = intval($statmemberlist[$tmp['statm_memberid']]['statm_ordernum']) + (($t = intval($order_list[$tmp['statm_memberid']]['ordernum'])) > 0 ? $t : 0);
  1361. $tmp['statm_orderamount'] = floatval($statmemberlist[$tmp['statm_memberid']]['statm_orderamount']) + (($t = floatval($order_list[$tmp['statm_memberid']]['orderamount'])) > 0 ? $t : 0);
  1362. $tmp['statm_goodsnum'] = intval($statmemberlist[$tmp['statm_memberid']]['statm_goodsnum']) + (($t = intval($ordergoods_list[$tmp['statm_memberid']]['goodsnum'])) ? $t : 0);
  1363. $tmp['statm_predincrease'] = (($t = floatval($predeposit_list[$tmp['statm_memberid']]['predincrease'])) ? $t : 0);
  1364. $tmp['statm_predreduce'] = (($t = floatval($predeposit_list[$tmp['statm_memberid']]['predreduce'])) ? $t : 0);
  1365. $tmp['statm_pointsincrease'] = (($t = intval($points_list[$tmp['statm_memberid']]['pointsincrease'])) ? $t : 0);
  1366. $tmp['statm_pointsreduce'] = (($t = intval($points_list[$tmp['statm_memberid']]['pointsreduce'])) ? $t : 0);
  1367. $insert_arr[] = $tmp;
  1368. }
  1369. //删除旧的统计数据
  1370. $stat_model->delByStatmember(array('statm_time' => $stime));
  1371. Db::name('statmember')->insertAll($insert_arr);
  1372. }
  1373. }
  1374. }
  1375. ?>