Setting.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace addons\StarChain\common\models;
  3. use Exception;
  4. use Yii;
  5. use yii\helpers\Json;
  6. /**
  7. * This is the model class for table "{{%addons_star_chain_setting}}".
  8. *
  9. * @property int $id
  10. * @property int|null $mall_id
  11. * @property string|null $app_key
  12. * @property string|null $app_secret
  13. * @property string|null $auto_update 自动更新策略
  14. * @property string|null $sell 售价 定价策略
  15. * @property string|null $cost 成本价 定价策略
  16. * @property string|null $original 原价 定价策略
  17. * @property int|null $updated_at
  18. * @property int|null $created_at
  19. */
  20. class Setting extends BaseModel
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%addons_star_chain_setting}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'updated_at', 'created_at'], 'integer'],
  36. [['auto_update', 'sell', 'cost', 'original'], 'string'],
  37. [['app_key', 'app_secret'], 'string', 'max' => 64],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => 'Mall ID',
  48. 'app_key' => 'App Key',
  49. 'app_secret' => 'App Secret',
  50. 'auto_update' => 'Auto Update',
  51. 'sell' => 'Sell',
  52. 'cost' => 'Cost',
  53. 'original' => 'Original',
  54. 'updated_at' => 'Updated At',
  55. 'created_at' => 'Created At',
  56. ];
  57. }
  58. /**
  59. * beforeSave
  60. *
  61. * @param boolean $insert
  62. * @return boolean
  63. */
  64. public function beforeSave($insert)
  65. {
  66. if (parent::beforeSave($insert)) {
  67. if (!$insert) {
  68. // 添加修改日志
  69. return SettingLog::addSettingLog($this);
  70. }
  71. return true;
  72. }
  73. }
  74. /**
  75. * 保存设置数据
  76. *
  77. * @param array $data
  78. * @return boolean
  79. */
  80. public static function setSetting($data)
  81. {
  82. $mall_id = $data['mall_id'];
  83. $setting = self::find()->where(['mall_id' => $mall_id])->one();
  84. if (empty($setting)) {
  85. $setting = new self();
  86. }
  87. if ($setting->load($data, '') && $setting->save()) {
  88. // 记录修改日志
  89. return true;
  90. }
  91. $errors = $setting->getFirstErrors();
  92. throw new Exception(
  93. $errors[array_key_first($errors)]
  94. );
  95. }
  96. /**
  97. * 获取设置
  98. *
  99. * @param integer $mall_id
  100. * @return array
  101. */
  102. public static function getSetting($mall_id)
  103. {
  104. $setting = self::find()->where(['mall_id' => $mall_id])->cache(1)->asArray()->one();
  105. if (empty($setting)) {
  106. return [];
  107. }
  108. return self::getSettingData($setting);
  109. }
  110. /**
  111. * 获取格式化设置数据
  112. *
  113. * @param array $data
  114. * @return array
  115. */
  116. private static function getSettingData($data)
  117. {
  118. if (empty($data)) return [];
  119. $app_secret = $data['app_secret'];
  120. $app_key = $data['app_key'];
  121. $sell = Json::decode($data['sell']);
  122. $cost = Json::decode($data['cost']);
  123. $original = Json::decode($data['original']);
  124. $auto_update = Json::decode($data['auto_update']);
  125. return array_merge(
  126. compact('app_secret', 'app_key'),
  127. $sell,
  128. $cost,
  129. $original,
  130. $auto_update,
  131. );
  132. }
  133. /**
  134. * 获取导入商品时候使用的设置
  135. *
  136. * @param integer $mall_id
  137. * @return array
  138. */
  139. public static function getImportSetting($mall_id)
  140. {
  141. $setting = self::find()->where(['mall_id' => $mall_id])->cache(5)->asArray()->one();
  142. if (empty($setting)) {
  143. return [];
  144. }
  145. return self::getImportSettingData($setting);
  146. }
  147. /**
  148. * 获取导入商品时候使用的设置数据格式化
  149. *
  150. * @param array $data
  151. * @return array
  152. */
  153. private static function getImportSettingData($data)
  154. {
  155. if (empty($data)) return [];
  156. $sell = Json::decode($data['sell']);
  157. $cost = Json::decode($data['cost']);
  158. $original = Json::decode($data['original']);
  159. $auto_update = Json::decode($data['auto_update']);
  160. $setting = [];
  161. // 售价
  162. switch ($sell['sell_strategy']) {
  163. // 官方销售价
  164. case '1':
  165. $setting['sell_name'] = 'officialDistriPrice';
  166. $setting['sell_value'] = $sell['sell_guide'];
  167. break;
  168. // 采购价
  169. case '2':
  170. $setting['sell_name'] = 'basePrice';
  171. $setting['sell_value'] = $sell['sell_sales'];
  172. break;
  173. // 建议零售价
  174. case '3':
  175. $setting['sell_name'] = 'suggestPrice';
  176. $setting['sell_value'] = $sell['sell_market'];
  177. break;
  178. // 默认100
  179. default:
  180. $setting['sell_name'] = 'officialDistriPrice';
  181. $setting['sell_value'] = 100;
  182. break;
  183. }
  184. // 成本价
  185. switch ($cost['cost_strategy']) {
  186. // 官方销售价
  187. case '1':
  188. $setting['cost_name'] = 'officialDistriPrice';
  189. $setting['cost_value'] = $cost['cost_guide'];
  190. break;
  191. // 采购价
  192. case '2':
  193. $setting['cost_name'] = 'basePrice';
  194. $setting['cost_value'] = $cost['cost_sales'];
  195. break;
  196. // 建议零售价
  197. case '3':
  198. $setting['cost_name'] = 'suggestPrice';
  199. $setting['cost_value'] = $cost['cost_market'];
  200. break;
  201. // 默认100
  202. default:
  203. $setting['cost_name'] = 'officialDistriPrice';
  204. $setting['cost_value'] = 100;
  205. break;
  206. }
  207. // 原价
  208. switch ($original['original_strategy']) {
  209. // 官方销售价
  210. case '1':
  211. $setting['original_name'] = 'officialDistriPrice';
  212. $setting['original_value'] = $original['original_guide'];
  213. break;
  214. // 采购价
  215. case '2':
  216. $setting['original_name'] = 'basePrice';
  217. $setting['original_value'] = $original['original_sales'];
  218. break;
  219. // 建议零售价
  220. case '3':
  221. $setting['original_name'] = 'suggestPrice';
  222. $setting['original_value'] = $original['original_market'];
  223. break;
  224. // 默认100
  225. default:
  226. $setting['original_name'] = 'officialDistriPrice';
  227. $setting['original_value'] = 100;
  228. break;
  229. }
  230. return array_merge(
  231. $setting,
  232. $auto_update
  233. );
  234. }
  235. }