Minutes.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. * DSMall多用户商城
  9. * ============================================================================
  10. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  11. * 网站地址: http://www.csdeshang.com
  12. * ----------------------------------------------------------------------------
  13. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  14. * 不允许对程序代码以任何形式任何目的的再发布。
  15. * ============================================================================
  16. * 定时器
  17. */
  18. class Minutes extends BaseCron {
  19. /**
  20. * 默认方法
  21. */
  22. public function index() {
  23. $this->_cron_common();
  24. $this->_cron_mail_send();
  25. $this->_cron_pintuan();
  26. $this->_cron_bonus();
  27. $this->_cron_pbargain();
  28. $this->_cron_presell();
  29. $this->_cron_sms();
  30. }
  31. /**
  32. * 短信的处理
  33. */
  34. private function _cron_sms(){
  35. $smslog_model=model('smslog');
  36. $condition=array();
  37. $condition[]=array('smslog_state','=',0);
  38. $smslog_list=$smslog_model->getSmsList($condition, '', 100, 'smslog_id asc');
  39. $sms = new \sendmsg\Sms();
  40. foreach($smslog_list as $val){
  41. $smslog_param=json_decode($val['smslog_msg'],true);
  42. $smslog_msg=$smslog_param['message'];
  43. $send_result = $sms->send($val['smslog_phone'], $smslog_param);
  44. if ($send_result['code'] == true) {
  45. $smslog_state=1;
  46. }else{
  47. $smslog_state=2;
  48. }
  49. $condition=array();
  50. $condition[]=array('smslog_id','=',$val['smslog_id']);
  51. $update=array(
  52. 'smslog_state'=>$smslog_state,
  53. 'smslog_msg'=>$smslog_msg,
  54. 'smslog_smstime'=>TIMESTAMP,
  55. );
  56. $smslog_model->editSms($update,$condition);
  57. }
  58. }
  59. /**
  60. * 预售活动的状态的处理
  61. */
  62. private function _cron_presell(){
  63. $presell_model = model('presell');
  64. //自动开始时间到期的店铺预售活动
  65. $condition=array();
  66. $condition[]=array('presell_state','=',1);
  67. $condition[] = array('presell_start_time','<',TIMESTAMP);
  68. $presell_model->editPresell(array('presell_state'=>2), $condition);
  69. //自动结束时间过期的店铺预售活动
  70. $condition=array();
  71. $condition[] = array('presell_end_time','<',TIMESTAMP);
  72. $presell_model->endPresell($condition);
  73. //预售结束后72小时未付尾款取消订单
  74. $order_model=model('order');
  75. $logic_order=model('order','logic');
  76. $condition=array();
  77. $condition[]=array('presell_deposit_amount','>',0);
  78. $condition[]=array('presell_end_time','<',TIMESTAMP-72*3600);
  79. $condition[]=array('order_state','in',[ORDER_STATE_NEW,ORDER_STATE_DEPOSIT,ORDER_STATE_REST]);
  80. $order_list=$order_model->getOrderList($condition,100);
  81. foreach($order_list as $order_info){
  82. $logic_order->changeOrderStateCancel($order_info, 'system', '预售结束后72小时未付尾款取消订单');
  83. }
  84. }
  85. /**
  86. * 砍价活动的状态的处理
  87. */
  88. private function _cron_pbargain(){
  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. $condition = array();
  211. $condition[] = array('bonus_endtime','<', TIMESTAMP);
  212. $condition[] = array('bonus_state','=',1);
  213. $data = array(
  214. 'bonus_state' => 2,
  215. );
  216. model('bonus')->editBonus($condition, $data);
  217. }
  218. /**
  219. * 发送邮件消息
  220. */
  221. private function _cron_mail_send() {
  222. //每次发送数量
  223. $_num = 50;
  224. $storemsgcron_model = model('mailcron');
  225. $cron_array = $storemsgcron_model->getMailCronList(array(), $_num);
  226. if (!empty($cron_array)) {
  227. $email = new \sendmsg\Email();
  228. $mail_array = array();
  229. foreach ($cron_array as $val) {
  230. $return = $email->send_sys_email($val['mailcron_address'],$val['mailcron_subject'],$val['mailcron_contnet']);
  231. if ($return) {
  232. // 记录需要删除的id
  233. $mail_array[] = $val['mailcron_id'];
  234. }
  235. }
  236. // 删除已发送的记录
  237. $storemsgcron_model->delMailCron(array(array('mailcron_id','in', $mail_array)));
  238. }
  239. }
  240. /**
  241. * 执行通用任务
  242. */
  243. private function _cron_common(){
  244. //查找待执行任务
  245. $cron_model = model('cron');
  246. $cron = $cron_model->getCronList(array(array('cron_exetime','<=',TIMESTAMP)));
  247. if (!is_array($cron)) return ;
  248. $cron_array = array();
  249. $QueueLogic = new Queue();
  250. foreach ($cron as $v) {
  251. $value = unserialize($v['cron_value']);
  252. $key = $v['cron_type'];
  253. $res=$QueueLogic->$key($value);
  254. if($res['code']){
  255. $cron_model->delCron(array(array('cron_id','=',$v['cron_id'])));
  256. }
  257. }
  258. }
  259. }
  260. ?>