store_id; $brand_name = $this->brand_name; $brand_cat_id = $this->brand_cat_id; $is_show = $this->is_show; $start_time = $this->start_time; $end_time = $this->end_time; $query = GoodsBrand::find()->where(['store_id' => $store_id, 'is_delete' => 0]);//->orderBy('sort DESC'); if (!empty($brand_name)) { $query->andWhere(['LIKE', 'brand_name', $brand_name]); } if (!empty($brand_cat_id)) { $query->andWhere(['brand_cat_id' => $brand_cat_id]); } if (isset($is_show) && $is_show > -1) { $query->andWhere(['is_show' => $is_show]); } if (!empty($start_time)) { $start_time = strtotime($start_time); $query->andWhere(['>=', 'created_at', $start_time]); } if (!empty($end_time)) { $end_time = strtotime($end_time); $query->andWhere(['<=', 'created_at', $end_time]); } $query->select('id, brand_name, brand_logo, is_show, sort, created_at, updated_at, brand_cat_id, brand_bg ,brand_desc')->orderBy('sort DESC, id DESC'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); $item['updated_at'] = date('Y-m-d H:i:s', $item['updated_at']); $item['is_show'] = (int)$item['is_show']; $item['goods_num'] = Goods::find()->where(['brand_id' => $item['id'], 'is_delete' => 0])->count() ?: '0'; $goodsBrandCat = GoodsBrandCat::getBrandCat($item['brand_cat_id']); $item['brand_cat_name'] = $goodsBrandCat->brand_cat_name ?: ''; $item['brand_bg'] = $item['brand_bg'] ?: ''; $item['brand_desc'] = $item['brand_desc'] ?: ''; } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list['list'], 'pageNo' => $list['pageNo'], 'totalCount' => $list['totalCount'] ] ]; } public function saveBrand() { try { $id = $this->id; $store_id = $this->store_id; $brand_cat_id = $this->brand_cat_id; $brand_name = trim($this->brand_name); $brand_logo = trim($this->brand_logo); $brand_bg = $this->brand_bg; $brand_desc = $this->brand_desc; $sort = (int)$this->sort; $is_show = (int)$this->is_show; if (empty($brand_name)) { throw new \Exception('品牌名称不能为空'); } if (empty($brand_logo)) { throw new \Exception('品牌Logo不能为空'); } if ($brand_cat_id) { if (!GoodsBrandCat::getBrandCat($brand_cat_id)) { throw new \Exception('品牌分类查询失败'); } } $goods_brand = GoodsBrand::findOne($id) ?: new GoodsBrand(); $goods_brand->store_id = $store_id; $goods_brand->brand_cat_id = $brand_cat_id; $goods_brand->brand_name = $brand_name ?: ''; $goods_brand->brand_logo = $brand_logo ?: ''; $goods_brand->brand_bg = $brand_bg ?: ''; $goods_brand->brand_desc = $brand_desc ?: ''; $goods_brand->sort = $sort ?: 0; $goods_brand->is_show = $is_show ?? 1; if (!$goods_brand->save()) { throw new \Exception(json_encode($goods_brand->errors, JSON_UNESCAPED_UNICODE)); } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function setStatus() { try { $brand_id_str = $this->brand_id; $type = (int)$this->type; $brand_id_arr = explode(',', $brand_id_str); foreach ($brand_id_arr as $brand_id) { $key = "is_show"; $value = 1; switch ($type) { case 1: $value = 1; break; case 2: $key = "is_delete"; break; default: $value = 0; } $goodsBrand = GoodsBrand::findOne($brand_id); if ($goodsBrand) { $goodsBrand->$key = $value; if (!$goodsBrand->save()) { throw new \Exception(json_encode($goodsBrand->errors, JSON_UNESCAPED_UNICODE)); } } } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }