Account.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace addon\store\siteapi\controller;
  13. use app\model\member\Balance as MemberBalance;
  14. use app\model\member\MemberAccount;
  15. use app\model\member\Point as MemberPoint;
  16. use app\model\system\Config;
  17. use app\model\system\Promotion as PromotionModel;
  18. /**
  19. * 积分控制器
  20. * Class Account
  21. * @package addon\shop\siteapi\controller
  22. */
  23. class Account extends BaseStoreApi
  24. {
  25. /**
  26. * 积分流水
  27. * @return mixed
  28. */
  29. public function pointLists()
  30. {
  31. $from_type = isset($this->params[ 'from_type' ]) ? $this->params[ 'from_type' ] : '';
  32. $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
  33. $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
  34. $start_date = isset($this->params[ 'start_date' ]) ? $this->params[ 'start_date' ] : '';
  35. $end_date = isset($this->params[ 'end_date' ]) ? $this->params[ 'end_date' ] : '';
  36. $search_text_type = isset($this->params[ 'search_text_type' ]) ? $this->params[ 'search_text_type' ] : '';
  37. $search_text = isset($this->params[ 'search_text' ]) ? $this->params[ 'search_text' ] : '';
  38. $type = isset($this->params[ 'type' ]) ? $this->params[ 'type' ] : '';
  39. $condition = [
  40. [ 'ma.site_id', '=', $this->site_id ],
  41. [ 'ma.account_type', '=', 'point' ]
  42. ];
  43. //来源类型
  44. if ($from_type != '') {
  45. $condition[] = [ 'ma.from_type', '=', $from_type ];
  46. }
  47. //发生时间
  48. if ($start_date != '' && $end_date != '') {
  49. $condition[] = [ 'ma.create_time', 'between', [ strtotime($start_date), strtotime($end_date) ] ];
  50. } else if ($start_date != '' && $end_date == '') {
  51. $condition[] = [ 'ma.create_time', '>=', strtotime($start_date) ];
  52. } else if ($start_date == '' && $end_date != '') {
  53. $condition[] = [ 'ma.create_time', '<=', strtotime($end_date) ];
  54. }
  55. if (!empty($search_text)) {
  56. if ($search_text_type == 'nickname') {
  57. $condition[] = [ 'm.nickname', 'like', "%" . $search_text . "%" ];
  58. } else {
  59. $condition[] = [ 'm.mobile', 'like', "%" . $search_text . "%" ];
  60. }
  61. }
  62. if ($type !== '') {
  63. if ($type == 2) {//获取
  64. $condition[] = [ 'ma.account_data', '>', 0 ];
  65. }
  66. if ($type == 3) {//消费
  67. $condition[] = [ 'ma.account_data', '<', 0 ];
  68. }
  69. }
  70. $join = [
  71. [ 'member m', 'm.member_id = ma.member_id', 'left' ],
  72. ];
  73. $field = 'ma.member_id,ma.fans_id,ma.account_type,ma.account_data,ma.from_type,ma.type_name,ma.remark,ma.create_time,m.nickname as m_nickname,m.headimg as m_headimg, m.mobile as m_mobile';
  74. $member_account_model = new MemberAccount();
  75. $res = $member_account_model->getMemberAccountPageList($condition, $page, $page_size, 'ma.create_time desc', $field, 'ma', $join);
  76. return $this->response($res);
  77. }
  78. /**
  79. * 积分
  80. */
  81. public function point()
  82. {
  83. $from_type = isset($this->params[ 'from_type' ]) ? $this->params[ 'from_type' ] : '';
  84. $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
  85. $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
  86. $start_date = isset($this->params[ 'start_date' ]) ? $this->params[ 'start_date' ] : '';
  87. $end_date = isset($this->params[ 'end_date' ]) ? $this->params[ 'end_date' ] : '';
  88. $from_type_id = isset($this->params[ 'from_type_id' ]) ? $this->params[ 'from_type_id' ] : '';
  89. $condition = [
  90. [ 'a.site_id', '=', $this->site_id ],
  91. ];
  92. //来源类型
  93. if ($from_type != '') {
  94. $condition[] = [ 'a.from_type', '=', $from_type ];
  95. }
  96. if ($from_type_id != '') {
  97. $condition[] = [ 'a.from_type_id', '=', $from_type_id ];
  98. }
  99. //发生时间
  100. if ($start_date != '' && $end_date != '') {
  101. $condition[] = [ 'a.create_time', 'between', [ strtotime($start_date), strtotime($end_date) ] ];
  102. } else if ($start_date != '' && $end_date == '') {
  103. $condition[] = [ 'a.create_time', '>=', strtotime($start_date) ];
  104. } else if ($start_date == '' && $end_date != '') {
  105. $condition[] = [ 'a.create_time', '<=', strtotime($end_date) ];
  106. }
  107. $join = [
  108. [ 'goods g', "g.goods_id = a.from_type_id and a.from_type = 'goods' ", 'left' ],
  109. [ 'promotion p', "p.promotion_id = a.from_type_id and a.from_type = 'promotion' ", 'left' ],
  110. ];
  111. $order = 'a.state WHEN 1 WHEN 0 WHEN 2 END';
  112. $field = "a.*,IF(a.from_type = 'goods',(g.goods_image),(p.image)) as images,IF(a.from_type = 'goods',(g.start_time),(p.start_time)) as start_time,IF(a.from_type = 'goods',(g.end_time),(p.end_time)) as end_time,IF(a.from_type = 'goods',(g.promotion_type),(p.promotion_type)) as promotion_type";
  113. $point = new MemberPoint();
  114. $res = $point->getPointPageList($condition, $page, $page_size, $order, $field, $alias = 'a', $join);
  115. $promotion_model = new PromotionModel();
  116. $promotion_type_list = $promotion_model->getPromotionType('all');
  117. if (!empty($res[ 'data' ][ 'list' ])) {
  118. foreach ($res[ 'data' ][ 'list' ] as $k => $v) {
  119. $res[ 'data' ][ 'list' ][ $k ][ 'promotion_type_name' ] = $promotion_type_list[ $v[ 'promotion_type' ] ][ 'title' ];
  120. $res[ 'data' ][ 'list' ][ $k ][ 'promotion_type_icon' ] = $promotion_type_list[ $v[ 'promotion_type' ] ][ 'icon' ];
  121. }
  122. }
  123. return $this->response($res);
  124. }
  125. /**
  126. * 积分配置项
  127. */
  128. public function pointConfig()
  129. {
  130. $point = new MemberPoint();
  131. $config = new Config();
  132. $config_type = $point->getConfigType();
  133. foreach ($config_type as $k => $v) {
  134. $config_info = $config->getConfig([ [ 'site_id', '=', $this->site_id ], [ 'app_module', '=', $this->app_module ], [ 'config_key', '=', $v[ 'key' ] ] ]);
  135. $config_type[ $k ][ 'config' ] = $config_info[ 'data' ];
  136. }
  137. return $this->response($this->success($config_type));
  138. }
  139. /**
  140. * 设置积分配置
  141. */
  142. public function setPointConfig()
  143. {
  144. $key = isset($this->params[ 'key' ]) ? $this->params[ 'key' ] : '';
  145. $value = isset($this->params[ 'value' ]) ? $this->params[ 'value' ] : '';
  146. $desc = isset($this->params[ 'desc' ]) ? $this->params[ 'desc' ] : '';
  147. $config = new Config();
  148. $res = $config->setConfig(json_decode($value), $desc, '1', [ [ 'site_id', '=', $this->site_id ], [ 'app_module', '=', $this->app_module ], [ 'config_key', '=', $key ] ]);
  149. return $this->response($this->success($res));
  150. }
  151. /**
  152. * 账户积分数据
  153. */
  154. public function pointAccount()
  155. {
  156. $account = new MemberAccount();
  157. //可用积分
  158. $surplus_point = $account->getAccountSum([ [ 'site_id', '=', $this->site_id ] ], 'point')[ 'data' ];
  159. //累计发放
  160. $total_point = $account->getMemberAccountSum([ [ 'site_id', '=', $this->site_id ], [ 'account_type', '=', 'point' ], [ 'account_data', '>', '0' ] ], 'account_data')[ 'data' ];
  161. //累计消耗
  162. $use_total_point = $account->getMemberAccountSum([ [ 'site_id', '=', $this->site_id ], [ 'account_type', '=', 'point' ], [ 'account_data', '<', '0' ] ], 'account_data')[ 'data' ];
  163. $data = [];
  164. $data[ 'surplus_point' ] = $surplus_point;
  165. $data[ 'total_point' ] = $total_point;
  166. $data[ 'use_total_point' ] = $use_total_point;
  167. return $this->response($this->success($data));
  168. }
  169. /**
  170. * 积分来源类型类型
  171. */
  172. public function pointType()
  173. {
  174. $point_type = ( new MemberAccount() )->getFromType();
  175. $point_type = $point_type[ 'point' ] ?? [];
  176. return $this->response($this->success($point_type));
  177. }
  178. /**
  179. * 余额类型
  180. */
  181. public function balanceType()
  182. {
  183. $balance_type = ( new MemberAccount() )->getFromType();
  184. $balance_type = $balance_type[ 'balance' ] ?? [];
  185. return $this->response($this->success($balance_type));
  186. }
  187. /**
  188. * 余额流水
  189. */
  190. public function balance()
  191. {
  192. $from_type = isset($this->params[ 'from_type' ]) ? $this->params[ 'from_type' ] : '';
  193. $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
  194. $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
  195. $start_date = isset($this->params[ 'start_date' ]) ? $this->params[ 'start_date' ] : '';
  196. $end_date = isset($this->params[ 'end_date' ]) ? $this->params[ 'end_date' ] : '';
  197. $from_type_id = isset($this->params[ 'from_type_id' ]) ? $this->params[ 'from_type_id' ] : '';
  198. $condition = [
  199. [ 'a.site_id', '=', $this->site_id ],
  200. ];
  201. //来源类型
  202. if ($from_type != '') {
  203. $condition[] = [ 'a.from_type', '=', $from_type ];
  204. }
  205. if ($from_type_id != '') {
  206. $condition[] = [ 'a.from_type_id', '=', $from_type_id ];
  207. }
  208. //发生时间
  209. if ($start_date != '' && $end_date != '') {
  210. $condition[] = [ 'a.create_time', 'between', [ strtotime($start_date), strtotime($end_date) ] ];
  211. } else if ($start_date != '' && $end_date == '') {
  212. $condition[] = [ 'a.create_time', '>=', strtotime($start_date) ];
  213. } else if ($start_date == '' && $end_date != '') {
  214. $condition[] = [ 'a.create_time', '<=', strtotime($end_date) ];
  215. }
  216. $balance = new MemberBalance();
  217. $join = [
  218. [ 'goods g', "g.goods_id = a.from_type_id and a.from_type = 'goods' ", 'left' ],
  219. [ 'promotion p', "p.promotion_id = a.from_type_id and a.from_type = 'promotion' ", 'left' ],
  220. ];
  221. $order = 'a.state WHEN 1 WHEN 0 WHEN 2 END';
  222. $field = "a.*,IF(a.from_type = 'goods',(g.goods_image),(p.image)) as images,IF(a.from_type = 'goods',(g.start_time),(p.start_time)) as start_time,IF(a.from_type = 'goods',(g.end_time),(p.end_time)) as end_time,IF(a.from_type = 'goods',(g.promotion_type),(p.promotion_type)) as promotion_type";
  223. $res = $balance->getBalancePageList($condition, $page, $page_size, $order, $field, $alias = 'a', $join);
  224. $promotion_model = new PromotionModel();
  225. $promotion_type_list = $promotion_model->getPromotionType('all');
  226. if (!empty($res[ 'data' ][ 'list' ])) {
  227. foreach ($res[ 'data' ][ 'list' ] as $k => $v) {
  228. $res[ 'data' ][ 'list' ][ $k ][ 'promotion_type_name' ] = $promotion_type_list[ $v[ 'promotion_type' ] ][ 'title' ];
  229. $res[ 'data' ][ 'list' ][ $k ][ 'promotion_type_icon' ] = $promotion_type_list[ $v[ 'promotion_type' ] ][ 'icon' ];
  230. }
  231. }
  232. return $this->response($res);
  233. }
  234. /**
  235. *账户余额数据
  236. */
  237. public function balanceAccount()
  238. {
  239. $site_id = $this->site_id;
  240. $account = new MemberAccount();
  241. //余额总额
  242. $balance_money = $account->getAccountSum([ [ 'site_id', '=', $site_id ] ], 'balance_money')[ 'data' ];
  243. $balance = $account->getAccountSum([ [ 'site_id', '=', $site_id ] ], 'balance')[ 'data' ];
  244. //累计发放
  245. $total_banance = $account->getMemberAccountSum([ [ 'site_id', '=', $site_id ], [ 'account_type', 'in', 'balance_money,balance' ], [ 'account_data', '>', '0' ] ], 'account_data')[ 'data' ];
  246. //累计消耗
  247. $use_total_banance = $account->getMemberAccountSum([ [ 'site_id', '=', $site_id ], [ 'account_type', 'in', 'balance_money,balance' ], [ 'account_data', '<', '0' ] ], 'account_data')[ 'data' ];
  248. $data = [];
  249. $data[ 'balance_money' ] = $balance_money;
  250. $data[ 'balance' ] = $balance;
  251. $data[ 'total_banance' ] = $total_banance;
  252. $data[ 'use_total_banance' ] = abs($use_total_banance);
  253. return $this->response($this->success($data));
  254. }
  255. /**
  256. * 余额流水
  257. * @return mixed
  258. */
  259. public function balanceLists()
  260. {
  261. $from_type = isset($this->params[ 'from_type' ]) ? $this->params[ 'from_type' ] : '';
  262. $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
  263. $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
  264. $start_date = isset($this->params[ 'start_date' ]) ? $this->params[ 'start_date' ] : '';
  265. $end_date = isset($this->params[ 'end_date' ]) ? $this->params[ 'end_date' ] : '';
  266. $search_text_type = isset($this->params[ 'search_text_type' ]) ? $this->params[ 'search_text_type' ] : '';
  267. $search_text = isset($this->params[ 'search_text' ]) ? $this->params[ 'search_text' ] : '';
  268. $type = isset($this->params[ 'type' ]) ? $this->params[ 'type' ] : '';
  269. $condition = [
  270. [ 'ma.site_id', '=', $this->site_id ],
  271. [ 'ma.account_type', 'in', [ 'balance', 'balance_money' ] ]
  272. ];
  273. //来源类型
  274. if ($from_type != '') {
  275. $condition[] = [ 'ma.from_type', '=', $from_type ];
  276. }
  277. //发生时间
  278. if ($start_date != '' && $end_date != '') {
  279. $condition[] = [ 'ma.create_time', 'between', [ strtotime($start_date), strtotime($end_date) ] ];
  280. } else if ($start_date != '' && $end_date == '') {
  281. $condition[] = [ 'ma.create_time', '>=', strtotime($start_date) ];
  282. } else if ($start_date == '' && $end_date != '') {
  283. $condition[] = [ 'ma.create_time', '<=', strtotime($end_date) ];
  284. }
  285. if (!empty($search_text)) {
  286. if ($search_text_type == 'nickname') {
  287. $condition[] = [ 'm.nickname', 'like', "%" . $search_text . "%" ];
  288. } else {
  289. $condition[] = [ 'm.mobile', 'like', "%" . $search_text . "%" ];
  290. }
  291. }
  292. if ($type !== '') {
  293. if ($type == 3) {//获取
  294. $condition[] = [ 'ma.account_data', '>', 0 ];
  295. }
  296. if ($type == 4) {//消费
  297. $condition[] = [ 'ma.account_data', '<', 0 ];
  298. }
  299. }
  300. $join = [
  301. [ 'member m', 'm.member_id = ma.member_id', 'left' ],
  302. ];
  303. $field = 'ma.member_id,ma.fans_id,ma.account_type,ma.account_data,ma.from_type,ma.type_name,ma.remark,ma.create_time,m.nickname as m_nickname,m.headimg as m_headimg, m.mobile as m_mobile';
  304. $member_account_model = new MemberAccount();
  305. $res = $member_account_model->getMemberAccountPageList($condition, $page, $page_size, 'ma.create_time desc', $field, 'ma', $join);
  306. return $this->response($res);
  307. }
  308. }