ShopMember.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的多应用管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\model\shop;
  12. use app\dict\common\ChannelDict;
  13. use app\dict\common\CommonDict;
  14. use app\dict\member\MemberDict;
  15. use app\dict\member\MemberLoginTypeDict;
  16. use app\dict\member\MemberRegisterChannelDict;
  17. use app\dict\member\MemberRegisterTypeDict;
  18. use app\dict\shop\ShopMemberIsFollowDict;
  19. use app\model\member\Member;
  20. use app\model\site\Site;
  21. use core\base\BaseModel;
  22. use think\db\Query;
  23. use think\model\relation\HasOne;
  24. /**
  25. * 店铺会员扩展
  26. * Class Shop
  27. * @package addon\mall\app\model\shop
  28. */
  29. class ShopMember extends BaseModel
  30. {
  31. protected $type = [
  32. 'first_consum_time' => 'timestamp',
  33. 'last_consum_time' => 'timestamp',
  34. ];
  35. /**
  36. * 数据表主键
  37. * @var string
  38. */
  39. protected $pk = 'id';
  40. /**
  41. * 模型名称
  42. * @var string
  43. */
  44. protected $name = 'site_shop_member';
  45. // 设置json类型字段
  46. protected $json = ['shop_member_label'];
  47. // 设置JSON数据返回数组
  48. protected $jsonAssoc = true;
  49. public function member()
  50. {
  51. return $this->hasOne(Member::class, 'member_id', 'member_id');
  52. }
  53. public function site()
  54. {
  55. return $this->hasOne(Site::class, 'site_id', 'site_id');
  56. }
  57. /**
  58. * 状态字段转化
  59. * @param $value
  60. * @param $data
  61. * @return mixed
  62. */
  63. public function getStatusNameAttr($value, $data)
  64. {
  65. if (empty($data['status']))
  66. return '';
  67. return MemberDict::getStatus()[$data['status']] ?? '';
  68. }
  69. /**
  70. * 关注状态字段转化
  71. * @param $value
  72. * @param $data
  73. * @return mixed
  74. */
  75. public function getIsFollowNameAttr($value, $data)
  76. {
  77. if ($data['is_follow'] == '')
  78. return '';
  79. return ShopMemberIsFollowDict::getStatus()[$data['is_follow']] ?? '';
  80. }
  81. /**
  82. * 注册来源字段转化
  83. * @param $value
  84. * @param $data
  85. * @return mixed
  86. */
  87. public function getRegisterChannelNameAttr($value, $data)
  88. {
  89. if (empty($data['register_channel']))
  90. return '';
  91. return MemberRegisterChannelDict::getType()[$data['register_channel']] ?? '';
  92. }
  93. /**
  94. * 注册方式字段转化
  95. * @param $value
  96. * @param $data
  97. * @return mixed
  98. */
  99. public function getRegisterTypeNameAttr($value, $data)
  100. {
  101. if (empty($data['register_type']))
  102. return '';
  103. return MemberRegisterTypeDict::getType()[$data['register_type']] ?? '';
  104. }
  105. /**
  106. * 登录渠道字段转化
  107. * @param $value
  108. * @param $data
  109. * @return mixed
  110. */
  111. public function getLoginChannelNameAttr($value, $data)
  112. {
  113. if (empty($data['login_channel']))
  114. return '';
  115. return ChannelDict::getType()[$data['login_channel']] ?? '';
  116. }
  117. /**
  118. * 登录方式字段转化
  119. * @param $value
  120. * @param $data
  121. * @return mixed
  122. */
  123. public function getLoginTypeNameAttr($value, $data)
  124. {
  125. if (empty($data['login_type']))
  126. return '';
  127. return MemberLoginTypeDict::getType()[$data['login_type']] ?? '';
  128. }
  129. /**
  130. * 性别名称
  131. * @param $value
  132. * @param $data
  133. * @return mixed|string
  134. */
  135. public function getSexNameAttr($value, $data)
  136. {
  137. if (empty($data['sex']))
  138. return '';
  139. return CommonDict::getSexType()[$data['sex']] ?? '';
  140. }
  141. /**
  142. * 会员信息搜索器
  143. * @param $query
  144. * @param $value
  145. * @param $data
  146. * @return void
  147. */
  148. public function searchKeywordAttr($query, $value, $data)
  149. {
  150. if ($value) {
  151. $query->where('member_no|nickname|mobile', 'like', "%$value%");
  152. }
  153. }
  154. /**
  155. * 注册方式搜索
  156. * @param $query
  157. * @param $value
  158. * @param $data
  159. */
  160. public function searchRegisterTypeAttr($query, $value, $data)
  161. {
  162. if ($value) {
  163. $query->where('register_type', '=', $value);
  164. }
  165. }
  166. /**
  167. * 关注状态搜索
  168. * @param $query
  169. * @param $value
  170. * @param $data
  171. */
  172. public function searchIsFollowAttr($query, $value, $data)
  173. {
  174. if ($value != '') {
  175. $query->where('is_follow', '=', $value);
  176. }
  177. }
  178. /**
  179. * 注册来源搜索
  180. * @param $query
  181. * @param $value
  182. * @param $data
  183. */
  184. public function searchRegisterChannelAttr($query, $value, $data)
  185. {
  186. if ($value) {
  187. $query->where('register_channel', '=', $value);
  188. }
  189. }
  190. /**
  191. * 标签筛选
  192. * @param Query $query
  193. * @param $value
  194. * @param $data
  195. * @return void
  196. */
  197. public function searchShopMemberLabelAttr(Query $query, $value, $data)
  198. {
  199. if ($value) {
  200. $query->whereLike('shop_member_label', '%' . $value . '%');
  201. }
  202. }
  203. /**
  204. * 注册时间搜索器
  205. * @param Query $query
  206. * @param $value
  207. * @param $data
  208. */
  209. public function searchCreateTimeAttr(Query $query, $value, $data)
  210. {
  211. $start_time = empty($value[0]) ? 0 : strtotime($value[0]);
  212. $end_time = empty($value[1]) ? 0 : strtotime($value[1]);
  213. if ($start_time > 0 && $end_time > 0) {
  214. $query->whereBetweenTime('create_time', $start_time, $end_time);
  215. } else if ($start_time > 0 && $end_time == 0) {
  216. $query->where([['create_time', '>=', $start_time]]);
  217. } else if ($start_time == 0 && $end_time > 0) {
  218. $query->where([['create_time', '<=', $end_time]]);
  219. }
  220. }
  221. }