SystemStoreBill.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace app\admin\model\system;
  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 SystemStoreBill extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'system_store_bill';
  23. use ModelTrait;
  24. /*
  25. * 获取总佣金
  26. * */
  27. public static function getBrokerage($uid, $category = 'brokerage_price', $type = '', $where = [])
  28. {
  29. $model = self::where('store_id', 'in', $uid)->where('category', $category)
  30. ->where('pm', 1)->where('status', 1);
  31. if ($type) $model = $model->where('type', $type);
  32. return self::getModelTime($where, $model)->sum('number');
  33. }
  34. //修改积分减少积分记录
  35. public static function expend($title, $store_id, $category, $type, $number, $link_id = 0, $balance = 0, $mark = '', $status = 1)
  36. {
  37. $pm = 0;
  38. $add_time = time();
  39. return self::create(compact('title', 'store_id', 'link_id', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time'));
  40. }
  41. //修改积分增加积分记录
  42. public static function income($title, $store_id, $category, $type, $number, $link_id = 0, $balance = 0, $mark = '', $status = 1)
  43. {
  44. $pm = 1;
  45. $add_time = time();
  46. return self::create(compact('title', 'store_id', 'link_id', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time'));
  47. }
  48. public static function getList($where, $store_id)
  49. {
  50. $model = self::where('store_id', $store_id);
  51. if (isset($where['time']) && $where['time'] != '') {
  52. $start_time = strtotime($where['time']);
  53. $end_time = strtotime('+1month', strtotime($where['time']));
  54. $model = $model->where('add_time', 'between', [$start_time, $end_time]);
  55. }
  56. if (isset($where['type']) && $where['type'] != '') {
  57. $model = $model->where('type', $where['type']);
  58. }
  59. $count = $model->count();
  60. $data = $model->order('add_time', 'desc')->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) {
  61. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  62. $item['_status'] = ($item['status'] == 1 ? '已结算' : ($item['status'] == 2 ? '已失效' : '待结算'));
  63. });
  64. return compact('count', 'data');
  65. }
  66. //获取柱状图和饼状图数据
  67. public static function getUserBillChart($where, $category = 'now_money', $type = 'brokerage', $pm = 1, $zoom = 15)
  68. {
  69. $model = self::getModelTime($where, new self());
  70. $list = $model->field('FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time,sum(number) as sum_number')
  71. ->order('un_time asc')
  72. ->where('category', $category)
  73. ->where('type', $type)
  74. ->where('pm', $pm)
  75. ->group('un_time')
  76. ->select();
  77. if (count($list)) $list = $list->toArray();
  78. $legdata = [];
  79. $listdata = [];
  80. $dataZoom = '';
  81. foreach ($list as $item) {
  82. $legdata[] = $item['un_time'];
  83. $listdata[] = $item['sum_number'];
  84. }
  85. if (count($legdata) >= $zoom) $dataZoom = $legdata[$zoom - 1];
  86. //获取用户分布钱数
  87. $fenbulist = self::getModelTime($where, new self(), 'a.add_time')
  88. ->alias('a')
  89. ->join('system_store r', 'a.store_id=r.id')
  90. ->field('a.store_id,sum(a.number) as sum_number,r.name')
  91. ->where('a.category', $category)
  92. ->where('a.type', $type)
  93. ->where('a.pm', $pm)
  94. ->order('sum_number desc')
  95. ->group('a.store_id')
  96. ->limit(8)
  97. ->select();
  98. //获取用户当前时间段总钱数
  99. $sum_number = self::getModelTime($where, new self())
  100. ->alias('a')
  101. ->where('a.category', $category)
  102. ->where('a.type', $type)
  103. ->where('a.pm', $pm)
  104. ->sum('number');
  105. if (count($fenbulist)) $fenbulist = $fenbulist->toArray();
  106. $fenbudate = [];
  107. $fenbu_legend = [];
  108. $color = ['#ffcccc', '#99cc00', '#fd99cc', '#669966', '#66CDAA', '#ADFF2F', '#00BFFF', '#00CED1', '#66cccc', '#ff9900', '#ffcc00', '#336699', '#cccc00', '#99ccff', '#990066'];
  109. foreach ($fenbulist as $key => $value) {
  110. $fenbu_legend[] = $value['nickname'];
  111. $items['name'] = $value['nickname'];
  112. $items['value'] = bcdiv($value['sum_number'], $sum_number, 2) * 100;
  113. $items['itemStyle']['color'] = $color[$key];
  114. $fenbudate[] = $items;
  115. }
  116. return compact('legdata', 'listdata', 'fenbudate', 'fenbu_legend', 'dataZoom');
  117. }
  118. //获取头部信息
  119. public static function getRebateBadge($where)
  120. {
  121. $datawhere = ['category' => 'now_money', 'type' => 'brokerage', 'pm' => 1];
  122. return [
  123. [
  124. 'name' => '返利数(笔)',
  125. 'field' => '个',
  126. 'count' => self::getModelTime($where, new self())->where($datawhere)->count(),
  127. 'content' => '返利总笔数',
  128. 'background_color' => 'layui-bg-blue',
  129. 'sum' => self::where($datawhere)->count(),
  130. 'class' => 'fa fa-bar-chart',
  131. ],
  132. [
  133. 'name' => '返利金额(元)',
  134. 'field' => '个',
  135. 'count' => self::getModelTime($where, new self())->where($datawhere)->sum('number'),
  136. 'content' => '返利总金额',
  137. 'background_color' => 'layui-bg-cyan',
  138. 'sum' => self::where($datawhere)->sum('number'),
  139. 'class' => 'fa fa-line-chart',
  140. ],
  141. ];
  142. }
  143. //获取返佣用户信息列表
  144. public static function getFanList($where)
  145. {
  146. $list = self::alias('a')->join('system_store r', 'a.store_id=r.id')
  147. ->where('a.category', 'now_money')
  148. ->where('a.type', 'brokerage')
  149. ->where('a.pm', 1)
  150. ->order('a.number desc')
  151. ->join('store_order o', 'o.id=a.link_id')
  152. ->field('o.order_id,FROM_UNIXTIME(a.add_time,"%Y-%c-%d") as add_time,a.store_id,o.uid as down_uid,r.name,r.image,a.number')
  153. ->page((int)$where['page'], (int)$where['limit'])
  154. ->select();
  155. if (count($list)) $list = $list->toArray();
  156. return $list;
  157. }
  158. //获取返佣用户总人数
  159. public static function getFanCount()
  160. {
  161. return self::alias('a')->join('system_store r', 'a.store_id=r.id')->join('store_order o', 'o.id=a.link_id')->where('a.category', 'now_money')->where('a.type', 'brokerage')->where('a.pm', 1)->count();
  162. }
  163. //获取佣金提现列表
  164. public static function getExtrctOneList($where, $uid)
  165. {
  166. $list = self::setOneWhere($where, $uid)
  167. ->field('number,link_id,mark,FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i:%s") as _add_time,status')
  168. ->select();
  169. count($list) && $list = $list->toArray();
  170. $count = self::setOneWhere($where, $uid)->count();
  171. foreach ($list as &$value) {
  172. $value['order_id'] = Db::name('store_order')->where('order_id', $value['link_id'])->value('order_id');
  173. }
  174. return ['data' => $list, 'count' => $count];
  175. }
  176. //设置单个用户查询
  177. public static function setOneWhere($where, $uid)
  178. {
  179. $model = self::where('store_id', $uid)->where('category', 'now_money')->where('type', 'brokerage');
  180. $time['data'] = '';
  181. if (strlen(trim($where['start_time'])) && strlen(trim($where['end_time']))) {
  182. $time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
  183. $model = self::getModelTime($time, $model);
  184. }
  185. if (strlen(trim($where['nickname']))) {
  186. $model = $model->where('link_id|mark', 'like', "%$where[nickname]%");
  187. }
  188. return $model;
  189. }
  190. //查询个人余额变动记录
  191. public static function getOneBalanceChangList($where)
  192. {
  193. $list = self::setWhereList(
  194. $where, '',
  195. // ['system_add','pay_product','extract','pay_product_refund','system_sub','brokerage','recharge','user_recharge_refund'],
  196. ['FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time', 'title', 'type', 'mark', 'number', 'balance', 'pm', 'status'],
  197. 'now_money'
  198. );
  199. foreach ($list as &$item) {
  200. switch ($item['type']) {
  201. case 'extract':
  202. $item['_type'] = '提现';
  203. break;
  204. case 'brokerage':
  205. $item['_type'] = '系统返佣';
  206. break;
  207. }
  208. $item['_pm'] = $item['pm'] == 1 ? '获得' : '支出';
  209. }
  210. return $list;
  211. }
  212. //设置where条件分页.返回数据
  213. public static function setWhereList($where, $type = '', $field = [], $category = 'integral')
  214. {
  215. $models = self::where('store_id', $where['store_id'])
  216. ->where('category', $category)
  217. ->page((int)$where['page'], (int)$where['limit'])
  218. ->order('id', 'desc')
  219. ->field($field);
  220. if (is_array($type)) {
  221. $models = $models->where('type', 'in', $type);
  222. } else {
  223. if (!empty($type)) $models = $models->where('type', $type);
  224. }
  225. return ($list = $models->select()) && count($list) ? $list->toArray() : [];
  226. }
  227. /**
  228. * 用户获得总佣金
  229. * @return float
  230. */
  231. public static function getBrokerageCount()
  232. {
  233. return self::where('type', 'brokerage')->where('category', 'now_money')->where('status', 1)->where('pm', 1)->sum('number');
  234. }
  235. }