123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- require_once(BASE_CONFIG_PATH . CONFIG_PREFIX . '/refill.ini.php');
- class providerControl extends SystemControl
- {
- public function __construct()
- {
- parent::__construct();
- }
- public function indexOp()
- {
- $this->sync_cfgs();
- $provider_model = Model('refill_provider');
- $condition = [];
- if (trim($_GET['name']) != '') {
- $condition['store_name'] = ['like', '%' . $_GET['name'] . '%'];
- Tpl::output('name', $_GET['name']);
- }
- if (in_array($_GET['type'], [1, 2])) {
- $condition['type'] = $_GET['type'];
- }
- $provider_list = $provider_model->table('refill_provider,store')
- ->field('refill_provider.*,store.store_name')
- ->join('inner')
- ->on('store.store_id=refill_provider.store_id')
- ->where($condition)
- ->order('opened asc, name asc')
- ->page(10)
- ->select();
- foreach ($provider_list as $key => $provider) {
- if (!empty($provider['start_period']) && !empty($provider['end_period'])) {
- $provider_list[$key]['period'] = $provider['start_period'] . '~' . $provider['end_period'];
- } else {
- $provider_list[$key]['period'] = '全时间段';
- }
- }
- $opened_text = ['使用中', '已禁用'];
- $type_text = ['油卡', '手机充值卡', '增值业务'];
- Tpl::output('opened_text', $opened_text);
- Tpl::output('type_text', $type_text);
- Tpl::output('provider_list', $provider_list);
- Tpl::output('show_page', $provider_model->showpage());
- Tpl::showpage('provider.index');
- }
- public function sync_cfgs()
- {
- $name_val = function ($items) {
- $result = [];
- foreach ($items as $item) {
- $name = $item['name'];
- $result[$name] = $item;
- }
- return $result;
- };
- $match = function ($all, $cur) {
- $inserts = $updates = [];
- foreach ($all as $key => $value) {
- if (!array_key_exists($key, $cur)) {
- $insert['name'] = $key;
- $insert['store_id'] = $value['cfg']['store_id'];
- $insert['qualitys'] = $value['cfg']['qualitys'];
- $inserts[] = $insert;
- } elseif ($value['cfg']['qualitys'] != $cur[$key]['qualitys']) {
- $update['provider_id'] = $cur[$key]['provider_id'];
- $update['qualitys'] = $value['cfg']['qualitys'];
- $updates[] = $update;
- }
- }
- return [$inserts, $updates];
- };
- $updater = function ($mod, $updates) {
- if (empty($updates)) return;
- foreach ($updates as $update) {
- $provider_id = $update['provider_id'];
- $data = ['qualitys' => $update['qualitys']];
- $mod->editProvider($data, ['provider_id' => $provider_id]);
- }
- };
- $inserter = function ($mod, $type, $names) {
- foreach ($names as $name) {
- $data = ['name' => $name['name'], 'type' => $type, 'store_id' => $name['store_id'], 'qualitys' => $name['qualitys'], 'opened' => 2];
- $mod->insert($data);
- }
- };
- global $config;
- $oil_configs = $config['oil_providers'];
- $pho_configs = $config['phone_providers'];
- $third_configs = $config['third_providers'];
- $oils = $name_val($oil_configs);
- $phones = $name_val($pho_configs);
- $third = $name_val($third_configs);
- $mod_prov = Model('refill_provider');
- $oil_items = $mod_prov->getProviderList(['type' => 1]);
- $oil_items = $name_val($oil_items);
- [$oil_inserts, $oil_updates] = $match($oils, $oil_items);
- $phone_items = $mod_prov->getProviderList(['type' => 2]);
- $phone_items = $name_val($phone_items);
- [$phone_inserts, $phone_updates] = $match($phones, $phone_items);
- $third_items = $mod_prov->getProviderList(['type' => 3]);
- $third_items = $name_val($third_items);
- [$third_inserts, $third_updates] = $match($third, $third_items);
- $inserter($mod_prov, 1, $oil_inserts);
- $inserter($mod_prov, 2, $phone_inserts);
- $inserter($mod_prov, 3, $third_inserts);
- $updater($mod_prov, $oil_updates);
- $updater($mod_prov, $phone_updates);
- $updater($mod_prov, $third_updates);
- }
- }
|