StoreDomain.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 fecshop\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 StoreDomain extends Service
  26. {
  27. public $numPerPage = 20;
  28. protected $_modelName = '\fecshop\models\mysqldb\StoreDomain';
  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. return [
  79. 'coll' => $coll,
  80. 'count'=> $query->limit(null)->offset(null)->count(),
  81. ];
  82. }
  83. // 通过appName得到相应的store配置数组
  84. public function getCollByAppName($app_name)
  85. {
  86. $filter = [
  87. 'where' => [
  88. ['app_name' => $app_name, 'status' => 1,],
  89. ],
  90. 'fetchAll' => true,
  91. 'asArray' => true,
  92. ];
  93. $query = $this->_model->find();
  94. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  95. return $query->all();
  96. }
  97. /**
  98. * @param $one|array
  99. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  100. */
  101. public function save($one)
  102. {
  103. $currentDateTime = \fec\helpers\CDate::getCurrentDateTime();
  104. $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
  105. if ($primaryVal) {
  106. $model = $this->_model->findOne($primaryVal);
  107. if (!$model) {
  108. Yii::$service->helper->errors->add('config {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]);
  109. return;
  110. }
  111. } else {
  112. $model = new $this->_modelName();
  113. $model->created_at = time();
  114. }
  115. $model->updated_at = time();
  116. $primaryKey = $this->getPrimaryKey();
  117. $model = Yii::$service->helper->ar->save($model, $one);
  118. $primaryVal = $model[$primaryKey];
  119. return true;
  120. }
  121. public function remove($ids)
  122. {
  123. if (!$ids) {
  124. Yii::$service->helper->errors->add('remove id is empty');
  125. return false;
  126. }
  127. if (is_array($ids) && !empty($ids)) {
  128. foreach ($ids as $id) {
  129. $model = $this->_model->findOne($id);
  130. $model->delete();
  131. }
  132. } else {
  133. $id = $ids;
  134. $model = $this->_model->findOne($id);
  135. $model->delete();
  136. }
  137. return true;
  138. }
  139. }