| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Cat;
- use app\models\Goods;
- use app\models\GoodsCat;
- use yii\base\Model;
- class IntelligentGoodsForm extends Model
- {
- public $store_id = 1;
- public $goods_name;
- public $status;
- public $model;
- public $goods_id;
- public $accessories_image;
- public $cat_id;
- /**
- * @return array
- */
- public function rules()
- {
- return [
- [['store_id', 'cat_id'], 'integer'],
- [['goods_name'], 'string', 'max' => 60],
- [['status'], 'default', 'value' => -1],
- [['accessories_image'], 'string'],
- [['goods_id'], 'safe']
- ];
- }
- /**
- * 保存商品搭配图片
- * @return array
- */
- public function save()
- {
- $goods_id = $this->goods_id;
- $store_id = $this->store_id;
- $accessories_image = $this->accessories_image;
- if (is_string($goods_id) || is_int($goods_id)) {
- $goods_id = explode(',', $goods_id);
- }
- if (!empty($goods_id)) {
- foreach ($goods_id as $item) {
- $goods = Goods::findOne(['store_id' => $store_id, 'id' => $item, 'is_delete' => 0]);
- if ($goods) {
- $goods->accessories_image = $accessories_image;
- if (!$goods->save()) {
- return [
- 'code' => 1,
- 'msg' => '商品' . $goods->name . '操作失败:' . json_encode($goods->errors, JSON_UNESCAPED_UNICODE)
- ];
- }
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => '提交成功'
- ];
- }
- /**
- * @return array
- */
- public function list() {
- $store_id = $this->store_id;
- $goods_name = $this->goods_name;
- $status = $this->status;
- $cat_id = $this->cat_id;
- //判断商品中智配图片不为空
- $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]]);
- if ($goods_name) {
- $query->andWhere(['LIKE', 'g.name', $goods_name]);
- }
- if (isset($status) && intval($status) !== -1) {
- $query->andWhere(['g.status' => $status]);
- }
- if ($cat_id) {
- $query->leftJoin(['gc' => GoodsCat::tableName()], 'g.id = gc.goods_id')->andWhere([
- 'gc.cat_id' => Cat::getCatId($cat_id)
- ])->groupBy('gc.goods_id');
- }
- $query->select('g.id, g.cover_pic, g.name, g.price, g.status, g.accessories_image')->orderBy('id desc');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $goods_cat = GoodsCat::find()->alias('gc')
- ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id')
- ->where([ 'gc.goods_id' => $item['id'] ])
- ->select(['c.name'])
- ->asArray()
- ->all();
- $item['cat'] = $goods_cat;
- $item['status'] = (int)$item['status'];
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ];
- }
- }
|