Minutes.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace app\crontab\controller;
  3. use app\common\logic\Queue;
  4. use think\facade\Cache;
  5. use think\facade\Db;
  6. /**
  7. *
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * 定时器
  12. */
  13. class Minutes extends BaseCron
  14. {
  15. /**
  16. * 默认方法
  17. */
  18. public function index()
  19. {
  20. $this->_cron_common();
  21. $this->_cron_mail_send();
  22. $this->_cron_pintuan();
  23. $this->_cron_bonus();
  24. $this->_cron_pbargain();
  25. $this->_cron_presell();
  26. $this->_cron_sms();
  27. }
  28. /**
  29. * 短信的处理
  30. */
  31. private function _cron_sms()
  32. {
  33. $smslog_model = model('smslog');
  34. $condition = array();
  35. $condition[] = array('smslog_state', '=', 0);
  36. $smslog_list = $smslog_model->getSmsList($condition, '', 100, 'smslog_id asc');
  37. $sms = new \sendmsg\Sms();
  38. foreach ($smslog_list as $val) {
  39. $smslog_param = json_decode($val['smslog_msg'], true);
  40. $smslog_msg = $smslog_param['message'];
  41. $send_result = $sms->send($val['smslog_phone'], $smslog_param);
  42. if ($send_result['code'] == true) {
  43. $smslog_state = 1;
  44. } else {
  45. $smslog_state = 2;
  46. }
  47. $condition = array();
  48. $condition[] = array('smslog_id', '=', $val['smslog_id']);
  49. $update = array(
  50. 'smslog_state' => $smslog_state,
  51. 'smslog_msg' => $smslog_msg,
  52. 'smslog_smstime' => TIMESTAMP,
  53. );
  54. $smslog_model->editSms($update, $condition);
  55. }
  56. }
  57. /**
  58. * 预售活动的状态的处理
  59. */
  60. private function _cron_presell()
  61. {
  62. $presell_model = model('presell');
  63. //自动开始时间到期的店铺预售活动
  64. $condition = array();
  65. $condition[] = array('presell_state', '=', 1);
  66. $condition[] = array('presell_start_time', '<', TIMESTAMP);
  67. $presell_model->editPresell(array('presell_state' => 2), $condition);
  68. //自动结束时间过期的店铺预售活动
  69. $condition = array();
  70. $condition[] = array('presell_end_time', '<', TIMESTAMP);
  71. $presell_model->endPresell($condition);
  72. //预售结束后72小时未付尾款取消订单
  73. $order_model = model('order');
  74. $logic_order = model('order', 'logic');
  75. $condition = array();
  76. $condition[] = array('presell_deposit_amount', '>', 0);
  77. $condition[] = array('presell_end_time', '<', TIMESTAMP - 72 * 3600);
  78. $condition[] = array('order_state', 'in', [ORDER_STATE_NEW, ORDER_STATE_DEPOSIT, ORDER_STATE_REST]);
  79. $order_list = $order_model->getOrderList($condition, 100);
  80. foreach ($order_list as $order_info) {
  81. $logic_order->changeOrderStateCancel($order_info, 'system', '预售结束后72小时未付尾款取消订单');
  82. }
  83. }
  84. /**
  85. * 砍价活动的状态的处理
  86. */
  87. private function _cron_pbargain()
  88. {
  89. $pbargain_model = model('pbargain');
  90. //自动开始时间到期的店铺砍价活动
  91. $condition = array();
  92. $condition[] = array('bargain_state', '=', 1);
  93. $condition[] = array('bargain_begintime', '<', TIMESTAMP);
  94. $pbargain_model->editBargain(array('bargain_state' => 2), $condition);
  95. //自动结束时间过期的店铺砍价活动
  96. $condition = array();
  97. $condition[] = array('bargain_endtime', '<', TIMESTAMP);
  98. $pbargain_model->endBargain($condition);
  99. //自动结束砍价失败的用户砍价活动
  100. $pbargainorder_model = model('pbargainorder');
  101. $condition = 'bargainorder_state=1 AND bargainorder_endtime<' . TIMESTAMP . ' AND bargainorder_times<bargain_total';
  102. $pbargainorder_model->editPbargainorder($condition, array('bargainorder_state' => 3));
  103. }
  104. /**
  105. * 拼团相关处理
  106. */
  107. private function _cron_pintuan()
  108. {
  109. $ppintuan_model = model('ppintuan');
  110. $ppintuangroup_model = model('ppintuangroup');
  111. $ppintuanorder_model = model('ppintuanorder');
  112. //自动关闭时间过期的店铺拼团活动
  113. $condition = array();
  114. $condition[] = array('pintuan_end_time', '<', TIMESTAMP);
  115. $ppintuan_model->endPintuan($condition);
  116. //查看正在进行开团的列表.
  117. $condition = array();
  118. $condition[] = array('pintuangroup_state', '=', 1);
  119. $pintuangroup_list = $ppintuangroup_model->getPpintuangroupList($condition);
  120. $success_ids = array(); #拼团开团成功的拼团开团ID
  121. $fail_ids = array(); #拼团开团失败的拼团开团ID
  122. foreach ($pintuangroup_list as $key => $pintuangroup) {
  123. //判断当前参团是否已过期
  124. if (TIMESTAMP >= $pintuangroup['pintuangroup_starttime'] + $pintuangroup['pintuangroup_limit_hour'] * 3600) {
  125. //当已参团人数 大于 当前开团的 参团人数
  126. if ($pintuangroup['pintuangroup_joined'] >= $pintuangroup['pintuangroup_limit_number']) {
  127. //满足开团人数,查看对应的订单是否付款,未付款则拼团失败,订单取消,订单款项退回.
  128. $condition = array();
  129. $condition[] = array('ppintuanorder.pintuangroup_id', '=', $pintuangroup['pintuangroup_id']);
  130. if (!$pintuangroup['pintuangroup_is_virtual']) {
  131. $condition[] = array('order.order_state', '=', 20);
  132. $count = Db::name('ppintuanorder')->alias('ppintuanorder')->join('order order', 'order.order_id=ppintuanorder.order_id')->where($condition)->count();
  133. } else {
  134. $condition[] = array('vrorder.order_state', '=', 20);
  135. $count = Db::name('ppintuanorder')->alias('ppintuanorder')->join('vrorder vrorder', 'vrorder.order_id=ppintuanorder.order_id')->where($condition)->count();
  136. }
  137. if ($count == $pintuangroup['pintuangroup_joined']) {
  138. //表示全部付款,拼团成功
  139. $success_ids[] = $pintuangroup['pintuangroup_id'];
  140. } else {
  141. $fail_ids[] = $pintuangroup['pintuangroup_id'];
  142. }
  143. } else {
  144. //未满足开团人数
  145. $fail_ids[] = $pintuangroup['pintuangroup_id'];
  146. }
  147. }
  148. }
  149. $condition = array();
  150. //在拼团失败的所有订单,已经付款的订单列表,取消订单,并且退款,未付款的订单自动取消订单
  151. $condition[] = array('ppintuanorder.pintuangroup_id', 'in', implode(',', $fail_ids));
  152. $condition[] = array('ppintuanorder.pintuanorder_type', '=', 0);
  153. $condition[] = array('order.order_state', '=', 20);
  154. $ppintuanorder_list = Db::name('ppintuanorder')->field('order.*')->alias('ppintuanorder')->join('order order', 'order.order_id=ppintuanorder.order_id')->where($condition)->select()->toArray();
  155. //针对已付款,拼团没成功的订单,进行取消订单以及退款操作
  156. $order_model = model('order');
  157. $logic_order = model('order', 'logic');
  158. foreach ($ppintuanorder_list as $key => $order_info) {
  159. try {
  160. Db::startTrans();
  161. $logic_order->changeOrderStateCancel($order_info, 'system', '系统', '拼团未成功系统自动关闭订单', true, false, true);
  162. } catch (\Exception $e) {
  163. Db::rollback();
  164. continue;
  165. }
  166. Db::commit();
  167. }
  168. $condition = array();
  169. $condition[] = array('ppintuanorder.pintuangroup_id', 'in', implode(',', $fail_ids));
  170. $condition[] = array('ppintuanorder.pintuanorder_type', '=', 1);
  171. $condition[] = array('vrorder.order_state', '=', 20);
  172. $ppintuanorder_list = Db::name('ppintuanorder')->field('vrorder.*')->alias('ppintuanorder')->join('vrorder vrorder', 'vrorder.order_id=ppintuanorder.order_id')->where($condition)->select()->toArray();
  173. $logic_vrorder = model('vrorder', 'logic');
  174. foreach ($ppintuanorder_list as $key => $order_info) {
  175. $logic_vrorder->changeOrderStateCancel($order_info, 'system', '系统', '拼团未成功系统自动关闭订单', false);
  176. }
  177. //失败修改拼团相关数据库信息
  178. $condition = array();
  179. $condition[] = array('pintuangroup_id', 'in', implode(',', $fail_ids));
  180. $ppintuangroup_model->failPpintuangroup($condition);
  181. //成功修改拼团相关数据库信息
  182. $condition = array();
  183. $condition[] = array('pintuangroup_is_virtual', '=', 0);
  184. $condition[] = array('pintuangroup_id', 'in', implode(',', $success_ids));
  185. $condition2 = array();
  186. $condition2[] = array('pintuangroup_id', 'in', implode(',', $success_ids));
  187. $ppintuangroup_model->successPpintuangroup($condition, $condition2);
  188. //给成功拼团的虚拟订单发送兑换码
  189. $vrorder_model = model('vrorder');
  190. $condition = array();
  191. $condition[] = array('order_promotion_type', '=', 2);
  192. $condition[] = array('promotions_id', 'in', implode(',', $success_ids));
  193. $vrorder_list = $vrorder_model->getVrorderList($condition, 1000);
  194. foreach ($vrorder_list as $vrorder) {
  195. if ($vrorder['virtual_type'] == 0) {
  196. $vrorder_model->addVrorderCode($vrorder);
  197. } else {
  198. $logic_vrorder = model('vrorder', 'logic');
  199. $logic_vrorder->changeOrderStateSuccess($vrorder['order_id']);
  200. }
  201. $condition = array();
  202. $condition[] = array('pintuangroup_id', '=', $vrorder['promotions_id']);
  203. $ppintuangroup_model->successPpintuangroup($condition, $condition);
  204. }
  205. }
  206. /**
  207. * 处理过期红包
  208. */
  209. private function _cron_bonus()
  210. {
  211. $condition = array();
  212. $condition[] = array('bonus_endtime', '<', TIMESTAMP);
  213. $condition[] = array('bonus_state', '=', 1);
  214. $data = array(
  215. 'bonus_state' => 2,
  216. );
  217. model('bonus')->editBonus($condition, $data);
  218. }
  219. /**
  220. * 发送邮件消息
  221. */
  222. private function _cron_mail_send()
  223. {
  224. //每次发送数量
  225. $_num = 50;
  226. $storemsgcron_model = model('mailcron');
  227. $cron_array = $storemsgcron_model->getMailCronList(array(), $_num);
  228. if (!empty($cron_array)) {
  229. $email = new \sendmsg\Email();
  230. $mail_array = array();
  231. foreach ($cron_array as $val) {
  232. $return = $email->send_sys_email($val['mailcron_address'], $val['mailcron_subject'], $val['mailcron_contnet']);
  233. if ($return) {
  234. // 记录需要删除的id
  235. $mail_array[] = $val['mailcron_id'];
  236. }
  237. }
  238. // 删除已发送的记录
  239. $storemsgcron_model->delMailCron(array(array('mailcron_id', 'in', $mail_array)));
  240. }
  241. }
  242. /**
  243. * 执行通用任务
  244. */
  245. private function _cron_common()
  246. {
  247. //查找待执行任务
  248. $cron_model = model('cron');
  249. $cron = $cron_model->getCronList(array(array('cron_exetime', '<=', TIMESTAMP)));
  250. if (!is_array($cron)) return;
  251. $cron_array = array();
  252. $QueueLogic = new Queue();
  253. foreach ($cron as $v) {
  254. $value = unserialize($v['cron_value']);
  255. $key = $v['cron_type'];
  256. $res = $QueueLogic->$key($value);
  257. if ($res['code']) {
  258. $cron_model->delCron(array(array('cron_id', '=', $v['cron_id'])));
  259. }
  260. }
  261. }
  262. }