merchant.php 33 KB

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