merchant.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. <?php
  2. /**
  3. * 机构管理界面
  4. *
  5. **by 好商城V3 www.33hao.com 运营版*/
  6. defined('InShopNC') or exit('Access Invalid!');
  7. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  8. class merchantControl extends SystemControl
  9. {
  10. const EXPORT_SIZE = 1000;
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. Language::read('merchant');
  15. }
  16. /**
  17. * 机构列表
  18. */
  19. public function merchantOp()
  20. {
  21. $model_merchant = Model('merchant');
  22. if (trim($_GET['merchant_name']) != '') {
  23. $condition['name'] = ['like', '%' . $_GET['merchant_name'] . '%'];
  24. Tpl::output('merchant_name', $_GET['merchant_name']);
  25. }
  26. $merchant_list = $model_merchant->getMerchantList($condition, 25, 'mchid desc');
  27. $merchant_state_text = ['使用中', '已禁用'];
  28. Tpl::output('merchant_state_text', $merchant_state_text);
  29. Tpl::output('merchant_list', $merchant_list);
  30. Tpl::output('page', $model_merchant->showpage('2'));
  31. Tpl::showpage('merchant.index');
  32. }
  33. public function changeStateOp()
  34. {
  35. $mchid = intval($_GET['mchid']);
  36. $state = intval($_GET['state']);
  37. $model_merchant = Model('merchant');
  38. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  39. if (empty($merchant_info) || !in_array($state, [1, 2])) {
  40. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  41. }
  42. $resp = $model_merchant->editMerchant(['merchant_state' => $state], ['mchid' => $mchid]);
  43. if (!$resp) {
  44. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  45. }
  46. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  47. }
  48. /**
  49. * 新增机构
  50. */
  51. public function merchant_addOp()
  52. {
  53. if (chksubmit()) {
  54. /**
  55. * 验证
  56. */
  57. $obj_validate = new Validator();
  58. $obj_validate->validateparam = [
  59. ["input" => $_POST["name"], "require" => "true", "message" => '机构账号不能为空'],
  60. ["input" => $_POST["company_name"], "require" => "true", "message" => '机构公司名称不能为空'],
  61. ["input" => $_POST["password"], "require" => "true", "message" => '密码不能为空']
  62. ];
  63. $error = $obj_validate->validate();
  64. if ($error != '') {
  65. showMessage($error);
  66. } else {
  67. $name = trim($_POST['name']);
  68. $company_name = trim($_POST['company_name']);
  69. $pwd = trim($_POST['password']);
  70. $alarm_amount = $_POST['alarm_amount'] ?? 0;
  71. $model_merchant = Model('merchant');
  72. $model_member = Model('member');
  73. try {
  74. $trans = new trans_wapper($model_merchant, __METHOD__);
  75. $insert_member['member_name'] = md5($name . time());
  76. $insert_member['member_passwd'] = $pwd;
  77. $insert_id = $model_member->addMember($insert_member);
  78. if ($insert_id == false) {
  79. $trans->rollback();
  80. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  81. }
  82. $insert_array['name'] = $name;
  83. $insert_array['company_name'] = $company_name;
  84. $insert_array['org_pwd'] = $pwd;
  85. $insert_array['password'] = md5($pwd);
  86. $insert_array['admin_id'] = trim($insert_id);
  87. $insert_array['alarm_amount'] = $alarm_amount;
  88. $result = $model_merchant->addMerchant($insert_array);
  89. if ($result) {
  90. $url = [
  91. [
  92. 'url' => 'index.php?act=merchant&op=merchant',
  93. 'msg' => '返回机构列表',
  94. ],
  95. [
  96. 'url' => 'index.php?act=merchant&op=merchant_add',
  97. 'msg' => '继续新增机构',
  98. ],
  99. ];
  100. $this->log('添加机构:' . '[ ' . $_POST['name'] . ']', 1);
  101. showMessage('机构添加成功', $url);
  102. } else {
  103. showMessage('机构添加失败');
  104. }
  105. $trans->commit();
  106. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  107. } catch (Exception $e) {
  108. $trans->rollback();
  109. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  110. }
  111. }
  112. }
  113. Tpl::showpage('merchant.add');
  114. }
  115. public function merchant_editOp()
  116. {
  117. $mchid = $_GET['mchid'] ?? $_POST['mchid'];
  118. $model_merchant = Model('merchant');
  119. $merchant = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  120. if(empty($merchant)){
  121. showMessage('机构信息有误');
  122. }
  123. if (chksubmit()) {
  124. $update['company_name'] = trim($_POST['company_name']);
  125. $update['org_pwd'] = trim($_POST['password']);
  126. $update['password'] = md5($update['org_pwd']);
  127. $update['alarm_amount'] = $_POST['alarm_amount'] ?? 0;
  128. $result = $model_merchant->editMerchant($update,['mchid' => $mchid]);
  129. if ($result) {
  130. $this->log('编辑机构:' . '[ ' . $merchant['name'] . ']', 1);
  131. showMessage('机构编辑成功', 'index.php?act=merchant&op=merchant');
  132. } else {
  133. showMessage('机构编辑失败', 'index.php?act=merchant&op=merchant');
  134. }
  135. }
  136. Tpl::output('merchant', $merchant);
  137. Tpl::showpage('merchant.edit');
  138. }
  139. public function orderStorgeOp()
  140. {
  141. if (chksubmit())
  142. {
  143. $mchids = $_POST['mchid'];
  144. $petrochinas = $_POST['petrochina'];
  145. $sinopecs = $_POST['sinopec'];
  146. foreach ($mchids as $pos => $mchid) {
  147. $mchid = intval($mchid);
  148. $data['mchid'] = $mchid;
  149. $data['petrochina'] = ['100' => intval($petrochinas[100][$pos]) , '200' => intval($petrochinas[200][$pos]) , '500' => intval($petrochinas[500][$pos]) , '1000' => intval($petrochinas[1000][$pos])];
  150. $data['sinopec'] = ['100' => intval($sinopecs[100][$pos]) , '200' => intval($sinopecs[200][$pos]) , '500' => intval($sinopecs[500][$pos]) , '1000' => intval($sinopecs[1000][$pos])];
  151. $data['time'] = time();
  152. $result[$mchid] = $data;
  153. }
  154. $this->cache_storge_limit($result);
  155. showMessage('编辑成功', 'index.php?act=merchant&op=merchant');
  156. }
  157. else
  158. {
  159. $order_limit = rcache('storge_limit' , 'merchant-');
  160. if(empty($order_limit)){
  161. $order_limit = [];
  162. }else{
  163. $order_limit = unserialize($order_limit['data']);
  164. }
  165. $initialiser = function ($merchant) {
  166. $amount = [
  167. '100' => -1,
  168. '200' => -1,
  169. '500' => -1,
  170. '1000' => -1
  171. ];
  172. $data['mchid'] = $merchant['mchid'];
  173. $data['petrochina'] = $data['sinopec'] = $amount;
  174. $data['time'] = time();
  175. return $data;
  176. };
  177. $reader = function ($mchid,$time)
  178. {
  179. $cond['mchid'] = $mchid;
  180. $cond['inner_status'] = 0;
  181. $cond['order_state'] = ORDER_STATE_SUCCESS;
  182. $cond['refill_order.order_time'] = ['egt', $time];
  183. $items = Model('')->table('refill_order,vr_order')->join('inner')
  184. ->on('refill_order.order_id=vr_order.order_id')
  185. ->field('refill_order.mchid,refill_order.card_type,refill_order.refill_amount,count(*) as num')
  186. ->group('refill_order.card_type,refill_order.refill_amount')
  187. ->where($cond)->select();
  188. return $items;
  189. };
  190. $type_map = [1 => 'petrochina' , 2 => 'sinopec'];
  191. $cur_limits = [];
  192. $model_merchant = model('merchant');
  193. $merchant_list = $model_merchant->getMerchantList([], 1000, 'mchid asc' , 'mchid,company_name');
  194. foreach ($merchant_list as $merchant)
  195. {
  196. $mchid = intval($merchant['mchid']);
  197. if(array_key_exists($mchid , $order_limit)) {
  198. $result[$mchid] = $order_limit[$mchid];
  199. }else{
  200. $result[$mchid] = $initialiser($merchant);
  201. }
  202. $start = $result[$mchid]['time'];
  203. $items = $reader($mchid,$start);
  204. $cur_limits[$mchid] = $result[$mchid];
  205. foreach ($items as $item) {
  206. $refill_amount = intval($item['refill_amount'] + 0.005);
  207. $card_type = $type_map[$item['card_type']];
  208. $cache_num = $result[$mchid][$card_type][$refill_amount];
  209. if ($cache_num == -1) {
  210. $new_num = -1;
  211. }else{
  212. $new_num = ($cache_num - $item['num']) >= 0 ? $cache_num - $item['num'] : 0;
  213. }
  214. $cur_limits[$mchid][$card_type][$refill_amount] = $new_num;
  215. }
  216. $cur_limits[$mchid]['company_name'] = $merchant['company_name'];
  217. }
  218. Tpl::output('data', $cur_limits);
  219. Tpl::showpage('merchant.orderlimit');
  220. }
  221. }
  222. private function cache_storge_limit($data)
  223. {
  224. Model('')->table('setting')->where(['name'=>'storge_limit'])->delete();
  225. Model('')->table('setting')->insert(['name'=>'storge_limit','value'=>serialize($data)]);
  226. wcache('storge_limit' , ['data' => serialize($data)] , 'merchant-');
  227. }
  228. public function priceOp()
  229. {
  230. if (chksubmit())
  231. {
  232. $mchid = $_POST['mchid'];
  233. //合并表单数据
  234. $card_types = $_POST['cardtype'];
  235. $specs = $_POST['spec'];
  236. $prices = $_POST['price'];
  237. foreach ($card_types as $key => $card_type) {
  238. $data['card_type'] = $card_type;
  239. $data['spec'] = intval($specs[$key]);
  240. $data['price'] = ncPriceFormat($prices[$key]);
  241. $params[] = $data;
  242. }
  243. foreach ($params as $param)
  244. {
  245. if($param['price'] > 0) {
  246. $insert['mchid'] = $mchid;
  247. $insert['spec'] = $param['spec'];
  248. $insert['price'] = $param['price'];
  249. $insert['card_types'] = $param['card_type'];
  250. $inserts[] = $insert;
  251. }
  252. }
  253. if(empty($inserts)) {
  254. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  255. }
  256. try {
  257. $model_merchant = Model('merchant');
  258. $trans = new trans_wapper($model_merchant, __METHOD__);
  259. //删除旧费率
  260. $model_merchant->delPrices($mchid);
  261. //更新新费率
  262. $model_merchant->insertPrices($inserts);
  263. $trans->commit();
  264. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  265. } catch (Exception $e) {
  266. $trans->rollback();
  267. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  268. }
  269. }
  270. $mchid = $_GET['mchid'] ?? 0;
  271. $goods = $this->GoodsFormat($mchid);
  272. Tpl::output('goods', $goods);
  273. Tpl::showpage('merchant.price');
  274. }
  275. public function getPriceOp()
  276. {
  277. $mchid = $_GET['mchid'] ?? 0;
  278. $model_merchant = Model('merchant');
  279. $items = $model_merchant->table('merchant_price')->where(['mchid' => $mchid])->select();
  280. $check = true;
  281. if (empty($items)) {
  282. $check = false;
  283. }
  284. $goods = $this->GoodsFormat($mchid);
  285. echo json_encode(['check' => $check, 'data' => $goods]);
  286. }
  287. private function GoodsFormat($mchid)
  288. {
  289. $all_spector = function ()
  290. {
  291. global $config;
  292. $refill_specs = $config['refill_specs'];
  293. $all_type_specs = [];
  294. foreach ($refill_specs as $scard_type => $specs)
  295. {
  296. if ($scard_type == 'petrochina') { //中石油
  297. $card_type = 1;
  298. } elseif ($scard_type == 'sinopec') { //中石化
  299. $card_type = 2;
  300. } elseif ($scard_type == 'chinamobile') { //中国移动
  301. $card_type = 4;
  302. } elseif ($scard_type == 'chinaunicom') { //中国联通
  303. $card_type = 5;
  304. } elseif ($scard_type == 'chinatelecom') { //中国电信
  305. $card_type = 6;
  306. } else {
  307. continue;
  308. }
  309. $all_type_specs[$card_type] = $specs;
  310. }
  311. $result =[];
  312. foreach ($all_type_specs as $card_type => $specs) {
  313. foreach ($specs as $spec){
  314. $result["{$card_type}-{$spec}"] = ['card_type' => $card_type , 'spec' => $spec];
  315. }
  316. }
  317. return $result;
  318. };
  319. $merch_spector = function ($mchid)
  320. {
  321. $model_merchant = Model('merchant');
  322. $items = $model_merchant->table('merchant_price')->where(['mchid' => $mchid])->select();
  323. $result = [];
  324. foreach ($items as $item)
  325. {
  326. $card_types = explode(',' , $item['card_types']);
  327. foreach ($card_types as $card_type) {
  328. $value['spec'] = intval($item['spec']);
  329. $result["{$card_type}-{$value['spec']}"] = ['card_type'=>$card_type,
  330. 'spec' => intval($item['spec']),
  331. 'price' => $item['price']];
  332. }
  333. }
  334. return $result;
  335. };
  336. $all_cardtype_specs = $all_spector();
  337. $merch_cardtype_specs = $merch_spector($mchid);
  338. $merger = function ($all_specs,$mech_specs)
  339. {
  340. $result = [];
  341. foreach ($all_specs as $cardtype_spec => $value)
  342. {
  343. $card_name = $this->scard_type($value['card_type']);
  344. $data = [];
  345. $data['goods_name'] = $card_name;
  346. $data['card_type'] = $value['card_type'];
  347. $data['spec'] = $value['spec'];
  348. if(array_key_exists($cardtype_spec , $mech_specs)) {
  349. $data['price'] = $mech_specs[$cardtype_spec]['price'];
  350. } else {
  351. $data['price'] = 0;
  352. }
  353. $result[$value['card_type']][] = $data;
  354. }
  355. return $result;
  356. };
  357. $result = $merger($all_cardtype_specs,$merch_cardtype_specs);
  358. return $result;
  359. }
  360. public function check_merchantOp()
  361. {
  362. $mchid = trim($_GET['mchid']);
  363. if (!$mchid) {
  364. echo '';
  365. die;
  366. }
  367. $model_merchant = Model('merchant');
  368. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid], '*');
  369. if (empty($merchant_info)) {
  370. echo '';
  371. die;
  372. }
  373. echo 'true';
  374. }
  375. /**
  376. * ajax操作
  377. */
  378. public function ajaxOp()
  379. {
  380. $model_merchant = Model('merchant');
  381. switch ($_GET['branch']) {
  382. /**
  383. * 验证机构名称是否重复
  384. */
  385. case 'check_mch_name':
  386. $condition['name'] = $_GET['name'];
  387. $list = $model_merchant->getMerchantInfo($condition);
  388. if (empty($list)) {
  389. echo 'true';
  390. } else {
  391. echo 'false';
  392. }
  393. exit;
  394. /**
  395. * 验证机构是否存在
  396. */
  397. case 'check_merchant':
  398. $condition['mchid'] = intval($_GET['mchid']);
  399. $list = $model_merchant->getMerchantInfo($condition);
  400. if (!empty($list)) {
  401. echo 'true';
  402. } else {
  403. echo 'false';
  404. }
  405. exit;
  406. }
  407. }
  408. /**
  409. * 充值申请列表
  410. */
  411. public function refill_evidenceOp()
  412. {
  413. $model_merchant = Model('merchant');
  414. $condition = [];
  415. if (trim($_GET['mch_name']) != '') {
  416. $condition['mch_name'] = array('like', '%' . $_GET['mch_name'] . '%');
  417. Tpl::output('mch_name', $_GET['mch_name']);
  418. }
  419. $state_sel = intval($_REQUEST['state_sel']);
  420. if ($state_sel == 1) {
  421. $condition['check_time'] = 0;
  422. $condition['status'] = 1;
  423. } elseif ($state_sel == 2) {
  424. $condition['check_time'] = ['gt', 0];
  425. $condition['status'] = 2;
  426. } elseif ($state_sel == 3) {
  427. $condition['check_time'] = ['gt', 0];
  428. $condition['status'] = 3;
  429. } else {
  430. }
  431. //充值申请列表
  432. $evidence_list = $model_merchant->getRefillEvidence($condition, 20, '*', 'add_time desc');
  433. $status_text = ['申请中', '已通过', '已驳回'];
  434. $operation_text = ['未预存', '已预存'];
  435. Tpl::output('evidence_list', $evidence_list);
  436. Tpl::output('status_text', $status_text);
  437. Tpl::output('operation_text', $operation_text);
  438. Tpl::output('page', $model_merchant->showpage('2'));
  439. Tpl::showpage('merchant.refill.evidence_list');
  440. }
  441. public function check_evidenceOp()
  442. {
  443. $status = $_GET['status'];
  444. $apply_id = intval($_GET['apply_id']);
  445. if ($apply_id > 0) {
  446. $model_merchant = Model('merchant');
  447. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  448. if (empty($evidence_info)) {
  449. showMessage('充值申请不存在');
  450. }
  451. if ($status == 'pass') {
  452. $model_merchant->editRefillEvidence(['apply_id' => $apply_id], ['status' => 2, 'check_time' => time()]);
  453. } elseif ($status == 'unpass') {
  454. $model_merchant->editRefillEvidence(['apply_id' => $apply_id], ['status' => 3, 'check_time' => time()]);
  455. } else {
  456. }
  457. }
  458. showMessage(L('nc_common_save_succ'), urlAdmin('merchant', 'refill_evidence'));
  459. }
  460. public function rechargeOp()
  461. {
  462. if (chksubmit()) {
  463. $mchid = $this->add_money();
  464. $model_merchant = Model('merchant');
  465. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  466. if ($merchant_info['alarm_amount'] < $merchant_info['available_predeposit'])
  467. {
  468. //更新预警短信通知限制
  469. $mch_cache = rcache("merchant-notify" , 'refill-');
  470. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  471. $caches[$mchid] = ['last_time' => 0, 'send_count' => 0];
  472. wcache("merchant-notify", ['data' => serialize($caches)], 'refill-');
  473. }
  474. showMessage('操作成功', 'index.php?act=merchant&op=refill_evidence');
  475. } else {
  476. $apply_id = $_GET['apply_id'];
  477. if (!empty($apply_id)) {
  478. $model_merchant = Model('merchant');
  479. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  480. Tpl::output('apply_id', $apply_id);
  481. Tpl::output('amount', $evidence_info['amount']);
  482. }
  483. Tpl::showpage('recharge.add');
  484. }
  485. }
  486. public function add_money()
  487. {
  488. $obj_validate = new Validator();
  489. $obj_validate->validateparam = array(
  490. array("input" => $_POST["apply_id"], "require" => "true", "message" => Language::get('admin_points_member_error_again')),
  491. array("input" => $_POST["pointsnum"], "require" => "true", 'validator' => 'Compare', 'operator' => ' >= ', 'to' => 1, "message" => Language::get('admin_points_points_min_error'))
  492. );
  493. $error = $obj_validate->validate();
  494. if ($error != '') {
  495. showMessage($error, '', '', 'error');
  496. }
  497. $money = abs(floatval($_POST['pointsnum']));
  498. $memo = trim($_POST['pointsdesc']);
  499. if ($money <= 0) {
  500. showMessage('输入的金额必需大于0', '', 'html', 'error');
  501. }
  502. $apply_id = intval($_POST['apply_id']);
  503. $model_merchant = Model('merchant');
  504. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  505. if (!is_array($evidence_info) || count($evidence_info) <= 0) {
  506. showMessage("无效的充值申请信息", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  507. }
  508. if ($evidence_info['status'] != 2) {
  509. showMessage("该充值申请未被审核通过", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  510. }
  511. if ($evidence_info['is_operation'] == 2) {
  512. showMessage("该充值申请未已被预存过", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  513. }
  514. //查询会员信息
  515. $obj_member = Model('member');
  516. $member_id = intval($evidence_info['member_id']);
  517. $member_info = $obj_member->getMemberInfo(array('member_id' => $member_id));
  518. $available_predeposit = floatval($member_info['available_predeposit']);
  519. $freeze_predeposit = floatval($member_info['freeze_predeposit']);
  520. if ($_POST['operatetype'] == 2 && $money > $available_predeposit) {
  521. showMessage(('预存款不足,会员当前预存款') . $available_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  522. }
  523. if ($_POST['operatetype'] == 3 && $money > $available_predeposit) {
  524. showMessage(('可冻结预存款不足,会员当前预存款') . $available_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  525. }
  526. if ($_POST['operatetype'] == 4 && $money > $freeze_predeposit) {
  527. showMessage(('可恢复冻结预存款不足,会员当前冻结预存款') . $freeze_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  528. }
  529. $model_pd = Model('predeposit');
  530. $order_sn = $apply_id;
  531. $admininfo = $this->getAdminInfo();
  532. $log_msg = "管理员【" . $admininfo['admin_name'] . "】操作会员【" . $member_info['member_name'] . "】预存款,金额为" . $money . ",编号为" . $order_sn;
  533. $admin_act = 'sys_add_money';
  534. switch ($_POST['operatetype']) {
  535. case 1:
  536. $admin_act = "sys_add_money";
  537. $log_msg = "管理员【" . $admininfo['admin_name'] . "】操作会员【" . $member_info['member_name'] . "】预存款【增加】,金额为" . $money . ",编号为" . $order_sn;
  538. break;
  539. case 2:
  540. $admin_act = "sys_del_money";
  541. $log_msg = "管理员【" . $admininfo['admin_name'] . "】操作会员【" . $member_info['member_name'] . "】预存款【减少】,金额为" . $money . ",编号为" . $order_sn;
  542. break;
  543. default:
  544. showMessage('操作失败', 'index.php?act=merchant&op=refill_evidence');
  545. break;
  546. }
  547. try {
  548. $trans = new trans_wapper($model_pd, __METHOD__);
  549. //扣除冻结的预存款
  550. $data = array();
  551. $data['member_id'] = $member_info['member_id'];
  552. $data['member_name'] = $member_info['member_name'];
  553. $data['amount'] = $money;
  554. $data['order_sn'] = $order_sn;
  555. $data['admin_name'] = $admininfo['name'];
  556. $data['pdr_sn'] = $order_sn;
  557. $data['lg_desc'] = $memo;
  558. $model_pd->changePd($admin_act, $data);
  559. $model_merchant->editRefillEvidence(['apply_id' => $apply_id], ['is_operation' => 2]);
  560. $trans->commit();
  561. $this->log($log_msg, 1);
  562. return $evidence_info['mchid'];
  563. } catch (Exception $e) {
  564. $trans->rollback();
  565. $this->log($log_msg, 0);
  566. showMessage($e->getMessage(), 'index.php?act=merchant&op=refill_evidence', 'html', 'error');
  567. exit;
  568. }
  569. }
  570. public function checkevidenceOp()
  571. {
  572. $apply_id = trim($_GET['apply_id']);
  573. if (!$apply_id) {
  574. echo '';
  575. die;
  576. }
  577. $model_merchant = Model('merchant');
  578. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  579. if (empty($evidence_info)) {
  580. echo '';
  581. die;
  582. }
  583. echo 'true';
  584. }
  585. public function refill_orderOp()
  586. {
  587. $model_refill_order = Model('refill_order');
  588. $condition['refill_order.inner_status'] = 0;
  589. $condition['refill_order.order_time'] = ['time', [strtotime(date("Y-m-d")), strtotime(date("Y-m-d"))]];
  590. if($_GET['order_state'] == ORDER_STATE_SEND) {
  591. unset($condition['refill_order.order_time']);
  592. }
  593. if (!empty($_GET['order_sn'])) {
  594. $condition['refill_order.order_sn'] = $_GET['order_sn'];
  595. unset($condition['refill_order.order_time']);
  596. }
  597. if (!empty($_GET['mch_order'])) {
  598. $condition['refill_order.mch_order'] = $_GET['mch_order'];
  599. unset($condition['refill_order.order_time']);
  600. }
  601. if (!empty($_GET['mchid'])) {
  602. $condition['refill_order.mchid'] = $_GET['mchid'];
  603. unset($condition['refill_order.order_time']);
  604. }
  605. if (!empty($_GET['channel_name'])) {
  606. $condition['refill_order.channel_name'] = $_GET['channel_name'];
  607. unset($condition['refill_order.order_time']);
  608. }
  609. if (!empty($_GET['card_type'])) {
  610. if(in_array($_GET['card_type'] , ['1' , '2' , '4' , '5' , '6'])) {
  611. $condition['refill_order.card_type'] = $_GET['card_type'];
  612. }
  613. if($_GET['card_type'] == 'oil') {
  614. $condition['refill_order.card_type'] = ['in' , ['1' , '2']];
  615. }
  616. if($_GET['card_type'] == 'phone') {
  617. $condition['refill_order.card_type'] = ['in' , ['4' , '5' , '6']];
  618. }
  619. }
  620. if (!empty($_GET['ch_trade_no'])) {
  621. $condition['refill_order.ch_trade_no'] = $_GET['ch_trade_no'];
  622. unset($condition['refill_order.order_time']);
  623. }
  624. if (!empty($_GET['card_no'])) {
  625. $condition['refill_order.card_no'] = $_GET['card_no'];
  626. unset($condition['refill_order.order_time']);
  627. }
  628. if (in_array($_GET['refill_amount'], ['10', '20', '30', '50' , '100', '200', '300', '500' , '1000', '20000'])) {
  629. $condition['refill_order.refill_amount'] = $_GET['refill_amount'];
  630. }
  631. $if_start_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $_GET['query_start_time']);
  632. $if_end_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $_GET['query_end_time']);
  633. $start_unixtime = $if_start_time ? strtotime($_GET['query_start_time']) : null;
  634. $end_unixtime = $if_end_time ? strtotime($_GET['query_end_time']) : null;
  635. if ($start_unixtime || $end_unixtime) {
  636. $condition['refill_order.order_time'] = ['time', [$start_unixtime, $end_unixtime]];
  637. }
  638. if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40'])) {
  639. $condition['vr_order.order_state'] = $_GET['order_state'];
  640. if($_GET['order_state'] == ORDER_STATE_SEND) {
  641. if($_GET['time'] == 1){
  642. $condition['refill_order.order_time'] = ['between', [(time() - 3600) , (time() - 1800)]];
  643. }
  644. if($_GET['time'] == 2){
  645. $condition['refill_order.order_time'] = ['lt', (time() - 3600)];
  646. }
  647. }
  648. }
  649. $merchant_list = Model('')->table('merchant')->limit(100)->select();
  650. foreach ($merchant_list as $key =>$value) {
  651. $merchants[$value['mchid']] = $value;
  652. }
  653. $order_list = $model_refill_order->getMerchantOrderList($condition, 50, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
  654. $stat = Model('')->table('refill_order,vr_order')->join('inner')
  655. ->on('refill_order.order_id=vr_order.order_id')
  656. ->field('count(*) as order_count ,sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts')
  657. ->where($condition)->select();
  658. $count = $this->refill_stat($condition);
  659. foreach ($order_list as $order_id => $order_info) {
  660. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  661. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  662. if($order_info['notify_time'] > 0)
  663. {
  664. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($order_info['notify_time'] - $order_info['order_time']);
  665. $order_list[$order_id]['diff_time'] = $order_info['notify_time'] - $order_info['order_time'];
  666. }
  667. else
  668. {
  669. $order_list[$order_id]['diff_time_text'] = $this->elapse_time(time() - $order_info['order_time']);
  670. $order_list[$order_id]['diff_time'] = time() - $order_info['order_time'];
  671. }
  672. }
  673. Tpl::output('stat', $stat[0]);
  674. Tpl::output('count', $count);
  675. Tpl::output('order_list', $order_list);
  676. Tpl::output('show_page', $model_refill_order->showpage());
  677. Tpl::showpage('refill.order.index');
  678. }
  679. public function notify_merchantOp()
  680. {
  681. $order_id = $_GET['order_id'];
  682. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  683. showMessage('操作成功', 'index.php?act=merchant&op=refill_order');
  684. }
  685. public function notify_manual_merchantOp()
  686. {
  687. $order_id = $_GET['order_id'];
  688. $type = $_GET['type'];
  689. $mod_order = Model('vr_order');
  690. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  691. if((time() - $order_info['order_time']) > 3600)
  692. {
  693. showMessage('订单时间未超过1小时', 'index.php?act=merchant&op=refill_order');
  694. }
  695. $logic_vr_order = Logic("vr_order");
  696. if($type == 'success') {
  697. $logic_vr_order->changeOrderStateSuccess($order_id);
  698. }elseif ($type == 'cancel') {
  699. $logic_vr_order->changeOrderStateCancel($order_info, '', "后台手动回调通知失败.");
  700. }else {
  701. showMessage('手动操作类型错误', 'index.php?act=merchant&op=refill_order');
  702. }
  703. $mod_refill = Model('refill_order');
  704. $mod_refill->edit($order_id, ['notify_time' => time(), 'notify_state' => 1]);
  705. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  706. showMessage('操作成功', 'index.php?act=merchant&op=refill_order');
  707. }
  708. public function providerOp()
  709. {
  710. $this->sync_cfgs();
  711. $provider_model = Model('refill_provider');
  712. $condition = [];
  713. if (trim($_GET['name']) != '') {
  714. $condition['name'] = ['like', '%' . $_GET['name'] . '%'];
  715. Tpl::output('name', $_GET['name']);
  716. }
  717. if (in_array($_GET['type'], [1, 2])) {
  718. $condition['type'] = $_GET['type'];
  719. }
  720. $provider_list = $provider_model->getProviderList($condition, 25);
  721. foreach ($provider_list as $key => $provider) {
  722. if(!empty($provider['start_period']) && !empty($provider['end_period'])){
  723. $provider_list[$key]['period'] = $provider['start_period'] . '~' . $provider['end_period'];
  724. }else{
  725. $provider_list[$key]['period'] = '全时间段';
  726. }
  727. }
  728. $opened_text = ['使用中', '已禁用'];
  729. $type_text = ['油卡', '手机充值卡'];
  730. Tpl::output('opened_text', $opened_text);
  731. Tpl::output('type_text', $type_text);
  732. Tpl::output('provider_list', $provider_list);
  733. Tpl::output('show_page', $provider_model->showpage());
  734. Tpl::showpage('provider.index');
  735. }
  736. public function sync_cfgs()
  737. {
  738. $name_val_cfg = function ($items) {
  739. $result = [];
  740. foreach ($items as $item) {
  741. $name = $item->name();
  742. $result[$name] = $item;
  743. }
  744. return $result;
  745. };
  746. $name_val_row = function ($items) {
  747. $result = [];
  748. foreach ($items as $item) {
  749. $name = $item['name'];
  750. $result[$name] = $item;
  751. }
  752. return $result;
  753. };
  754. $match = function ($all,$cur)
  755. {
  756. $insert = [];
  757. foreach ($all as $key => $value)
  758. {
  759. if(!array_key_exists($key,$cur)) {
  760. $insert[] = $key;
  761. }
  762. }
  763. return $insert;
  764. };
  765. $inserter = function ($mod,$type,$names)
  766. {
  767. foreach ($names as $name) {
  768. $data = ['name' => $name,'type' => $type];
  769. $mod->insert($data);
  770. }
  771. };
  772. $providers = refill\RefillFactory::instance()->providers();
  773. $oils = $name_val_cfg($providers['oil']);
  774. $phones = $name_val_cfg($providers['phone']);
  775. $mod_prov = Model('refill_provider');
  776. $oil_items = $mod_prov->getProviderList(['type' => 1]);
  777. $oil_items = $name_val_row($oil_items);
  778. $oil_inserts = $match($oils,$oil_items);
  779. $phone_items = $mod_prov->getProviderList(['type' => 2]);
  780. $phone_items = $name_val_row($phone_items);
  781. $phone_inserts = $match($phones,$phone_items);
  782. if(!in_array('afandeng' , $phone_items)) {
  783. $mod_prov->insert(['name' => 'afandeng' , 'type' => 2]);
  784. }
  785. $inserter($mod_prov,1,$oil_inserts);
  786. $inserter($mod_prov,2,$phone_inserts);
  787. }
  788. public function changeProviderStateOp()
  789. {
  790. $provider_id = intval($_GET['id']);
  791. $state = intval($_GET['state']);
  792. $provider_model = Model('refill_provider');
  793. $provider_info = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  794. if (empty($provider_info) || !in_array($state, [1, 2])) {
  795. showMessage('操作成功', 'index.php?act=merchant&op=provider');
  796. }
  797. $resp = $provider_model->editProvider(['opened' => $state], ['provider_id' => $provider_id]);
  798. if (!$resp) {
  799. showMessage('操作失败', 'index.php?act=merchant&op=provider', 'html', 'error');
  800. }
  801. showMessage('操作成功', 'index.php?act=merchant&op=provider');
  802. }
  803. /**
  804. * 新增通道
  805. */
  806. public function provider_addOp()
  807. {
  808. $provider_model = Model('refill_provider');
  809. if (chksubmit()) {
  810. $params = $_POST;
  811. unset($params['form_submit']);
  812. $result = $provider_model->addProvider($params);
  813. if ($result) {
  814. $url = [
  815. [
  816. 'url' => 'index.php?act=merchant&op=provider',
  817. 'msg' => '返回通道列表',
  818. ],
  819. [
  820. 'url' => 'index.php?act=merchant&op=provider_add',
  821. 'msg' => '继续新增通道',
  822. ],
  823. ];
  824. $this->log('添加通道:' . '[ ' . $_POST['name'] . ']', 1);
  825. showMessage('通道添加成功', $url);
  826. } else {
  827. showMessage('通道添加失败');
  828. }
  829. }
  830. Tpl::showpage('provider.add');
  831. }
  832. public function provider_editOp()
  833. {
  834. $provider_model = Model('refill_provider');
  835. if (chksubmit()) {
  836. $provider_id = intval($_POST['provider_id']) ?? '';
  837. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  838. if (empty($provider)) {
  839. showMessage('通道信息有误');
  840. }
  841. $params = $_POST;
  842. unset($params['form_submit']);
  843. if (empty($params)) {
  844. showMessage('通道编辑成功', 'index.php?act=merchant&op=provider');
  845. }
  846. $result = $provider_model->editProvider($params, ['provider_id' => $provider_id]);
  847. if ($result) {
  848. $this->log('编辑通道:' . '[ ' . $provider['name'] . ']', 1);
  849. showMessage('通道编辑成功', 'index.php?act=merchant&op=provider');
  850. } else {
  851. showMessage('通道编辑失败', "index.php?act=merchant&op=provider_edit&id={$provider_id}");
  852. }
  853. }
  854. $provider_id = intval($_GET['id']) ?? '';
  855. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  856. if (empty($provider)) {
  857. showMessage('通道信息有误');
  858. }
  859. $type_text = ['油卡', '手机充值卡'];
  860. Tpl::output('type_text', $type_text);
  861. Tpl::output('provider', $provider);
  862. Tpl::showpage('provider.edit');
  863. }
  864. public function provider_delOp()
  865. {
  866. $provider_model = Model('refill_provider');
  867. $provider_id = intval($_GET['id']) ?? '';
  868. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  869. if (empty($provider)) {
  870. showMessage('通道信息有误');
  871. }
  872. $result = $provider_model->delProvider(['provider_id' => $provider_id]);
  873. if ($result) {
  874. $this->log('删除通道:' . '[ ' . $provider['name'] . ']', 1);
  875. showMessage('通道删除成功', 'index.php?act=merchant&op=provider');
  876. } else {
  877. showMessage('通道删除失败', "index.php?act=merchant&op=provider");
  878. }
  879. }
  880. private function scard_type(int $card_type)
  881. {
  882. if ($card_type == 1) { //中石油
  883. return '中石油';
  884. } elseif ($card_type == 2) { //中石化
  885. return '中石化';
  886. } elseif ($card_type == 4) { //中国移动
  887. return '中国移动';
  888. } elseif ($card_type == 5) { //中国联通
  889. return '中国联通';
  890. } elseif ($card_type == 6) { //中国电信
  891. return '中国电信';
  892. } else {
  893. return 'unknown';
  894. }
  895. }
  896. private function elapse_time($seconds)
  897. {
  898. $minutes = intval($seconds / 60);
  899. $second = intval($seconds % 60);
  900. if($minutes >= 60) {
  901. $minute = $minutes % 60;
  902. $hours = intval($minutes / 60);
  903. $result = "{$minute}m{$second}s";
  904. }
  905. else {
  906. if($minutes > 0){
  907. $result = "{$minutes}m{$second}s";
  908. }else{
  909. $result = "{$second}s";
  910. }
  911. }
  912. if(isset($hours))
  913. {
  914. $result = "{$hours}h{$minute}m";
  915. }
  916. return $result;
  917. }
  918. private function refill_stat($condition)
  919. {
  920. $model_refill_order = Model('refill_order');
  921. $i = $half_count = $hour_count = 0;
  922. while (true)
  923. {
  924. $start = $i * 100000;
  925. $orders = $model_refill_order->getMerchantOrderList($condition, '', 'refill_order.*,vr_order.order_state','order_time desc' ,"{$start},100000");
  926. $i++;
  927. if (empty($orders)) {
  928. break;
  929. }
  930. foreach ($orders as $order)
  931. {
  932. if($order['notify_time'] == 0 || $order['notify_time'] == null)
  933. {
  934. $order['diff_time'] = time() - $order['order_time'];
  935. if($order['order_state'] == ORDER_STATE_SEND) {
  936. if($order['diff_time'] > 1800 && $order['diff_time'] < 3600) {
  937. $half_count++;
  938. }
  939. if($order['diff_time'] > 3600) {
  940. $hour_count++;
  941. }
  942. }
  943. }
  944. }
  945. if (count($orders) < 100000) {
  946. break;
  947. }
  948. }
  949. return ['half_count' => $half_count , 'hour_count' => $hour_count];
  950. }
  951. }