|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- /**
- *
- * ============================================================================
- * * 版权所有 蜘蛛出行 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 倪宗锋
- * PhpStorm SynchroController.php
- * Create By 2017/6/26 17:26 $
- */
-
- namespace console\controllers;
-
-
- use common\models\BaseArea;
- use common\models\ProdCategory;
- use common\util\CurlInterface;
- use common\util\Util;
- use console\models\Synchro;
- use yii\base\Exception;
- use yii\console\Controller;
-
- class SynchroController extends Controller
- {
- public $log = '';
- public $service = null;
-
- public function service()
- {
- if ($this->service == null) {
- $this->service = new Synchro();
- }
- return $this->service;
- }
-
- /**
- * Des:更新baseArea
- * Name: actionUpBaseArea
- * @return string
- * @author 倪宗锋
- */
- public function actionUpBaseArea()
- {
- $this->log .= ' 更新base_area: ';
- //1.cs查票种信息,存缓存
- $time = time();
- $siteConfig = Util::getSiteConfig();
- $send_data = [
- 'auth_code' => Util::authCodeForCs($time),
- 'user_key' => $siteConfig['adm_user_key'],
- 'user' => $siteConfig['adm_user'],
- 'user_id' => $siteConfig['adm_user_id'],
- 'request_time' => $time,
- 'action' => 'up_base_area',
- 'org_id' => $siteConfig['adm_source_id']
- ];
- $curlInterface = new CurlInterface($send_data, 4);
- $getList = $curlInterface->execute($siteConfig['adm_host1'] . '/api/nfx', 'POST');
- if (!isset($getList['code']) || $getList['code'] != 0 || count($getList['list']) == 0) {
- $this->log .= '获取base_area数据失败';
- return '获取base_area数据失败';
- }
- $baseArea_1 = new BaseArea();
- $transaction = \Yii::$app->db->beginTransaction();
- try {
- $baseArea_1::deleteAll();
- $this->log .= ' 保存失败的记录:';
- foreach ($getList['list'] as $value) {
- echo '-';
- $baseArea = clone $baseArea_1;
- $baseArea->isNewRecord = true;
- $baseArea->setAttributes($value);
- $res = $baseArea->save();
- if ($res === false) {
- $this->log .= $value['ID'];
- continue;
- }
- }
- $transaction->commit();
- } catch (Exception $e) {
- $this->log .= ' 保存失败';
- $transaction->rollBack();
- return ' 保存失败';
- }
- $this->log .= ' 更新成功';
- return 'OK';
- }
-
- /**
- * Des:更新巴士自由行产品数据
- * Name: actionUpFreeWalker
- * @return string
- * @author 倪宗锋
- */
- public function actionUpFreeWalker()
- {
- $this->log .= '更新巴士自由行 ';
- $ProdCategory = new ProdCategory();
- //1.查当前数据库的未删除的巴士自由行产品的产品ID数组
- $localProdSign = $ProdCategory->getFreeWalkSigns();
- //2.cs查询巴士自由行产品数据
- $getCsProdArr = $this->service()->getCsFreeWalk();
- if ($getCsProdArr['flag'] == false) {
- $this->log .= $getCsProdArr['msg'];
- return Util::returnJsEr($getCsProdArr['msg']);
- }
- $csProdArr = $getCsProdArr['data'];
- $addProd = $this->service()->addFreeWalk($csProdArr, array_keys($localProdSign));
- $this->log .= "添加产品:成功-{$addProd['data']['success']}、失败-{$addProd['data']['fail']}";
- $updateProd = $this->service()->updateFreeWalk($csProdArr, $localProdSign);
- $this->log .= "更新产品:成功-{$updateProd['data']['success']}、失败-{$updateProd['data']['fail']}";
- $deleteProd = $this->service()->deleteFreeWalk($csProdArr, array_keys($localProdSign));
- if ($deleteProd['flag'] == false) {
- $this->log .= "删除产品:失败-{$addProd['data']['success']}}";
- } else {
- $this->log .= "删除产品:成功-{$addProd['data']['success']}}";
- }
- return Util::returnJsSu();
- }
-
- /**
- * 结束时更新日志
- */
- public function __destruct()
- {
- $logUrl = ROOT_PATH . '/console/log/synchro/' . date("Y-m-d") . '.log';
- $msg = date('Y-m-d H:i:s') . $this->log;
- file_put_contents($logUrl, $msg . PHP_EOL, FILE_APPEND);
- }
- }
|