IntelligentGoodsForm.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Cat;
  9. use app\models\Goods;
  10. use app\models\GoodsCat;
  11. use yii\base\Model;
  12. class IntelligentGoodsForm extends Model
  13. {
  14. public $store_id = 1;
  15. public $goods_name;
  16. public $status;
  17. public $model;
  18. public $goods_id;
  19. public $accessories_image;
  20. public $cat_id;
  21. /**
  22. * @return array
  23. */
  24. public function rules()
  25. {
  26. return [
  27. [['store_id', 'cat_id'], 'integer'],
  28. [['goods_name'], 'string', 'max' => 60],
  29. [['status'], 'default', 'value' => -1],
  30. [['accessories_image'], 'string'],
  31. [['goods_id'], 'safe']
  32. ];
  33. }
  34. /**
  35. * 保存商品搭配图片
  36. * @return array
  37. */
  38. public function save()
  39. {
  40. $goods_id = $this->goods_id;
  41. $store_id = $this->store_id;
  42. $accessories_image = $this->accessories_image;
  43. if (is_string($goods_id) || is_int($goods_id)) {
  44. $goods_id = explode(',', $goods_id);
  45. }
  46. if (!empty($goods_id)) {
  47. foreach ($goods_id as $item) {
  48. $goods = Goods::findOne(['store_id' => $store_id, 'id' => $item, 'is_delete' => 0]);
  49. if ($goods) {
  50. $goods->accessories_image = $accessories_image;
  51. if (!$goods->save()) {
  52. return [
  53. 'code' => 1,
  54. 'msg' => '商品' . $goods->name . '操作失败:' . json_encode($goods->errors, JSON_UNESCAPED_UNICODE)
  55. ];
  56. }
  57. }
  58. }
  59. }
  60. return [
  61. 'code' => 0,
  62. 'msg' => '提交成功'
  63. ];
  64. }
  65. /**
  66. * @return array
  67. */
  68. public function list() {
  69. $store_id = $this->store_id;
  70. $goods_name = $this->goods_name;
  71. $status = $this->status;
  72. $cat_id = $this->cat_id;
  73. //判断商品中智配图片不为空
  74. $query = Goods::find()->alias('g')->where(['g.store_id' => $store_id, 'g.is_delete' => 0])->andWhere(['AND', ['<>', 'g.accessories_image', ''], ['IS NOT', 'g.accessories_image', NULL]]);
  75. if ($goods_name) {
  76. $query->andWhere(['LIKE', 'g.name', $goods_name]);
  77. }
  78. if (isset($status) && intval($status) !== -1) {
  79. $query->andWhere(['g.status' => $status]);
  80. }
  81. if ($cat_id) {
  82. $query->leftJoin(['gc' => GoodsCat::tableName()], 'g.id = gc.goods_id')->andWhere([
  83. 'gc.cat_id' => Cat::getCatId($cat_id)
  84. ])->groupBy('gc.goods_id');
  85. }
  86. $query->select('g.id, g.cover_pic, g.name, g.price, g.status, g.accessories_image')->orderBy('id desc');
  87. $list = pagination_make($query);
  88. foreach ($list['list'] as &$item) {
  89. $goods_cat = GoodsCat::find()->alias('gc')
  90. ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id')
  91. ->where([ 'gc.goods_id' => $item['id'] ])
  92. ->select(['c.name'])
  93. ->asArray()
  94. ->all();
  95. $item['cat'] = $goods_cat;
  96. $item['status'] = (int)$item['status'];
  97. }
  98. return [
  99. 'code' => 0,
  100. 'msg' => 'success',
  101. 'data' => [
  102. 'data' => $list['list'],
  103. 'pageNo' => $list['pageNo'],
  104. 'totalCount' => $list['totalCount']
  105. ]
  106. ];
  107. }
  108. }