| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace app\modules\admin\models;
- use app\models\Goods;
- use app\models\GoodsBrand;
- use app\models\GoodsBrandCat;
- class GoodsBrandForm extends \yii\base\Model
- {
- public $brand_name;
- public $brand_logo;
- public $id;
- public $store_id;
- public $brand_cat_id;
- public $brand_bg;
- public $brand_desc;
- public $sort;
- public $is_show;
- public $brand_id;
- public $type;//0隐藏 1显示 2删除
- public $start_time;
- public $end_time;
- public function rules()
- {
- return [
- [['brand_name', 'brand_logo', 'brand_id', 'start_time', 'end_time', 'brand_bg', 'brand_desc'], 'string'],
- [['id', 'store_id', 'sort', 'is_show', 'type', 'brand_cat_id'], 'integer']
- ];
- }
- public function getList() {
- $store_id = $this->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()
- ];
- }
- }
- }
|