UserBill.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace app\admin\model\user;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. use think\facade\Db;
  6. /**
  7. * 用户消费新增金额明细 model
  8. * Class User
  9. * @package app\admin\model\user
  10. */
  11. class UserBill extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'user_bill';
  23. use ModelTrait;
  24. /*
  25. * 获取总佣金
  26. * */
  27. public static function getBrokerage($uid, $category = 'now_money', $type = 'brokerage', $where)
  28. {
  29. return self::getModelTime($where, self::where('uid', 'in', $uid)->where('category', $category)
  30. ->where('type', $type)->where('pm', 1)->where('status', 1))->sum('number');
  31. }
  32. //修改积分减少积分记录
  33. public static function expend($title, $uid, $category, $type, $number, $link_id = 0, $balance = 0, $mark = '', $status = 1, $admin_mark = '')
  34. {
  35. $pm = 0;
  36. $add_time = time();
  37. return self::create(compact('title', 'uid', 'link_id', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time', 'admin_mark'));
  38. }
  39. //修改积分增加积分记录
  40. public static function income($title, $uid, $category, $type, $number, $link_id = 0, $balance = 0, $mark = '', $status = 1, $admin_mark = '')
  41. {
  42. $pm = 1;
  43. $add_time = time();
  44. return self::create(compact('title', 'uid', 'link_id', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time', 'admin_mark'));
  45. }
  46. //获取柱状图和饼状图数据
  47. public static function getUserBillChart($where, $category = 'now_money', $type = 'brokerage', $pm = 1, $zoom = 15)
  48. {
  49. $model = self::getModelTime($where, new self());
  50. $list = $model->field('FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time,sum(number) as sum_number')
  51. ->order('un_time asc')
  52. ->where('category', $category)
  53. ->where('type', $type)
  54. ->where('pm', $pm)
  55. ->group('un_time')
  56. ->select();
  57. if (count($list)) $list = $list->toArray();
  58. $legdata = [];
  59. $listdata = [];
  60. $dataZoom = '';
  61. foreach ($list as $item) {
  62. $legdata[] = $item['un_time'];
  63. $listdata[] = $item['sum_number'];
  64. }
  65. if (count($legdata) >= $zoom) $dataZoom = $legdata[$zoom - 1];
  66. //获取用户分布钱数
  67. $fenbulist = self::getModelTime($where, new self(), 'a.add_time')
  68. ->alias('a')
  69. ->join('user r', 'a.uid=r.uid')
  70. ->field('a.uid,sum(a.number) as sum_number,r.nickname')
  71. ->where('a.category', $category)
  72. ->where('a.type', $type)
  73. ->where('a.pm', $pm)
  74. ->order('sum_number desc')
  75. ->group('a.uid')
  76. ->limit(8)
  77. ->select();
  78. //获取用户当前时间段总钱数
  79. $sum_number = self::getModelTime($where, new self())
  80. ->alias('a')
  81. ->where('a.category', $category)
  82. ->where('a.type', $type)
  83. ->where('a.pm', $pm)
  84. ->sum('number');
  85. if (count($fenbulist)) $fenbulist = $fenbulist->toArray();
  86. $fenbudate = [];
  87. $fenbu_legend = [];
  88. $color = ['#ffcccc', '#99cc00', '#fd99cc', '#669966', '#66CDAA', '#ADFF2F', '#00BFFF', '#00CED1', '#66cccc', '#ff9900', '#ffcc00', '#336699', '#cccc00', '#99ccff', '#990066'];
  89. foreach ($fenbulist as $key => $value) {
  90. $fenbu_legend[] = $value['nickname'];
  91. $items['name'] = $value['nickname'];
  92. $items['value'] = bcdiv($value['sum_number'], $sum_number, 2) * 100;
  93. $items['itemStyle']['color'] = $color[$key];
  94. $fenbudate[] = $items;
  95. }
  96. return compact('legdata', 'listdata', 'fenbudate', 'fenbu_legend', 'dataZoom');
  97. }
  98. //获取头部信息
  99. public static function getRebateBadge($where)
  100. {
  101. $datawhere = ['category' => 'now_money', 'type' => 'brokerage', 'pm' => 1];
  102. return [
  103. [
  104. 'name' => '返利数(笔)',
  105. 'field' => '个',
  106. 'count' => self::getModelTime($where, new self())->where($datawhere)->count(),
  107. 'content' => '返利总笔数',
  108. 'background_color' => 'layui-bg-blue',
  109. 'sum' => self::where($datawhere)->count(),
  110. 'class' => 'fa fa-bar-chart',
  111. ],
  112. [
  113. 'name' => '返利金额(元)',
  114. 'field' => '个',
  115. 'count' => self::getModelTime($where, new self())->where($datawhere)->sum('number'),
  116. 'content' => '返利总金额',
  117. 'background_color' => 'layui-bg-cyan',
  118. 'sum' => self::where($datawhere)->sum('number'),
  119. 'class' => 'fa fa-line-chart',
  120. ],
  121. ];
  122. }
  123. //获取返佣用户信息列表
  124. public static function getFanList($where)
  125. {
  126. $list = self::alias('a')->join('user r', 'a.uid=r.uid')
  127. ->where('a.category', 'now_money')
  128. ->where('a.type', 'brokerage')
  129. ->where('a.pm', 1)
  130. ->order('a.number desc')
  131. ->join('store_order o', 'o.id=a.link_id')
  132. ->field('o.order_id,FROM_UNIXTIME(a.add_time,"%Y-%c-%d") as add_time,a.uid,o.uid as down_uid,r.nickname,r.avatar,r.spread_uid,r.level,a.number')
  133. ->page((int)$where['page'], (int)$where['limit'])
  134. ->select();
  135. if (count($list)) $list = $list->toArray();
  136. return $list;
  137. }
  138. //获取返佣用户总人数
  139. public static function getFanCount()
  140. {
  141. return self::alias('a')->join('user r', 'a.uid=r.uid')->join('store_order o', 'o.id=a.link_id')->where('a.category', 'now_money')->where('a.type', 'brokerage')->where('a.pm', 1)->count();
  142. }
  143. //获取用户充值数据
  144. public static function getEchartsRecharge($where, $limit = 15)
  145. {
  146. $datawhere = ['category' => 'now_money', 'pm' => 1];
  147. $list = self::getModelTime($where, self::where($datawhere)->where('type', 'in', ['recharge', 'system_add']))
  148. ->field('sum(number) as sum_money,FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time,count(id) as count')
  149. ->group('un_time')
  150. ->order('un_time asc')
  151. ->select();
  152. if (count($list)) $list = $list->toArray();
  153. $sum_count = self::getModelTime($where, self::where($datawhere)->where('type', 'in', 'recharge,system_add'))->count();
  154. $xdata = [];
  155. $seriesdata = [];
  156. $data = [];
  157. $zoom = '';
  158. foreach ($list as $value) {
  159. $xdata[] = $value['un_time'];
  160. $seriesdata[] = $value['sum_money'];
  161. $data[] = $value['count'];
  162. }
  163. if (count($xdata) > $limit) {
  164. $zoom = $xdata[$limit - 5];
  165. }
  166. return compact('xdata', 'seriesdata', 'data', 'zoom');
  167. }
  168. //获取佣金提现列表
  169. public static function getExtrctOneList($where, $uid)
  170. {
  171. $list = self::setOneWhere($where, $uid)
  172. ->field('number,link_id,mark,FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i:%s") as _add_time,status')
  173. ->select();
  174. count($list) && $list = $list->toArray();
  175. $count = self::setOneWhere($where, $uid)->count();
  176. foreach ($list as &$value) {
  177. $value['order_id'] = Db::name('store_order')->where('order_id', $value['link_id'])->value('order_id');
  178. }
  179. return ['data' => $list, 'count' => $count];
  180. }
  181. //设置单个用户查询
  182. public static function setOneWhere($where, $uid)
  183. {
  184. $model = self::where('uid', $uid)->where('category', 'now_money')->where('type', 'brokerage');
  185. $time['data'] = '';
  186. if (strlen(trim($where['start_time'])) && strlen(trim($where['end_time']))) {
  187. $time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
  188. $model = self::getModelTime($time, $model);
  189. }
  190. if (strlen(trim($where['nickname']))) {
  191. $model = $model->where('link_id|mark', 'like', "%$where[nickname]%");
  192. }
  193. return $model;
  194. }
  195. //查询积分个人明细
  196. public static function getOneIntegralList($where)
  197. {
  198. return self::setWhereList(
  199. $where, '',
  200. // ['deduction','system_add','sign'],
  201. ['title', 'number', 'balance', 'mark', 'FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time'],
  202. 'integral'
  203. );
  204. }
  205. //查询个人签到明细
  206. public static function getOneSignList($where)
  207. {
  208. return self::setWhereList(
  209. $where, 'sign',
  210. ['title', 'number', 'mark', 'FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time']
  211. );
  212. }
  213. //查询个人余额变动记录
  214. public static function getOneBalanceChangList($where)
  215. {
  216. $list = self::setWhereList(
  217. $where, '',
  218. // ['system_add','pay_product','extract','pay_product_refund','system_sub','brokerage','recharge','user_recharge_refund'],
  219. ['FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time', 'title', 'type', 'mark', 'number', 'balance', 'pm', 'status'],
  220. 'now_money'
  221. );
  222. foreach ($list as &$item) {
  223. switch ($item['type']) {
  224. case 'system_add':
  225. $item['_type'] = '系统添加';
  226. break;
  227. case 'pay_product':
  228. $item['_type'] = '商品购买';
  229. break;
  230. case 'extract':
  231. $item['_type'] = '提现';
  232. break;
  233. case 'pay_product_refund':
  234. $item['_type'] = '退款';
  235. break;
  236. case 'system_sub':
  237. $item['_type'] = '系统减少';
  238. break;
  239. case 'brokerage':
  240. $item['_type'] = '系统返佣';
  241. break;
  242. case 'recharge':
  243. $item['_type'] = '余额充值';
  244. break;
  245. case 'user_recharge_refund':
  246. $item['_type'] = '系统退款';
  247. break;
  248. }
  249. $item['_pm'] = $item['pm'] == 1 ? '获得' : '支出';
  250. }
  251. return $list;
  252. }
  253. //设置where条件分页.返回数据
  254. public static function setWhereList($where, $type = '', $field = [], $category = 'integral')
  255. {
  256. $models = self::where('uid', $where['uid'])
  257. ->where('category', $category)
  258. ->page((int)$where['page'], (int)$where['limit'])
  259. ->order('id', 'desc')
  260. ->field($field);
  261. if (is_array($type)) {
  262. $models = $models->where('type', 'in', $type);
  263. } else {
  264. if (!empty($type)) $models = $models->where('type', $type);
  265. }
  266. return ($list = $models->select()) && count($list) ? $list->toArray() : [];
  267. }
  268. //获取积分统计头部信息
  269. public static function getScoreBadgeList($where)
  270. {
  271. return [
  272. [
  273. 'name' => '历史总积分',
  274. 'field' => '个',
  275. 'count' => self::getModelTime($where, new self())->where('category', 'integral')->where('type', 'in', 'gain,system_sub,deduction,sign')->sum('number'),
  276. 'background_color' => 'layui-bg-blue',
  277. 'col' => 4,
  278. ],
  279. [
  280. 'name' => '已使用积分',
  281. 'field' => '个',
  282. 'count' => self::getModelTime($where, new self())->where('category', 'integral')->where('type', 'deduction')->sum('number'),
  283. 'background_color' => 'layui-bg-cyan',
  284. 'col' => 4,
  285. ],
  286. [
  287. 'name' => '未使用积分',
  288. 'field' => '个',
  289. 'count' => self::getModelTime($where, Db::name('user'))->sum('integral'),
  290. 'background_color' => 'layui-bg-cyan',
  291. 'col' => 4,
  292. ],
  293. ];
  294. }
  295. //获取积分统计曲线图和柱状图
  296. public static function getScoreCurve($where)
  297. {
  298. //发放积分趋势图
  299. $list = self::getModelTime($where, self::where('category', 'integral')
  300. ->field('FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time,sum(number) as sum_number')
  301. ->group('_add_time')->order('_add_time asc'))->select()->toArray();
  302. $date = [];
  303. $zoom = '';
  304. $seriesdata = [];
  305. foreach ($list as $item) {
  306. $date[] = $item['_add_time'];
  307. $seriesdata[] = $item['sum_number'];
  308. }
  309. unset($item);
  310. if (count($date) > $where['limit']) {
  311. $zoom = $date[$where['limit'] - 5];
  312. }
  313. //使用积分趋势图
  314. $deductionlist = self::getModelTime($where, self::where('category', 'integral')->where('type', 'deduction')
  315. ->field('FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time,sum(number) as sum_number')
  316. ->group('_add_time')->order('_add_time asc'))->select()->toArray();
  317. $deduction_date = [];
  318. $deduction_zoom = '';
  319. $deduction_seriesdata = [];
  320. foreach ($deductionlist as $item) {
  321. $deduction_date[] = $item['_add_time'];
  322. $deduction_seriesdata[] = $item['sum_number'];
  323. }
  324. if (count($deductionlist) > $where['limit']) {
  325. $deduction_zoom = $deductionlist[$where['limit'] - 5];
  326. }
  327. return compact('date', 'seriesdata', 'zoom', 'deduction_date', 'deduction_zoom', 'deduction_seriesdata');
  328. }
  329. /**
  330. * 用户获得总佣金
  331. * @return float
  332. */
  333. public static function getBrokerageCount()
  334. {
  335. return self::where('type', 'brokerage')->where('category', 'now_money')->where('status', 1)->where('pm', 1)->sum('number');
  336. }
  337. }