ShopMember.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\shop;
  13. use app\model\BaseModel;
  14. use app\model\system\Stat;
  15. use think\facade\Db;
  16. use app\model\system\Address;
  17. /**
  18. * 店铺会员表
  19. */
  20. class ShopMember extends BaseModel
  21. {
  22. /**
  23. * 添加店铺关注会员
  24. * @param $site_id
  25. * @param $member_id
  26. * @param int $is_subscribe
  27. * @return array
  28. */
  29. public function addShopMember($site_id, $member_id, $is_subscribe = 1)
  30. {
  31. $shop_member_info = model("shop_member")->getInfo([ [ 'site_id', '=', $site_id ], [ 'member_id', '=', $member_id ] ], 'id');
  32. if (!empty($shop_member_info)) {
  33. $res = model("shop_member")->update([ 'is_subscribe' => $is_subscribe, 'create_time' => time(), 'cancel_time' => 0 ], [ [ 'site_id', '=', $site_id ], [ 'member_id', '=', $member_id ] ]);
  34. } else {
  35. $data = [
  36. 'site_id' => $site_id,
  37. 'member_id' => $member_id,
  38. 'subscribe_time' => time(),
  39. 'is_subscribe' => $is_subscribe,
  40. 'create_time' => time(),
  41. ];
  42. $res = model("shop_member")->add($data);
  43. }
  44. model("shop")->setInc([ [ 'site_id', '=', $site_id ] ], 'sub_num');
  45. //添加统计
  46. $stat = new Stat();
  47. $stat->addShopStat([ 'collect_shop' => 1, 'site_id' => $site_id ]);
  48. return $this->success($res);
  49. }
  50. /**
  51. * 取消店铺关注会员
  52. * @param array $data
  53. */
  54. public function deleteShopMember($site_id, $member_id)
  55. {
  56. $is_sub = $this->isSubscribe($site_id, $member_id);
  57. if ($is_sub['data'] == 1) {
  58. $res = model("shop_member")->update([ 'is_subscribe' => 0, 'cancel_time' => time(), 'create_time' => 0 ], [ [ 'site_id', '=', $site_id ], [ 'member_id', '=', $member_id ] ]);
  59. return $this->success($res);
  60. } else {
  61. return $this->error();
  62. }
  63. }
  64. /**
  65. * 检测是否关注
  66. * @param int $site_id
  67. * @param int $member_id
  68. * @return number
  69. */
  70. public function isSubscribe($site_id, $member_id)
  71. {
  72. $info = model("shop_member")->getInfo([ [ 'site_id', '=', $site_id ], [ 'member_id', '=', $member_id ], [ 'is_subscribe', '=', 1 ] ], 'id');
  73. if (empty($info)) {
  74. return $this->success(0);
  75. } else {
  76. return $this->success(1);
  77. }
  78. }
  79. /**
  80. * 获取店铺会员分页列表
  81. * @param array $condition
  82. * @param number $page
  83. * @param string $page_size
  84. * @param string $order
  85. * @param string $field
  86. */
  87. public function getShopMemberPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '')
  88. {
  89. $field = 'nm.member_id, nm.source_member, nm.username, nm.nickname, nm.mobile, nm.email, nm.headimg, nm.status, nsm.subscribe_time, nsm.site_id, nsm.is_subscribe';
  90. $alias = 'nsm';
  91. $join = [
  92. [
  93. 'member nm',
  94. 'nsm.member_id = nm.member_id',
  95. 'inner'
  96. ],
  97. ];
  98. $list = model("shop_member")->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  99. return $this->success($list);
  100. }
  101. /**
  102. * 获取会员店铺分页列表
  103. * @param array $condition
  104. * @param number $page
  105. * @param string $page_size
  106. * @param string $order
  107. * @param string $field
  108. */
  109. 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')
  110. {
  111. $alias = 'nsm';
  112. $join = [
  113. [
  114. 'shop ns',
  115. 'nsm.site_id = ns.site_id',
  116. 'inner'
  117. ],
  118. ];
  119. $list = model("shop_member")->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  120. return $this->success($list);
  121. }
  122. /**
  123. * 获取店铺会员数量
  124. * @param unknown $condition
  125. * @param string $alias
  126. * @param unknown $join
  127. */
  128. public function getMemberCount($condition, $alias = 'a', $join = [], $group = null){
  129. $db = Db::name('shop_member')->where($condition);
  130. if (!empty($join)) {
  131. $db = $this->parseJoin($db->alias($alias), $join);
  132. }
  133. if(!empty($group)){
  134. $db = $db->group($group);
  135. }
  136. $count = $db->count();
  137. return $this->success($count);
  138. }
  139. /**
  140. * 获取店铺已购会员数
  141. */
  142. public function getPurchasedMemberCount($site_id){
  143. $prefix = config("database")["connections"]["mysql"]["prefix"];
  144. $res = model("shop_member")->query("SELECT GROUP_CONCAT(member_id) as member_id FROM {$prefix}shop_member WHERE site_id = {$site_id}");
  145. if (isset($res[0]) && isset($res[0]['member_id']) && !empty($res[0]['member_id'])) {
  146. $condition = [
  147. ['site_id', '=', $site_id],
  148. ['member_id', 'in', $res[0]['member_id']]
  149. ];
  150. $res = model("order")->getList($condition, 'order_id', '', 'a', [], 'member_id');
  151. return $this->success(count($res));
  152. }
  153. return $this->success(0);
  154. }
  155. /**
  156. * 按地域分布查询会员数量
  157. * @param unknown $site_id
  158. * @param string $handle
  159. */
  160. public function getMemberCountByArea($site_id, $handle = false){
  161. $total_count = $this->getMemberCount([ ['site_id', '=', $site_id] ]);
  162. $address = new Address();
  163. $list = $address->getAreaList([ ['pid', '=', 0] ], 'id,shortname', 'sort asc');
  164. $data = [];
  165. if ($total_count['data']) {
  166. foreach ($list['data'] as $item) {
  167. $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');
  168. if ($handle) {
  169. if ($count['data'] > 0) {
  170. array_push($data, [
  171. 'name' => $item['shortname'],
  172. 'value' => $count['data'],
  173. 'ratio' => $count['data'] > 0 ? sprintf("%.2f", $count['data'] / $total_count['data'] * 100) : 0
  174. ]);
  175. }
  176. } else {
  177. array_push($data, [
  178. 'name' => $item['shortname'],
  179. 'value' => $count['data'],
  180. 'ratio' => $count['data'] > 0 ? sprintf("%.2f", $count['data'] / $total_count['data'] * 100) : 0
  181. ]);
  182. }
  183. }
  184. }
  185. if ($handle) {
  186. array_multisort(array_column($data,'value'), SORT_DESC, $data);
  187. }
  188. return $this->success([
  189. 'page_count' => 1,
  190. 'count' => $total_count['data'],
  191. 'list' => $data
  192. ]);
  193. }
  194. /**
  195. * 处理表连接
  196. * @param unknown $db_obj
  197. * @param unknown $join
  198. */
  199. protected function parseJoin($db_obj, $join)
  200. {
  201. foreach ($join as $item) {
  202. list($table, $on, $type) = $item;
  203. $type = strtolower($type);
  204. switch ($type)
  205. {
  206. case "left":
  207. $db_obj = $db_obj->leftJoin($table, $on);
  208. break;
  209. case "inner":
  210. $db_obj = $db_obj->join($table, $on);
  211. break;
  212. case "right":
  213. $db_obj = $db_obj->rightjoin($table, $on);
  214. break;
  215. case "full":
  216. $db_obj = $db_obj->fulljoin($table, $on);
  217. break;
  218. default:
  219. break;
  220. }
  221. }
  222. return $db_obj;
  223. }
  224. /**
  225. * 查询会员信息
  226. * @param unknown $where
  227. * @param string $field
  228. * @param string $alias
  229. * @param string $join
  230. * @param string $data
  231. */
  232. public function getMemberInfo($where = [], $field = true, $alias = 'a', $join = null, $data = null){
  233. $info = model("shop_member")->getInfo($where, $field, $alias, $join, $data);
  234. if (empty($info)) return $this->error('', 'MEMBER_NOT_EXIST');
  235. else return $this->success($info);
  236. }
  237. }