DcwjFinish.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * 调查问卷发放书币
  4. * Created by PhpStorm.
  5. * User: lytian
  6. * Date: 2019/7/25
  7. * Time: 21:46
  8. */
  9. namespace app\admin\command;
  10. use app\common\library\Redis;
  11. use app\common\library\WeChatObject;
  12. use app\common\model\AdminConfig;
  13. use app\common\model\User;
  14. use app\main\constants\CacheConstants;
  15. use app\main\service\AdminService;
  16. use app\main\service\OfficialAccountsService;
  17. use app\main\service\OpenPlatformService;
  18. use app\main\service\UserService;
  19. use EasyWeChat\Kernel\Messages\Text;
  20. use think\console\Command;
  21. use think\console\Input;
  22. use think\console\input\Option;
  23. use think\console\Output;
  24. use think\Exception;
  25. use think\Log;
  26. use think\Request;
  27. class DcwjFinish extends Command
  28. {
  29. /**
  30. * @var User
  31. */
  32. private $model = null;
  33. private $redis = null;
  34. protected function configure()
  35. {
  36. $this->setName('dcwjFinish')
  37. ->setDescription('调查问卷发放书币');
  38. }
  39. protected function execute(Input $input, Output $output)
  40. {
  41. Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
  42. $filename = APP_PATH."dcwj.txt";
  43. $handle = fopen ($filename, "r");
  44. $adminIds = [];
  45. $i = 1;
  46. while (!feof($handle)) {
  47. $buffer = fgets($handle, 4096);
  48. $content = trim($buffer);
  49. if (empty($content)) {
  50. break;
  51. }
  52. try {
  53. $userId = $content;
  54. if (!is_numeric($userId)) {
  55. continue;
  56. }
  57. $this->model = model("User");
  58. $this->redis = Redis::instance();
  59. $key = 'U_DCWJ:'. $userId;
  60. if ($this->redis->exists($key)) {
  61. echo "第{$i}行--userid:{$userId}--处理结果:已赠送\n";
  62. continue;
  63. }
  64. $res = $this->editUserKandian($userId,1,2,0,500,'调查问卷脚本执行添加');
  65. if ($res) {
  66. echo "第{$i}行--userid:{$userId}--处理结果:成功\n";
  67. $this->redis->set($key, '1', 86400*30);
  68. } else {
  69. echo "第{$i}行--userid:{$userId}--处理结果:失败\n";
  70. }
  71. }catch (Exception $e) {
  72. echo "第{$i}行--userid:{$userId}--处理结果:异常\n";
  73. }
  74. $i++;
  75. }
  76. fclose ($handle);
  77. $output->writeln("\n处理完毕!");
  78. }
  79. /**
  80. * @param $id 用户id
  81. * @param $type 增加/减少 书币
  82. * @param $czlx 充值类型
  83. * @param int $kandian 书币
  84. * @param int $free_kandian 减少多少免费书币
  85. * @param string $bz
  86. * @return bool
  87. */
  88. public function editUserKandian($id,$type,$czlx,$kandian=0,$free_kandian=0,$bz=''){
  89. $user = $this->model->getUserInfo($id);
  90. if($user){
  91. $saveDate = []; //新增充值记录
  92. $saveDate['user_id'] = $id;
  93. $saveDate['type'] = 6; //活动赠送
  94. $saveDate['notes'] = $bz;
  95. $saveDate['edit_type'] = $czlx;
  96. $saveDate['createtime'] = time();
  97. if($type==1){ //增加书币
  98. if($kandian){
  99. $saveDate['kandian'] = $kandian;
  100. }
  101. if($free_kandian){
  102. $saveDate['free_kandian'] = $free_kandian;
  103. $saveDate['remain_free_kandian'] = $free_kandian;
  104. $saveDate['free_endtime'] = 5*86400 + time();
  105. }
  106. }
  107. if(isset($saveDate['free_kandian']) || isset($saveDate['kandian'])){
  108. $flagFreeRecharge = true;
  109. $flagRecharge = true;
  110. if(!empty($saveDate['kandian'])){
  111. $kandianSaveDate = $saveDate;
  112. if(!empty($kandianSaveDate['free_kandian'])){
  113. unset($kandianSaveDate['free_kandian']);
  114. unset($kandianSaveDate['remain_free_kandian']);
  115. unset($kandianSaveDate['free_endtime']);
  116. }
  117. $flagRecharge = model('Recharge')->setConnect($id)->insertGetId($kandianSaveDate); //插入充值记录
  118. }
  119. if(!empty($saveDate['free_kandian'])){
  120. $freeSaveDate = $saveDate;
  121. if(!empty($freeSaveDate['kandian'])){
  122. unset($freeSaveDate['kandian']);
  123. }
  124. $flagFreeRecharge = model('Recharge')->setConnect($id)->insertGetId($freeSaveDate); //插入免费充值记录
  125. if($flagFreeRecharge) {
  126. $insertId = $flagFreeRecharge;
  127. $rData = [];
  128. $rData['id'] = $insertId;
  129. $rData['remain_free_kandian'] = $saveDate['remain_free_kandian'];
  130. $rData['free_endtime'] = $saveDate['free_endtime'];
  131. if ($flagFreeRecharge) {
  132. $this->redis->del('ZR:' . $id);
  133. }
  134. }
  135. }
  136. if(!$flagRecharge || !$flagFreeRecharge){
  137. Log::error('修改用户书币:数据库recharge插入失败!');
  138. return false;
  139. }
  140. }
  141. }else{ //没有此用户
  142. return false;
  143. }
  144. return true;
  145. }
  146. }