SpecsService.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace backend\modules\mall\services;
  3. use common\models\goods\GoodsSpecs;
  4. use yii\helpers\Json;
  5. /**
  6. * 规格服务类
  7. * SpecsService class
  8. * @package backend\modules\mall\services
  9. */
  10. class SpecsService extends BaseService
  11. {
  12. /**
  13. * StoreId
  14. *
  15. * @var integer
  16. */
  17. public $store_id = 0;
  18. /**
  19. * 获取分页列表
  20. *
  21. * @return array
  22. */
  23. public function getPageList($params = [], $store_id = 0)
  24. {
  25. $page = $this->getPage($params);
  26. $limit = $this->getLimit($params);
  27. // list处理
  28. $list_callback = function($list) {
  29. return array_map(function($item) {
  30. $item['values'] = Json::decode($item['values']);
  31. return $item;
  32. }, $list);
  33. };
  34. return GoodsSpecs::getPageList($page, $limit, function($model) use ($store_id) {
  35. $model->where(['mall_id' => $this->mall_id, 'store_id' => $store_id]);
  36. }, 'id desc', $list_callback);
  37. }
  38. /**
  39. * 添加规格
  40. *
  41. * @param string $name
  42. * @param array $values
  43. * @return boolean
  44. */
  45. public function add(string $name, array $values, $store_id = 0)
  46. {
  47. return GoodsSpecs::addGoodsSpecs(
  48. $this->mall_id,
  49. $name,
  50. $values,
  51. $store_id
  52. );
  53. }
  54. /**
  55. * 修改规格
  56. *
  57. * @param integer $id
  58. * @param string $name
  59. * @param array $values
  60. * @return boolean
  61. */
  62. public function edit(int $id, string $name, array $values)
  63. {
  64. /**
  65. * @var GoodsSpecs|null
  66. */
  67. $specs = GoodsSpecs::getSpecsById($this->mall_id, $id);
  68. if (empty($specs)) {
  69. return $this->error('规格不存在');
  70. }
  71. return $specs->editSpecs($name, $values);
  72. }
  73. /**
  74. * 删除
  75. *
  76. * @return boolean
  77. */
  78. public function del(int $id)
  79. {
  80. /**
  81. * @var GoodsSpecs|null
  82. */
  83. $specs = GoodsSpecs::getSpecsById($this->mall_id, $id);
  84. if (empty($specs)) {
  85. return $this->error('规格不存在');
  86. }
  87. return $specs->delete();
  88. }
  89. }