123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?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,$operator='system')
- {
- 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' => $operator, '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' => $operator, '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()
- {
- global $config;
- $config['oil_providers'] = $this->load_type(mtopcard\OilCardPaper);
- $config['phone_providers'] = $this->load_type(mtopcard\PhoneCardPaper);
- }
- }
|