AccountService.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\AccountModel;
  13. use App\Models\MemberModel;
  14. use App\Services\BaseService;
  15. use Illuminate\Support\Facades\DB;
  16. /**
  17. * 交易管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class AccountService
  21. * @package App\Services\Common
  22. */
  23. class AccountService extends BaseService
  24. {
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * AccountService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new AccountModel();
  34. }
  35. /**
  36. * 静态入口
  37. * @return static|null
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = (new static());
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * @param $params
  48. * @param int $pageSize
  49. * @return array
  50. */
  51. public function getDataList($params, $pageSize = 15)
  52. {
  53. $where = ['a.mark' => 1];
  54. $status = isset($params['status'])? $params['status'] : 0;
  55. $type = isset($params['type'])? $params['type'] : 0;
  56. $coinType = isset($params['coin_type'])? $params['coin_type'] : 0;
  57. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  58. $shopId = isset($params['shop_id'])? $params['shop_id'] : 0;
  59. if($status>0){
  60. $where['a.status'] = $status;
  61. }
  62. if($type>0){
  63. $where['a.type'] = $type;
  64. }
  65. if($coinType>0){
  66. $where['a.coin_type'] = $coinType;
  67. }
  68. if($userId>0){
  69. $where['a.user_id'] = $userId;
  70. }
  71. if($shopId>0){
  72. $where['a.shop_id'] = $shopId;
  73. }
  74. $list = $this->model->from('account_log as a')
  75. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  76. ->leftJoin('shop as c', 'c.id', '=', 'a.shop_id')
  77. ->leftJoin('member as d', 'd.id', '=', 'a.source_uid')
  78. ->where($where)
  79. ->where(function ($query) use($params){
  80. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  81. if($keyword){
  82. $query->where('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%");
  83. }
  84. // 日期
  85. $date = isset($params['date']) ? $params['date'] : [];
  86. $start = isset($date[0])? $date[0] : '';
  87. $end = isset($date[1])? $date[1] : '';
  88. $end = $start>=$end? '' : $end;
  89. if ($start) {
  90. $query->where('a.create_time','>=', strtotime($start));
  91. }
  92. if($end){
  93. $query->where('a.create_time','<=', strtotime($end));
  94. }
  95. })
  96. ->select(['a.*','b.username','b.mobile','b.nickname','c.name as shop_name','d.nickname as source_username'])
  97. ->orderBy('a.create_time','desc')
  98. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  99. $list = $list? $list->toArray() :[];
  100. if($list){
  101. foreach($list['data'] as &$item){
  102. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  103. $titles = ['','交易','推广佣金','平台充值','平台扣除','佣金转换'];
  104. if(in_array($item['type'], [1,2])){
  105. $item['remark'] = $item['remark']? $item['remark'] : $titles[$item['type']];
  106. }
  107. if($item['coin_type'] == 2 && $item['type'] == 3){
  108. $item['remark'] = $item['remark']? $item['remark'] : ($item['money']>0?'平台充值':'平台扣除');
  109. }
  110. }
  111. }
  112. return [
  113. 'pageSize'=> $pageSize,
  114. 'total'=>isset($list['total'])? $list['total'] : 0,
  115. 'list'=> isset($list['data'])? $list['data'] : []
  116. ];
  117. }
  118. /**
  119. * 添加或编辑
  120. * @return array
  121. * @since 2020/11/11
  122. * @author laravel开发员
  123. */
  124. public function edit()
  125. {
  126. $data = request()->all();
  127. return parent::edit($data); // TODO: Change the autogenerated stub
  128. }
  129. /**
  130. * 获取店铺交易统计
  131. * @param $shopId
  132. * @param int $type
  133. * @param int $coinType
  134. * @return mixed
  135. */
  136. public function getShopAccountTotal($shopId, $type = 1, $coinType=1)
  137. {
  138. $where = ['shop_id'=>$shopId,'status'=>1,'mark'=>1];
  139. return $this->model->where($where)
  140. ->where(function($query) use($type, $coinType){
  141. if($type){
  142. $query->whereIn('type', is_array($type)? $type : [$type]);
  143. }
  144. if($coinType){
  145. $query->whereIn('coin_type', is_array($coinType)? $coinType : [$coinType]);
  146. }
  147. })
  148. ->sum('money');
  149. }
  150. /**
  151. * 获取交易数
  152. * @param $shopId
  153. * @param int $type
  154. * @param int $coinType
  155. * @return mixed
  156. */
  157. public function getShopAccountCount($shopId, $type = 1, $coinType=1)
  158. {
  159. $where = ['shop_id'=>$shopId,'status'=>1,'mark'=>1];
  160. if($coinType){
  161. $where['coin_type'] = $coinType;
  162. }
  163. return $this->model->where($where)
  164. ->where(function($query) use($type, $coinType){
  165. if($type){
  166. $query->whereIn('type', is_array($type)? $type : [$type]);
  167. }
  168. if($coinType){
  169. $query->whereIn('coin_type', is_array($coinType)? $coinType : [$coinType]);
  170. }
  171. })
  172. ->count('id');
  173. }
  174. /**
  175. * 调整用户佣金
  176. * @param $params
  177. * @return bool
  178. */
  179. public function changeBonus($params)
  180. {
  181. $userId = isset($params['user_id'])? $params['user_id']:0;
  182. $money = isset($params['money'])? $params['money']:0;
  183. $type = isset($params['type'])? $params['type']:0;
  184. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])->select(['id','login_shop_id','bonus','bonus_total'])->first();
  185. $bonus = isset($userInfo['bonus'])? $userInfo['bonus'] : 0;
  186. $bonusTotal = isset($userInfo['bonus_total'])? $userInfo['bonus_total'] : 0;
  187. if(empty($userInfo)){
  188. $this->error = '2201';
  189. return false;
  190. }
  191. // 扣除佣金验证
  192. if($type == 2 && $money > $bonus){
  193. $this->error = '2202';
  194. return false;
  195. }
  196. // 处理
  197. DB::beginTransaction();
  198. $money = $type==1? $money : -$money;
  199. $data = ['bonus'=> max(0, $bonus+$money), 'bonus_total'=> max(0, $bonusTotal+$money),'update_time'=>time()];
  200. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($data)){
  201. DB::rollBack();
  202. $this->error = '2203';
  203. return false;
  204. }
  205. $logData = [
  206. 'user_id'=> $userId,
  207. 'shop_id'=> $userInfo['login_shop_id'],
  208. 'type'=> 3,
  209. 'coin_type'=> 2,
  210. 'money'=> $money,
  211. 'balance'=> $bonus,
  212. 'create_time'=> time(),
  213. 'remark'=> '佣金调整',
  214. 'status'=> 1,
  215. 'mark'=>1
  216. ];
  217. if (!AccountModel::insertGetId($logData)) {
  218. $this->error = 2204;
  219. DB::rollBack();
  220. return true;
  221. }
  222. DB::commit();
  223. // 结算统计
  224. FinanceService::make()->settleBonus($bonus, 2);
  225. $this->error = 2205;
  226. return true;
  227. }
  228. /**
  229. * 调整用户积分
  230. * @param $params
  231. * @return bool
  232. */
  233. public function changeScore($params)
  234. {
  235. $userId = isset($params['user_id'])? $params['user_id']:0;
  236. $money = isset($params['money'])? $params['money']:0;
  237. $type = isset($params['type'])? $params['type']:0;
  238. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])->select(['id','login_shop_id','score'])->first();
  239. $score = isset($userInfo['score'])? $userInfo['score'] : 0;
  240. if(empty($userInfo)){
  241. $this->error = '2201';
  242. return false;
  243. }
  244. // 扣除积分验证
  245. if($type == 2 && $money > $score){
  246. $this->error = '2212';
  247. return false;
  248. }
  249. // 处理
  250. DB::beginTransaction();
  251. $money = $type==1? $money : -$money;
  252. $data = ['score'=> max(0, $score+$money),'update_time'=>time()];
  253. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($data)){
  254. DB::rollBack();
  255. $this->error = '2213';
  256. return false;
  257. }
  258. $logData = [
  259. 'user_id'=> $userId,
  260. 'shop_id'=> $userInfo['login_shop_id'],
  261. 'type'=> 3,
  262. 'coin_type'=> 3,
  263. 'money'=> $money,
  264. 'balance'=> $score,
  265. 'create_time'=> time(),
  266. 'remark'=> '积分调整',
  267. 'status'=> 1,
  268. 'mark'=>1
  269. ];
  270. if (!AccountModel::insertGetId($logData)) {
  271. $this->error = 2214;
  272. DB::rollBack();
  273. return true;
  274. }
  275. DB::commit();
  276. $this->error = 2215;
  277. return true;
  278. }
  279. }