merchant.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 orderDayLimitOp()
  140. {
  141. if (chksubmit()) {
  142. $mchids = $_POST['mchid'];
  143. $petrochinas = $_POST['petrochina'];
  144. $sinopecs = $_POST['sinopec'];
  145. foreach ($mchids as $key => $mchid) {
  146. $data['mchid'] = $mchid;
  147. $data['petrochina'] = ['100' => $petrochinas[100][$key] , '200' => $petrochinas[200][$key] , '500' => $petrochinas[500][$key] , '1000' => $petrochinas[1000][$key]];
  148. $data['sinopec'] = ['100' => $sinopecs[100][$key] , '200' => $sinopecs[200][$key] , '500' => $sinopecs[500][$key] , '1000' => $sinopecs[1000][$key]];
  149. $result[$mchid] = $data;
  150. }
  151. wcache('order_limit' , ['data' => serialize($result)] , 'merchant-');
  152. showMessage('编辑成功', 'index.php?act=merchant&op=merchant');
  153. }
  154. $order_limit = rcache('order_limit' , 'merchant-');
  155. if(empty($order_limit)){
  156. $order_limit = [];
  157. }else{
  158. $order_limit = unserialize($order_limit['data']);
  159. }
  160. $default = function ($merchant) {
  161. $amount = [
  162. '100' => 0,
  163. '200' => 0,
  164. '500' => 0,
  165. '1000' => 0
  166. ];
  167. $data['mchid'] = $merchant['mchid'];
  168. $data['petrochina'] = $data['sinopec'] = $amount;
  169. return $data;
  170. };
  171. $model_merchant = model('merchant');
  172. $merchant_list = $model_merchant->getMerchantList([], 1000, 'mchid asc' , 'mchid,company_name');
  173. foreach ($merchant_list as $merchant)
  174. {
  175. if(array_key_exists($merchant['mchid'] , $order_limit)) {
  176. $result[$merchant['mchid']] = $order_limit[$merchant['mchid']];
  177. }else{
  178. $result[$merchant['mchid']] = $default($merchant);
  179. }
  180. }
  181. Tpl::output('data', $result);
  182. Tpl::showpage('merchant.orderlimit');
  183. }
  184. public function priceOp()
  185. {
  186. if (chksubmit())
  187. {
  188. $mchid = $_POST['mchid'];
  189. //合并表单数据
  190. $card_types = $_POST['cardtype'];
  191. $specs = $_POST['spec'];
  192. $prices = $_POST['price'];
  193. foreach ($card_types as $key => $card_type) {
  194. $data['card_type'] = $card_type;
  195. $data['spec'] = intval($specs[$key]);
  196. $data['price'] = ncPriceFormat($prices[$key]);
  197. $params[] = $data;
  198. }
  199. foreach ($params as $param)
  200. {
  201. if($param['price'] > 0) {
  202. $insert['mchid'] = $mchid;
  203. $insert['spec'] = $param['spec'];
  204. $insert['price'] = $param['price'];
  205. $insert['card_types'] = $param['card_type'];
  206. $inserts[] = $insert;
  207. }
  208. }
  209. if(empty($inserts)) {
  210. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  211. }
  212. try {
  213. $model_merchant = Model('merchant');
  214. $trans = new trans_wapper($model_merchant, __METHOD__);
  215. //删除旧费率
  216. $model_merchant->delPrices($mchid);
  217. //更新新费率
  218. $model_merchant->insertPrices($inserts);
  219. $trans->commit();
  220. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  221. } catch (Exception $e) {
  222. $trans->rollback();
  223. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  224. }
  225. }
  226. $mchid = $_GET['mchid'] ?? 0;
  227. $goods = $this->GoodsFormat($mchid);
  228. Tpl::output('goods', $goods);
  229. Tpl::showpage('merchant.price');
  230. }
  231. public function getPriceOp()
  232. {
  233. $mchid = $_GET['mchid'] ?? 0;
  234. $model_merchant = Model('merchant');
  235. $items = $model_merchant->table('merchant_price')->where(['mchid' => $mchid])->select();
  236. $check = true;
  237. if (empty($items)) {
  238. $check = false;
  239. }
  240. $goods = $this->GoodsFormat($mchid);
  241. echo json_encode(['check' => $check, 'data' => $goods]);
  242. }
  243. private function GoodsFormat($mchid)
  244. {
  245. $all_spector = function ()
  246. {
  247. global $config;
  248. $refill_specs = $config['refill_specs'];
  249. $all_type_specs = [];
  250. foreach ($refill_specs as $scard_type => $specs)
  251. {
  252. if ($scard_type == 'petrochina') { //中石油
  253. $card_type = 1;
  254. } elseif ($scard_type == 'sinopec') { //中石化
  255. $card_type = 2;
  256. } elseif ($scard_type == 'chinamobile') { //中国移动
  257. $card_type = 4;
  258. } elseif ($scard_type == 'chinaunicom') { //中国联通
  259. $card_type = 5;
  260. } elseif ($scard_type == 'chinatelecom') { //中国电信
  261. $card_type = 6;
  262. } else {
  263. continue;
  264. }
  265. $all_type_specs[$card_type] = $specs;
  266. }
  267. $result =[];
  268. foreach ($all_type_specs as $card_type => $specs) {
  269. foreach ($specs as $spec){
  270. $result["{$card_type}-{$spec}"] = ['card_type' => $card_type , 'spec' => $spec];
  271. }
  272. }
  273. return $result;
  274. };
  275. $merch_spector = function ($mchid)
  276. {
  277. $model_merchant = Model('merchant');
  278. $items = $model_merchant->table('merchant_price')->where(['mchid' => $mchid])->select();
  279. $result = [];
  280. foreach ($items as $item)
  281. {
  282. $card_types = explode(',' , $item['card_types']);
  283. foreach ($card_types as $card_type) {
  284. $value['spec'] = intval($item['spec']);
  285. $result["{$card_type}-{$value['spec']}"] = ['card_type'=>$card_type,
  286. 'spec' => intval($item['spec']),
  287. 'price' => $item['price']];
  288. }
  289. }
  290. return $result;
  291. };
  292. $all_cardtype_specs = $all_spector();
  293. $merch_cardtype_specs = $merch_spector($mchid);
  294. $merger = function ($all_specs,$mech_specs)
  295. {
  296. $result = [];
  297. foreach ($all_specs as $cardtype_spec => $value)
  298. {
  299. $card_name = $this->scard_type($value['card_type']);
  300. $data = [];
  301. $data['goods_name'] = $card_name;
  302. $data['card_type'] = $value['card_type'];
  303. $data['spec'] = $value['spec'];
  304. if(array_key_exists($cardtype_spec , $mech_specs)) {
  305. $data['price'] = $mech_specs[$cardtype_spec]['price'];
  306. } else {
  307. $data['price'] = 0;
  308. }
  309. $result[] = $data;
  310. }
  311. return $result;
  312. };
  313. $result = $merger($all_cardtype_specs,$merch_cardtype_specs);
  314. return $result;
  315. }
  316. public function check_merchantOp()
  317. {
  318. $mchid = trim($_GET['mchid']);
  319. if (!$mchid) {
  320. echo '';
  321. die;
  322. }
  323. $model_merchant = Model('merchant');
  324. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid], '*');
  325. if (empty($merchant_info)) {
  326. echo '';
  327. die;
  328. }
  329. echo 'true';
  330. }
  331. /**
  332. * ajax操作
  333. */
  334. public function ajaxOp()
  335. {
  336. $model_merchant = Model('merchant');
  337. switch ($_GET['branch']) {
  338. /**
  339. * 验证机构名称是否重复
  340. */
  341. case 'check_mch_name':
  342. $condition['name'] = $_GET['name'];
  343. $list = $model_merchant->getMerchantInfo($condition);
  344. if (empty($list)) {
  345. echo 'true';
  346. } else {
  347. echo 'false';
  348. }
  349. exit;
  350. /**
  351. * 验证机构是否存在
  352. */
  353. case 'check_merchant':
  354. $condition['mchid'] = intval($_GET['mchid']);
  355. $list = $model_merchant->getMerchantInfo($condition);
  356. if (!empty($list)) {
  357. echo 'true';
  358. } else {
  359. echo 'false';
  360. }
  361. exit;
  362. }
  363. }
  364. /**
  365. * 充值申请列表
  366. */
  367. public function refill_evidenceOp()
  368. {
  369. $model_merchant = Model('merchant');
  370. $condition = [];
  371. if (trim($_GET['mch_name']) != '') {
  372. $condition['mch_name'] = array('like', '%' . $_GET['mch_name'] . '%');
  373. Tpl::output('mch_name', $_GET['mch_name']);
  374. }
  375. $state_sel = intval($_REQUEST['state_sel']);
  376. if ($state_sel == 1) {
  377. $condition['check_time'] = 0;
  378. $condition['status'] = 1;
  379. } elseif ($state_sel == 2) {
  380. $condition['check_time'] = ['gt', 0];
  381. $condition['status'] = 2;
  382. } elseif ($state_sel == 3) {
  383. $condition['check_time'] = ['gt', 0];
  384. $condition['status'] = 3;
  385. } else {
  386. }
  387. //充值申请列表
  388. $evidence_list = $model_merchant->getRefillEvidence($condition, 20, '*', 'add_time desc');
  389. $status_text = ['申请中', '已通过', '已驳回'];
  390. $operation_text = ['未预存', '已预存'];
  391. Tpl::output('evidence_list', $evidence_list);
  392. Tpl::output('status_text', $status_text);
  393. Tpl::output('operation_text', $operation_text);
  394. Tpl::output('page', $model_merchant->showpage('2'));
  395. Tpl::showpage('merchant.refill.evidence_list');
  396. }
  397. public function check_evidenceOp()
  398. {
  399. $status = $_GET['status'];
  400. $apply_id = intval($_GET['apply_id']);
  401. if ($apply_id > 0) {
  402. $model_merchant = Model('merchant');
  403. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  404. if (empty($evidence_info)) {
  405. showMessage('充值申请不存在');
  406. }
  407. if ($status == 'pass') {
  408. $model_merchant->editRefillEvidence(['apply_id' => $apply_id], ['status' => 2, 'check_time' => time()]);
  409. } elseif ($status == 'unpass') {
  410. $model_merchant->editRefillEvidence(['apply_id' => $apply_id], ['status' => 3, 'check_time' => time()]);
  411. } else {
  412. }
  413. }
  414. showMessage(L('nc_common_save_succ'), urlAdmin('merchant', 'refill_evidence'));
  415. }
  416. public function rechargeOp()
  417. {
  418. if (chksubmit()) {
  419. $this->add_money();
  420. } else {
  421. $apply_id = $_GET['apply_id'];
  422. if (!empty($apply_id)) {
  423. $model_merchant = Model('merchant');
  424. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  425. Tpl::output('apply_id', $apply_id);
  426. Tpl::output('amount', $evidence_info['amount']);
  427. }
  428. Tpl::showpage('recharge.add');
  429. }
  430. }
  431. public function add_money()
  432. {
  433. $obj_validate = new Validator();
  434. $obj_validate->validateparam = array(
  435. array("input" => $_POST["apply_id"], "require" => "true", "message" => Language::get('admin_points_member_error_again')),
  436. array("input" => $_POST["pointsnum"], "require" => "true", 'validator' => 'Compare', 'operator' => ' >= ', 'to' => 1, "message" => Language::get('admin_points_points_min_error'))
  437. );
  438. $error = $obj_validate->validate();
  439. if ($error != '') {
  440. showMessage($error, '', '', 'error');
  441. }
  442. $money = abs(floatval($_POST['pointsnum']));
  443. $memo = trim($_POST['pointsdesc']);
  444. if ($money <= 0) {
  445. showMessage('输入的金额必需大于0', '', 'html', 'error');
  446. }
  447. $apply_id = intval($_POST['apply_id']);
  448. $model_merchant = Model('merchant');
  449. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  450. if (!is_array($evidence_info) || count($evidence_info) <= 0) {
  451. showMessage("无效的充值申请信息", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  452. }
  453. if ($evidence_info['status'] != 2) {
  454. showMessage("该充值申请未被审核通过", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  455. }
  456. if ($evidence_info['is_operation'] == 2) {
  457. showMessage("该充值申请未已被预存过", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  458. }
  459. //查询会员信息
  460. $obj_member = Model('member');
  461. $member_id = intval($evidence_info['member_id']);
  462. $member_info = $obj_member->getMemberInfo(array('member_id' => $member_id));
  463. $available_predeposit = floatval($member_info['available_predeposit']);
  464. $freeze_predeposit = floatval($member_info['freeze_predeposit']);
  465. if ($_POST['operatetype'] == 2 && $money > $available_predeposit) {
  466. showMessage(('预存款不足,会员当前预存款') . $available_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  467. }
  468. if ($_POST['operatetype'] == 3 && $money > $available_predeposit) {
  469. showMessage(('可冻结预存款不足,会员当前预存款') . $available_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  470. }
  471. if ($_POST['operatetype'] == 4 && $money > $freeze_predeposit) {
  472. showMessage(('可恢复冻结预存款不足,会员当前冻结预存款') . $freeze_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  473. }
  474. $model_pd = Model('predeposit');
  475. $order_sn = $apply_id;
  476. $admininfo = $this->getAdminInfo();
  477. $log_msg = "管理员【" . $admininfo['admin_name'] . "】操作会员【" . $member_info['member_name'] . "】预存款,金额为" . $money . ",编号为" . $order_sn;
  478. $admin_act = 'sys_add_money';
  479. switch ($_POST['operatetype']) {
  480. case 1:
  481. $admin_act = "sys_add_money";
  482. $log_msg = "管理员【" . $admininfo['admin_name'] . "】操作会员【" . $member_info['member_name'] . "】预存款【增加】,金额为" . $money . ",编号为" . $order_sn;
  483. break;
  484. case 2:
  485. $admin_act = "sys_del_money";
  486. $log_msg = "管理员【" . $admininfo['admin_name'] . "】操作会员【" . $member_info['member_name'] . "】预存款【减少】,金额为" . $money . ",编号为" . $order_sn;
  487. break;
  488. default:
  489. showMessage('操作失败', 'index.php?act=merchant&op=refill_evidence');
  490. break;
  491. }
  492. try {
  493. $trans = new trans_wapper($model_pd, __METHOD__);
  494. //扣除冻结的预存款
  495. $data = array();
  496. $data['member_id'] = $member_info['member_id'];
  497. $data['member_name'] = $member_info['member_name'];
  498. $data['amount'] = $money;
  499. $data['order_sn'] = $order_sn;
  500. $data['admin_name'] = $admininfo['name'];
  501. $data['pdr_sn'] = $order_sn;
  502. $data['lg_desc'] = $memo;
  503. $model_pd->changePd($admin_act, $data);
  504. $model_merchant->editRefillEvidence(['apply_id' => $apply_id], ['is_operation' => 2]);
  505. $trans->commit();
  506. $this->log($log_msg, 1);
  507. showMessage('操作成功', 'index.php?act=merchant&op=refill_evidence');
  508. } catch (Exception $e) {
  509. $trans->rollback();
  510. $this->log($log_msg, 0);
  511. showMessage($e->getMessage(), 'index.php?act=merchant&op=refill_evidence', 'html', 'error');
  512. }
  513. }
  514. public function checkevidenceOp()
  515. {
  516. $apply_id = trim($_GET['apply_id']);
  517. if (!$apply_id) {
  518. echo '';
  519. die;
  520. }
  521. $model_merchant = Model('merchant');
  522. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  523. if (empty($evidence_info)) {
  524. echo '';
  525. die;
  526. }
  527. echo 'true';
  528. }
  529. public function refill_orderOp()
  530. {
  531. $model_refill_order = Model('refill_order');
  532. $condition['inner_status'] = 0;
  533. if (!empty($_GET['order_sn'])) {
  534. $condition['refill_order.order_sn'] = $_GET['order_sn'];
  535. }
  536. if (!empty($_GET['mchid'])) {
  537. $condition['refill_order.mchid'] = $_GET['mchid'];
  538. }
  539. if (!empty($_GET['card_type'])) {
  540. if(in_array($_GET['card_type'] , ['1' , '2' , '4' , '5' , '6'])) {
  541. $condition['refill_order.card_type'] = $_GET['card_type'];
  542. }
  543. if($_GET['card_type'] == 'oil') {
  544. $condition['refill_order.card_type'] = ['in' , ['1' , '2']];
  545. }
  546. if($_GET['card_type'] == 'phone') {
  547. $condition['refill_order.card_type'] = ['in' , ['4' , '5' , '6']];
  548. }
  549. }
  550. if (!empty($_GET['ch_trade_no'])) {
  551. $condition['refill_order.ch_trade_no'] = $_GET['ch_trade_no'];
  552. }
  553. if (!empty($_GET['card_no'])) {
  554. $condition['refill_order.card_no'] = $_GET['card_no'];
  555. }
  556. if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40'])) {
  557. $condition['vr_order.order_state'] = $_GET['order_state'];
  558. }
  559. if (in_array($_GET['refill_amount'], ['10', '20', '30', '50' , '100', '200', '300', '500' , '1000', '20000'])) {
  560. $condition['refill_order.refill_amount'] = $_GET['refill_amount'];
  561. }
  562. $if_start_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $_GET['query_start_time']);
  563. $if_end_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $_GET['query_end_time']);
  564. $start_unixtime = $if_start_time ? strtotime($_GET['query_start_time']) : null;
  565. $end_unixtime = $if_end_time ? strtotime($_GET['query_end_time']) : null;
  566. if ($start_unixtime || $end_unixtime) {
  567. $condition['refill_order.order_time'] = ['time', [$start_unixtime, $end_unixtime]];
  568. }
  569. $merchant_list = Model('')->table('merchant')->select();
  570. foreach ($merchant_list as $key =>$value) {
  571. $merchants[$value['mchid']] = $value;
  572. }
  573. $order_list = $model_refill_order->getMerchantOrderList($condition, 30, 'refill_order.*,vr_order.order_state');
  574. $stat = Model('')->table('refill_order,vr_order')->join('inner')
  575. ->on('refill_order.order_id=vr_order.order_id')
  576. ->field('count(*) as order_count ,sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts')
  577. ->where($condition)->select();
  578. $count = $this->refill_stat($condition);
  579. foreach ($order_list as $order_id => $order_info) {
  580. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  581. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  582. if($order_info['notify_time'] > 0)
  583. {
  584. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($order_info['notify_time'] - $order_info['order_time']);
  585. $order_list[$order_id]['diff_time'] = $order_info['notify_time'] - $order_info['order_time'];
  586. }
  587. else
  588. {
  589. $order_list[$order_id]['diff_time_text'] = $this->elapse_time(time() - $order_info['order_time']);
  590. $order_list[$order_id]['diff_time'] = time() - $order_info['order_time'];
  591. }
  592. }
  593. Tpl::output('stat', $stat[0]);
  594. Tpl::output('count', $count);
  595. Tpl::output('order_list', $order_list);
  596. Tpl::output('show_page', $model_refill_order->showpage());
  597. Tpl::showpage('refill.order.index');
  598. }
  599. public function notify_merchantOp()
  600. {
  601. $order_id = $_GET['order_id'];
  602. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  603. showMessage('操作成功', 'index.php?act=merchant&op=refill_order');
  604. }
  605. public function providerOp()
  606. {
  607. $this->sync_cfgs();
  608. $provider_model = Model('refill_provider');
  609. $condition = [];
  610. if (trim($_GET['name']) != '') {
  611. $condition['name'] = ['like', '%' . $_GET['name'] . '%'];
  612. Tpl::output('name', $_GET['name']);
  613. }
  614. if (in_array($_GET['type'], [1, 2])) {
  615. $condition['type'] = $_GET['type'];
  616. }
  617. $provider_list = $provider_model->getProviderList($condition, 25);
  618. foreach ($provider_list as $key => $provider) {
  619. if(!empty($provider['start_period']) && !empty($provider['end_period'])){
  620. $provider_list[$key]['period'] = $provider['start_period'] . '~' . $provider['end_period'];
  621. }else{
  622. $provider_list[$key]['period'] = '全时间段';
  623. }
  624. }
  625. $opened_text = ['使用中', '已禁用'];
  626. $type_text = ['油卡', '手机充值卡'];
  627. Tpl::output('opened_text', $opened_text);
  628. Tpl::output('type_text', $type_text);
  629. Tpl::output('provider_list', $provider_list);
  630. Tpl::output('show_page', $provider_model->showpage());
  631. Tpl::showpage('provider.index');
  632. }
  633. public function sync_cfgs()
  634. {
  635. $name_val_cfg = function ($items) {
  636. $result = [];
  637. foreach ($items as $item) {
  638. $name = $item->name();
  639. $result[$name] = $item;
  640. }
  641. return $result;
  642. };
  643. $name_val_row = function ($items) {
  644. $result = [];
  645. foreach ($items as $item) {
  646. $name = $item['name'];
  647. $result[$name] = $item;
  648. }
  649. return $result;
  650. };
  651. $match = function ($all,$cur)
  652. {
  653. $insert = [];
  654. foreach ($all as $key => $value)
  655. {
  656. if(!array_key_exists($key,$cur)) {
  657. $insert[] = $key;
  658. }
  659. }
  660. return $insert;
  661. };
  662. $inserter = function ($mod,$type,$names)
  663. {
  664. foreach ($names as $name) {
  665. $data = ['name' => $name,'type' => $type];
  666. $mod->insert($data);
  667. }
  668. };
  669. $providers = refill\RefillFactory::instance()->providers();
  670. $oils = $name_val_cfg($providers['oil']);
  671. $phones = $name_val_cfg($providers['phone']);
  672. $mod_prov = Model('refill_provider');
  673. $oil_items = $mod_prov->getProviderList(['type' => 1]);
  674. $oil_items = $name_val_row($oil_items);
  675. $oil_inserts = $match($oils,$oil_items);
  676. $phone_items = $mod_prov->getProviderList(['type' => 2]);
  677. $phone_items = $name_val_row($phone_items);
  678. $phone_inserts = $match($phones,$phone_items);
  679. $inserter($mod_prov,1,$oil_inserts);
  680. $inserter($mod_prov,2,$phone_inserts);
  681. }
  682. public function changeProviderStateOp()
  683. {
  684. $provider_id = intval($_GET['id']);
  685. $state = intval($_GET['state']);
  686. $provider_model = Model('refill_provider');
  687. $provider_info = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  688. if (empty($provider_info) || !in_array($state, [1, 2])) {
  689. showMessage('操作成功', 'index.php?act=merchant&op=provider');
  690. }
  691. $resp = $provider_model->editProvider(['opened' => $state], ['provider_id' => $provider_id]);
  692. if (!$resp) {
  693. showMessage('操作失败', 'index.php?act=merchant&op=provider', 'html', 'error');
  694. }
  695. showMessage('操作成功', 'index.php?act=merchant&op=provider');
  696. }
  697. /**
  698. * 新增通道
  699. */
  700. public function provider_addOp()
  701. {
  702. $provider_model = Model('refill_provider');
  703. if (chksubmit()) {
  704. $params = $_POST;
  705. unset($params['form_submit']);
  706. $result = $provider_model->addProvider($params);
  707. if ($result) {
  708. $url = [
  709. [
  710. 'url' => 'index.php?act=merchant&op=provider',
  711. 'msg' => '返回通道列表',
  712. ],
  713. [
  714. 'url' => 'index.php?act=merchant&op=provider_add',
  715. 'msg' => '继续新增通道',
  716. ],
  717. ];
  718. $this->log('添加通道:' . '[ ' . $_POST['name'] . ']', 1);
  719. showMessage('通道添加成功', $url);
  720. } else {
  721. showMessage('通道添加失败');
  722. }
  723. }
  724. Tpl::showpage('provider.add');
  725. }
  726. public function provider_editOp()
  727. {
  728. $provider_model = Model('refill_provider');
  729. if (chksubmit()) {
  730. $provider_id = intval($_POST['provider_id']) ?? '';
  731. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  732. if (empty($provider)) {
  733. showMessage('通道信息有误');
  734. }
  735. $params = $_POST;
  736. unset($params['form_submit']);
  737. if (empty($params)) {
  738. showMessage('通道编辑成功', 'index.php?act=merchant&op=provider');
  739. }
  740. $result = $provider_model->editProvider($params, ['provider_id' => $provider_id]);
  741. if ($result) {
  742. $this->log('编辑通道:' . '[ ' . $provider['name'] . ']', 1);
  743. showMessage('通道编辑成功', 'index.php?act=merchant&op=provider');
  744. } else {
  745. showMessage('通道编辑失败', "index.php?act=merchant&op=provider_edit&id={$provider_id}");
  746. }
  747. }
  748. $provider_id = intval($_GET['id']) ?? '';
  749. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  750. if (empty($provider)) {
  751. showMessage('通道信息有误');
  752. }
  753. $type_text = ['油卡', '手机充值卡'];
  754. Tpl::output('type_text', $type_text);
  755. Tpl::output('provider', $provider);
  756. Tpl::showpage('provider.edit');
  757. }
  758. public function provider_delOp()
  759. {
  760. $provider_model = Model('refill_provider');
  761. $provider_id = intval($_GET['id']) ?? '';
  762. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  763. if (empty($provider)) {
  764. showMessage('通道信息有误');
  765. }
  766. $result = $provider_model->delProvider(['provider_id' => $provider_id]);
  767. if ($result) {
  768. $this->log('删除通道:' . '[ ' . $provider['name'] . ']', 1);
  769. showMessage('通道删除成功', 'index.php?act=merchant&op=provider');
  770. } else {
  771. showMessage('通道删除失败', "index.php?act=merchant&op=provider");
  772. }
  773. }
  774. private function scard_type(int $card_type)
  775. {
  776. if ($card_type == 1) { //中石油
  777. return '中石油';
  778. } elseif ($card_type == 2) { //中石化
  779. return '中石化';
  780. } elseif ($card_type == 4) { //中国移动
  781. return '中国移动';
  782. } elseif ($card_type == 5) { //中国联通
  783. return '中国联通';
  784. } elseif ($card_type == 6) { //中国电信
  785. return '中国电信';
  786. } else {
  787. return 'unknown';
  788. }
  789. }
  790. private function elapse_time($seconds)
  791. {
  792. $minutes = intval($seconds / 60);
  793. $second = intval($seconds % 60);
  794. if($minutes > 0){
  795. $result = "{$minutes}分钟{$second}秒";
  796. }else{
  797. $result = "{$second}秒";
  798. }
  799. return $result;
  800. }
  801. private function refill_stat($condition)
  802. {
  803. $model_refill_order = Model('refill_order');
  804. $i = $half_count = $hour_count = 0;
  805. while (true)
  806. {
  807. $start = $i * 100000;
  808. $orders = $model_refill_order->getMerchantOrderList($condition, '', 'refill_order.*,vr_order.order_state','order_time desc' ,"{$start},100000");
  809. $i++;
  810. if (empty($orders)) {
  811. break;
  812. }
  813. foreach ($orders as $order)
  814. {
  815. if($order['notify_time'] == 0 || $order['notify_time'] == null)
  816. {
  817. $order['diff_time'] = time() - $order['order_time'];
  818. if($order['order_state'] == ORDER_STATE_SEND) {
  819. if($order['diff_time'] > 1800 && $order['diff_time'] < 3600) {
  820. $half_count++;
  821. }
  822. if($order['diff_time'] > 3600) {
  823. $hour_count++;
  824. }
  825. }
  826. }
  827. }
  828. if (count($orders) < 100000) {
  829. break;
  830. }
  831. }
  832. return ['half_count' => $half_count , 'hour_count' => $hour_count];
  833. }
  834. }