SystemStoreExtract.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: lianghuan
  5. * Date: 2018-03-03
  6. * Time: 16:47
  7. */
  8. namespace app\admin\model\system;
  9. use app\admin\model\wechat\WechatUser;
  10. use app\models\routine\RoutineTemplate;
  11. use think\facade\Route as Url;
  12. use crmeb\traits\ModelTrait;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\services\WechatTemplateService;
  15. /**
  16. * 门店提现管理 model
  17. * Class User
  18. * @package app\admin\model\user
  19. */
  20. class SystemStoreExtract extends BaseModel
  21. {
  22. /**
  23. * 数据表主键
  24. * @var string
  25. */
  26. protected $pk = 'id';
  27. /**
  28. * 模型名称
  29. * @var string
  30. */
  31. protected $name = 'system_store_extract';
  32. protected static $extractType = ['alipay', 'bank', 'weixin'];
  33. use ModelTrait;
  34. /**
  35. * 获得用户提现总金额
  36. * @param $uid
  37. * @return mixed
  38. */
  39. public static function userExtractTotalPrice($uid, $status = 1, $where = [])
  40. {
  41. return self::getModelTime($where, self::where('store_id', 'in', $uid)->where('status', $status))->sum('extract_price') ?: 0;
  42. }
  43. public static function extractStatistics()
  44. {
  45. //待提现金额
  46. $data['price'] = floatval(self::where('status', 0)->sum('extract_price'));
  47. //佣金总金额
  48. $data['brokerage_count'] = floatval(SystemStoreBill::getBrokerageCount());
  49. //已提现金额
  50. $data['priced'] = floatval(self::where('status', 1)->sum('extract_price'));
  51. //未提现金额
  52. $data['brokerage_not'] = bcsub(bcsub($data['brokerage_count'], $data['priced'], 2), $data['price'], 2);
  53. return compact('data');
  54. }
  55. /**
  56. * @param $where
  57. * @return array
  58. */
  59. public static function systemPage($where)
  60. {
  61. $model = new self;
  62. if ($where['date'] != '') {
  63. list($startTime, $endTime) = explode(' - ', $where['date']);
  64. $model = $model->where('a.add_time', '>', strtotime($startTime));
  65. $model = $model->where('a.add_time', '<', (int)bcadd(strtotime($endTime), 86400, 0));
  66. }
  67. if ($where['status'] != '') $model = $model->where('a.status', $where['status']);
  68. if ($where['extract_type'] != '') $model = $model->where('a.extract_type', $where['extract_type']);
  69. if ($where['nireid'] != '') $model = $model->where('a.real_name|a.id|b.name|a.bank_code|a.alipay_code', 'like', "%$where[nireid]%");
  70. $model = $model->alias('a');
  71. $model = $model->field('a.*,b.name');
  72. $model = $model->join('system_store b', 'b.id=a.store_id', 'LEFT');
  73. $model = $model->order('a.id desc');
  74. return self::page($model, $where);
  75. }
  76. public static function changeFail($id, $fail_msg)
  77. {
  78. $fail_time = time();
  79. $data = self::get($id);
  80. $extract_number = $data['extract_price'];
  81. $mark = '提现失败,退回佣金' . $extract_number . '元';
  82. $uid = $data['store_id'];
  83. $status = -1;
  84. $User = SystemStore::where('id', $uid)->find()->toArray();
  85. SystemStoreBill::income('提现失败', $uid, 'brokerage_price', 'extract', $extract_number, $id, bcadd($User['brokerage_price'], $extract_number, 2), $mark);
  86. SystemStore::bcInc($uid, 'brokerage_price', $extract_number, 'id');
  87. return self::edit(compact('fail_time', 'fail_msg', 'status'), $id);
  88. }
  89. public static function changeSuccess($id)
  90. {
  91. return self::edit(['status' => 1], $id);
  92. }
  93. public static function userExtract($userInfo, $data)
  94. {
  95. if (!in_array($data['extract_type'], self::$extractType))
  96. return self::setErrorInfo('提现方式不存在');
  97. $userInfo = SystemStore::get($userInfo['id']);
  98. $extractPrice = $userInfo['brokerage_price'];
  99. if ($extractPrice < 0) return self::setErrorInfo('提现佣金不足' . $data['money']);
  100. if ($data['money'] > $extractPrice) return self::setErrorInfo('提现佣金不足' . $data['money']);
  101. if ($data['money'] <= 0) return self::setErrorInfo('提现佣金大于0');
  102. $balance = bcsub($userInfo['brokerage_price'], $data['money'], 2);
  103. if ($balance < 0) $balance = 0;
  104. $insertData = [
  105. 'store_id' => $userInfo['id'],
  106. 'extract_type' => $data['extract_type'],
  107. 'extract_price' => $data['money'],
  108. 'add_time' => time(),
  109. 'balance' => $balance,
  110. 'status' => 0
  111. ];
  112. if (isset($data['name']) && strlen(trim($data['name']))) $insertData['real_name'] = $data['name'];
  113. else $insertData['real_name'] = $userInfo['name'];
  114. if (isset($data['cardnum'])) $insertData['bank_code'] = $data['cardnum'];
  115. else $insertData['bank_code'] = '';
  116. if (isset($data['bankname'])) $insertData['bank_address'] = $data['bankname'];
  117. else $insertData['bank_address'] = '';
  118. if (isset($data['weixin'])) $insertData['wechat'] = $data['weixin'];
  119. else $insertData['wechat'] = $userInfo['name'];
  120. if ($data['extract_type'] == 'alipay') {
  121. if (!$data['alipay_code']) return self::setErrorInfo('请输入支付宝账号');
  122. $insertData['alipay_code'] = $data['alipay_code'];
  123. $mark = '使用支付宝提现' . $insertData['extract_price'] . '元';
  124. } else if ($data['extract_type'] == 'bank') {
  125. if (!$data['cardnum']) return self::setErrorInfo('请输入银行卡账号');
  126. if (!$data['bankname']) return self::setErrorInfo('请输入开户行信息');
  127. $mark = '使用银联卡' . $insertData['bank_code'] . '提现' . $insertData['extract_price'] . '元';
  128. } else if ($data['extract_type'] == 'weixin') {
  129. if (!$data['weixin']) return self::setErrorInfo('请输入微信账号');
  130. $mark = '使用微信提现' . $insertData['extract_price'] . '元';
  131. }
  132. self::beginTrans();
  133. try {
  134. $res1 = self::create($insertData);
  135. if (!$res1) return self::setErrorInfo('提现失败');
  136. $res2 = SystemStore::edit(['brokerage_price' => $balance], $userInfo['id'], 'id');
  137. $res3 = SystemStoreBill::expend('佣金提现', $userInfo['id'], 'brokerage_price', 'extract', $data['money'], $res1['id'], $balance, $mark ?? '');
  138. $res = $res2 && $res3;
  139. if ($res) {
  140. self::commitTrans();
  141. return true;
  142. } else return self::setErrorInfo('提现失败!');
  143. } catch (\Exception $e) {
  144. self::rollbackTrans();
  145. return self::setErrorInfo('提现失败!');
  146. }
  147. }
  148. //获取头部提现信息
  149. public static function getExtractHead()
  150. {
  151. //本月提现人数
  152. $month = self::getModelTime(['data' => 'month'], self::where('status', 1))->group('store_id')->count();
  153. //本月提现笔数
  154. $new_month = self::getModelTime(['data' => 'month'], self::where('status', 1))->distinct(true)->count();
  155. //上月提现人数
  156. $last_month = self::whereTime('add_time', 'last month')->where('status', 1)->group('store_id')->distinct(true)->count();
  157. //上月提现笔数
  158. $last_count = self::whereTime('add_time', 'last month')->where('status', 1)->count();
  159. //本月提现金额
  160. $extract_price = self::getModelTime(['data' => 'month'], self::where('status', 1))->sum('extract_price');
  161. //上月提现金额
  162. $last_extract_price = self::whereTime('add_time', 'last month')->where('status', 1)->sum('extract_price');
  163. return [
  164. [
  165. 'name' => '总提现店铺数',
  166. 'field' => '个',
  167. 'count' => self::where('status', 1)->group('store_id')->count(),
  168. 'content' => '',
  169. 'background_color' => 'layui-bg-blue',
  170. 'sum' => '',
  171. 'class' => 'fa fa-bar-chart',
  172. ],
  173. [
  174. 'name' => '总提现笔数',
  175. 'field' => '笔',
  176. 'count' => self::where('status', 1)->distinct(true)->count(),
  177. 'content' => '',
  178. 'background_color' => 'layui-bg-cyan',
  179. 'sum' => '',
  180. 'class' => 'fa fa-line-chart',
  181. ],
  182. [
  183. 'name' => '本月提现店铺数',
  184. 'field' => '人',
  185. 'count' => $month,
  186. 'content' => '',
  187. 'background_color' => 'layui-bg-orange',
  188. 'sum' => '',
  189. 'class' => 'fa fa-line-chart',
  190. ],
  191. [
  192. 'name' => '本月提现笔数',
  193. 'field' => '笔',
  194. 'count' => $new_month,
  195. 'content' => '',
  196. 'background_color' => 'layui-bg-green',
  197. 'sum' => '',
  198. 'class' => 'fa fa-line-chart',
  199. ],
  200. [
  201. 'name' => '本月提现金额',
  202. 'field' => '元',
  203. 'count' => $extract_price,
  204. 'content' => '提现总金额',
  205. 'background_color' => 'layui-bg-cyan',
  206. 'sum' => self::where('status', 1)->sum('extract_price'),
  207. 'class' => 'fa fa-line-chart',
  208. ],
  209. [
  210. 'name' => '上月提现店铺数',
  211. 'field' => '个',
  212. 'count' => $last_month,
  213. 'content' => '环比增幅',
  214. 'background_color' => 'layui-bg-blue',
  215. 'sum' => $last_month == 0 ? '100%' : bcdiv($month, $last_month, 2) * 100,
  216. 'class' => $last_month == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  217. ],
  218. [
  219. 'name' => '上月提现笔数',
  220. 'field' => '笔',
  221. 'count' => $last_count,
  222. 'content' => '环比增幅',
  223. 'background_color' => 'layui-bg-black',
  224. 'sum' => $last_count == 0 ? '100%' : bcdiv($new_month, $last_count, 2) * 100,
  225. 'class' => $last_count == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  226. ],
  227. [
  228. 'name' => '上月提现金额',
  229. 'field' => '元',
  230. 'count' => $last_extract_price,
  231. 'content' => '环比增幅',
  232. 'background_color' => 'layui-bg-gray',
  233. 'sum' => $last_extract_price == 0 ? '100%' : bcdiv($extract_price, $last_extract_price, 2) * 100,
  234. 'class' => $last_extract_price == 0 ? 'fa fa-level-up' : 'fa fa-level-down',
  235. ],
  236. ];
  237. }
  238. //获取提现分布图和提现人数金额曲线图
  239. public static function getExtractList($where, $limit = 15)
  240. {
  241. $legdata = ['提现店铺数', '提现金额'];
  242. $list = self::getModelTime($where, self::where('status', 1))
  243. ->field('FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time,count(store_id) as count,sum(extract_price) as sum_price')->group('un_time')->order('un_time asc')->select();
  244. if (count($list)) $list = $list->toArray();
  245. $xdata = [];
  246. $itemList = [0 => [], 1 => []];
  247. $chatrList = [];
  248. $zoom = '';
  249. foreach ($list as $value) {
  250. $xdata[] = $value['un_time'];
  251. $itemList[0][] = $value['count'];
  252. $itemList[1][] = $value['sum_price'];
  253. }
  254. foreach ($legdata as $key => $name) {
  255. $item['name'] = $name;
  256. $item['type'] = 'line';
  257. $item['data'] = $itemList[$key];
  258. $chatrList[] = $item;
  259. }
  260. unset($item, $name, $key);
  261. if (count($xdata) > $limit) $zoom = $xdata[$limit - 5];
  262. //饼状图
  263. $cake = ['支付宝', '银行卡', '微信'];
  264. $fenbulist = self::getModelTime($where, self::where('status', 1))
  265. ->field('count(store_id) as count,extract_type')->group('extract_type')->order('count asc')->select();
  266. if (count($fenbulist)) $fenbulist = $fenbulist->toArray();
  267. $sum_count = self::getModelTime($where, self::where('status', 1))->count();
  268. $color = ['#FB7773', '#81BCFE', '#91F3FE'];
  269. $fenbudata = [];
  270. foreach ($fenbulist as $key => $item) {
  271. if ($item['extract_type'] == 'bank') {
  272. $item_date['name'] = '银行卡';
  273. } else if ($item['extract_type'] == 'alipay') {
  274. $item_date['name'] = '支付宝';
  275. } else if ($item['extract_type'] == 'weixin') {
  276. $item_date['name'] = '微信';
  277. }
  278. $item_date['value'] = bcdiv($item['count'], $sum_count, 2) * 100;
  279. $item_date['itemStyle']['color'] = $color[$key];
  280. $fenbudata[] = $item_date;
  281. }
  282. return compact('xdata', 'chatrList', 'legdata', 'zoom', 'cake', 'fenbudata');
  283. }
  284. /**
  285. * 获取用户累计提现金额
  286. * @param int $uid
  287. * @return int|mixed
  288. */
  289. public static function getUserCountPrice($uid = 0)
  290. {
  291. if (!$uid) return 0;
  292. $price = self::where('store_id', $uid)->where('status', 1)->sum('extract_price');
  293. return $price ? $price : 0;
  294. }
  295. /**
  296. * 获取用户累计提现次数
  297. * @param int $uid
  298. * @return int|string
  299. */
  300. public static function getUserCountNum($uid = 0)
  301. {
  302. if (!$uid) return 0;
  303. return self::where('store_id', $uid)->count();
  304. }
  305. }