Cron.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace Task\Lib;
  3. use Dever;
  4. # 放到计划任务中的:
  5. # task/lib/cron.clearTask 一天一次 86400
  6. # task/lib/cron.task_commit 任务报告完成 1个小时1次 3600
  7. # task/lib/cron.cash_commit 兑现审核完成 1个小时1次 3600
  8. class Cron
  9. {
  10. /**
  11. * 将过期的任务,置为已结束,建议一天运行一次
  12. *
  13. * @return mixed
  14. */
  15. public function clearTask_api()
  16. {
  17. $data = Dever::db('task/info')->gets();
  18. $time = time();
  19. foreach ($data as $k => $v) {
  20. # 未开始的变成进行中
  21. $update['where_id'] = $v['id'];
  22. if ($v['status'] == 2 && $v['sdate'] >= $time) {
  23. $update['status'] = 1;
  24. Dever::db('task/info')->update($update);
  25. } elseif ($v['status'] == 1 && $v['edate'] < $time) {
  26. $update['status'] = 3;
  27. Dever::db('task/info')->update($update);
  28. }
  29. }
  30. }
  31. /**
  32. * 资料认证审核后的操作
  33. *
  34. * @return mixed
  35. */
  36. public function submit_commit()
  37. {
  38. }
  39. /**
  40. * 任务报告完成后的操作
  41. *
  42. * @return mixed
  43. */
  44. public function task_commit_api()
  45. {
  46. # 审核成功 入账
  47. $where['status'] = 2;
  48. $where['score_status'] = 1;
  49. $data = Dever::db('task/user_report')->state($where);
  50. if ($data) {
  51. foreach ($data as $k => $v) {
  52. $update['where_id'] = $v['id'];
  53. $update['score_status'] = 2;
  54. Dever::db('task/user_report')->update($update);
  55. $this->updateUserScore($v['uid'], $v['score']);
  56. $this->updateScoreLog($v['uid'], $v['score'], 4, 2, $v['task_id'], $v['id']);
  57. $this->updateMsg($v['uid'], $v['score'], 4, $v['task_id']);
  58. $this->group($v['uid'], $v['group_score'], $v['task_id'], $v['id']);
  59. }
  60. }
  61. return 'yes';
  62. }
  63. /**
  64. * 兑现审核完成后的操作
  65. *
  66. * @return mixed
  67. */
  68. public function cash_commit_api()
  69. {
  70. # 成功
  71. $where['status'] = 3;
  72. $where['cron'] = 1;
  73. $data = Dever::db('task/user_cash')->state($where);
  74. if ($data) {
  75. foreach ($data as $k => $v) {
  76. $update['where_id'] = $v['id'];
  77. $update['cron'] = 2;
  78. Dever::db('task/user_cash')->update($update);
  79. $this->updateScoreLog($v['uid'], $v['score'], 2, 1, -1, -1, $v['type'], $v['cash'], $v['cdate']);
  80. $this->updateMsg($v['uid'], $v['score'], 2, false, false, $v['type'], $v['cash'], $v['cdate']);
  81. }
  82. }
  83. # 失败
  84. $where['status'] = 4;
  85. $where['cron'] = 1;
  86. $data = Dever::db('task/user_cash')->state($where);
  87. if ($data) {
  88. foreach ($data as $k => $v) {
  89. $update['where_id'] = $v['id'];
  90. $update['cron'] = 2;
  91. Dever::db('task/user_cash')->update($update);
  92. # 失败了就把积分再加回去吧
  93. $this->updateUserScore($v['uid'], $v['score']);
  94. $this->updateScoreLog($v['uid'], $v['score'], 3, 1, -1, -1, $v['type'], $v['cash'], $v['cdate'], $v['status_desc']);
  95. $this->updateMsg($v['uid'], $v['score'], 3, false, false, $v['type'], $v['cash'], $v['cdate'], $v['status_desc']);
  96. }
  97. }
  98. return 'yes';
  99. }
  100. /**
  101. * 寻找这个用户的上线 给上线带来收益
  102. *
  103. * @return mixed
  104. */
  105. private function group($uid, $score, $task_id, $report_id)
  106. {
  107. $group_member = Dever::db('task/user_group_member')->one(array('uid' => $uid));
  108. if ($group_member) {
  109. $group_info = Dever::db('task/user_group')->one($group_member['group_id']);
  110. if ($group_info && $group_info['state'] == 1 && $group_info['uid'] > 0 && $group_info['uid'] != $uid) {
  111. $this->updateUserScore($group_info['uid'], $score);
  112. $this->updateScoreLog($group_info['uid'], $score, 5, 2, $task_id, $report_id, 1, 0, 0, '', $uid);
  113. $this->updateMsg($group_info['uid'], $score, 5, $task_id, $uid);
  114. }
  115. }
  116. }
  117. /**
  118. * 更新用户积分数量
  119. *
  120. * @return mixed
  121. */
  122. private function updateUserScore($uid, $score)
  123. {
  124. $update['where_uid'] = $uid;
  125. $update['score'] = $score;
  126. Dever::db('task/user_score')->updateScore($update);
  127. }
  128. /**
  129. * 日志记录
  130. *
  131. * @return mixed
  132. */
  133. private function updateScoreLog($uid, $score, $type, $status, $task_id = -1, $report_id = -1, $cash_type = 1, $cash = 0, $cash_date = 0, $type_desc = '', $group_uid = false)
  134. {
  135. $insert = array();
  136. $insert['type'] = $type;
  137. $insert['status'] = $status;
  138. $insert['score'] = $score;
  139. $insert['uid'] = $uid;
  140. $insert['group_uid'] = $group_uid;
  141. $insert['task_id'] = $task_id;
  142. $insert['report_id'] = $report_id;
  143. $insert['cash_type'] = $cash_type;
  144. $insert['cash'] = $cash;
  145. if ($cash_date) {
  146. $insert['cash_date'] = $cash_date;
  147. }
  148. $insert['type_desc'] = $type_desc;
  149. Dever::db('task/user_score_log')->insert($insert);
  150. }
  151. /**
  152. * 日志记录
  153. *
  154. * @return mixed
  155. */
  156. private function updateMsg($uid, $score, $type, $task_id = -1, $group_uid = false, $cash_type = 1, $cash = 0, $cash_date = 0, $desc = '')
  157. {
  158. $config = Dever::db('main/config')->one();
  159. if ($type == 4 || $type == 5) {
  160. $task = Dever::db('task/info')->one($task_id);
  161. # 完成任务
  162. $desc = '';
  163. if ($type == 5) {
  164. $desc = '提成';
  165. $user = Dever::db('passport/user')->one($group_uid);
  166. } else {
  167. $user = Dever::db('passport/user')->one($uid);
  168. }
  169. $name = $config['score_name'] . '入账通知';
  170. $content = '入账'.$config['score_name'].':'.$score.$config['score_name'].'
  171. <br /> '.$config['score_name'].'来源:'.$user['username'].'完成任务#'.$task['name'].'的'.$desc.'奖励
  172. <br /> 入账时间:' . date('Y-m-d H:i');
  173. }
  174. if ($cash_type == 1) {
  175. $cash_type = '微信';
  176. } else {
  177. $cash_type = '支付宝';
  178. }
  179. if ($type == 2) {
  180. $name = $config['score_name'].'兑现成功通知';
  181. $content = '本次兑现:'.$score.$config['score_name'].'
  182. <br />兑现途径:'.$cash_type.'
  183. <br />申请兑现时间:' . date('Y-m-d H:i', $cash_date).'
  184. <br />兑现完成时间:' . date('Y-m-d H:i');
  185. }
  186. if ($type == 3) {
  187. $name = $config['score_name'].'兑现失败通知';
  188. $content = '本次兑现:'.$score.$config['score_name'].'
  189. <br />兑现途径:'.$cash_type.'
  190. <br />申请兑现时间:' . date('Y-m-d H:i', $cash_date).'
  191. <br />'.$desc;
  192. }
  193. $type = 11;
  194. if (isset($name)) {
  195. Dever::load('message/lib/data.push', -1, $uid, $name, $content, $type);
  196. }
  197. }
  198. }