StoreBaseConfig.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace common\local\local_services;
  10. use Yii;
  11. /**
  12. * Cart services. 购物车service, 执行购物车部分对应的方法。
  13. *
  14. * @property \fecshop\services\cart\Coupon $coupon coupon sub-service of cart
  15. * @property \fecshop\services\cart\Info $info info sub-service of cart
  16. * @property \fecshop\services\cart\Quote $quote quote sub-service of cart
  17. * @property \fecshop\services\cart\QuoteItem $quoteItem quoteItem sub-service of cart
  18. *
  19. * @method getCartInfo($activeProduct, $shippingMethod = '', $country = '', $region = '*') see [[\fecshop\services\Cart::actionGetCartInfo()]]
  20. * @method mergeCartAfterUserLogin() see [[\fecshop\services\Cart::actionmergeCartAfterUserLogin()]]
  21. *
  22. * @author Terry Zhao <2358269014@qq.com>
  23. * @since 1.0
  24. */
  25. class StoreBaseConfig extends \fecshop\services\Service
  26. {
  27. public $numPerPage = 20;
  28. protected $_modelName = '\common\local\local_models\mysqldb\StoreBaseConfig';
  29. protected $_model;
  30. //protected $_serilizeAttr = [
  31. // "service_db",
  32. //];
  33. public function init()
  34. {
  35. parent::init();
  36. list($this->_modelName, $this->_model) = Yii::mapGet($this->_modelName);
  37. }
  38. public function getPrimaryKey()
  39. {
  40. return 'id';
  41. }
  42. public function getByPrimaryKey($primaryKey)
  43. {
  44. if ($primaryKey) {
  45. $one = $this->_model->findOne($primaryKey);
  46. return $one;
  47. } else {
  48. return new $this->_modelName();
  49. }
  50. }
  51. public function getByKey($key)
  52. {
  53. if ($key) {
  54. $one = $this->_model->findOne(['key' => $key]);
  55. return $one;
  56. }
  57. return null;
  58. }
  59. /*
  60. * example filter:
  61. * [
  62. * 'numPerPage' => 20,
  63. * 'pageNum' => 1,
  64. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  65. 'where' => [
  66. ['>','price',1],
  67. ['<=','price',10]
  68. * ['sku' => 'uk10001'],
  69. * ],
  70. * 'asArray' => true,
  71. * ]
  72. */
  73. public function coll($filter = '')
  74. {
  75. $query = $this->_model->find();
  76. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  77. $coll = $query->all();
  78. //if (!empty($coll)) {
  79. // foreach ($coll as $k => $one) {
  80. //if (in_array($one['key'], $this->_serilizeAttr)) {
  81. // $one['key'] = unserialize($one)
  82. //}
  83. //foreach ($this->_lang_attr as $attr) {
  84. // $one[$attr] = $one[$attr] ? unserialize($one[$attr]) : '';
  85. //}
  86. $coll[$k] = $one;
  87. //}
  88. //}
  89. //var_dump($one);
  90. return [
  91. 'coll' => $coll,
  92. 'count'=> $query->limit(null)->offset(null)->count(),
  93. ];
  94. }
  95. public function getAllConfig()
  96. {
  97. $arr = [];
  98. $baseConfigs = $this->_model->find()->select(['key', 'value'])->asArray()->all();
  99. foreach ($baseConfigs as $one) {
  100. $arr[$one['key']] = unserialize($one['value']);
  101. }
  102. return $arr;
  103. }
  104. /**
  105. * @param $one|array
  106. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  107. */
  108. public function save($one)
  109. {
  110. $currentDateTime = \fec\helpers\CDate::getCurrentDateTime();
  111. $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
  112. if ($primaryVal) {
  113. $model = $this->_model->findOne($primaryVal);
  114. if (!$model) {
  115. Yii::$service->helper->errors->add('config {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]);
  116. return;
  117. }
  118. } else {
  119. $model = new $this->_modelName();
  120. $model->created_at = time();
  121. }
  122. $model->updated_at = time();
  123. $primaryKey = $this->getPrimaryKey();
  124. $model = Yii::$service->helper->ar->save($model, $one);
  125. $primaryVal = $model[$primaryKey];
  126. return true;
  127. }
  128. // 保存配置值
  129. public function saveConfig($one)
  130. {
  131. if (!$one['key'] || !$one['value']) {
  132. return false;
  133. }
  134. $model = $this->_model->findOne(['key' => $one['key']]);
  135. if (!$model['id']) {
  136. $model = new $this->_modelName();
  137. $model->created_at = time();
  138. $model->key = $one['key'];
  139. }
  140. if (is_array($one['value'])) {
  141. $model->value = serialize($one['value']);
  142. } else {
  143. $model->value = $one['value'];
  144. }
  145. $model->updated_at = time();
  146. return $model->save();
  147. }
  148. public function remove($ids)
  149. {
  150. if (!$ids) {
  151. Yii::$service->helper->errors->add('remove id is empty');
  152. return false;
  153. }
  154. if (is_array($ids) && !empty($ids)) {
  155. foreach ($ids as $id) {
  156. $model = $this->_model->findOne($id);
  157. $model->delete();
  158. }
  159. } else {
  160. $id = $ids;
  161. foreach ($ids as $id) {
  162. $model = $this->_model->findOne($id);
  163. $model->delete();
  164. }
  165. }
  166. return true;
  167. }
  168. }