|
@@ -0,0 +1,155 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace refill;
|
|
|
+
|
|
|
+use mtopcard;
|
|
|
+use trans_wapper;
|
|
|
+use Exception;
|
|
|
+use Log;
|
|
|
+
|
|
|
+class chprice_helper
|
|
|
+{
|
|
|
+ public function update_from_file()
|
|
|
+ {
|
|
|
+ include(BASE_CONFIG_PATH . CONFIG_PREFIX . '/refill.ini.php');
|
|
|
+ global $config;
|
|
|
+
|
|
|
+ $oils = $config['oil_providers'];
|
|
|
+ $phones = $config['phone_providers'];
|
|
|
+ $this->update_db($oils,mtopcard\OilCardPaper);
|
|
|
+ $this->update_db($phones,mtopcard\PhoneCardPaper);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function update_db($providers,$type)
|
|
|
+ {
|
|
|
+ $updater = function($name,$cfg,$type)
|
|
|
+ {
|
|
|
+ $json_cfg = json_encode($cfg);
|
|
|
+ $file_hash = md5($json_cfg);
|
|
|
+
|
|
|
+ $mod_price = Model('provider_price');
|
|
|
+ $store_id = intval($cfg['store_id']);
|
|
|
+ $item = $mod_price->getPirce(['provider_name' => $name,'product_type' => $type]);
|
|
|
+
|
|
|
+ if(empty($item)) {
|
|
|
+ $ret = $mod_price->insert(['product_type' => $type, 'provider_name' => $name, 'store_id' => $store_id,
|
|
|
+ 'cfg' => $json_cfg, 'cfg_hash' => $file_hash, 'operator' => 'system', 'update_time' => time()]);
|
|
|
+ }
|
|
|
+ elseif($item['cfg_hash'] != $file_hash)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ $tran = new trans_wapper($mod_price,'notify change order state trans');
|
|
|
+
|
|
|
+ $item = $mod_price->getPirce(['provider_name' => $name,'product_type' => $type],'*',true,true);
|
|
|
+ $effect_time = intval($item['effect_time']);
|
|
|
+ if($effect_time > 0)
|
|
|
+ {
|
|
|
+ $ret = $mod_price->where(['price_id' => $item['price_id']])
|
|
|
+ ->update(['new_cfg' => $json_cfg, 'cfg_hash' => $file_hash, 'operator' => 'system', 'update_time' => time()]);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ $ret = $mod_price->where(['price_id' => $item['price_id']])
|
|
|
+ ->update(['cfg' => $json_cfg, 'cfg_hash' => $file_hash, 'operator' => 'system', 'update_time' => time()]);
|
|
|
+ }
|
|
|
+ $tran->commit();
|
|
|
+ }
|
|
|
+ catch (Exception $ex)
|
|
|
+ {
|
|
|
+ $tran->rollback();
|
|
|
+ Log::record($ex->getMessage(),Log::ERR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $ret = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $ret;
|
|
|
+ };
|
|
|
+
|
|
|
+ foreach ($providers as $provider) {
|
|
|
+ $name = $provider['name'];
|
|
|
+ $cfg = $provider['cfg'];
|
|
|
+
|
|
|
+ if(!empty($cfg)) {
|
|
|
+ $updater($name,$cfg,$type);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function update($price_id,$cfgs,$effect_time)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ $json_cfg = json_encode($cfgs);
|
|
|
+
|
|
|
+ $mod_price = Model('provider_price');
|
|
|
+ $tran = new trans_wapper($mod_price,'notify change order state trans');
|
|
|
+
|
|
|
+ $item = $mod_price->getPirce(['price_id' => $price_id], '*', true, true);
|
|
|
+ if ($effect_time > 0) {
|
|
|
+ $ret = $mod_price->where(['price_id' => $item['price_id']])
|
|
|
+ ->update(['new_cfg' => $json_cfg, 'operator' => 'system', 'effect_time' => $effect_time, 'update_time' => time()]);
|
|
|
+ } else {
|
|
|
+ $ret = $mod_price->where(['price_id' => $item['price_id']])
|
|
|
+ ->update(['cfg' => $json_cfg, 'new_cfg' => '', 'effect_time' => 0, 'operator' => 'system', 'update_time' => time()]);
|
|
|
+ }
|
|
|
+ $tran->commit();
|
|
|
+ }
|
|
|
+ catch (Exception $ex)
|
|
|
+ {
|
|
|
+ $tran->rollback();
|
|
|
+ Log::record($ex->getMessage(),Log::ERR);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function effect($price_id)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ $mod_price = Model('provider_price');
|
|
|
+ $tran = new trans_wapper($mod_price,'notify change order state trans');
|
|
|
+
|
|
|
+ $item = $mod_price->getPirce(['price_id' => $price_id], '*', true, true);
|
|
|
+ $effect_time = intval($item['effect_time']);
|
|
|
+
|
|
|
+ if ($effect_time > 0) {
|
|
|
+ $ret = $mod_price->where(['price_id' => $item['price_id']])
|
|
|
+ ->update(['cfg' => ['exp', 'new_cfg'], 'new_cfg' => '', 'effect_time' => 0]);
|
|
|
+ }
|
|
|
+ $tran->commit();
|
|
|
+ }
|
|
|
+ catch (Exception $ex)
|
|
|
+ {
|
|
|
+ $tran->rollback();
|
|
|
+ Log::record($ex->getMessage(),Log::ERR);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function load_type($type)
|
|
|
+ {
|
|
|
+ $mod_price = Model('provider_price');
|
|
|
+ $items = $mod_price->getPirces(['product_type' => $type,'opened' => 1],'provider_name,cfg');
|
|
|
+
|
|
|
+ $result = [];
|
|
|
+ foreach ($items as $item) {
|
|
|
+ $result[] = ['name' => $item['provider_name'],'cfg' => json_decode($item['cfg'],true)];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function load_from_db()
|
|
|
+ {
|
|
|
+ include(BASE_CONFIG_PATH . CONFIG_PREFIX . '/refill.ini.php');
|
|
|
+
|
|
|
+ global $config;
|
|
|
+ $config['oil_providers'] = $this->load_type(mtopcard\OilCardPaper);
|
|
|
+ $config['phone_providers'] = $this->load_type(mtopcard\PhoneCardPaper);
|
|
|
+ }
|
|
|
+}
|