Minutes.php 12 KB

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