merchant_info.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <?php
  2. class merchant_infoControl extends mbMerchantControl
  3. {
  4. public function __construct()
  5. {
  6. parent::__construct();
  7. }
  8. public function indexOp()
  9. {
  10. $mchid = $this->mchid();
  11. $model_merchant = Model('merchant');
  12. $field = 'mchid,admin_id,name,alarm_amount,ip_white_list,use_key,contact_name,contact_phone,warning_phone,company_name,quality,time_out,credit_bonus,query_total_times,query_left_times';
  13. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid], $field);
  14. $model_member = Model('member');
  15. $member_info = $model_member->getMemberInfo(['member_id' => $merchant_info['admin_id']], 'available_predeposit', true);
  16. if (empty($member_info)) {
  17. $member_info['available_predeposit'] = 0;
  18. }
  19. if(!empty($merchant_info['credit_bonus'])) {
  20. $member_info['available_predeposit'] = ncPriceFormat($member_info['available_predeposit'] - $merchant_info['credit_bonus']);
  21. }
  22. $merchant_info['member'] = $member_info;
  23. if (empty($merchant_info['ip_white_list'])) {
  24. $merchant_info['ips'] = [];
  25. } else {
  26. $merchant_info['ips'] = unserialize($merchant_info['ip_white_list']);
  27. }
  28. if (empty($merchant_info['warning_phone'])) {
  29. $merchant_info['warning_phone'] = [];
  30. } else {
  31. $merchant_info['warning_phone'] = unserialize($merchant_info['warning_phone']);
  32. }
  33. $cond['status'] = $cond['is_operation'] = 2;
  34. $cond['mchid'] = $mchid;
  35. $stats = Model('')->table('refill_evidence')
  36. ->field('sum(amount) as amounts')
  37. ->where($cond)->select();
  38. $merchant_info['evidence_amounts'] = empty($stats) ? 0 : $stats[0]['amounts'];
  39. $cond['add_time'] = ['between', [strtotime(date("Y-m-d",time())), time()]];
  40. $day_evidence = $model_merchant->getRefillEvidence($cond);
  41. $merchant_info['evidence_count'] = count($day_evidence);
  42. $merchant_info['time_out'] = intval($merchant_info['time_out'] / 60);
  43. $merchant_info['quality'] = intval($merchant_info['quality']);
  44. $merchant_info['debt'] = 0.00;
  45. $merchant_debts = rcache("merchant-debts-detail", 'refill-', $mchid);
  46. if(!empty($merchant_debts)) {
  47. Log::record("merchant-debts-detail {$merchant_debts[$mchid]}", Log::DEBUG);
  48. $debt_detail = unserialize($merchant_debts[$mchid]);
  49. $merchant_info['debt'] = -1 * number_format($debt_detail['debt'],2,'.','');
  50. }
  51. // $pub = new message\publisher();
  52. // $pub->modify_refill_merchant();
  53. return self::outsuccess($merchant_info);
  54. }
  55. public function SetQualityTimeOutOp()
  56. {
  57. $mchid = $this->mchid();
  58. $save = [];
  59. $quality = intval($_POST['quality']);
  60. if($quality > 0) {
  61. if(!in_array($quality , [1,2,3])) {
  62. return self::outerr(errcode::ErrParamter, "通道质量类型错误");
  63. }
  64. $save['quality'] = $quality;
  65. }
  66. $time_out = intval($_POST['time_out']);
  67. if($quality > 0) {
  68. if($time_out <3) {
  69. return self::outerr(errcode::ErrParamter, "超时时间应大于3分钟");
  70. }
  71. $time_out = intval($time_out * 60);
  72. $save['time_out'] = $time_out;
  73. }
  74. if(empty($save)) {
  75. return self::outsuccess([]);
  76. }
  77. $model_merchant = Model('merchant');
  78. $resp = $model_merchant->editMerchant($save, ['mchid' => $mchid]);
  79. if($resp) {
  80. return self::outsuccess([]);
  81. }else{
  82. return self::outerr(errcode::ErrOperation, "系统错误.");
  83. }
  84. }
  85. public function homeOp()
  86. {
  87. $mchid = $this->mchid();
  88. $model_merchant = Model('merchant');
  89. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid], 'mchid,admin_id,name,company_name,credit_bonus');
  90. $model_member = Model('member');
  91. $member_info = $model_member->getMemberInfo(['member_id' => $merchant_info['admin_id']], 'available_predeposit');
  92. if (empty($member_info)) {
  93. $merchant_info['available_predeposit'] = 0;
  94. } else {
  95. $merchant_info['available_predeposit'] = ncPriceFormat($member_info['available_predeposit'] - $merchant_info['credit_bonus']);
  96. }
  97. $statistics = $this->statistics();
  98. ksort($statistics);
  99. $result['merchant_info'] = $merchant_info;
  100. $result['todayStatistics'] = $statistics[strtotime(date("Y-m-d",time()))];
  101. $result['weeksStatistics'] = $statistics;
  102. $max = 0;
  103. foreach ($statistics as $key => $val){
  104. if($max == 0){
  105. $max = $val['successAmounts'];
  106. }
  107. if($max < $val['successAmounts']){
  108. $max = $val['successAmounts'];
  109. }
  110. }
  111. $result['max'] = ncPriceFormat(ceil($max * 1.1));
  112. $week_month_stats = $this->refillStats();
  113. $result['month'] = $week_month_stats['month'];
  114. $result['week'] = $week_month_stats['week'];
  115. return self::outsuccess($result);
  116. }
  117. private function statistics()
  118. {
  119. $date = date('Y-m-d',time());
  120. $today = strtotime("{$date}");
  121. $times_begin = function ($start) {
  122. $times = [];
  123. $begin = $start;
  124. for($i = 0; $i < 7; $i++) {
  125. $times[] = $begin;
  126. $begin -= 86400;
  127. }
  128. return $times;
  129. };
  130. $reader = function ($mchid,$time) {
  131. $cond['mchid'] = $mchid;
  132. $cond['inner_status'] = 0;
  133. $cond['refill_order.order_time'] = ['between', [$time, $time + 86400 -1]];
  134. $items = Model('')->table('refill_order,vr_order')->join('inner')
  135. ->on('refill_order.order_id=vr_order.order_id')
  136. ->field('count(*) as order_count, vr_order.order_state, sum(mch_amount) as mch_amounts')
  137. ->group('order_state')
  138. ->where($cond)->select();
  139. return $this->refillCountDispose($items);
  140. };
  141. $begins = $times_begin($today);
  142. $states = rcache($this->mchid() , 'refillstat-');
  143. if(empty($states)){
  144. $states = [];
  145. }else{
  146. $states = unserialize($states['data']);
  147. }
  148. $result = [];
  149. $cache = [];
  150. foreach ($begins as $begin)
  151. {
  152. if(array_key_exists($begin,$states)) {
  153. $item = $states[$begin];
  154. }
  155. else {
  156. $item = $reader($this->mchid(),$begin);
  157. }
  158. $result[$begin] = $item;
  159. //判断item 中充值中的状态是否为0,为0且不是今天的情况下放进cache
  160. if($item['sendCount'] == 0 && $begin != $today){
  161. $cache[$begin] = $item;
  162. }
  163. }
  164. if(!empty($cache) && !$this->isSame($states,$cache)) {
  165. wcache($this->mchid() ,['data' => serialize($cache)], 'refillstat-');
  166. }
  167. return $result;
  168. }
  169. private function isSame($arr1, $arr2)
  170. {
  171. ksort($arr1);
  172. ksort($arr2);
  173. return $arr1 == $arr2;
  174. }
  175. private function refillCountDispose($stats)
  176. {
  177. $result['count'] = $result['sendCount'] = $result['errorCount'] = $result['successCount'] = $result['amountCount'] = $result['errorAmounts'] = $result['successAmounts'] = 0;
  178. foreach ($stats as $count) {
  179. $result['count'] += $count['order_count'];
  180. $result['amountCount'] += ncPriceFormat($count['mch_amounts']);
  181. if($count['order_state'] == ORDER_STATE_SEND) {
  182. $result['sendCount'] = $count['order_count'];
  183. }
  184. if($count['order_state'] == ORDER_STATE_CANCEL) {
  185. $result['errorCount'] = $count['order_count'];
  186. $result['errorAmounts'] = ncPriceFormat($count['mch_amounts']);
  187. }
  188. if($count['order_state'] == ORDER_STATE_SUCCESS) {
  189. $result['successCount'] = $count['order_count'];
  190. $result['successAmounts'] = ncPriceFormat($count['mch_amounts']);
  191. }
  192. }
  193. $result['amountCount'] = ncPriceFormat($result['amountCount']);
  194. $result['errorAmounts'] = ncPriceFormat($result['errorAmounts']);
  195. $result['successAmounts'] = ncPriceFormat($result['successAmounts']);
  196. return $result;
  197. }
  198. private function refillStats()
  199. {
  200. $reader = function ($mchid,$time,$end_time) {
  201. $cond['mchid'] = $mchid;
  202. $cond['inner_status'] = 0;
  203. $cond['refill_order.order_time'] = ['between', [$time, $end_time]];
  204. $items = Model('')->table('refill_order,vr_order')->join('inner')
  205. ->on('refill_order.order_id=vr_order.order_id')
  206. ->field('count(*) as order_count, vr_order.order_state, sum(mch_amount) as mch_amounts')
  207. ->group('order_state')
  208. ->where($cond)->select();
  209. return $this->refillCountDispose($items);
  210. };
  211. $date = date("Y-m-d");
  212. $first = 1;
  213. $w = date('w',strtotime($date));
  214. $week_start = strtotime(date('Y-m-d',strtotime("$date -".($w ? $w - $first : 6).' days')));
  215. $w = 7 - $w + 1;
  216. $week_end = strtotime(date('Y-m-d',strtotime("+{$w} days"))) - 1;
  217. $week = $reader($this->mchid(), $week_start,$week_end);
  218. $month_start = strtotime(date("Y-m-1"));
  219. $month_end = strtotime(date("Y-m-1",strtotime("+1month")))-1;
  220. $month = $reader($this->mchid(), $month_start, $month_end);
  221. return ['week' => $week , 'month' => $month];
  222. }
  223. public function addipOp()
  224. {
  225. $mchid = $this->mchid();
  226. $ip = $_POST['ip'];
  227. if (empty($ip)) {
  228. return self::outerr(errcode::ErrParamter, "参数错误");
  229. }
  230. $ip = trim($ip);
  231. if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
  232. {
  233. $model_merchant = Model('merchant');
  234. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  235. if (empty($merchant_info['ip_white_list'])) {
  236. $ips = [];
  237. } else {
  238. $ips = unserialize($merchant_info['ip_white_list']);
  239. }
  240. $ips[] = $ip;
  241. $ips = array_unique($ips);
  242. $model_merchant->editMerchant(['ip_white_list' => serialize($ips)], ['mchid' => $merchant_info['mchid']]);
  243. return self::outsuccess([]);
  244. } else {
  245. return self::outerr(errcode::ErrParamter, "ip地址错误");
  246. }
  247. }
  248. public function ipdelOp()
  249. {
  250. $mchid = $this->mchid();
  251. $ip = $_POST['ip'];
  252. if (empty($ip)) {
  253. return self::outerr(errcode::ErrParamter, "参数错误");
  254. }
  255. $ip = trim($ip);
  256. $model_merchant = Model('merchant');
  257. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  258. $ips = unserialize($merchant_info['ip_white_list']);
  259. $new_ips = [];
  260. foreach ($ips as $value) {
  261. if ($value != $ip) {
  262. $new_ips[] = $value;
  263. }
  264. }
  265. if (empty($new_ips)) {
  266. $new_ips = '';
  267. } else {
  268. $new_ips = serialize($new_ips);
  269. }
  270. $model_merchant->editMerchant(['ip_white_list' => $new_ips], ['mchid' => $merchant_info['mchid']]);
  271. return self::outsuccess([]);
  272. }
  273. public function setcontactOp(){
  274. $mchid = $this->mchid();
  275. if(!empty($_POST['contact_name']))
  276. {
  277. $params['contact_name'] = trim($_POST['contact_name']);
  278. }
  279. if(!empty($_POST['contact_phone']))
  280. {
  281. $params['contact_phone'] = trim($_POST['contact_phone']);
  282. }
  283. if(!empty($_POST['alarm_amount']))
  284. {
  285. $params['alarm_amount'] = trim($_POST['alarm_amount']);
  286. }
  287. if(empty($params))
  288. {
  289. return self::outsuccess([]);
  290. }
  291. $model_merchant = Model('merchant');
  292. $ret = $model_merchant->editMerchant($params, ['mchid' => $mchid]);
  293. if ($ret) {
  294. return self::outsuccess([]);
  295. } else {
  296. return self::outerr(errcode::ErrOperation, "系统错误.");
  297. }
  298. }
  299. public function addwphoneOp()
  300. {
  301. $mchid = $this->mchid();
  302. $phone = $_POST['phone'];
  303. if (empty($phone)) {
  304. return self::outerr(errcode::ErrParamter, "参数错误");
  305. }
  306. $phone = explode(',' ,trim($phone));
  307. $model_merchant = Model('merchant');
  308. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  309. if (empty($merchant_info['warning_phone'])) {
  310. $phones = [];
  311. } else {
  312. $phones = unserialize($merchant_info['warning_phone']);
  313. }
  314. foreach ($phone as $item) {
  315. $phones[] = $item;
  316. }
  317. $phones = array_unique($phones);
  318. $model_merchant->editMerchant(['warning_phone' => serialize($phones)], ['mchid' => $merchant_info['mchid']]);
  319. return self::outsuccess([]);
  320. }
  321. public function wphonedelOp()
  322. {
  323. $mchid = $this->mchid();
  324. $phone = $_POST['phone'];
  325. if (empty($phone)) {
  326. return self::outerr(errcode::ErrParamter, "参数错误");
  327. }
  328. $phone = trim($phone);
  329. $model_merchant = Model('merchant');
  330. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  331. $phones = unserialize($merchant_info['warning_phone']);
  332. $new_phones = [];
  333. foreach ($phones as $value) {
  334. if ($value != $phone) {
  335. $new_phones[] = $value;
  336. }
  337. }
  338. if (empty($new_phones)) {
  339. $new_phones = '';
  340. } else {
  341. $new_phones = serialize($new_phones);
  342. }
  343. $model_merchant->editMerchant(['warning_phone' => $new_phones], ['mchid' => $merchant_info['mchid']]);
  344. return self::outsuccess([]);
  345. }
  346. public function setkeyOp()
  347. {
  348. $mchid = $this->mchid();
  349. $secure_key = $_POST['secure_key'];
  350. if (empty($secure_key)) {
  351. return self::outerr(errcode::ErrParamter, "参数错误");
  352. }
  353. $model_merchant = Model('merchant');
  354. $ret = $model_merchant->editMerchant(['secure_key' => $secure_key, 'use_key' => 1], ['mchid' => $mchid]);
  355. if ($ret) {
  356. return self::outsuccess([]);
  357. } else {
  358. return self::outerr(errcode::ErrOperation, "系统错误.");
  359. }
  360. }
  361. public function modifypwOp()
  362. {
  363. $mchid = $this->mchid();
  364. $new_pw = $_POST['new_pw'];
  365. $new_pw2 = $_POST['new_pw2'];
  366. if (trim($new_pw) !== trim($new_pw2)) {
  367. return self::outerr(errcode::ErrPasswd, "密码错误");
  368. }
  369. $model_merchant = Model('merchant');
  370. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  371. if (!$merchant_info) {
  372. return self::outerr(errcode::ErrMemberNotExist, "用户不存在.");
  373. }
  374. $pwd = trim($new_pw);
  375. if (md5($pwd) == $merchant_info['password']) {
  376. return self::outsuccess([]);
  377. }
  378. $ret = $model_merchant->editMerchant(['password' => md5($pwd), 'org_pwd' => $pwd], ['mchid' => $merchant_info['mchid']]);
  379. if ($ret) {
  380. return self::outsuccess([]);
  381. } else {
  382. return self::outerr(errcode::ErrOperation, "系统错误.");
  383. }
  384. }
  385. public function pdlogOp()
  386. {
  387. $mchid = $this->mchid();
  388. $model_merchant = Model('merchant');
  389. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  390. if (!$merchant_info) {
  391. return self::outerr(errcode::ErrMemberNotExist, "用户不存在.");
  392. }
  393. $model_pd = Model('merchant');
  394. if (empty($merchant_info['admin_id'])) {
  395. $result['data'] = [];
  396. $result['total'] = 0;
  397. return self::outsuccess($result);
  398. }
  399. $cond['lg_member_id'] = $merchant_info['admin_id'];
  400. if (!empty($_GET['lg_type'])) {
  401. $cond['lg_type'] = $_GET['lg_type'];
  402. }
  403. if ($_GET['start_time'] && $_GET['end_time']) {
  404. $cond['lg_add_time'] = ['between', [$_GET['start_time'], $_GET['end_time']]];
  405. }
  406. $list = $model_pd->getPdlog($cond, $this->page, '*', 'lg_id desc');
  407. $list = $this->PdLogFormat($list);
  408. $result['data'] = $list;
  409. $result['total'] = $model_pd->gettotalpage();
  410. return self::outsuccess($result);
  411. }
  412. private function PdLogFormat($pdlog)
  413. {
  414. $data = [];
  415. foreach ($pdlog as $key => $value) {
  416. if (isset($value['lg_add_time'])) {
  417. $value['lg_add_time'] = date('Y-m-d H:i:s', $value['lg_add_time']);
  418. }
  419. switch ($value['lg_type']) {
  420. case 'order_pay':
  421. $value['lg_type_text'] = '下单减款';
  422. break;
  423. case 'order_cancel':
  424. $value['lg_type_text'] = '下单失败返回余款';
  425. break;
  426. case 'recharge':
  427. $value['lg_type_text'] = '余款充值';
  428. break;
  429. case 'sys_add_money':
  430. $value['lg_type_text'] = '管理员调节预存款【增加】';
  431. break;
  432. case 'sys_del_money':
  433. $value['lg_type_text'] = '管理员调节预存款【减少】';
  434. break;
  435. case 'sys_freeze_money':
  436. $value['lg_type_text'] = '管理员调节预存款【冻结】';
  437. break;
  438. case 'sys_unfreeze_money':
  439. $value['lg_type_text'] = '管理员调节预存款【解冻】';
  440. break;
  441. default:
  442. $value['lg_type_text'] = 'unknown';
  443. break;
  444. }
  445. $data[] = $value;
  446. }
  447. return $data;
  448. }
  449. public function pdlogexportOp()
  450. {
  451. $mchid = $this->mchid();
  452. $model_merchant = Model('merchant');
  453. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  454. if (!$merchant_info) {
  455. return self::outerr(errcode::ErrMemberNotExist, "用户不存在.");
  456. }
  457. $condition['lg_member_id'] = $merchant_info['admin_id'];
  458. if (!empty($_GET['lg_type'])) {
  459. $condition['lg_type'] = $_GET['lg_type'];
  460. }
  461. if ($_GET['start_time'] && $_GET['end_time']) {
  462. $condition['lg_add_time'] = ['between', [$_GET['start_time'], $_GET['end_time']]];
  463. }
  464. $list = Model('')->table('pd_log,refill_order')->join('left')
  465. ->on('pd_log.lg_order_sn=refill_order.order_sn')
  466. ->field('pd_log.lg_type,pd_log.lg_av_amount,pd_log.lg_add_time,refill_order.card_no,refill_order.mchid,refill_order.card_type,pd_log.lg_available,refill_order.refill_amount')
  467. ->where($condition)->select();
  468. $result = $this->export_pdlog_exec($list);
  469. return self::outsuccess($result);
  470. }
  471. private function export_pdlog_exec($list)
  472. {
  473. $title = [
  474. ['value' => '代理商账号'],
  475. ['value' => '金额'],
  476. ['value' => '账户余额'],
  477. ['value' => '交易日期'],
  478. ['value' => '类型'],
  479. ['value' => '备注']
  480. ];
  481. $mchid = $this->mchid();
  482. $card_type = ['1'=>'中石油' , '2' =>'中石化' , '4' => '移动' , '5' => '联通' , '6' => '电信'];
  483. foreach ($list as $value) {
  484. if (isset($value['lg_add_time'])) {
  485. $value['lg_add_time'] = date('Y-m-d H:i:s', $value['lg_add_time']);
  486. }
  487. switch ($value['lg_type']) {
  488. case 'order_pay':
  489. $value['lg_type_text'] = '消费';
  490. break;
  491. case 'order_cancel':
  492. $value['lg_type_text'] = '退款';
  493. break;
  494. case 'recharge':
  495. $value['lg_type_text'] = '充值';
  496. break;
  497. case 'sys_add_money':
  498. $value['lg_type_text'] = '管理员充值【增加】';
  499. break;
  500. case 'sys_del_money':
  501. $value['lg_type_text'] = '管理员减款';
  502. break;
  503. default:
  504. $value['lg_type_text'] = '';
  505. break;
  506. }
  507. $data = [];
  508. $str = "{$value['card_no']}-{$card_type[$value['card_type']]}{$value['refill_amount']}元";
  509. if(empty($value['card_no']) || empty($value['card_type']) || empty($value['refill_amount'])) {
  510. $str = '';
  511. }
  512. $data[] = ['value' => $mchid];
  513. $data[] = ['value' => $value['lg_av_amount']];
  514. $data[] = ['value' => ncPriceFormat($value['lg_available'] + $value['lg_av_amount'])];
  515. $data[] = ['value' => $value['lg_add_time']];
  516. $data[] = ['value' => $value['lg_type_text']];
  517. $data[] = ['value' => $str];
  518. $datas[] = $data;
  519. }
  520. Log::record(json_encode($datas),Log::DEBUG);
  521. return ['title' => $title , 'data' => $datas];
  522. }
  523. }