DescriptionTemplateService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace backend\modules\mall\services;
  3. use common\enums\StatusEnum;
  4. use common\models\goods\Goods;
  5. use common\models\goods\GoodsDescriptionTemplate;
  6. use common\models\goods\GoodsExtra;
  7. class DescriptionTemplateService extends BaseService
  8. {
  9. /**
  10. * @param array $params
  11. *
  12. * @return array
  13. */
  14. public function list(array $params): array
  15. {
  16. $baseWhere = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
  17. $where = [$baseWhere];
  18. if (isset($params['name']) && $params['name'] !== '') {
  19. $where[] = ['like', 'name', $params['name']];
  20. }
  21. return GoodsDescriptionTemplate::listPage([
  22. 'where' => $where,
  23. 'order' => 'id desc'
  24. ]);
  25. }
  26. /**
  27. * @param int $id
  28. *
  29. * @return bool
  30. */
  31. public function del(int $id): bool
  32. {
  33. $goodsIds = GoodsExtra::find()
  34. ->where([
  35. 'description_template_id' => $id,
  36. 'status' => StatusEnum::ENABLED,
  37. 'mall_id' => $this->mall_id
  38. ])
  39. ->select('goods_id')
  40. ->column();
  41. if (
  42. !empty($goodsIds) &&
  43. Goods::find()
  44. ->where([
  45. 'mall_id' => $this->mall_id,
  46. 'status' => StatusEnum::ENABLED,
  47. 'id' => $goodsIds
  48. ])
  49. ->exists()
  50. ) {
  51. $this->error('当前模板已有商品关联,不允许删除');
  52. return false;
  53. }
  54. if (!$this->edit(['id' => $id, 'status' => StatusEnum::DELETE])) {
  55. return false;
  56. }
  57. return true;
  58. }
  59. /**
  60. * @param array $data
  61. *
  62. * @return bool
  63. */
  64. public function edit(array $data): bool
  65. {
  66. $data['mall_id'] = $this->mall_id;
  67. if (GoodsDescriptionTemplate::setData($data) === false) {
  68. $this->error(GoodsDescriptionTemplate::getStaticError());
  69. return false;
  70. }
  71. return true;
  72. }
  73. /**
  74. * @param int $id
  75. *
  76. * @return array|\yii\db\ActiveRecord|null
  77. */
  78. public function detail(int $id)
  79. {
  80. return GoodsDescriptionTemplate::find()
  81. ->select('id, name, frontend_name, fields')
  82. ->where(['id' => $id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id])
  83. ->one();
  84. }
  85. /**
  86. * @return array
  87. */
  88. public function selectList(): array
  89. {
  90. $list = GoodsDescriptionTemplate::find()
  91. ->select('id, name, fields')
  92. ->where(['status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id])
  93. ->indexBy('id')
  94. ->asArray()
  95. ->all();
  96. foreach ($list as &$item) {
  97. $item['fields'] = json_decode($item['fields'], true) ?: [];
  98. }
  99. return $list;
  100. }
  101. }