StoreMember.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\store\model;
  11. use app\model\BaseModel;
  12. use think\facade\Db;
  13. use app\model\system\Address;
  14. /**
  15. * 店铺会员表
  16. */
  17. class StoreMember extends BaseModel
  18. {
  19. /**
  20. * 添加店铺关注会员
  21. * @param $site_id
  22. * @param $member_id
  23. * @param int $is_subscribe
  24. * @return array
  25. */
  26. public function addStoreMember($store_id, $member_id)
  27. {
  28. $shop_member_info = model("store_member")->getInfo([ [ 'store_id', '=', $store_id ], [ 'member_id', '=', $member_id ] ], 'id');
  29. if (!empty($shop_member_info)) {
  30. return $this->success();
  31. } else {
  32. $data = [
  33. 'store_id' => $store_id,
  34. 'member_id' => $member_id,
  35. 'create_time' => time(),
  36. ];
  37. $res = model("store_member")->add($data);
  38. }
  39. return $this->success($res);
  40. }
  41. /**
  42. * 获取店铺会员分页列表
  43. * @param array $condition
  44. * @param number $page
  45. * @param string $page_size
  46. * @param string $order
  47. * @param string $field
  48. */
  49. public function getStoreMemberPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '')
  50. {
  51. $field = 'nm.member_id, nm.source_member, nm.username, nm.nickname, nm.mobile, nm.email, nm.headimg, nm.status, nm.member_level,nm.member_level_name,nm.balance,nm.balance_money,nsm.store_id, nsm.create_time';
  52. $alias = 'nsm';
  53. $join = [
  54. [
  55. 'member nm',
  56. 'nsm.member_id = nm.member_id',
  57. 'inner'
  58. ],
  59. ];
  60. $list = model("store_member")->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  61. return $this->success($list);
  62. }
  63. /**
  64. * 获取会员店铺分页列表
  65. * @param array $condition
  66. * @param number $page
  67. * @param string $page_size
  68. * @param string $order
  69. * @param string $field
  70. */
  71. public function getMemberShopPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'nsm.subscribe_time desc', $field = 'ns.site_id, ns.site_name, ns.is_own, ns.logo, ns.telephone,ns.sub_num, nsm.subscribe_time,ns.seo_description,ns.shop_desccredit,ns.shop_servicecredit,ns.shop_deliverycredit,ns.shop_sales,ns.shop_sales,ns.is_own')
  72. {
  73. $alias = 'nsm';
  74. $join = [
  75. [
  76. 'shop ns',
  77. 'nsm.site_id = ns.site_id',
  78. 'inner'
  79. ],
  80. ];
  81. $list = model("shop_member")->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  82. return $this->success($list);
  83. }
  84. /**
  85. * 获取门店会员数量
  86. * @param $condition
  87. * @param string $alias
  88. * @param array $join
  89. * @param null $group
  90. * @return array
  91. */
  92. public function getMemberCount($condition, $alias = 'a', $join = [], $group = null)
  93. {
  94. $db = Db::name('store_member')->where($condition);
  95. if (!empty($join)) {
  96. $db = $this->parseJoin($db->alias($alias), $join);
  97. }
  98. if (!empty($group)) {
  99. $db = $db->group($group);
  100. }
  101. $count = $db->count();
  102. return $this->success($count);
  103. }
  104. /**
  105. * 获取门店已购会员数
  106. */
  107. public function getPurchasedMemberCount($store_id)
  108. {
  109. $prefix = config("database")[ "connections" ][ "mysql" ][ "prefix" ];
  110. $res = model("store_member")->query("SELECT GROUP_CONCAT(member_id) as member_id FROM {$prefix}store_member WHERE store_id = {$store_id}");
  111. if (isset($res[ 0 ]) && isset($res[ 0 ][ 'member_id' ]) && !empty($res[ 0 ][ 'member_id' ])) {
  112. $condition = [
  113. [ 'delivery_store_id', '=', $store_id ],
  114. [ 'member_id', 'in', $res[ 0 ][ 'member_id' ] ]
  115. ];
  116. $res = model('order')->getList($condition, 'order_id', '', 'a', [], 'member_id');
  117. return $this->success(count($res));
  118. }
  119. return $this->success(0);
  120. }
  121. /**
  122. * 按地域分布查询会员数量
  123. * @param $site_id
  124. * @param bool $handle
  125. * @return array
  126. */
  127. public function getMemberCountByArea($site_id, $handle = false)
  128. {
  129. $total_count = $this->getMemberCount([ [ 'site_id', '=', $site_id ] ]);
  130. $address = new Address();
  131. $list = $address->getAreaList([ [ 'pid', '=', 0 ] ], 'id,shortname', 'sort asc');
  132. $data = [];
  133. if ($total_count[ 'data' ]) {
  134. foreach ($list[ 'data' ] as $item) {
  135. $count = $this->getMemberCount([ [ 'nsm.site_id', '=', $site_id ], [ 'nma.is_default', '=', 1 ], [ 'nma.province_id', '=', $item[ 'id' ] ] ], 'nsm', [ [ 'member_address nma', 'nsm.member_id = nma.member_id', 'left' ] ], 'nma.member_id');
  136. if ($handle) {
  137. if ($count[ 'data' ] > 0) {
  138. array_push($data, [
  139. 'name' => $item[ 'shortname' ],
  140. 'value' => $count[ 'data' ],
  141. 'ratio' => $count[ 'data' ] > 0 ? sprintf("%.2f", $count[ 'data' ] / $total_count[ 'data' ] * 100) : 0
  142. ]);
  143. }
  144. } else {
  145. array_push($data, [
  146. 'name' => $item[ 'shortname' ],
  147. 'value' => $count[ 'data' ],
  148. 'ratio' => $count[ 'data' ] > 0 ? sprintf("%.2f", $count[ 'data' ] / $total_count[ 'data' ] * 100) : 0
  149. ]);
  150. }
  151. }
  152. }
  153. if ($handle) {
  154. array_multisort(array_column($data, 'value'), SORT_DESC, $data);
  155. }
  156. return $this->success([
  157. 'page_count' => 1,
  158. 'count' => $total_count[ 'data' ],
  159. 'list' => $data
  160. ]);
  161. }
  162. /**
  163. * 处理表连接
  164. * @param unknown $db_obj
  165. * @param unknown $join
  166. */
  167. protected function parseJoin($db_obj, $join)
  168. {
  169. foreach ($join as $item) {
  170. list($table, $on, $type) = $item;
  171. $type = strtolower($type);
  172. switch ( $type ) {
  173. case "left":
  174. $db_obj = $db_obj->leftJoin($table, $on);
  175. break;
  176. case "inner":
  177. $db_obj = $db_obj->join($table, $on);
  178. break;
  179. case "right":
  180. $db_obj = $db_obj->rightjoin($table, $on);
  181. break;
  182. case "full":
  183. $db_obj = $db_obj->fulljoin($table, $on);
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. return $db_obj;
  190. }
  191. /**
  192. * 查询会员信息
  193. * @param unknown $where
  194. * @param string $field
  195. * @param string $alias
  196. * @param string $join
  197. * @param string $data
  198. */
  199. public function getMemberInfo($where = [], $field = true, $alias = 'a', $join = null, $data = null)
  200. {
  201. $info = model("store_member")->getInfo($where, $field, $alias, $join, $data);
  202. if (empty($info)) return $this->error('', 'MEMBER_NOT_EXIST');
  203. else return $this->success($info);
  204. }
  205. }