Minutes.php 12 KB

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