CustomerCron.Class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wxj
  5. * Date: 2019/11/14
  6. * Time: 15:45
  7. */
  8. namespace JinDouYun\Controller\Cron;
  9. use Mall\Framework\Core\ErrorCode;
  10. use Mall\Framework\Core\ResultWrapper;
  11. use JinDouYun\Cache\CustomerCache;
  12. use JinDouYun\Dao\Market\DVipCardOrder;
  13. use Mall\Framework\Core\StatusCode;
  14. class CustomerCron
  15. {
  16. private $objCustomerCache;
  17. private $objDVipCardOrder;
  18. public function __construct()
  19. {
  20. $this->objCustomerCache = new CustomerCache();
  21. $this->objDVipCardOrder = new DVipCardOrder('default');
  22. }
  23. //删除redis中一个月前注册的客户
  24. public function delCustomerOfOneMonthAgo()
  25. {
  26. $result = $this->objCustomerCache->delCustomerOfOneMonthAgo();
  27. if ($result) {
  28. echo "删除30天前注册的客户--成功";
  29. die;
  30. }
  31. echo "删除30天前注册的客户--失败";
  32. }
  33. //删除redis中7天前有加购行为的客户
  34. public function delInterestCustomerOfSevenDaysAgo()
  35. {
  36. $result = $this->objCustomerCache->delInterestCustomerOfSevenDaysAgo();
  37. if ($result) {
  38. echo "删除7天有加购行为的客户--成功";
  39. die;
  40. }
  41. echo "删除7天有加购行为的客户--失败";
  42. }
  43. //每半小时执行一次,把下单超过半小时仍未支付的订单删掉
  44. public function AutoDelNoPayVipCardOrder() {
  45. $sql = "show tables like 'qianniao_vip_card_order_%';";
  46. $tables = $this->objDVipCardOrder->query($sql);
  47. if (empty($tables)) {
  48. echo "无会员卡订单表";
  49. die;
  50. }
  51. $vipCardOrderTables = [];
  52. foreach ($tables as $tableArr) {
  53. foreach ($tableArr as $table) {
  54. if (strpos($table,'qianniao_vip_card_order_') !== false) {
  55. $vipCardOrderTables[] = $table;
  56. }
  57. }
  58. }
  59. if (empty($vipCardOrderTables)) {
  60. echo "无会员卡订单表";
  61. die;
  62. }
  63. $this->objDVipCardOrder->beginTransaction();
  64. foreach ($vipCardOrderTables as $vipCardOrderTable) {
  65. $enterpriseId = substr($vipCardOrderTable,strlen('qianniao_vip_card_order_'));
  66. $endTime = time()- 30 * 60;
  67. $sql = 'SELECT * FROM '.$vipCardOrderTable.' WHERE payStatus='.StatusCode::$delete.' AND createTime <='.$endTime;
  68. $orders = $this->objDVipCardOrder->query($sql);
  69. if ($orders === false) {
  70. $this->objDVipCardOrder->rollBack();
  71. file_put_contents('/www/wwwroot/cron.log',date('Y-m-d H:i:s').'查询会员卡订单表失败'.var_export($orders,true).PHP_EOL,FILE_APPEND);
  72. }
  73. //循环处理订单 把会员卡销售数量减1,并删除订单
  74. if (!empty($orders)) {
  75. foreach ($orders as $order) {
  76. $sql = "UPDATE qianniao_vip_card_".$enterpriseId.' SET num=num -1 where id='.$order['vipCardId'];
  77. $result = $this->objDVipCardOrder->query($sql);
  78. if ($result === false) {
  79. $this->objDVipCardOrder->rollBack();
  80. file_put_contents('/www/wwwroot/cron.log',date('Y-m-d H:i:s').'修改会员卡销售数量失败'.var_export($result,true).PHP_EOL,FILE_APPEND);
  81. }
  82. $sql = "DELETE FROM ".$vipCardOrderTable.' where id='.$order['id'];
  83. $dbResult = $this->objDVipCardOrder->query($sql);
  84. if ($dbResult === false) {
  85. $this->objDVipCardOrder->rollBack();
  86. file_put_contents('/www/wwwroot/cron.log',date('Y-m-d H:i:s').'删除会员卡订单失败'.var_export($result,true).PHP_EOL,FILE_APPEND);
  87. }
  88. }
  89. }
  90. }
  91. $this->objDVipCardOrder->commit();
  92. }
  93. }