Business.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class Business extends \think\Model
  5. {
  6. protected $pk = 'id';
  7. protected $table = 'cy_business';
  8. protected $dateFormat = false; //时间戳格式不自动转换
  9. public function getUpdateTimeAttr($value)
  10. {
  11. return date('Y-m-d H:i:s', $value);
  12. }
  13. public function getChannelIds($admin_id, $level = 0)
  14. {
  15. $status = true;
  16. $admin = model('common/Admin')->alias('a')->join('nw_admin_role_user b', 'a.id=b.user_id')->where('a.id', $admin_id)->field('a.id,b.role_id')->find();
  17. if (!in_array($admin['role_id'], [12, 17, 18])) {
  18. return -1;
  19. }
  20. $rolelist = [];
  21. $role_id = $admin['role_id'];
  22. while ($status) {
  23. if ($role = Db::table('nw_admin_role')->cache('business:getChannelIds:parent_id' . $role_id, 60)->field('id,parent_id')->where('parent_id', $role_id)->find()) {
  24. $role_id = $role['id'];
  25. $rolelist[] = $role['id'];
  26. } else {
  27. $status = false;
  28. }
  29. }
  30. $list = Db::table('nw_admin_role_user')->cache('business:getChannelIds:role_' . md5(implode(',', $rolelist)), 60)->alias('a')->join('cy_business b', 'a.user_id=b.admin_id')->whereIn('a.role_id', $rolelist)->field('channel_ids')->select();
  31. $bus = $this->where('admin_id', $admin_id)->field('channel_ids')->find();
  32. $tmp = explode(',', $bus['channel_ids']);
  33. foreach ($list as $k => $v) {
  34. $tmp = array_merge($tmp, explode(',', $v['channel_ids']));
  35. }
  36. if ($level == 0) {
  37. if ($tmp && $tmp[0] != '') {
  38. return $tmp;
  39. } else {
  40. return -2;
  41. }
  42. } else if ($level == 1) { // 会长
  43. if (!$tmp && $tmp[0] != '') {
  44. return -2;
  45. }
  46. $channel = model('Channel')->cache('business:getChannelIds:level_B' . md5(implode(',', $tmp)), 60)->whereIn('parent_id', $tmp)->column('id');
  47. if ($channel) {
  48. return $channel;
  49. } else {
  50. return -2;
  51. }
  52. } else if ($level == 2) { // 子会长
  53. if (!$tmp && $tmp[0] != '') {
  54. return -2;
  55. }
  56. $where = [];
  57. foreach ($tmp as $k => $v) {
  58. $where[] = sprintf(' id_path like ",%s', $v) . '%" ';
  59. }
  60. $str = implode(' or ', $where);
  61. $channel = model('Channel')->cache('business:getChannelIds:level_B_' . md5(implode(',', $tmp)), 60)->where('level', $level)->whereRaw($str)->column('id');
  62. if ($channel) {
  63. return $channel;
  64. } else {
  65. return -2;
  66. }
  67. } else {//推广员
  68. if (!$tmp && $tmp[0] != '') {
  69. return -2;
  70. }
  71. $where = [];
  72. foreach ($tmp as $k => $v) {
  73. $where[] = sprintf(' id_path like ",%s', $v) . '%" ';
  74. }
  75. $str = implode(' or ', $where);
  76. $channel = model('Channel')->cache('business:getChannelIds:level_C' . md5(implode(',', $tmp)), 60)->where('level', $level)->whereRaw($str)->column('id');
  77. if ($channel) {
  78. return $channel;
  79. } else {
  80. return -2;
  81. }
  82. }
  83. }
  84. }