merchant.php 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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. require_once(BASE_CONFIG_PATH . CONFIG_PREFIX . '/refill.ini.php');
  9. class merchantControl extends SystemControl
  10. {
  11. const EXPORT_SIZE = 1000;
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. Language::read('merchant');
  16. }
  17. /**
  18. * 机构列表
  19. */
  20. public function merchantOp()
  21. {
  22. $model_merchant = Model('merchant');
  23. if (trim($_GET['merchant_name']) != '') {
  24. $condition['name'] = ['like', '%' . $_GET['merchant_name'] . '%'];
  25. Tpl::output('merchant_name', $_GET['merchant_name']);
  26. }
  27. $merchant_list = $model_merchant->getMerchantList($condition, 25, 'available_predeposit desc');
  28. $merchant_state_text = ['使用中', '已禁用'];
  29. Tpl::output('merchant_state_text', $merchant_state_text);
  30. Tpl::output('merchant_list', $merchant_list);
  31. Tpl::output('page', $model_merchant->showpage('2'));
  32. Tpl::showpage('merchant.index');
  33. }
  34. public function changeStateOp()
  35. {
  36. $mchid = intval($_GET['mchid']);
  37. $state = intval($_GET['state']);
  38. $model_merchant = Model('merchant');
  39. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  40. if (empty($merchant_info) || !in_array($state, [1, 2])) {
  41. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  42. }
  43. $resp = $model_merchant->editMerchant(['merchant_state' => $state], ['mchid' => $mchid]);
  44. if (!$resp) {
  45. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  46. }
  47. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  48. }
  49. /**
  50. * 新增机构
  51. */
  52. public function merchant_addOp()
  53. {
  54. if (chksubmit()) {
  55. /**
  56. * 验证
  57. */
  58. $obj_validate = new Validator();
  59. $obj_validate->validateparam = [
  60. ["input" => $_POST["name"], "require" => "true", "message" => '机构账号不能为空'],
  61. ["input" => $_POST["company_name"], "require" => "true", "message" => '机构公司名称不能为空'],
  62. ["input" => $_POST["password"], "require" => "true", "message" => '密码不能为空']
  63. ];
  64. $error = $obj_validate->validate();
  65. if ($error != '') {
  66. showMessage($error);
  67. } else {
  68. $name = trim($_POST['name']);
  69. $company_name = trim($_POST['company_name']);
  70. $pwd = trim($_POST['password']);
  71. $alarm_amount = $_POST['alarm_amount'] ?? 0;
  72. $model_merchant = Model('merchant');
  73. $model_member = Model('member');
  74. try {
  75. $trans = new trans_wapper($model_merchant, __METHOD__);
  76. $insert_member['member_name'] = md5($name . time());
  77. $insert_member['member_passwd'] = $pwd;
  78. $insert_id = $model_member->addMember($insert_member);
  79. if ($insert_id == false) {
  80. $trans->rollback();
  81. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  82. }
  83. $insert_array['name'] = $name;
  84. $insert_array['company_name'] = $company_name;
  85. $insert_array['org_pwd'] = $pwd;
  86. $insert_array['password'] = md5($pwd);
  87. $insert_array['admin_id'] = trim($insert_id);
  88. $insert_array['alarm_amount'] = $alarm_amount;
  89. $result = $model_merchant->addMerchant($insert_array);
  90. if ($result) {
  91. $url = [
  92. [
  93. 'url' => 'index.php?act=merchant&op=merchant',
  94. 'msg' => '返回机构列表',
  95. ],
  96. [
  97. 'url' => 'index.php?act=merchant&op=merchant_add',
  98. 'msg' => '继续新增机构',
  99. ],
  100. ];
  101. $this->log('添加机构:' . '[ ' . $_POST['name'] . ']', 1);
  102. showMessage('机构添加成功', $url);
  103. } else {
  104. showMessage('机构添加失败');
  105. }
  106. $trans->commit();
  107. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  108. } catch (Exception $e) {
  109. $trans->rollback();
  110. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  111. }
  112. }
  113. }
  114. Tpl::showpage('merchant.add');
  115. }
  116. public function merchant_editOp()
  117. {
  118. $mchid = $_GET['mchid'] ?? $_POST['mchid'];
  119. $model_merchant = Model('merchant');
  120. $merchant = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  121. if(empty($merchant)){
  122. showMessage('机构信息有误');
  123. }
  124. if (chksubmit()) {
  125. $update['company_name'] = trim($_POST['company_name']);
  126. $update['org_pwd'] = trim($_POST['password']);
  127. $update['password'] = md5($update['org_pwd']);
  128. $update['alarm_amount'] = $_POST['alarm_amount'] ?? 0;
  129. $update['credit_bonus'] = ncPriceFormat($_POST['credit_bonus']);
  130. $update['time_out'] = intval($_POST['time_out'] * 60);
  131. $update['quality'] = intval($_POST['quality']);
  132. $member_id = $merchant['admin_id'];
  133. try {
  134. $model_merchant = Model('merchant');
  135. $trans = new trans_wapper($model_merchant, __METHOD__);
  136. if($merchant['credit_bonus'] == $update['credit_bonus']) {
  137. unset($update['credit_bonus']);
  138. } elseif ($merchant['credit_bonus'] > $update['credit_bonus']) {
  139. $credit_bonus = ncPriceFormat($merchant['credit_bonus'] - $update['credit_bonus']);
  140. $this->credit_save_money($credit_bonus , 2 , $member_id);
  141. } elseif ($merchant['credit_bonus'] < $update['credit_bonus']) {
  142. $credit_bonus = ncPriceFormat($update['credit_bonus'] - $merchant['credit_bonus']);
  143. $this->credit_save_money($credit_bonus , 1 , $member_id);
  144. }
  145. $result = $model_merchant->editMerchant($update,['mchid' => $mchid]);
  146. if(!$result) {
  147. $trans->rollback();
  148. showMessage('机构编辑失败', 'index.php?act=merchant&op=merchant');
  149. }
  150. $this->log('编辑机构:' . '[ ' . $merchant['name'] . ']', 1);
  151. $trans->commit();
  152. showMessage('机构编辑成功', 'index.php?act=merchant&op=merchant');
  153. } catch (Exception $e) {
  154. $trans->rollback();
  155. showMessage('机构编辑失败', 'index.php?act=merchant&op=merchant');
  156. }
  157. }
  158. Tpl::output('merchant', $merchant);
  159. Tpl::showpage('merchant.edit');
  160. }
  161. private function credit_save_money($money , $operatetype , $member_id , $bz = '')
  162. {
  163. $obj_member = Model('member');
  164. $member_id = intval($member_id);
  165. $member_info = $obj_member->getMemberInfo(['member_id' => $member_id]);
  166. $admininfo = $this->getAdminInfo();
  167. switch ($operatetype) {
  168. case 1:
  169. $admin_act = "sys_add_money";
  170. $log_msg = "管理员【" . $admininfo['admin_name'] . "】操作会员【" . $member_info['member_name'] . "】,预存款【增加】,金额为" . $money ;
  171. break;
  172. case 2:
  173. $admin_act = "sys_del_money";
  174. $log_msg = "管理员【" . $admininfo['admin_name'] . "】操作会员【" . $member_info['member_name'] . "】,预存款【减少】,金额为" . $money;
  175. break;
  176. default:
  177. return [false , '调节类型错误'];
  178. break;
  179. }
  180. $model_pd = Model('predeposit');
  181. //调节预存款
  182. $data = [];
  183. $data['member_id'] = $member_info['member_id'];
  184. $data['member_name'] = $member_info['member_name'];
  185. $data['amount'] = $money;
  186. $data['order_sn'] = '';
  187. $data['admin_name'] = $admininfo['name'];
  188. $data['pdr_sn'] = '';
  189. $data['lg_desc'] = $bz ? $bz : '管理员操作客户授信,更改会员余额。';
  190. $model_pd->changePd($admin_act, $data);
  191. $this->log($log_msg, 1);
  192. return [true , 'success'];
  193. }
  194. public function orderStorgeOp()
  195. {
  196. if (chksubmit())
  197. {
  198. $mchids = $_POST['mchid'];
  199. $petrochinas = $_POST['petrochina'];
  200. $sinopecs = $_POST['sinopec'];
  201. foreach ($mchids as $pos => $mchid) {
  202. $mchid = intval($mchid);
  203. $data['mchid'] = $mchid;
  204. $data['petrochina'] = ['100' => intval($petrochinas[100][$pos]) , '200' => intval($petrochinas[200][$pos]) , '500' => intval($petrochinas[500][$pos]) , '1000' => intval($petrochinas[1000][$pos])];
  205. $data['sinopec'] = ['100' => intval($sinopecs[100][$pos]) , '200' => intval($sinopecs[200][$pos]) , '500' => intval($sinopecs[500][$pos]) , '1000' => intval($sinopecs[1000][$pos])];
  206. $data['time'] = time();
  207. $result[$mchid] = $data;
  208. }
  209. $this->cache_storge_limit($result);
  210. showMessage('编辑成功', 'index.php?act=merchant&op=merchant');
  211. }
  212. else
  213. {
  214. $order_limit = rcache('storge_limit' , 'merchant-');
  215. if(empty($order_limit)){
  216. $order_limit = [];
  217. }else{
  218. $order_limit = unserialize($order_limit['data']);
  219. }
  220. $initialiser = function ($merchant) {
  221. $amount = [
  222. '100' => -1,
  223. '200' => -1,
  224. '500' => -1,
  225. '1000' => -1
  226. ];
  227. $data['mchid'] = $merchant['mchid'];
  228. $data['petrochina'] = $data['sinopec'] = $amount;
  229. $data['time'] = time();
  230. return $data;
  231. };
  232. $reader = function ($mchid,$time)
  233. {
  234. $cond['mchid'] = $mchid;
  235. $cond['inner_status'] = 0;
  236. $cond['order_state'] = ORDER_STATE_SUCCESS;
  237. $cond['refill_order.order_time'] = ['egt', $time];
  238. $items = Model('')->table('refill_order,vr_order')->join('inner')
  239. ->on('refill_order.order_id=vr_order.order_id')
  240. ->field('refill_order.mchid,refill_order.card_type,refill_order.refill_amount,count(*) as num')
  241. ->group('refill_order.card_type,refill_order.refill_amount')
  242. ->where($cond)->select();
  243. return $items;
  244. };
  245. $type_map = [1 => 'petrochina' , 2 => 'sinopec'];
  246. $cur_limits = [];
  247. $model_merchant = model('merchant');
  248. $merchant_list = $model_merchant->getMerchantList([], 1000, 'mchid asc' , 'mchid,company_name');
  249. foreach ($merchant_list as $merchant)
  250. {
  251. $mchid = intval($merchant['mchid']);
  252. if(array_key_exists($mchid , $order_limit)) {
  253. $result[$mchid] = $order_limit[$mchid];
  254. }else{
  255. $result[$mchid] = $initialiser($merchant);
  256. }
  257. $start = $result[$mchid]['time'];
  258. $items = $reader($mchid,$start);
  259. $cur_limits[$mchid] = $result[$mchid];
  260. foreach ($items as $item) {
  261. $refill_amount = intval($item['refill_amount'] + 0.005);
  262. $card_type = $type_map[$item['card_type']];
  263. $cache_num = $result[$mchid][$card_type][$refill_amount];
  264. if ($cache_num == -1) {
  265. $new_num = -1;
  266. }else{
  267. $new_num = ($cache_num - $item['num']) >= 0 ? $cache_num - $item['num'] : 0;
  268. }
  269. $cur_limits[$mchid][$card_type][$refill_amount] = $new_num;
  270. }
  271. $cur_limits[$mchid]['company_name'] = $merchant['company_name'];
  272. }
  273. Tpl::output('data', $cur_limits);
  274. Tpl::showpage('merchant.orderlimit');
  275. }
  276. }
  277. private function cache_storge_limit($data)
  278. {
  279. Model('')->table('setting')->where(['name'=>'storge_limit'])->delete();
  280. Model('')->table('setting')->insert(['name'=>'storge_limit','value'=>serialize($data)]);
  281. wcache('storge_limit' , ['data' => serialize($data)] , 'merchant-');
  282. }
  283. public function priceOp()
  284. {
  285. $quality = $_GET['quality'] ? $_GET['quality'] : 1;
  286. if (chksubmit())
  287. {
  288. $mchid = $_POST['mchid'];
  289. //合并表单数据
  290. $card_types = $_POST['cardtype'];
  291. $specs = $_POST['spec'];
  292. $prices = $_POST['price'];
  293. foreach ($card_types as $key => $card_type) {
  294. $data['card_type'] = $card_type;
  295. $data['spec'] = intval($specs[$key]);
  296. $data['price'] = ncPriceFormat($prices[$key]);
  297. $params[] = $data;
  298. }
  299. foreach ($params as $param)
  300. {
  301. if($param['price'] > 0) {
  302. $insert['mchid'] = $mchid;
  303. $insert['spec'] = $param['spec'];
  304. $insert['price'] = $param['price'];
  305. $insert['card_types'] = $param['card_type'];
  306. $insert['quality'] = $quality;
  307. $inserts[] = $insert;
  308. }
  309. }
  310. if(empty($inserts)) {
  311. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  312. }
  313. try {
  314. $model_merchant = Model('merchant');
  315. $trans = new trans_wapper($model_merchant, __METHOD__);
  316. //删除旧费率
  317. $model_merchant->delPrices($mchid,$quality);
  318. //更新新费率
  319. $model_merchant->insertPrices($inserts);
  320. $trans->commit();
  321. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  322. } catch (Exception $e) {
  323. $trans->rollback();
  324. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  325. }
  326. }
  327. $mchid = $_GET['mchid'] ?? 0;
  328. $goods = $this->GoodsFormat($mchid,$quality);
  329. Tpl::output('goods', $goods);
  330. Tpl::showpage('merchant.price');
  331. }
  332. private function GoodsFormat($mchid,$quality)
  333. {
  334. $all_spector = function ()
  335. {
  336. global $config;
  337. $refill_specs = $config['refill_specs'];
  338. $all_type_specs = [];
  339. foreach ($refill_specs as $scard_type => $specs)
  340. {
  341. if ($scard_type == 'petrochina') { //中石油
  342. $card_type = 1;
  343. } elseif ($scard_type == 'sinopec') { //中石化
  344. $card_type = 2;
  345. } elseif ($scard_type == 'chinamobile') { //中国移动
  346. $card_type = 4;
  347. } elseif ($scard_type == 'chinaunicom') { //中国联通
  348. $card_type = 5;
  349. } elseif ($scard_type == 'chinatelecom') { //中国电信
  350. $card_type = 6;
  351. } else {
  352. continue;
  353. }
  354. $all_type_specs[$card_type] = $specs;
  355. }
  356. $result =[];
  357. foreach ($all_type_specs as $card_type => $specs) {
  358. foreach ($specs as $spec){
  359. $result["{$card_type}-{$spec}"] = ['card_type' => $card_type , 'spec' => $spec];
  360. }
  361. }
  362. return $result;
  363. };
  364. $merch_spector = function ($mchid , $quality)
  365. {
  366. $model_merchant = Model('merchant');
  367. $items = $model_merchant->table('merchant_price')->where(['mchid' => $mchid , 'quality' => $quality])->select();
  368. $result = [];
  369. foreach ($items as $item)
  370. {
  371. $card_types = explode(',' , $item['card_types']);
  372. foreach ($card_types as $card_type) {
  373. $value['spec'] = intval($item['spec']);
  374. $result["{$card_type}-{$value['spec']}"] = ['card_type'=>$card_type,
  375. 'spec' => intval($item['spec']),
  376. 'price' => $item['price']];
  377. }
  378. }
  379. return $result;
  380. };
  381. $all_cardtype_specs = $all_spector();
  382. $merch_cardtype_specs = $merch_spector($mchid,$quality);
  383. $merger = function ($all_specs,$mech_specs)
  384. {
  385. $result = [];
  386. foreach ($all_specs as $cardtype_spec => $value)
  387. {
  388. $card_name = $this->scard_type($value['card_type']);
  389. $data = [];
  390. $data['goods_name'] = $card_name;
  391. $data['card_type'] = $value['card_type'];
  392. $data['spec'] = $value['spec'];
  393. if(array_key_exists($cardtype_spec , $mech_specs)) {
  394. $data['price'] = $mech_specs[$cardtype_spec]['price'];
  395. } else {
  396. $data['price'] = 0;
  397. }
  398. $result[$value['card_type']][] = $data;
  399. }
  400. return $result;
  401. };
  402. $result = $merger($all_cardtype_specs,$merch_cardtype_specs);
  403. return $result;
  404. }
  405. public function check_merchantOp()
  406. {
  407. $mchid = trim($_GET['mchid']);
  408. if (!$mchid) {
  409. echo '';
  410. die;
  411. }
  412. $model_merchant = Model('merchant');
  413. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid], '*');
  414. if (empty($merchant_info)) {
  415. echo '';
  416. die;
  417. }
  418. echo 'true';
  419. }
  420. /**
  421. * ajax操作
  422. */
  423. public function ajaxOp()
  424. {
  425. $model_merchant = Model('merchant');
  426. switch ($_GET['branch']) {
  427. /**
  428. * 验证机构名称是否重复
  429. */
  430. case 'check_mch_name':
  431. $condition['name'] = $_GET['name'];
  432. $list = $model_merchant->getMerchantInfo($condition);
  433. if (empty($list)) {
  434. echo 'true';
  435. } else {
  436. echo 'false';
  437. }
  438. exit;
  439. /**
  440. * 验证机构是否存在
  441. */
  442. case 'check_merchant':
  443. $condition['mchid'] = intval($_GET['mchid']);
  444. $list = $model_merchant->getMerchantInfo($condition);
  445. if (!empty($list)) {
  446. echo 'true';
  447. } else {
  448. echo 'false';
  449. }
  450. exit;
  451. }
  452. }
  453. /**
  454. * 充值申请列表
  455. */
  456. public function refill_evidenceOp()
  457. {
  458. $model_merchant = Model('merchant');
  459. $condition = [];
  460. if (trim($_GET['mch_name']) != '') {
  461. $condition['mch_name'] = ['like', '%' . $_GET['mch_name'] . '%'];
  462. Tpl::output('mch_name', $_GET['mch_name']);
  463. }
  464. $state_sel = intval($_REQUEST['state_sel']);
  465. if ($state_sel == 1) {
  466. $condition['check_time'] = 0;
  467. $condition['status'] = 1;
  468. } elseif ($state_sel == 2) {
  469. $condition['check_time'] = ['gt', 0];
  470. $condition['status'] = 2;
  471. } elseif ($state_sel == 3) {
  472. $condition['check_time'] = ['gt', 0];
  473. $condition['status'] = 3;
  474. } else {
  475. }
  476. //充值申请列表
  477. $evidence_list = $model_merchant->getRefillEvidence($condition, 20, '*,member.available_predeposit', 'refill_evidence.add_time desc');
  478. $status_text = ['申请中', '已通过', '已驳回'];
  479. $operation_text = ['未预存', '已预存'];
  480. Tpl::output('evidence_list', $evidence_list);
  481. Tpl::output('status_text', $status_text);
  482. Tpl::output('operation_text', $operation_text);
  483. Tpl::output('page', $model_merchant->showpage('2'));
  484. Tpl::showpage('merchant.refill.evidence_list');
  485. }
  486. public function check_evidenceOp()
  487. {
  488. $status = $_GET['status'];
  489. $apply_id = intval($_GET['apply_id']);
  490. if ($apply_id > 0) {
  491. $model_merchant = Model('merchant');
  492. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  493. if (empty($evidence_info)) {
  494. showMessage('充值申请不存在');
  495. }
  496. if ($status == 'pass') {
  497. $model_merchant->editRefillEvidence(['apply_id' => $apply_id], ['status' => 2, 'check_time' => time()]);
  498. } elseif ($status == 'unpass') {
  499. $model_merchant->editRefillEvidence(['apply_id' => $apply_id], ['status' => 3, 'check_time' => time()]);
  500. } else {
  501. }
  502. }
  503. showMessage(L('nc_common_save_succ'), urlAdmin('merchant', 'refill_evidence'));
  504. }
  505. public function rechargeOp()
  506. {
  507. if (chksubmit()) {
  508. $mchid = $this->add_money();
  509. $model_merchant = Model('merchant');
  510. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  511. if ($merchant_info['alarm_amount'] < $merchant_info['available_predeposit'])
  512. {
  513. //更新预警短信通知限制
  514. $mch_cache = rcache("merchant-notify" , 'refill-');
  515. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  516. $caches[$mchid] = ['last_time' => 0, 'send_count' => 0];
  517. wcache("merchant-notify", ['data' => serialize($caches)], 'refill-');
  518. }
  519. showMessage('操作成功', 'index.php?act=merchant&op=refill_evidence');
  520. } else {
  521. $apply_id = $_GET['apply_id'];
  522. if (!empty($apply_id)) {
  523. $model_merchant = Model('merchant');
  524. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*,member.available_predeposit');
  525. Tpl::output('apply_id', $apply_id);
  526. Tpl::output('amount', $evidence_info['amount']);
  527. Tpl::output('available_predeposit', $evidence_info['available_predeposit']);
  528. }
  529. Tpl::showpage('recharge.add');
  530. }
  531. }
  532. public function recharge_manualOp()
  533. {
  534. if (chksubmit()) {
  535. $obj_validate = new Validator();
  536. $obj_validate->validateparam = [
  537. ["input" => $_POST["mch_id"], "require" => "true", "message" => '机构号不能为空'],
  538. ["input" => $_POST["bank_username"], "require" => "true", "message" => '开户人姓名不能为空'],
  539. ["input" => $_POST["bank_name"], "require" => "true", "message" => '开户银行不能为空'],
  540. ["input" => $_POST["pointsnum"], "require" => "true", "message" => '预存金额不能为空']
  541. ];
  542. $error = $obj_validate->validate();
  543. if ($error != '') {
  544. showMessage($error);
  545. }
  546. $mchid = $_POST["mch_id"];
  547. $model_merchant = Model('merchant');
  548. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  549. if(!$merchant_info) {
  550. showMessage('对应机构不存在');
  551. }
  552. $pointsnum = $_POST['pointsnum'];
  553. $money = abs($pointsnum);
  554. if($money == 0) {
  555. showMessage('预存金额错误');
  556. }
  557. try {
  558. $model_merchant = Model('merchant');
  559. $trans = new trans_wapper($model_merchant, __METHOD__);
  560. $member_id = $merchant_info['admin_id'];
  561. if($pointsnum > 0) {
  562. $bz = '管理员操作手动预存金额增加';
  563. $this->credit_save_money($money , 1 , $member_id , $bz);
  564. }elseif ($pointsnum < 0) {
  565. $bz = '管理员操作手动预存金额减少';
  566. $this->credit_save_money($money , 2 , $member_id , $bz);
  567. }else{
  568. showMessage('预存金额错误');
  569. }
  570. $result = $this->ct_refill_evidence($_POST,$merchant_info);
  571. if(!$result) {
  572. $trans->rollback();
  573. showMessage('操作失败', 'index.php?act=merchant&op=merchant');
  574. }
  575. $trans->commit();
  576. if ($merchant_info['alarm_amount'] < $merchant_info['available_predeposit'])
  577. {
  578. //更新预警短信通知限制
  579. $mch_cache = rcache("merchant-notify" , 'refill-');
  580. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  581. $caches[$mchid] = ['last_time' => 0, 'send_count' => 0];
  582. wcache("merchant-notify", ['data' => serialize($caches)], 'refill-');
  583. }
  584. showMessage('操作成功', 'index.php?act=merchant&op=refill_evidence');
  585. } catch (Exception $e) {
  586. $trans->rollback();
  587. showMessage('操作失败', 'index.php?act=merchant&op=refill_evidence');
  588. }
  589. } else {
  590. Tpl::showpage('recharge.manual.add');
  591. }
  592. }
  593. private function ct_refill_evidence($params,$merchant_info)
  594. {
  595. $admininfo = $this->getAdminInfo();
  596. $mem_info = Model('member')->getMemberInfo(['member_id' => $merchant_info['admin_id']]);
  597. $input['mchid'] = $merchant_info['mchid'];
  598. $input['mch_name'] = $merchant_info['name'];
  599. $input['member_id'] = $merchant_info['admin_id'];
  600. $input['amount'] = $params['pointsnum'];
  601. $input['bank_username'] = $params['bank_username'];
  602. $input['bank_name'] = $params['bank_name'];
  603. $input['bz'] = $params['pointsdesc'];
  604. $input['voucher_name'] = '/';
  605. $input['status'] = 2;
  606. $input['is_operation'] = 2;
  607. $input['add_time'] = $input['check_time'] = time();
  608. $input['after_available'] = ncPriceFormat($mem_info['available_predeposit'] + $params['pointsnum']);
  609. $input['admin_name'] = $admininfo['name'];
  610. $input['admin_id'] = $admininfo['id'];
  611. $model_merchant = Model('merchant');
  612. return $model_merchant->addRefillEvidence($input);
  613. }
  614. public function add_money()
  615. {
  616. $obj_validate = new Validator();
  617. $obj_validate->validateparam = [
  618. ["input" => $_POST["apply_id"], "require" => "true", "message" => Language::get('admin_points_member_error_again')],
  619. ["input" => $_POST["pointsnum"], "require" => "true", 'validator' => 'Compare', 'operator' => ' >= ', 'to' => 1, "message" => Language::get('admin_points_points_min_error')]
  620. ];
  621. $error = $obj_validate->validate();
  622. if ($error != '') {
  623. showMessage($error, '', '', 'error');
  624. }
  625. $money = abs(floatval($_POST['pointsnum']));
  626. $memo = trim($_POST['pointsdesc']);
  627. if ($money <= 0) {
  628. showMessage('输入的金额必需大于0', '', 'html', 'error');
  629. }
  630. $apply_id = intval($_POST['apply_id']);
  631. $model_merchant = Model('merchant');
  632. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  633. if (!is_array($evidence_info) || count($evidence_info) <= 0) {
  634. showMessage("无效的充值申请信息", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  635. }
  636. if ($evidence_info['status'] != 2) {
  637. showMessage("该充值申请未被审核通过", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  638. }
  639. if ($evidence_info['is_operation'] == 2) {
  640. showMessage("该充值申请未已被预存过", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  641. }
  642. //查询会员信息
  643. $obj_member = Model('member');
  644. $member_id = intval($evidence_info['member_id']);
  645. $member_info = $obj_member->getMemberInfo(['member_id' => $member_id]);
  646. $available_predeposit = floatval($member_info['available_predeposit']);
  647. $freeze_predeposit = floatval($member_info['freeze_predeposit']);
  648. if ($_POST['operatetype'] == 2 && $money > $available_predeposit) {
  649. showMessage(('预存款不足,会员当前预存款') . $available_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  650. }
  651. if ($_POST['operatetype'] == 3 && $money > $available_predeposit) {
  652. showMessage(('可冻结预存款不足,会员当前预存款') . $available_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  653. }
  654. if ($_POST['operatetype'] == 4 && $money > $freeze_predeposit) {
  655. showMessage(('可恢复冻结预存款不足,会员当前冻结预存款') . $freeze_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  656. }
  657. $model_pd = Model('predeposit');
  658. $order_sn = $apply_id;
  659. $admininfo = $this->getAdminInfo();
  660. $log_msg = "管理员【" . $admininfo['name'] . "】操作会员【" . $member_info['member_name'] . "】预存款,金额为" . $money . ",编号为" . $order_sn;
  661. $admin_act = 'sys_add_money';
  662. switch ($_POST['operatetype']) {
  663. case 1:
  664. $admin_act = "sys_add_money";
  665. $log_msg = "管理员【" . $admininfo['name'] . "】操作会员【" . $member_info['member_name'] . "】预存款【增加】,金额为" . $money . ",编号为" . $order_sn;
  666. break;
  667. case 2:
  668. $admin_act = "sys_del_money";
  669. $log_msg = "管理员【" . $admininfo['name'] . "】操作会员【" . $member_info['member_name'] . "】预存款【减少】,金额为" . $money . ",编号为" . $order_sn;
  670. break;
  671. default:
  672. showMessage('操作失败', 'index.php?act=merchant&op=refill_evidence');
  673. break;
  674. }
  675. try {
  676. $trans = new trans_wapper($model_pd, __METHOD__);
  677. //扣除冻结的预存款
  678. $data = [];
  679. $data['member_id'] = $member_info['member_id'];
  680. $data['member_name'] = $member_info['member_name'];
  681. $data['amount'] = $money;
  682. $data['order_sn'] = $order_sn;
  683. $data['admin_name'] = $admininfo['name'];
  684. $data['pdr_sn'] = $order_sn;
  685. $data['lg_desc'] = $memo;
  686. $model_pd->changePd($admin_act, $data);
  687. $after_available = ncPriceFormat($available_predeposit + $evidence_info['amount']);
  688. $model_merchant->editRefillEvidence(
  689. ['apply_id' => $apply_id],
  690. ['is_operation' => 2 , 'after_available' => $after_available , 'admin_id' => $admininfo['id'] ,'admin_name' => $admininfo['name']]
  691. );
  692. $trans->commit();
  693. $this->log($log_msg, 1);
  694. return $evidence_info['mchid'];
  695. } catch (Exception $e) {
  696. $trans->rollback();
  697. $this->log($log_msg, 0);
  698. showMessage($e->getMessage(), 'index.php?act=merchant&op=refill_evidence', 'html', 'error');
  699. exit;
  700. }
  701. }
  702. public function checkevidenceOp()
  703. {
  704. $apply_id = trim($_GET['apply_id']);
  705. if (!$apply_id) {
  706. echo '';
  707. die;
  708. }
  709. $model_merchant = Model('merchant');
  710. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  711. if (empty($evidence_info)) {
  712. echo '';
  713. die;
  714. }
  715. echo 'true';
  716. }
  717. public function OrderQueryOp()
  718. {
  719. $model_vr_order = Model('vr_order');
  720. $condition['order_state'] = ORDER_STATE_SEND;
  721. $orders = $model_vr_order->getOrderList($condition);
  722. if(!empty($orders)) {
  723. foreach ($orders as $order) {
  724. $order_id = $order['order_id'];
  725. QueueClient::push("QueryRefillState",['order_id' => $order_id]);
  726. }
  727. }
  728. showMessage('操作成功', 'index.php?act=merchant&op=refill_order');
  729. }
  730. public function mch_notifyOp()
  731. {
  732. $model_refill_order = Model('refill_order');
  733. $condition['mch_notify_state'] = 0;
  734. $condition['inner_status'] = 0;
  735. $orders = $model_refill_order->getMerchantOrderList($condition, 1000, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
  736. if(!empty($orders)) {
  737. foreach ($orders as $order) {
  738. $order_id = $order['order_id'];
  739. if($order['order_state'] == ORDER_STATE_SEND) {
  740. QueueClient::push("QueryRefillState",['order_id' => $order_id]);
  741. }else{
  742. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  743. }
  744. }
  745. }
  746. showMessage('操作成功', 'index.php?act=merchant&op=refill_order');
  747. }
  748. public function refill_orderOp()
  749. {
  750. $model_refill_order = Model('refill_order');
  751. $condition['inner_status'] = 0;
  752. $fSingle = false;
  753. if (!empty($_GET['order_sn'])) {
  754. $condition['refill_order.order_sn'] = $_GET['order_sn'];
  755. $fSingle = true;
  756. }
  757. if (!empty($_GET['mch_order'])) {
  758. $condition['refill_order.mch_order'] = $_GET['mch_order'];
  759. $fSingle = true;
  760. }
  761. if (!empty($_GET['ch_trade_no'])) {
  762. $condition['refill_order.ch_trade_no'] = $_GET['ch_trade_no'];
  763. $fSingle = true;
  764. }
  765. if (!empty($_GET['mchid'])) {
  766. $condition['refill_order.mchid'] = $_GET['mchid'];
  767. }
  768. if (!empty($_GET['channel_name'])) {
  769. $condition['refill_order.channel_name'] = $_GET['channel_name'];
  770. }
  771. if (!empty($_GET['store_id'])) {
  772. $condition['vr_order.store_id'] = $_GET['store_id'];
  773. }
  774. if (!empty($_GET['card_no'])) {
  775. $condition['refill_order.card_no'] = $_GET['card_no'];
  776. }
  777. if (!empty($_GET['card_type']))
  778. {
  779. if(in_array($_GET['card_type'] , ['1' , '2' , '4' , '5' , '6'])) {
  780. $condition['refill_order.card_type'] = $_GET['card_type'];
  781. }
  782. if($_GET['card_type'] == 'oil') {
  783. $condition['refill_order.card_type'] = ['in' , ['1' , '2']];
  784. }
  785. if($_GET['card_type'] == 'phone') {
  786. $condition['refill_order.card_type'] = ['in' , ['4' , '5' , '6']];
  787. }
  788. }
  789. $fToday = false;
  790. if(!$fSingle)
  791. {
  792. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  793. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  794. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  795. $condition['refill_order.order_time'] = [ ['egt', $start_unixtime] , ['lt', $end_unixtime] , 'and'];
  796. }
  797. elseif ($start_unixtime > 0) {
  798. $condition['refill_order.order_time'] = ['egt', $start_unixtime];
  799. }
  800. elseif ($end_unixtime > 0) {
  801. $condition['refill_order.order_time'] = ['lt', $end_unixtime];
  802. }
  803. else {
  804. $start = strtotime(date('Y-m-d',time()));
  805. $condition['refill_order.order_time'] = ['egt', $start];
  806. $fToday = true;
  807. }
  808. if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40']))
  809. {
  810. $condition['vr_order.order_state'] = $_GET['order_state'];
  811. if($_GET['order_state'] == ORDER_STATE_SEND)
  812. {
  813. if($_GET['time'] == 1){
  814. $condition['refill_order.order_time'] = ['between', [(time() - 3600) , (time() - 1800)]];
  815. }
  816. if($_GET['time'] == 2){
  817. $condition['refill_order.order_time'] = ['lt', (time() - 3600)];
  818. }
  819. }
  820. }
  821. }
  822. $merchants = [];
  823. $merchant_list = Model('')->table('merchant')->limit(100)->select();
  824. foreach ($merchant_list as $key => $value) {
  825. $merchants[$value['mchid']] = $value;
  826. }
  827. $order_list = $model_refill_order->getMerchantOrderList($condition, 50, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
  828. $stat = $this->all_order_state_stat($condition);
  829. foreach ($order_list as $order_id => $order_info)
  830. {
  831. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  832. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  833. if ($order_info['notify_time'] > 0) {
  834. $diff_time = $order_info['notify_time'] - $order_info['order_time'];
  835. } else {
  836. $diff_time = time() - $order_info['order_time'];
  837. }
  838. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
  839. $order_list[$order_id]['diff_time'] = $diff_time;
  840. }
  841. $provider_list = Model('')->table('refill_provider,store')
  842. ->field('refill_provider.*,store.store_name')
  843. ->join('inner')
  844. ->on('store.store_id=refill_provider.store_id')
  845. ->where(['refill_provider.opened' => 1])
  846. ->limit(100)
  847. ->select();
  848. Tpl::output('stat', $stat);
  849. Tpl::output('ftoday', $fToday);
  850. Tpl::output('order_list', $order_list);
  851. Tpl::output('provider_list', $provider_list);
  852. Tpl::output('merchant_list', $merchant_list);
  853. Tpl::output('show_page', $model_refill_order->showpage());
  854. Tpl::showpage('refill.order.index');
  855. }
  856. public function notify_merchantOp()
  857. {
  858. $order_id = $_GET['order_id'];
  859. $mod_order = Model('vr_order');
  860. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  861. if($order_info['order_state'] == ORDER_STATE_SEND) {
  862. QueueClient::push("QueryRefillState",['order_id' => $order_id]);
  863. }else{
  864. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  865. }
  866. showMessage('操作成功', 'index.php?act=merchant&op=refill_order');
  867. }
  868. public function notify_manual_merchantOp()
  869. {
  870. $order_id = $_GET['order_id'];
  871. $type = $_GET['type'];
  872. $mod_order = Model('vr_order');
  873. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  874. if((time() - $order_info['order_time']) < 3600)
  875. {
  876. showMessage('订单时间未超过1小时', 'index.php?act=merchant&op=refill_order');
  877. }
  878. $logic_vr_order = Logic("vr_order");
  879. if($type == 'success') {
  880. $logic_vr_order->changeOrderStateSuccess($order_id);
  881. }elseif ($type == 'cancel') {
  882. $logic_vr_order->changeOrderStateCancel($order_info, '', "后台手动回调通知失败.");
  883. }else {
  884. showMessage('手动操作类型错误', 'index.php?act=merchant&op=refill_order');
  885. }
  886. $mod_refill = Model('refill_order');
  887. $mod_refill->edit($order_id, ['notify_time' => time(), 'notify_state' => 1]);
  888. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  889. showMessage('操作成功', 'index.php?act=merchant&op=refill_order');
  890. }
  891. public function providerStatsOp()
  892. {
  893. $provider_model = Model('refill_provider');
  894. $condition = [];
  895. $provider_list = $provider_model->getProviderList($condition, 100);
  896. $opened_text = ['使用中', '已禁用'];
  897. $type_text = ['油卡', '手机充值卡'];
  898. Tpl::output('opened_text', $opened_text);
  899. Tpl::output('type_text', $type_text);
  900. Tpl::output('provider_list', $provider_list);
  901. Tpl::output('show_page', $provider_model->showpage());
  902. Tpl::showpage('provider.stats');
  903. }
  904. public function providerOp()
  905. {
  906. $this->sync_cfgs();
  907. $provider_model = Model('refill_provider');
  908. $condition = [];
  909. if (trim($_GET['name']) != '') {
  910. $condition['name'] = ['like', '%' . $_GET['name'] . '%'];
  911. Tpl::output('name', $_GET['name']);
  912. }
  913. if (in_array($_GET['type'], [1, 2])) {
  914. $condition['type'] = $_GET['type'];
  915. }
  916. $provider_list = $provider_model->getProviderList($condition, 100);
  917. foreach ($provider_list as $key => $provider) {
  918. if(!empty($provider['start_period']) && !empty($provider['end_period'])){
  919. $provider_list[$key]['period'] = $provider['start_period'] . '~' . $provider['end_period'];
  920. }else{
  921. $provider_list[$key]['period'] = '全时间段';
  922. }
  923. }
  924. $opened_text = ['使用中', '已禁用'];
  925. $type_text = ['油卡', '手机充值卡'];
  926. Tpl::output('opened_text', $opened_text);
  927. Tpl::output('type_text', $type_text);
  928. Tpl::output('provider_list', $provider_list);
  929. Tpl::output('show_page', $provider_model->showpage());
  930. Tpl::showpage('provider.index');
  931. }
  932. public function sync_cfgs()
  933. {
  934. $name_val = function ($items) {
  935. $result = [];
  936. foreach ($items as $item) {
  937. $name = $item['name'];
  938. $result[$name] = $item;
  939. }
  940. return $result;
  941. };
  942. $match = function ($all,$cur)
  943. {
  944. $inserts = [];
  945. foreach ($all as $key => $value)
  946. {
  947. if(!array_key_exists($key,$cur)) {
  948. $insert['name'] = $key;
  949. $insert['store_id'] = $value['cfg']['store_id'];
  950. $inserts[] = $insert;
  951. }
  952. }
  953. return $inserts;
  954. };
  955. $inserter = function ($mod,$type,$names)
  956. {
  957. foreach ($names as $name) {
  958. $data = ['name' => $name['name'],'type' => $type,'store_id' => $name['store_id']];
  959. $mod->insert($data);
  960. }
  961. };
  962. global $config;
  963. $oil_configs = $config['oil_providers'];
  964. $pho_configs = $config['phone_providers'];
  965. $oils = $name_val($oil_configs);
  966. $phones = $name_val($pho_configs);
  967. $mod_prov = Model('refill_provider');
  968. $oil_items = $mod_prov->getProviderList(['type' => 1]);
  969. $oil_items = $name_val($oil_items);
  970. $oil_inserts = $match($oils,$oil_items);
  971. $phone_items = $mod_prov->getProviderList(['type' => 2]);
  972. $phone_items = $name_val($phone_items);
  973. $phone_inserts = $match($phones,$phone_items);
  974. $inserter($mod_prov,1,$oil_inserts);
  975. $inserter($mod_prov,2,$phone_inserts);
  976. }
  977. public function changeProviderStateOp()
  978. {
  979. $provider_id = intval($_GET['id']);
  980. $state = intval($_GET['state']);
  981. $provider_model = Model('refill_provider');
  982. $provider_info = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  983. if (empty($provider_info) || !in_array($state, [1, 2])) {
  984. showMessage('操作成功', 'index.php?act=merchant&op=provider');
  985. }
  986. $resp = $provider_model->editProvider(['opened' => $state], ['provider_id' => $provider_id]);
  987. if (!$resp) {
  988. showMessage('操作失败', 'index.php?act=merchant&op=provider', 'html', 'error');
  989. }
  990. showMessage('操作成功', 'index.php?act=merchant&op=provider');
  991. }
  992. /**
  993. * 新增通道
  994. */
  995. public function provider_addOp()
  996. {
  997. $provider_model = Model('refill_provider');
  998. if (chksubmit()) {
  999. $params = $_POST;
  1000. unset($params['form_submit']);
  1001. $result = $provider_model->addProvider($params);
  1002. if ($result) {
  1003. $url = [
  1004. [
  1005. 'url' => 'index.php?act=merchant&op=provider',
  1006. 'msg' => '返回通道列表',
  1007. ],
  1008. [
  1009. 'url' => 'index.php?act=merchant&op=provider_add',
  1010. 'msg' => '继续新增通道',
  1011. ],
  1012. ];
  1013. $this->log('添加通道:' . '[ ' . $_POST['name'] . ']', 1);
  1014. showMessage('通道添加成功', $url);
  1015. } else {
  1016. showMessage('通道添加失败');
  1017. }
  1018. }
  1019. Tpl::showpage('provider.add');
  1020. }
  1021. public function provider_editOp()
  1022. {
  1023. $provider_model = Model('refill_provider');
  1024. if (chksubmit()) {
  1025. $provider_id = intval($_POST['provider_id']) ?? '';
  1026. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  1027. if (empty($provider)) {
  1028. showMessage('通道信息有误');
  1029. }
  1030. $params = $_POST;
  1031. unset($params['form_submit']);
  1032. if (empty($params)) {
  1033. showMessage('通道编辑成功', 'index.php?act=merchant&op=provider');
  1034. }
  1035. $result = $provider_model->editProvider($params, ['provider_id' => $provider_id]);
  1036. if ($result) {
  1037. $this->log('编辑通道:' . '[ ' . $provider['name'] . ']', 1);
  1038. showMessage('通道编辑成功', 'index.php?act=merchant&op=provider');
  1039. } else {
  1040. showMessage('通道编辑失败', "index.php?act=merchant&op=provider_edit&id={$provider_id}");
  1041. }
  1042. }
  1043. $provider_id = intval($_GET['id']) ?? '';
  1044. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  1045. if (empty($provider)) {
  1046. showMessage('通道信息有误');
  1047. }
  1048. $type_text = ['油卡', '手机充值卡'];
  1049. Tpl::output('type_text', $type_text);
  1050. Tpl::output('provider', $provider);
  1051. Tpl::showpage('provider.edit');
  1052. }
  1053. public function provider_delOp()
  1054. {
  1055. $provider_model = Model('refill_provider');
  1056. $provider_id = intval($_GET['id']) ?? '';
  1057. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  1058. if (empty($provider)) {
  1059. showMessage('通道信息有误');
  1060. }
  1061. $result = $provider_model->delProvider(['provider_id' => $provider_id]);
  1062. if ($result) {
  1063. $this->log('删除通道:' . '[ ' . $provider['name'] . ']', 1);
  1064. showMessage('通道删除成功', 'index.php?act=merchant&op=provider');
  1065. } else {
  1066. showMessage('通道删除失败', "index.php?act=merchant&op=provider");
  1067. }
  1068. }
  1069. public function provider_amount_controlOp()
  1070. {
  1071. global $config;
  1072. $type = $_GET['type'] ?? $_POST['type'];
  1073. $form = $_GET['form'] ?? $_POST['form'];
  1074. $rkey = $this->speed_key($type , $form);
  1075. if (chksubmit())
  1076. {
  1077. $pids = $_POST['pid'];
  1078. $names = $_POST['name'];
  1079. $types = $_POST['types'];
  1080. $amounts = $_POST['amount'];
  1081. $speeds = $_POST['speed'];
  1082. $sorts = $_POST['sort'];
  1083. $openeds = $_POST['opened'];
  1084. $hass = $_POST['has'];
  1085. foreach ($pids as $key => $pid) {
  1086. if($hass[$key] == 0) {
  1087. continue;
  1088. }
  1089. $data['pid'] = intval($pid);
  1090. $data['name'] = $names[$key];
  1091. $data['type'] = intval($types[$key]);
  1092. $data['amount'] = intval($amounts[$key]);
  1093. $data['speed'] = intval($speeds[$key]);
  1094. $data['sort'] = intval($sorts[$key]);
  1095. $data['opened'] = intval($openeds[$key]);
  1096. $index = "{$data['type']}-{$data['amount']}";
  1097. $result[$index][] = $data;
  1098. }
  1099. wcache($rkey , ['data' => serialize($result)] , 'provider-');
  1100. showMessage('编辑成功', 'index.php?act=merchant&op=provider');
  1101. }
  1102. else
  1103. {
  1104. $speed_limit = rcache($rkey , 'provider-');
  1105. if(empty($speed_limit)){
  1106. $speed_limit = [];
  1107. }else{
  1108. $speed_limit = unserialize($speed_limit['data']);
  1109. }
  1110. $initialiser = function ($provider) {
  1111. $data['pid'] = $provider['provider_id'];
  1112. $data['name'] = $provider['name'];
  1113. $data['speed'] = -1;
  1114. $data['sort'] = 1;
  1115. $data['opened'] = 1;
  1116. return $data;
  1117. };
  1118. $providers = Model('')->table('refill_provider,store')
  1119. ->field('refill_provider.*,store.store_name')
  1120. ->join('inner')
  1121. ->on('store.store_id=refill_provider.store_id')
  1122. ->where(['type' => $type])
  1123. ->limit(100)
  1124. ->order('opened asc , provider_id desc')
  1125. ->select();
  1126. if(empty($providers)) {
  1127. showMessage('暂无相关通道', 'index.php?act=merchant&op=provider');
  1128. }
  1129. $formData = $this->formData($type , $form);
  1130. $amounts = $formData['amounts'];
  1131. $cfgs = $formData['cfgs'];
  1132. $types = $formData['types'];
  1133. foreach ($speed_limit as $key => $speed) {
  1134. foreach ($speed as $value) {
  1135. $speed_limit[$key][$value['pid']] = $value;
  1136. }
  1137. }
  1138. $ProviderManager = new \refill\ProviderManager();
  1139. $ProviderManager->load();
  1140. $QPTA = $ProviderManager->getQPTA();
  1141. if($form == 'common') {
  1142. $QPTA = $QPTA[1];
  1143. }
  1144. if($form == 'fast') {
  1145. $QPTA = $QPTA[2];
  1146. }
  1147. foreach ($providers as $key => $provider)
  1148. {
  1149. $result = [];
  1150. $pid = $provider['provider_id'];
  1151. $name = $provider['name'];
  1152. if(empty($cfgs) || !array_key_exists($name , $cfgs)) {
  1153. unset($providers[$key]);
  1154. continue;
  1155. }
  1156. foreach ($types as $type)
  1157. {
  1158. foreach ($amounts as $k => $amount)
  1159. {
  1160. $key = $type.'-'.$amount;
  1161. if(is_array($speed_limit[$key]) && array_key_exists($pid , $speed_limit[$key])) {
  1162. $speed = $speed_limit[$key][$pid];
  1163. }else{
  1164. $speed = $initialiser($provider);
  1165. }
  1166. if( is_array($QPTA["{$name}-{$type}-{$amount}"]) && array_key_exists("{$name}-{$type}-{$amount}" , $QPTA)) {
  1167. $speed['has'] = 1;
  1168. }else{
  1169. $speed['has'] = 0;
  1170. }
  1171. $result[] = $speed;
  1172. }
  1173. }
  1174. $datas[$pid] = $result;
  1175. }
  1176. if(empty($datas)) {
  1177. showMessage('暂无相关通道配置', 'index.php?act=merchant&op=provider');
  1178. }
  1179. Tpl::output('form', $formData);
  1180. Tpl::output('datas', $datas);
  1181. Tpl::output('providers', $providers);
  1182. Tpl::showpage('provider.amount.control');
  1183. }
  1184. }
  1185. private function speed_key($type,$form)
  1186. {
  1187. if ($type == 1) {
  1188. $rkey = "channel-ctl-oil-{$form}-limit";
  1189. }elseif ($type == 2) {
  1190. $rkey = "channel-ctl-phone-{$form}-limit";
  1191. }else {
  1192. showMessage('类型有误', "index.php?act=merchant&op=provider");
  1193. }
  1194. Log::record($rkey , Log::DEBUG);
  1195. return $rkey;
  1196. }
  1197. private function formData($type , $form)
  1198. {
  1199. global $config;
  1200. $texts = [
  1201. ["中石油" , '中石化'],['移动' , '联通' , '电信']
  1202. ];
  1203. $type_data = [[1,2] , [4,5,6]];
  1204. $type_text = $texts[$type-1];
  1205. $type_data = $type_data[$type-1];
  1206. $amounts = [];
  1207. if($type == 1) {
  1208. $amounts = $config['refill_oil_specs'];
  1209. $cfgs = $config['oil_providers'];
  1210. }
  1211. if($type == 2) {
  1212. $amounts = $config['refill_phone_specs'];
  1213. $cfgs = $config['phone_providers'];
  1214. }
  1215. foreach ($type_text as $key => $value)
  1216. {
  1217. foreach ($amounts as $amount)
  1218. {
  1219. $data['text'] = $value . '-' . $amount . '元';
  1220. $data['type'] = $type_data[$key];
  1221. $data['amount'] = $amount;
  1222. $amountData[] = $data;
  1223. }
  1224. }
  1225. foreach ($cfgs as $cfg) {
  1226. $pcfgs[$cfg['name']] = $cfg;
  1227. }
  1228. return [ 'amountData' => $amountData , 'form' => $form , 'amounts' => $amounts , 'types' => $type_data , 'cfgs' => $pcfgs];
  1229. }
  1230. private function scard_type(int $card_type)
  1231. {
  1232. if ($card_type == 1) { //中石油
  1233. return '中石油';
  1234. } elseif ($card_type == 2) { //中石化
  1235. return '中石化';
  1236. } elseif ($card_type == 4) { //中国移动
  1237. return '中国移动';
  1238. } elseif ($card_type == 5) { //中国联通
  1239. return '中国联通';
  1240. } elseif ($card_type == 6) { //中国电信
  1241. return '中国电信';
  1242. } else {
  1243. return 'unknown';
  1244. }
  1245. }
  1246. private function elapse_time($seconds)
  1247. {
  1248. $minutes = intval($seconds / 60);
  1249. $second = intval($seconds % 60);
  1250. if($minutes >= 60) {
  1251. $minute = $minutes % 60;
  1252. $hours = intval($minutes / 60);
  1253. $result = "{$minute}m{$second}s";
  1254. }
  1255. elseif($minutes > 0) {
  1256. $result = "{$minutes}m{$second}s";
  1257. } else {
  1258. $result = "{$second}s";
  1259. }
  1260. if(isset($hours))
  1261. {
  1262. $result = "{$hours}h{$minute}m";
  1263. }
  1264. return $result;
  1265. }
  1266. private function refill_stat($condition , $times)
  1267. {
  1268. $stat_order = function ($condition) {
  1269. $stat = Model('')->table('refill_order,vr_order')->join('inner')
  1270. ->on('refill_order.order_id=vr_order.order_id')
  1271. ->field('count(*) as order_count ')
  1272. ->where($condition)->find();
  1273. return $stat['order_count'];
  1274. };
  1275. $result = [];
  1276. $condition['order_state'] = ORDER_STATE_SEND;
  1277. foreach ($times as $time) {
  1278. $condition['refill_order.order_time'] = $time;
  1279. $result[] = $stat_order($condition);
  1280. }
  1281. return $result;
  1282. }
  1283. private function all_order_state_stat($condition)
  1284. {
  1285. $counts = Model('')->table('refill_order,vr_order')->join('inner')
  1286. ->on('refill_order.order_id=vr_order.order_id')
  1287. ->field('count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts, order_state')
  1288. ->where($condition)
  1289. ->group('order_state')
  1290. ->select();
  1291. $all = [];
  1292. $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
  1293. $sending = $success = $cancel = $data;
  1294. foreach ($counts as $count)
  1295. {
  1296. if ($count['order_state'] == ORDER_STATE_SEND) {
  1297. $sending = $count;
  1298. } elseif ($count['order_state'] == ORDER_STATE_SUCCESS) {
  1299. $success = $count;
  1300. } elseif ($count['order_state'] == ORDER_STATE_CANCEL) {
  1301. $cancel = $count;
  1302. }
  1303. $all['order_count'] += $count['order_count'];
  1304. $all['refill_amounts'] += ncPriceFormat($count['refill_amounts']);
  1305. $all['channel_amounts'] += ncPriceFormat($count['channel_amounts']);
  1306. $all['mch_amounts'] += ncPriceFormat($count['mch_amounts']);
  1307. }
  1308. return ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
  1309. }
  1310. public function OrderSendListOp()
  1311. {
  1312. $model_refill_order = Model('refill_order');
  1313. $condition['refill_order.inner_status'] = 0;
  1314. $condition['vr_order.order_state'] = ORDER_STATE_SEND;
  1315. $condition['refill_order.order_time'] = ['lt', (time() - 1800)];
  1316. $merchant_list = Model('')->table('merchant')->limit(1000)->select();
  1317. foreach ($merchant_list as $key =>$value) {
  1318. $merchants[$value['mchid']] = $value;
  1319. }
  1320. $order_list = $model_refill_order->getMerchantOrderList($condition, 1000, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
  1321. $stat = Model('')->table('refill_order,vr_order')->join('inner')
  1322. ->on('refill_order.order_id=vr_order.order_id')
  1323. ->field('count(*) as order_count ,sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts')
  1324. ->where($condition)->find();
  1325. $count = $this->refill_stat($condition ,
  1326. [['between', [(time() - 3600) , (time() - 1800)]],
  1327. ['lt', (time() - 3600)]]
  1328. );
  1329. foreach ($order_list as $order_id => $order_info) {
  1330. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  1331. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  1332. $order_list[$order_id]['diff_time_text'] = $this->elapse_time(time() - $order_info['order_time']);
  1333. $order_list[$order_id]['diff_time'] = time() - $order_info['order_time'];
  1334. }
  1335. $provider_list = Model('')->table('refill_provider')->limit(100)->select();
  1336. Tpl::output('stat', $stat);
  1337. Tpl::output('count', $count);
  1338. Tpl::output('order_list', $order_list);
  1339. Tpl::output('provider_list', $provider_list);
  1340. Tpl::output('show_page', $model_refill_order->showpage());
  1341. Tpl::showpage('refill.order.send.index');
  1342. }
  1343. public function OrderStatsOp()
  1344. {
  1345. $type = $_GET['type'] ? $_GET['type'] : 'system';
  1346. $page = "{$type}.order.stats";
  1347. $model_refill_order = Model('refill_order');
  1348. $condition['type'] = $type;
  1349. if(!empty($_GET['cid'])) {
  1350. $condition['cid'] = $_GET['cid'];
  1351. }
  1352. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  1353. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  1354. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  1355. $condition['time_stamp'] = [ ['egt', $start_unixtime] , ['lt', $end_unixtime] , 'and'];
  1356. }
  1357. elseif ($start_unixtime > 0) {
  1358. $condition['time_stamp'] = ['egt', $start_unixtime];
  1359. }
  1360. elseif ($end_unixtime > 0) {
  1361. $condition['time_stamp'] = ['lt', $end_unixtime];
  1362. }
  1363. $stats_list = $model_refill_order->getOrderStatsList($condition);
  1364. if($type == 'provider') {
  1365. $provider_list = Model('')->table('refill_provider,store')->field('refill_provider.store_id,store.store_name')->join('inner')
  1366. ->on('store.store_id=refill_provider.store_id')->limit(100)->select();
  1367. Tpl::output('provider_list', $provider_list);
  1368. }elseif ($type == 'merchant') {
  1369. $merchant_list = Model('')->table('merchant')->limit(100)->select();
  1370. Tpl::output('merchant_list', $merchant_list);
  1371. }
  1372. Tpl::output('stats_list', $stats_list);
  1373. Tpl::output('show_page', $model_refill_order->showpage());
  1374. Tpl::showpage($page);
  1375. }
  1376. }