merchant.php 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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['refill_amount'])) {
  775. $condition['refill_order.refill_amount'] = $_GET['refill_amount'];
  776. }
  777. if (!empty($_GET['card_no'])) {
  778. $condition['refill_order.card_no'] = $_GET['card_no'];
  779. }
  780. if (!empty($_GET['card_type']))
  781. {
  782. if(in_array($_GET['card_type'] , ['1' , '2' , '4' , '5' , '6'])) {
  783. $condition['refill_order.card_type'] = $_GET['card_type'];
  784. }
  785. if($_GET['card_type'] == 'oil') {
  786. $condition['refill_order.card_type'] = ['in' , ['1' , '2']];
  787. }
  788. if($_GET['card_type'] == 'phone') {
  789. $condition['refill_order.card_type'] = ['in' , ['4' , '5' , '6']];
  790. }
  791. }
  792. $fToday = false;
  793. if(!$fSingle)
  794. {
  795. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  796. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  797. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  798. $condition['refill_order.order_time'] = [ ['egt', $start_unixtime] , ['lt', $end_unixtime] , 'and'];
  799. }
  800. elseif ($start_unixtime > 0) {
  801. $condition['refill_order.order_time'] = ['egt', $start_unixtime];
  802. }
  803. elseif ($end_unixtime > 0) {
  804. $condition['refill_order.order_time'] = ['lt', $end_unixtime];
  805. }
  806. else {
  807. $start = strtotime(date('Y-m-d',time()));
  808. $condition['refill_order.order_time'] = ['egt', $start];
  809. $fToday = true;
  810. }
  811. if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40']))
  812. {
  813. $condition['vr_order.order_state'] = $_GET['order_state'];
  814. if($_GET['order_state'] == ORDER_STATE_SEND)
  815. {
  816. if($_GET['time'] == 1){
  817. $condition['refill_order.order_time'] = ['between', [(time() - 3600) , (time() - 1800)]];
  818. }
  819. if($_GET['time'] == 2){
  820. $condition['refill_order.order_time'] = ['lt', (time() - 3600)];
  821. }
  822. }
  823. }
  824. }
  825. $merchants = [];
  826. $merchant_list = Model('')->table('merchant')->limit(100)->select();
  827. foreach ($merchant_list as $key => $value) {
  828. $merchants[$value['mchid']] = $value;
  829. }
  830. $order_list = $model_refill_order->getMerchantOrderList($condition, 50, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
  831. $stat = $this->all_order_state_stat($condition);
  832. foreach ($order_list as $order_id => $order_info)
  833. {
  834. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  835. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  836. if ($order_info['notify_time'] > 0) {
  837. $diff_time = $order_info['notify_time'] - $order_info['order_time'];
  838. } else {
  839. $diff_time = time() - $order_info['order_time'];
  840. }
  841. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($diff_time);
  842. $order_list[$order_id]['diff_time'] = $diff_time;
  843. }
  844. $provider_list = Model('')->table('refill_provider,store')
  845. ->field('refill_provider.*,store.store_name')
  846. ->join('inner')
  847. ->on('store.store_id=refill_provider.store_id')
  848. ->where(['refill_provider.opened' => 1])
  849. ->limit(100)
  850. ->select();
  851. Tpl::output('stat', $stat);
  852. Tpl::output('ftoday', $fToday);
  853. Tpl::output('order_list', $order_list);
  854. Tpl::output('provider_list', $provider_list);
  855. Tpl::output('merchant_list', $merchant_list);
  856. Tpl::output('show_page', $model_refill_order->showpage());
  857. Tpl::showpage('refill.order.index');
  858. }
  859. public function notify_merchantOp()
  860. {
  861. $order_id = $_GET['order_id'];
  862. $mod_order = Model('vr_order');
  863. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  864. if($order_info['order_state'] == ORDER_STATE_SEND) {
  865. QueueClient::push("QueryRefillState",['order_id' => $order_id]);
  866. }else{
  867. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  868. }
  869. showMessage('操作成功', 'index.php?act=merchant&op=refill_order');
  870. }
  871. public function notify_manual_merchantOp()
  872. {
  873. $order_id = $_GET['order_id'];
  874. $type = $_GET['type'];
  875. $mod_order = Model('vr_order');
  876. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  877. if((time() - $order_info['order_time']) < 3600)
  878. {
  879. showMessage('订单时间未超过1小时', 'index.php?act=merchant&op=refill_order');
  880. }
  881. $logic_vr_order = Logic("vr_order");
  882. if($type == 'success') {
  883. if($order_info['order_state'] == ORDER_STATE_CANCEL){
  884. showMessage('此订单状态已为已取消,不可手动成功。', 'index.php?act=merchant&op=refill_order');
  885. }
  886. $logic_vr_order->changeOrderStateSuccess($order_id);
  887. }elseif ($type == 'cancel') {
  888. if($order_info['order_state'] == ORDER_STATE_SUCCESS){
  889. showMessage('此订单状态已为交易完成,不可手动失败。', 'index.php?act=merchant&op=refill_order');
  890. }
  891. $logic_vr_order->changeOrderStateCancel($order_info, '', "后台手动回调通知失败.");
  892. }else {
  893. showMessage('手动操作类型错误', 'index.php?act=merchant&op=refill_order');
  894. }
  895. $mod_refill = Model('refill_order');
  896. $mod_refill->edit($order_id, ['notify_time' => time(), 'notify_state' => 1]);
  897. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  898. showMessage('操作成功', 'index.php?act=merchant&op=refill_order');
  899. }
  900. public function providerStatsOp()
  901. {
  902. $provider_model = Model('refill_provider');
  903. $condition = [];
  904. $provider_list = $provider_model->getProviderList($condition, 100);
  905. $opened_text = ['使用中', '已禁用'];
  906. $type_text = ['油卡', '手机充值卡'];
  907. Tpl::output('opened_text', $opened_text);
  908. Tpl::output('type_text', $type_text);
  909. Tpl::output('provider_list', $provider_list);
  910. Tpl::output('show_page', $provider_model->showpage());
  911. Tpl::showpage('provider.stats');
  912. }
  913. public function providerOp()
  914. {
  915. $this->sync_cfgs();
  916. $provider_model = Model('refill_provider');
  917. $condition = [];
  918. if (trim($_GET['name']) != '') {
  919. $condition['name'] = ['like', '%' . $_GET['name'] . '%'];
  920. Tpl::output('name', $_GET['name']);
  921. }
  922. if (in_array($_GET['type'], [1, 2])) {
  923. $condition['type'] = $_GET['type'];
  924. }
  925. $provider_list = $provider_model->getProviderList($condition, 100);
  926. foreach ($provider_list as $key => $provider) {
  927. if(!empty($provider['start_period']) && !empty($provider['end_period'])){
  928. $provider_list[$key]['period'] = $provider['start_period'] . '~' . $provider['end_period'];
  929. }else{
  930. $provider_list[$key]['period'] = '全时间段';
  931. }
  932. }
  933. $opened_text = ['使用中', '已禁用'];
  934. $type_text = ['油卡', '手机充值卡'];
  935. Tpl::output('opened_text', $opened_text);
  936. Tpl::output('type_text', $type_text);
  937. Tpl::output('provider_list', $provider_list);
  938. Tpl::output('show_page', $provider_model->showpage());
  939. Tpl::showpage('provider.index');
  940. }
  941. public function sync_cfgs()
  942. {
  943. $name_val = function ($items) {
  944. $result = [];
  945. foreach ($items as $item) {
  946. $name = $item['name'];
  947. $result[$name] = $item;
  948. }
  949. return $result;
  950. };
  951. $match = function ($all,$cur)
  952. {
  953. $inserts = [];
  954. foreach ($all as $key => $value)
  955. {
  956. if(!array_key_exists($key,$cur)) {
  957. $insert['name'] = $key;
  958. $insert['store_id'] = $value['cfg']['store_id'];
  959. $insert['qualitys'] = $value['cfg']['qualitys'];
  960. $inserts[] = $insert;
  961. }
  962. }
  963. return $inserts;
  964. };
  965. $inserter = function ($mod,$type,$names)
  966. {
  967. foreach ($names as $name) {
  968. $data = ['name' => $name['name'],'type' => $type,'store_id' => $name['store_id'] , 'qualitys' => $name['qualitys']];
  969. $mod->insert($data);
  970. }
  971. };
  972. global $config;
  973. $oil_configs = $config['oil_providers'];
  974. $pho_configs = $config['phone_providers'];
  975. $oils = $name_val($oil_configs);
  976. $phones = $name_val($pho_configs);
  977. $mod_prov = Model('refill_provider');
  978. $oil_items = $mod_prov->getProviderList(['type' => 1]);
  979. $oil_items = $name_val($oil_items);
  980. $oil_inserts = $match($oils,$oil_items);
  981. $phone_items = $mod_prov->getProviderList(['type' => 2]);
  982. $phone_items = $name_val($phone_items);
  983. $phone_inserts = $match($phones,$phone_items);
  984. $inserter($mod_prov,1,$oil_inserts);
  985. $inserter($mod_prov,2,$phone_inserts);
  986. }
  987. public function changeProviderStateOp()
  988. {
  989. $provider_id = intval($_GET['id']);
  990. $state = intval($_GET['state']);
  991. $provider_model = Model('refill_provider');
  992. $provider_info = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  993. if (empty($provider_info) || !in_array($state, [1, 2])) {
  994. showMessage('操作成功', 'index.php?act=merchant&op=provider');
  995. }
  996. $resp = $provider_model->editProvider(['opened' => $state], ['provider_id' => $provider_id]);
  997. if (!$resp) {
  998. showMessage('操作失败', 'index.php?act=merchant&op=provider', 'html', 'error');
  999. }
  1000. showMessage('操作成功', 'index.php?act=merchant&op=provider');
  1001. }
  1002. /**
  1003. * 新增通道
  1004. */
  1005. public function provider_addOp()
  1006. {
  1007. $provider_model = Model('refill_provider');
  1008. if (chksubmit()) {
  1009. $params = $_POST;
  1010. unset($params['form_submit']);
  1011. $result = $provider_model->addProvider($params);
  1012. if ($result) {
  1013. $url = [
  1014. [
  1015. 'url' => 'index.php?act=merchant&op=provider',
  1016. 'msg' => '返回通道列表',
  1017. ],
  1018. [
  1019. 'url' => 'index.php?act=merchant&op=provider_add',
  1020. 'msg' => '继续新增通道',
  1021. ],
  1022. ];
  1023. $this->log('添加通道:' . '[ ' . $_POST['name'] . ']', 1);
  1024. showMessage('通道添加成功', $url);
  1025. } else {
  1026. showMessage('通道添加失败');
  1027. }
  1028. }
  1029. Tpl::showpage('provider.add');
  1030. }
  1031. public function provider_editOp()
  1032. {
  1033. $provider_model = Model('refill_provider');
  1034. if (chksubmit()) {
  1035. $provider_id = intval($_POST['provider_id']) ?? '';
  1036. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  1037. if (empty($provider)) {
  1038. showMessage('通道信息有误');
  1039. }
  1040. $params = $_POST;
  1041. unset($params['form_submit']);
  1042. if (empty($params)) {
  1043. showMessage('通道编辑成功', 'index.php?act=merchant&op=provider');
  1044. }
  1045. $result = $provider_model->editProvider($params, ['provider_id' => $provider_id]);
  1046. if ($result) {
  1047. $this->log('编辑通道:' . '[ ' . $provider['name'] . ']', 1);
  1048. showMessage('通道编辑成功', 'index.php?act=merchant&op=provider');
  1049. } else {
  1050. showMessage('通道编辑失败', "index.php?act=merchant&op=provider_edit&id={$provider_id}");
  1051. }
  1052. }
  1053. $provider_id = intval($_GET['id']) ?? '';
  1054. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  1055. if (empty($provider)) {
  1056. showMessage('通道信息有误');
  1057. }
  1058. $type_text = ['油卡', '手机充值卡'];
  1059. Tpl::output('type_text', $type_text);
  1060. Tpl::output('provider', $provider);
  1061. Tpl::showpage('provider.edit');
  1062. }
  1063. public function provider_delOp()
  1064. {
  1065. $provider_model = Model('refill_provider');
  1066. $provider_id = intval($_GET['id']) ?? '';
  1067. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  1068. if (empty($provider)) {
  1069. showMessage('通道信息有误');
  1070. }
  1071. $result = $provider_model->delProvider(['provider_id' => $provider_id]);
  1072. if ($result) {
  1073. $this->log('删除通道:' . '[ ' . $provider['name'] . ']', 1);
  1074. showMessage('通道删除成功', 'index.php?act=merchant&op=provider');
  1075. } else {
  1076. showMessage('通道删除失败', "index.php?act=merchant&op=provider");
  1077. }
  1078. }
  1079. public function provider_amount_controlOp()
  1080. {
  1081. global $config;
  1082. $type = $_GET['type'] ?? $_POST['type'];
  1083. $form = $_GET['form'] ?? $_POST['form'];
  1084. $rkey = $this->speed_key($type , $form);
  1085. $quality = [ 'common' => 1 , 'fast' => 2 , 'card' => 3 , 'third' => 4 , 'slow' => 5];
  1086. if (chksubmit())
  1087. {
  1088. $pids = $_POST['pid'];
  1089. $names = $_POST['name'];
  1090. $types = $_POST['types'];
  1091. $amounts = $_POST['amount'];
  1092. $speeds = $_POST['speed'];
  1093. $sorts = $_POST['sort'];
  1094. $openeds = $_POST['opened'];
  1095. $hass = $_POST['has'];
  1096. foreach ($pids as $key => $pid) {
  1097. if($hass[$key] == 0) {
  1098. continue;
  1099. }
  1100. $data['pid'] = intval($pid);
  1101. $data['name'] = $names[$key];
  1102. $data['type'] = intval($types[$key]);
  1103. $data['amount'] = intval($amounts[$key]);
  1104. $data['speed'] = intval($speeds[$key]);
  1105. $data['sort'] = intval($sorts[$key]);
  1106. $data['opened'] = intval($openeds[$key]);
  1107. $index = "{$data['type']}-{$data['amount']}";
  1108. $result[$index][] = $data;
  1109. }
  1110. wcache($rkey , ['data' => serialize($result)] , 'provider-');
  1111. showMessage('编辑成功', 'index.php?act=merchant&op=provider');
  1112. }
  1113. else
  1114. {
  1115. $speed_limit = rcache($rkey , 'provider-');
  1116. if(empty($speed_limit)){
  1117. $speed_limit = [];
  1118. }else{
  1119. $speed_limit = unserialize($speed_limit['data']);
  1120. }
  1121. $initialiser = function ($provider) {
  1122. $data['pid'] = $provider['provider_id'];
  1123. $data['name'] = $provider['name'];
  1124. $data['speed'] = -1;
  1125. $data['sort'] = 1;
  1126. $data['opened'] = 1;
  1127. return $data;
  1128. };
  1129. $providers = Model('')->table('refill_provider,store')
  1130. ->field('refill_provider.*,store.store_name')
  1131. ->join('inner')
  1132. ->on('store.store_id=refill_provider.store_id')
  1133. ->where(['type' => $type , 'qualitys' => ['like', '%' . $quality[$form] . '%']])
  1134. ->limit(100)
  1135. ->order('opened asc , provider_id desc')
  1136. ->select();
  1137. if(empty($providers)) {
  1138. showMessage('暂无相关通道', 'index.php?act=merchant&op=provider');
  1139. }
  1140. $formData = $this->formData($type , $form);
  1141. $amounts = $formData['amounts'];
  1142. $cfgs = $formData['cfgs'];
  1143. $types = $formData['types'];
  1144. foreach ($speed_limit as $key => $speed) {
  1145. foreach ($speed as $value) {
  1146. $speed_limit[$key][$value['pid']] = $value;
  1147. }
  1148. }
  1149. $ProviderManager = new \refill\ProviderManager();
  1150. $ProviderManager->load();
  1151. $QPTA = $ProviderManager->getQPTA();
  1152. $QPTA = $QPTA[$quality[$form]];
  1153. foreach ($providers as $key => $provider)
  1154. {
  1155. $result = [];
  1156. $pid = $provider['provider_id'];
  1157. $name = $provider['name'];
  1158. if(empty($cfgs) || !array_key_exists($name , $cfgs)) {
  1159. unset($providers[$key]);
  1160. continue;
  1161. }
  1162. foreach ($types as $type)
  1163. {
  1164. foreach ($amounts as $k => $amount)
  1165. {
  1166. $key = $type.'-'.$amount;
  1167. if(is_array($speed_limit[$key]) && array_key_exists($pid , $speed_limit[$key])) {
  1168. $speed = $speed_limit[$key][$pid];
  1169. }else{
  1170. $speed = $initialiser($provider);
  1171. }
  1172. if( is_array($QPTA["{$name}-{$type}-{$amount}"]) && array_key_exists("{$name}-{$type}-{$amount}" , $QPTA)) {
  1173. $speed['has'] = 1;
  1174. }else{
  1175. $speed['has'] = 0;
  1176. }
  1177. $result[] = $speed;
  1178. }
  1179. }
  1180. $datas[$pid] = $result;
  1181. }
  1182. if(empty($datas)) {
  1183. showMessage('暂无相关通道配置', 'index.php?act=merchant&op=provider');
  1184. }
  1185. Tpl::output('form', $formData);
  1186. Tpl::output('datas', $datas);
  1187. Tpl::output('providers', $providers);
  1188. Tpl::showpage('provider.amount.control');
  1189. }
  1190. }
  1191. private function speed_key($type,$form)
  1192. {
  1193. if ($type == 1) {
  1194. $rkey = "channel-ctl-oil-{$form}-limit";
  1195. }elseif ($type == 2) {
  1196. $rkey = "channel-ctl-phone-{$form}-limit";
  1197. }else {
  1198. showMessage('类型有误', "index.php?act=merchant&op=provider");
  1199. }
  1200. Log::record($rkey , Log::DEBUG);
  1201. return $rkey;
  1202. }
  1203. private function formData($type , $form)
  1204. {
  1205. global $config;
  1206. $texts = [
  1207. ["中石油" , '中石化'],['移动' , '联通' , '电信']
  1208. ];
  1209. $type_data = [[1,2] , [4,5,6]];
  1210. $type_text = $texts[$type-1];
  1211. $type_data = $type_data[$type-1];
  1212. $amounts = [];
  1213. if($type == 1) {
  1214. $amounts = $config['refill_oil_specs'];
  1215. $cfgs = $config['oil_providers'];
  1216. }
  1217. if($type == 2) {
  1218. $amounts = $config['refill_phone_specs'];
  1219. $cfgs = $config['phone_providers'];
  1220. }
  1221. foreach ($type_text as $key => $value)
  1222. {
  1223. foreach ($amounts as $amount)
  1224. {
  1225. $data['text'] = $value . '-' . $amount . '元';
  1226. $data['type'] = $type_data[$key];
  1227. $data['amount'] = $amount;
  1228. $amountData[] = $data;
  1229. }
  1230. }
  1231. foreach ($cfgs as $cfg) {
  1232. $pcfgs[$cfg['name']] = $cfg;
  1233. }
  1234. return [ 'amountData' => $amountData , 'form' => $form , 'amounts' => $amounts , 'types' => $type_data , 'cfgs' => $pcfgs];
  1235. }
  1236. private function scard_type(int $card_type)
  1237. {
  1238. if ($card_type == 1) { //中石油
  1239. return '中石油';
  1240. } elseif ($card_type == 2) { //中石化
  1241. return '中石化';
  1242. } elseif ($card_type == 4) { //中国移动
  1243. return '中国移动';
  1244. } elseif ($card_type == 5) { //中国联通
  1245. return '中国联通';
  1246. } elseif ($card_type == 6) { //中国电信
  1247. return '中国电信';
  1248. } else {
  1249. return 'unknown';
  1250. }
  1251. }
  1252. private function elapse_time($seconds)
  1253. {
  1254. $minutes = intval($seconds / 60);
  1255. $second = intval($seconds % 60);
  1256. if($minutes >= 60) {
  1257. $minute = $minutes % 60;
  1258. $hours = intval($minutes / 60);
  1259. $result = "{$minute}m{$second}s";
  1260. }
  1261. elseif($minutes > 0) {
  1262. $result = "{$minutes}m{$second}s";
  1263. } else {
  1264. $result = "{$second}s";
  1265. }
  1266. if(isset($hours))
  1267. {
  1268. $result = "{$hours}h{$minute}m";
  1269. }
  1270. return $result;
  1271. }
  1272. private function refill_stat($condition , $times)
  1273. {
  1274. $stat_order = function ($condition) {
  1275. $stat = Model('')->table('refill_order,vr_order')->join('inner')
  1276. ->on('refill_order.order_id=vr_order.order_id')
  1277. ->field('count(*) as order_count ')
  1278. ->where($condition)->find();
  1279. return $stat['order_count'];
  1280. };
  1281. $result = [];
  1282. $condition['order_state'] = ORDER_STATE_SEND;
  1283. foreach ($times as $time) {
  1284. $condition['refill_order.order_time'] = $time;
  1285. $result[] = $stat_order($condition);
  1286. }
  1287. return $result;
  1288. }
  1289. private function all_order_state_stat($condition)
  1290. {
  1291. $counts = Model('')->table('refill_order,vr_order')->join('inner')
  1292. ->on('refill_order.order_id=vr_order.order_id')
  1293. ->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')
  1294. ->where($condition)
  1295. ->group('order_state')
  1296. ->select();
  1297. $all = [];
  1298. $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
  1299. $sending = $success = $cancel = $data;
  1300. foreach ($counts as $count)
  1301. {
  1302. if ($count['order_state'] == ORDER_STATE_SEND) {
  1303. $sending = $count;
  1304. } elseif ($count['order_state'] == ORDER_STATE_SUCCESS) {
  1305. $success = $count;
  1306. } elseif ($count['order_state'] == ORDER_STATE_CANCEL) {
  1307. $cancel = $count;
  1308. }
  1309. $all['order_count'] += $count['order_count'];
  1310. $all['refill_amounts'] += ncPriceFormat($count['refill_amounts']);
  1311. $all['channel_amounts'] += ncPriceFormat($count['channel_amounts']);
  1312. $all['mch_amounts'] += ncPriceFormat($count['mch_amounts']);
  1313. }
  1314. return ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
  1315. }
  1316. public function OrderSendListOp()
  1317. {
  1318. $model_refill_order = Model('refill_order');
  1319. $condition['refill_order.inner_status'] = 0;
  1320. $condition['vr_order.order_state'] = ORDER_STATE_SEND;
  1321. $condition['refill_order.order_time'] = ['lt', (time() - 1800)];
  1322. $merchant_list = Model('')->table('merchant')->limit(1000)->select();
  1323. foreach ($merchant_list as $key =>$value) {
  1324. $merchants[$value['mchid']] = $value;
  1325. }
  1326. $order_list = $model_refill_order->getMerchantOrderList($condition, 1000, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
  1327. $stat = Model('')->table('refill_order,vr_order')->join('inner')
  1328. ->on('refill_order.order_id=vr_order.order_id')
  1329. ->field('count(*) as order_count ,sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts')
  1330. ->where($condition)->find();
  1331. $count = $this->refill_stat($condition ,
  1332. [['between', [(time() - 3600) , (time() - 1800)]],
  1333. ['lt', (time() - 3600)]]
  1334. );
  1335. foreach ($order_list as $order_id => $order_info) {
  1336. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  1337. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  1338. $order_list[$order_id]['diff_time_text'] = $this->elapse_time(time() - $order_info['order_time']);
  1339. $order_list[$order_id]['diff_time'] = time() - $order_info['order_time'];
  1340. }
  1341. $provider_list = Model('')->table('refill_provider')->limit(100)->select();
  1342. Tpl::output('stat', $stat);
  1343. Tpl::output('count', $count);
  1344. Tpl::output('order_list', $order_list);
  1345. Tpl::output('provider_list', $provider_list);
  1346. Tpl::output('show_page', $model_refill_order->showpage());
  1347. Tpl::showpage('refill.order.send.index');
  1348. }
  1349. public function OrderStatsOp()
  1350. {
  1351. $type = $_GET['type'] ? $_GET['type'] : 'system';
  1352. $page = "{$type}.order.stats";
  1353. $model_refill_order = Model('refill_order');
  1354. $condition['type'] = $type;
  1355. if(!empty($_GET['cid'])) {
  1356. $condition['cid'] = $_GET['cid'];
  1357. }
  1358. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  1359. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  1360. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  1361. $condition['time_stamp'] = [ ['egt', $start_unixtime] , ['lt', $end_unixtime] , 'and'];
  1362. }
  1363. elseif ($start_unixtime > 0) {
  1364. $condition['time_stamp'] = ['egt', $start_unixtime];
  1365. }
  1366. elseif ($end_unixtime > 0) {
  1367. $condition['time_stamp'] = ['lt', $end_unixtime];
  1368. }
  1369. $stats_list = $model_refill_order->getOrderStatsList($condition);
  1370. if($type == 'provider') {
  1371. $provider_list = Model('')->table('refill_provider,store')->field('refill_provider.store_id,store.store_name')->join('inner')
  1372. ->on('store.store_id=refill_provider.store_id')->limit(100)->select();
  1373. Tpl::output('provider_list', $provider_list);
  1374. }elseif ($type == 'merchant') {
  1375. $merchant_list = Model('')->table('merchant')->limit(100)->select();
  1376. Tpl::output('merchant_list', $merchant_list);
  1377. }
  1378. Tpl::output('stats_list', $stats_list);
  1379. Tpl::output('show_page', $model_refill_order->showpage());
  1380. Tpl::showpage($page);
  1381. }
  1382. }