merchant.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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 = $updates = [];
  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. } elseif ($value['cfg']['qualitys'] != $cur[$key]['qualitys']) {
  962. $update['provider_id'] = $cur[$key]['provider_id'];
  963. $update['qualitys'] = $value['cfg']['qualitys'];
  964. $updates[] = $update;
  965. }
  966. }
  967. return [$inserts, $updates];
  968. };
  969. $updater = function ($mod,$updates)
  970. {
  971. if(empty($updates)) return;
  972. foreach ($updates as $update) {
  973. $provider_id = $update['provider_id'];
  974. $data = ['qualitys' => $update['qualitys']];
  975. $mod->editProvider($data, ['provider_id' => $provider_id]);
  976. }
  977. };
  978. $inserter = function ($mod,$type,$names)
  979. {
  980. foreach ($names as $name) {
  981. $data = ['name' => $name['name'],'type' => $type,'store_id' => $name['store_id'] , 'qualitys' => $name['qualitys']];
  982. $mod->insert($data);
  983. }
  984. };
  985. global $config;
  986. $oil_configs = $config['oil_providers'];
  987. $pho_configs = $config['phone_providers'];
  988. $oils = $name_val($oil_configs);
  989. $phones = $name_val($pho_configs);
  990. $mod_prov = Model('refill_provider');
  991. $oil_items = $mod_prov->getProviderList(['type' => 1]);
  992. $oil_items = $name_val($oil_items);
  993. [$oil_inserts,$oil_updates] = $match($oils,$oil_items);
  994. $phone_items = $mod_prov->getProviderList(['type' => 2]);
  995. $phone_items = $name_val($phone_items);
  996. [$phone_inserts,$phone_updates] = $match($phones,$phone_items);
  997. $inserter($mod_prov,1,$oil_inserts);
  998. $inserter($mod_prov,2,$phone_inserts);
  999. $updater($mod_prov,$oil_updates);
  1000. $updater($mod_prov,$phone_updates);
  1001. }
  1002. public function changeProviderStateOp()
  1003. {
  1004. $provider_id = intval($_GET['id']);
  1005. $state = intval($_GET['state']);
  1006. $provider_model = Model('refill_provider');
  1007. $provider_info = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  1008. if (empty($provider_info) || !in_array($state, [1, 2])) {
  1009. showMessage('操作成功', 'index.php?act=merchant&op=provider');
  1010. }
  1011. $resp = $provider_model->editProvider(['opened' => $state], ['provider_id' => $provider_id]);
  1012. if (!$resp) {
  1013. showMessage('操作失败', 'index.php?act=merchant&op=provider', 'html', 'error');
  1014. }
  1015. showMessage('操作成功', 'index.php?act=merchant&op=provider');
  1016. }
  1017. /**
  1018. * 新增通道
  1019. */
  1020. public function provider_addOp()
  1021. {
  1022. $provider_model = Model('refill_provider');
  1023. if (chksubmit()) {
  1024. $params = $_POST;
  1025. unset($params['form_submit']);
  1026. $result = $provider_model->addProvider($params);
  1027. if ($result) {
  1028. $url = [
  1029. [
  1030. 'url' => 'index.php?act=merchant&op=provider',
  1031. 'msg' => '返回通道列表',
  1032. ],
  1033. [
  1034. 'url' => 'index.php?act=merchant&op=provider_add',
  1035. 'msg' => '继续新增通道',
  1036. ],
  1037. ];
  1038. $this->log('添加通道:' . '[ ' . $_POST['name'] . ']', 1);
  1039. showMessage('通道添加成功', $url);
  1040. } else {
  1041. showMessage('通道添加失败');
  1042. }
  1043. }
  1044. Tpl::showpage('provider.add');
  1045. }
  1046. public function provider_editOp()
  1047. {
  1048. $provider_model = Model('refill_provider');
  1049. if (chksubmit()) {
  1050. $provider_id = intval($_POST['provider_id']) ?? '';
  1051. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  1052. if (empty($provider)) {
  1053. showMessage('通道信息有误');
  1054. }
  1055. $params = $_POST;
  1056. unset($params['form_submit']);
  1057. if (empty($params)) {
  1058. showMessage('通道编辑成功', 'index.php?act=merchant&op=provider');
  1059. }
  1060. $result = $provider_model->editProvider($params, ['provider_id' => $provider_id]);
  1061. if ($result) {
  1062. $this->log('编辑通道:' . '[ ' . $provider['name'] . ']', 1);
  1063. showMessage('通道编辑成功', 'index.php?act=merchant&op=provider');
  1064. } else {
  1065. showMessage('通道编辑失败', "index.php?act=merchant&op=provider_edit&id={$provider_id}");
  1066. }
  1067. }
  1068. $provider_id = intval($_GET['id']) ?? '';
  1069. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  1070. if (empty($provider)) {
  1071. showMessage('通道信息有误');
  1072. }
  1073. $type_text = ['油卡', '手机充值卡'];
  1074. Tpl::output('type_text', $type_text);
  1075. Tpl::output('provider', $provider);
  1076. Tpl::showpage('provider.edit');
  1077. }
  1078. public function provider_delOp()
  1079. {
  1080. $provider_model = Model('refill_provider');
  1081. $provider_id = intval($_GET['id']) ?? '';
  1082. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  1083. if (empty($provider)) {
  1084. showMessage('通道信息有误');
  1085. }
  1086. $result = $provider_model->delProvider(['provider_id' => $provider_id]);
  1087. if ($result) {
  1088. $this->log('删除通道:' . '[ ' . $provider['name'] . ']', 1);
  1089. showMessage('通道删除成功', 'index.php?act=merchant&op=provider');
  1090. } else {
  1091. showMessage('通道删除失败', "index.php?act=merchant&op=provider");
  1092. }
  1093. }
  1094. public function provider_amount_controlOp()
  1095. {
  1096. global $config;
  1097. $type = $_GET['type'] ?? $_POST['type'];
  1098. $form = $_GET['form'] ?? $_POST['form'];
  1099. $rkey = $this->speed_key($type , $form);
  1100. $quality = [ 'common' => 1 , 'fast' => 2 , 'card' => 3 , 'third' => 4 , 'slow' => 5];
  1101. if (chksubmit())
  1102. {
  1103. $pids = $_POST['pid'];
  1104. $names = $_POST['name'];
  1105. $types = $_POST['types'];
  1106. $amounts = $_POST['amount'];
  1107. $speeds = $_POST['speed'];
  1108. $sorts = $_POST['sort'];
  1109. $openeds = $_POST['opened'];
  1110. $hass = $_POST['has'];
  1111. foreach ($pids as $key => $pid) {
  1112. if($hass[$key] == 0) {
  1113. continue;
  1114. }
  1115. $data['pid'] = intval($pid);
  1116. $data['name'] = $names[$key];
  1117. $data['type'] = intval($types[$key]);
  1118. $data['amount'] = intval($amounts[$key]);
  1119. $data['speed'] = intval($speeds[$key]);
  1120. $data['sort'] = intval($sorts[$key]);
  1121. $data['opened'] = intval($openeds[$key]);
  1122. $index = "{$data['type']}-{$data['amount']}";
  1123. $result[$index][] = $data;
  1124. }
  1125. wcache($rkey , ['data' => serialize($result)] , 'provider-');
  1126. showMessage('编辑成功', 'index.php?act=merchant&op=provider');
  1127. }
  1128. else
  1129. {
  1130. $speed_limit = rcache($rkey , 'provider-');
  1131. if(empty($speed_limit)){
  1132. $speed_limit = [];
  1133. }else{
  1134. $speed_limit = unserialize($speed_limit['data']);
  1135. }
  1136. $initialiser = function ($provider) {
  1137. $data['pid'] = $provider['provider_id'];
  1138. $data['name'] = $provider['name'];
  1139. $data['speed'] = -1;
  1140. $data['sort'] = 1;
  1141. $data['opened'] = 1;
  1142. return $data;
  1143. };
  1144. $providers = Model('')->table('refill_provider,store')
  1145. ->field('refill_provider.*,store.store_name')
  1146. ->join('inner')
  1147. ->on('store.store_id=refill_provider.store_id')
  1148. ->where(['type' => $type , 'qualitys' => ['like', '%' . $quality[$form] . '%']])
  1149. ->limit(100)
  1150. ->order('opened asc , provider_id desc')
  1151. ->select();
  1152. if(empty($providers)) {
  1153. showMessage('暂无相关通道', 'index.php?act=merchant&op=provider');
  1154. }
  1155. $formData = $this->formData($type , $form);
  1156. $amounts = $formData['amounts'];
  1157. $cfgs = $formData['cfgs'];
  1158. $types = $formData['types'];
  1159. foreach ($speed_limit as $key => $speed) {
  1160. foreach ($speed as $value) {
  1161. $speed_limit[$key][$value['pid']] = $value;
  1162. }
  1163. }
  1164. $ProviderManager = new \refill\ProviderManager();
  1165. $ProviderManager->load();
  1166. $QPTA = $ProviderManager->getQPTA();
  1167. $QPTA = $QPTA[$quality[$form]];
  1168. foreach ($providers as $key => $provider)
  1169. {
  1170. $result = [];
  1171. $pid = $provider['provider_id'];
  1172. $name = $provider['name'];
  1173. if(empty($cfgs) || !array_key_exists($name , $cfgs)) {
  1174. unset($providers[$key]);
  1175. continue;
  1176. }
  1177. foreach ($types as $type)
  1178. {
  1179. foreach ($amounts as $k => $amount)
  1180. {
  1181. $key = $type.'-'.$amount;
  1182. if(is_array($speed_limit[$key]) && array_key_exists($pid , $speed_limit[$key])) {
  1183. $speed = $speed_limit[$key][$pid];
  1184. }else{
  1185. $speed = $initialiser($provider);
  1186. }
  1187. if( is_array($QPTA["{$name}-{$type}-{$amount}"]) && array_key_exists("{$name}-{$type}-{$amount}" , $QPTA)) {
  1188. $speed['has'] = 1;
  1189. }else{
  1190. $speed['has'] = 0;
  1191. }
  1192. $result[] = $speed;
  1193. }
  1194. }
  1195. $datas[$pid] = $result;
  1196. }
  1197. if(empty($datas)) {
  1198. showMessage('暂无相关通道配置', 'index.php?act=merchant&op=provider');
  1199. }
  1200. Tpl::output('form', $formData);
  1201. Tpl::output('datas', $datas);
  1202. Tpl::output('providers', $providers);
  1203. Tpl::showpage('provider.amount.control');
  1204. }
  1205. }
  1206. private function speed_key($type,$form)
  1207. {
  1208. if ($type == 1) {
  1209. $rkey = "channel-ctl-oil-{$form}-limit";
  1210. }elseif ($type == 2) {
  1211. $rkey = "channel-ctl-phone-{$form}-limit";
  1212. }else {
  1213. showMessage('类型有误', "index.php?act=merchant&op=provider");
  1214. }
  1215. Log::record($rkey , Log::DEBUG);
  1216. return $rkey;
  1217. }
  1218. private function formData($type , $form)
  1219. {
  1220. global $config;
  1221. $texts = [
  1222. ["中石油" , '中石化'],['移动' , '联通' , '电信']
  1223. ];
  1224. $type_data = [[1,2] , [4,5,6]];
  1225. $type_text = $texts[$type-1];
  1226. $type_data = $type_data[$type-1];
  1227. $amounts = [];
  1228. if($type == 1) {
  1229. $amounts = $config['refill_oil_specs'];
  1230. $cfgs = $config['oil_providers'];
  1231. }
  1232. if($type == 2) {
  1233. $amounts = $config['refill_phone_specs'];
  1234. $cfgs = $config['phone_providers'];
  1235. }
  1236. foreach ($type_text as $key => $value)
  1237. {
  1238. foreach ($amounts as $amount)
  1239. {
  1240. $data['text'] = $value . '-' . $amount . '元';
  1241. $data['type'] = $type_data[$key];
  1242. $data['amount'] = $amount;
  1243. $amountData[] = $data;
  1244. }
  1245. }
  1246. foreach ($cfgs as $cfg) {
  1247. $pcfgs[$cfg['name']] = $cfg;
  1248. }
  1249. return [ 'amountData' => $amountData , 'form' => $form , 'amounts' => $amounts , 'types' => $type_data , 'cfgs' => $pcfgs];
  1250. }
  1251. private function scard_type(int $card_type)
  1252. {
  1253. if ($card_type == 1) { //中石油
  1254. return '中石油';
  1255. } elseif ($card_type == 2) { //中石化
  1256. return '中石化';
  1257. } elseif ($card_type == 4) { //中国移动
  1258. return '中国移动';
  1259. } elseif ($card_type == 5) { //中国联通
  1260. return '中国联通';
  1261. } elseif ($card_type == 6) { //中国电信
  1262. return '中国电信';
  1263. } else {
  1264. return 'unknown';
  1265. }
  1266. }
  1267. private function elapse_time($seconds)
  1268. {
  1269. $minutes = intval($seconds / 60);
  1270. $second = intval($seconds % 60);
  1271. if($minutes >= 60) {
  1272. $minute = $minutes % 60;
  1273. $hours = intval($minutes / 60);
  1274. $result = "{$minute}m{$second}s";
  1275. }
  1276. elseif($minutes > 0) {
  1277. $result = "{$minutes}m{$second}s";
  1278. } else {
  1279. $result = "{$second}s";
  1280. }
  1281. if(isset($hours))
  1282. {
  1283. $result = "{$hours}h{$minute}m";
  1284. }
  1285. return $result;
  1286. }
  1287. private function refill_stat($condition , $times)
  1288. {
  1289. $stat_order = function ($condition) {
  1290. $stat = Model('')->table('refill_order,vr_order')->join('inner')
  1291. ->on('refill_order.order_id=vr_order.order_id')
  1292. ->field('count(*) as order_count ')
  1293. ->where($condition)->find();
  1294. return $stat['order_count'];
  1295. };
  1296. $result = [];
  1297. $condition['order_state'] = ORDER_STATE_SEND;
  1298. foreach ($times as $time) {
  1299. $condition['refill_order.order_time'] = $time;
  1300. $result[] = $stat_order($condition);
  1301. }
  1302. return $result;
  1303. }
  1304. private function all_order_state_stat($condition)
  1305. {
  1306. $counts = Model('')->table('refill_order,vr_order')->join('inner')
  1307. ->on('refill_order.order_id=vr_order.order_id')
  1308. ->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')
  1309. ->where($condition)
  1310. ->group('order_state')
  1311. ->select();
  1312. $all = [];
  1313. $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
  1314. $sending = $success = $cancel = $data;
  1315. foreach ($counts as $count)
  1316. {
  1317. if ($count['order_state'] == ORDER_STATE_SEND) {
  1318. $sending = $count;
  1319. } elseif ($count['order_state'] == ORDER_STATE_SUCCESS) {
  1320. $success = $count;
  1321. } elseif ($count['order_state'] == ORDER_STATE_CANCEL) {
  1322. $cancel = $count;
  1323. }
  1324. $all['order_count'] += $count['order_count'];
  1325. $all['refill_amounts'] += ncPriceFormat($count['refill_amounts']);
  1326. $all['channel_amounts'] += ncPriceFormat($count['channel_amounts']);
  1327. $all['mch_amounts'] += ncPriceFormat($count['mch_amounts']);
  1328. }
  1329. return ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
  1330. }
  1331. public function OrderSendListOp()
  1332. {
  1333. $model_refill_order = Model('refill_order');
  1334. $condition['refill_order.inner_status'] = 0;
  1335. $condition['vr_order.order_state'] = ORDER_STATE_SEND;
  1336. $condition['refill_order.order_time'] = ['lt', (time() - 1800)];
  1337. $merchant_list = Model('')->table('merchant')->limit(1000)->select();
  1338. foreach ($merchant_list as $key =>$value) {
  1339. $merchants[$value['mchid']] = $value;
  1340. }
  1341. $order_list = $model_refill_order->getMerchantOrderList($condition, 1000, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
  1342. $stat = Model('')->table('refill_order,vr_order')->join('inner')
  1343. ->on('refill_order.order_id=vr_order.order_id')
  1344. ->field('count(*) as order_count ,sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts')
  1345. ->where($condition)->find();
  1346. $count = $this->refill_stat($condition ,
  1347. [['between', [(time() - 3600) , (time() - 1800)]],
  1348. ['lt', (time() - 3600)]]
  1349. );
  1350. foreach ($order_list as $order_id => $order_info) {
  1351. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  1352. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  1353. $order_list[$order_id]['diff_time_text'] = $this->elapse_time(time() - $order_info['order_time']);
  1354. $order_list[$order_id]['diff_time'] = time() - $order_info['order_time'];
  1355. }
  1356. $provider_list = Model('')->table('refill_provider')->limit(100)->select();
  1357. Tpl::output('stat', $stat);
  1358. Tpl::output('count', $count);
  1359. Tpl::output('order_list', $order_list);
  1360. Tpl::output('provider_list', $provider_list);
  1361. Tpl::output('show_page', $model_refill_order->showpage());
  1362. Tpl::showpage('refill.order.send.index');
  1363. }
  1364. public function OrderStatsOp()
  1365. {
  1366. $type = $_GET['type'] ? $_GET['type'] : 'system';
  1367. $page = "{$type}.order.stats";
  1368. $model_refill_order = Model('refill_order');
  1369. $condition['type'] = $type;
  1370. if(!empty($_GET['cid'])) {
  1371. $condition['cid'] = $_GET['cid'];
  1372. }
  1373. $start_unixtime = intval(strtotime($_GET['query_start_time']));
  1374. $end_unixtime = intval(strtotime($_GET['query_end_time']));
  1375. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  1376. $condition['time_stamp'] = [ ['egt', $start_unixtime] , ['lt', $end_unixtime] , 'and'];
  1377. }
  1378. elseif ($start_unixtime > 0) {
  1379. $condition['time_stamp'] = ['egt', $start_unixtime];
  1380. }
  1381. elseif ($end_unixtime > 0) {
  1382. $condition['time_stamp'] = ['lt', $end_unixtime];
  1383. }
  1384. $stats_list = $model_refill_order->getOrderStatsList($condition);
  1385. if($type == 'provider') {
  1386. $provider_list = Model('')->table('refill_provider,store')->field('refill_provider.store_id,store.store_name')->join('inner')
  1387. ->on('store.store_id=refill_provider.store_id')->limit(100)->select();
  1388. Tpl::output('provider_list', $provider_list);
  1389. }elseif ($type == 'merchant') {
  1390. $merchant_list = Model('')->table('merchant')->limit(100)->select();
  1391. Tpl::output('merchant_list', $merchant_list);
  1392. }
  1393. Tpl::output('stats_list', $stats_list);
  1394. Tpl::output('show_page', $model_refill_order->showpage());
  1395. Tpl::showpage($page);
  1396. }
  1397. }