merchant.php 62 KB

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