weixin_distributionModel.class.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. *
  4. */
  5. class weixin_distributionModel extends NewModel
  6. {
  7. public function addWithUnionId($unionid, $pid)
  8. {
  9. if (!$unionid) {
  10. return false;
  11. }
  12. $user = self::build('user')->field('id')->selectOne(['wx_unionid' => $unionid]);
  13. if (!$user) {
  14. return false;
  15. }
  16. return $this->add($user['id'], $pid);
  17. }
  18. public function add($user_id, $pid, $first_rate = 0, $second_rate = 0)
  19. {
  20. if ($pid) {
  21. $p = $this->field('topid')->selectOne(['user_id' => $pid]);
  22. $topid = intval($p['topid']);
  23. } else {
  24. $topid = $user_id;
  25. }
  26. if (!$topid) {
  27. return $topid;
  28. }
  29. $data = [
  30. 'user_id' => intval($user_id),
  31. 'pid' => intval($pid),
  32. 'topid' => intval($topid),
  33. 'first_rate' => intval($first_rate),
  34. 'second_rate' => intval($second_rate),
  35. 'create_time' => NOW_TIME,
  36. ];
  37. return $this->insert($data);
  38. }
  39. public function getParent($user_id)
  40. {
  41. $self = $this->field('pid')->selectOne(['user_id' => $user_id]);
  42. if ($self['pid']) {
  43. return $this->selectOne(['user_id' => $self['pid']]);
  44. }
  45. return null;
  46. }
  47. public function getChild($pid, $field = false)
  48. {
  49. if (!$pid) {
  50. return [];
  51. }
  52. if (is_array($pid)) {
  53. $where = ['pid' => ['in', $pid]];
  54. } else {
  55. $where = ['pid' => intval($pid)];
  56. }
  57. if ($field === false) {
  58. $child = $this->field('user_id')->select($where);
  59. $array = [];
  60. foreach ($child as $value) {
  61. $array[] = intval($value['user_id']);
  62. }
  63. return $array;
  64. } else {
  65. return $this->field($field)->select($where);
  66. }
  67. }
  68. public function getUserListByPid($pid, $topid = 0)
  69. {
  70. $table = 'user u,weixin_distribution d';
  71. $field = 'u.id,u.nick_name,u.head_image,p.pid,p.first_rate,p.second_rate,p.create_time';
  72. $where = ['pid' => intval($pid)];
  73. if ($topid) {
  74. $where['topid'] = intval($topid);
  75. }
  76. return self::parseValue($this->table($table)->field($field)->select($where));
  77. }
  78. protected static function parseValue($data)
  79. {
  80. if (isset($data[0])) {
  81. foreach ($data as $key => $value) {
  82. $data[$key] = self::parseValue($value);
  83. }
  84. } else {
  85. if (isset($data['head_image'])) {
  86. $data['head_image'] = get_spec_image($data['head_image']);
  87. }
  88. }
  89. return $data;
  90. }
  91. }