| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- namespace addons\StarChain\common\models;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%addons_star_chain_setting}}".
- *
- * @property int $id
- * @property int|null $mall_id
- * @property string|null $app_key
- * @property string|null $app_secret
- * @property string|null $auto_update 自动更新策略
- * @property string|null $sell 售价 定价策略
- * @property string|null $cost 成本价 定价策略
- * @property string|null $original 原价 定价策略
- * @property int|null $updated_at
- * @property int|null $created_at
- */
- class Setting extends BaseModel
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_star_chain_setting}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'updated_at', 'created_at'], 'integer'],
- [['auto_update', 'sell', 'cost', 'original'], 'string'],
- [['app_key', 'app_secret'], 'string', 'max' => 64],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'app_key' => 'App Key',
- 'app_secret' => 'App Secret',
- 'auto_update' => 'Auto Update',
- 'sell' => 'Sell',
- 'cost' => 'Cost',
- 'original' => 'Original',
- 'updated_at' => 'Updated At',
- 'created_at' => 'Created At',
- ];
- }
-
- /**
- * beforeSave
- *
- * @param boolean $insert
- * @return boolean
- */
- public function beforeSave($insert)
- {
- if (parent::beforeSave($insert)) {
- if (!$insert) {
- // 添加修改日志
- return SettingLog::addSettingLog($this);
- }
- return true;
- }
- }
- /**
- * 保存设置数据
- *
- * @param array $data
- * @return boolean
- */
- public static function setSetting($data)
- {
- $mall_id = $data['mall_id'];
- $setting = self::find()->where(['mall_id' => $mall_id])->one();
- if (empty($setting)) {
- $setting = new self();
- }
-
- if ($setting->load($data, '') && $setting->save()) {
- // 记录修改日志
- return true;
- }
- $errors = $setting->getFirstErrors();
- throw new Exception(
- $errors[array_key_first($errors)]
- );
- }
- /**
- * 获取设置
- *
- * @param integer $mall_id
- * @return array
- */
- public static function getSetting($mall_id)
- {
- $setting = self::find()->where(['mall_id' => $mall_id])->cache(1)->asArray()->one();
- if (empty($setting)) {
- return [];
- }
- return self::getSettingData($setting);
- }
- /**
- * 获取格式化设置数据
- *
- * @param array $data
- * @return array
- */
- private static function getSettingData($data)
- {
- if (empty($data)) return [];
- $app_secret = $data['app_secret'];
- $app_key = $data['app_key'];
- $sell = Json::decode($data['sell']);
- $cost = Json::decode($data['cost']);
- $original = Json::decode($data['original']);
- $auto_update = Json::decode($data['auto_update']);
- return array_merge(
- compact('app_secret', 'app_key'),
- $sell,
- $cost,
- $original,
- $auto_update,
- );
- }
- /**
- * 获取导入商品时候使用的设置
- *
- * @param integer $mall_id
- * @return array
- */
- public static function getImportSetting($mall_id)
- {
- $setting = self::find()->where(['mall_id' => $mall_id])->cache(5)->asArray()->one();
- if (empty($setting)) {
- return [];
- }
- return self::getImportSettingData($setting);
- }
- /**
- * 获取导入商品时候使用的设置数据格式化
- *
- * @param array $data
- * @return array
- */
- private static function getImportSettingData($data)
- {
- if (empty($data)) return [];
- $sell = Json::decode($data['sell']);
- $cost = Json::decode($data['cost']);
- $original = Json::decode($data['original']);
- $auto_update = Json::decode($data['auto_update']);
- $setting = [];
- // 售价
- switch ($sell['sell_strategy']) {
- // 官方销售价
- case '1':
- $setting['sell_name'] = 'officialDistriPrice';
- $setting['sell_value'] = $sell['sell_guide'];
- break;
- // 采购价
- case '2':
- $setting['sell_name'] = 'basePrice';
- $setting['sell_value'] = $sell['sell_sales'];
- break;
- // 建议零售价
- case '3':
- $setting['sell_name'] = 'suggestPrice';
- $setting['sell_value'] = $sell['sell_market'];
- break;
- // 默认100
- default:
- $setting['sell_name'] = 'officialDistriPrice';
- $setting['sell_value'] = 100;
- break;
- }
- // 成本价
- switch ($cost['cost_strategy']) {
- // 官方销售价
- case '1':
- $setting['cost_name'] = 'officialDistriPrice';
- $setting['cost_value'] = $cost['cost_guide'];
- break;
- // 采购价
- case '2':
- $setting['cost_name'] = 'basePrice';
- $setting['cost_value'] = $cost['cost_sales'];
- break;
- // 建议零售价
- case '3':
- $setting['cost_name'] = 'suggestPrice';
- $setting['cost_value'] = $cost['cost_market'];
- break;
- // 默认100
- default:
- $setting['cost_name'] = 'officialDistriPrice';
- $setting['cost_value'] = 100;
- break;
- }
- // 原价
- switch ($original['original_strategy']) {
- // 官方销售价
- case '1':
- $setting['original_name'] = 'officialDistriPrice';
- $setting['original_value'] = $original['original_guide'];
- break;
- // 采购价
- case '2':
- $setting['original_name'] = 'basePrice';
- $setting['original_value'] = $original['original_sales'];
- break;
- // 建议零售价
- case '3':
- $setting['original_name'] = 'suggestPrice';
- $setting['original_value'] = $original['original_market'];
- break;
- // 默认100
- default:
- $setting['original_name'] = 'officialDistriPrice';
- $setting['original_value'] = 100;
- break;
- }
- return array_merge(
- $setting,
- $auto_update
- );
- }
- }
|