MemberGroup.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/2/23
  6. * Time: 下午6:04
  7. */
  8. namespace app\backend\modules\member\models;
  9. use Illuminate\Validation\Rule;
  10. class MemberGroup extends \app\common\models\MemberGroup
  11. {
  12. static protected $needLog = true;
  13. public $guarded = [''];
  14. //关联 member 数据表 一对多
  15. public function member()
  16. {
  17. return $this->hasMany('app\backend\modules\member\models\MemberShopInfo','group_id','id');
  18. }
  19. /*
  20. * 获取会员分页列表 17.3.31 auto::yitian
  21. *
  22. * @param int $pageSize
  23. *
  24. * @return object */
  25. public static function getGroupPageList($pageSize)
  26. {
  27. return self::select(['id', 'group_name'])->uniacid()
  28. ->with(['member' => function($query){
  29. return $query->select(['member_id','group_id'])->where('uniacid', \YunShop::app()->uniacid);
  30. }])
  31. ->paginate($pageSize);
  32. }
  33. /**
  34. * Get membership information through member group ID
  35. *
  36. * @param int $groupId
  37. *
  38. * @return array */
  39. public static function getMemberGroupByGroupId($groupId)
  40. {
  41. return MemberGroup::select('id', 'group_name')->uniacid()->where('id', $groupId)->first();
  42. }
  43. /**
  44. * Get a list of members of the current public number
  45. *
  46. * @param int $uniacid
  47. *
  48. * @return array */
  49. public static function getMemberGroupList()
  50. {
  51. $memberGroup = MemberGroup::select('id', 'group_name')
  52. ->uniacid()
  53. ->with(['member' => function($query){
  54. return $query->select(['member_id','group_id'])->where('uniacid', \YunShop::app()->uniacid);
  55. }])
  56. ->get()
  57. ->toArray();
  58. return $memberGroup;
  59. }
  60. /**
  61. * 获取指定"GroupId"下的关联用户数据
  62. * @param $groupId
  63. * @return mixed
  64. */
  65. public static function getMembersByGroupId($groupId)
  66. {
  67. $memberGroup = static::uniacid()
  68. ->select('id', 'group_name')
  69. ->where('id', '=', $groupId)
  70. ->with(['member' => function($query){
  71. return $query->select(['member_id','group_id'])->where('uniacid', \YunShop::app()->uniacid);
  72. }])
  73. ->first();
  74. return $memberGroup;
  75. }
  76. /**
  77. * Delete member list
  78. *
  79. * @param int $groupId
  80. *
  81. * @return 1 or 0
  82. **/
  83. public static function deleteMemberGroup($groupId)
  84. {
  85. return static::where('id', $groupId)->delete();
  86. }
  87. /**
  88. * 定义字段名
  89. *
  90. * @return array */
  91. public function atributeNames() {
  92. return [
  93. 'group_name' => '分组名',
  94. 'uniacid' => '公众号ID',
  95. ];
  96. }
  97. /**
  98. * 字段规则
  99. *
  100. * @return array */
  101. public function rules()
  102. {
  103. return [
  104. 'group_name' => [
  105. 'required',
  106. Rule::unique($this->table)->where('uniacid', \YunShop::app()->uniacid)->where('deleted_at','')->ignore($this->id),
  107. 'max:45'
  108. ],
  109. 'uniacid' => 'required'
  110. ];
  111. }
  112. }