| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\Cart;
- use app\models\Cat;
- use app\models\common\admin\log\CommonActionLog;
- use app\models\Goods;
- use app\models\GoodsBrand;
- use app\models\GoodsBrandCat;
- use app\models\GoodsBrandSubscribeLog;
- use app\models\GoodsCat;
- use app\modules\mch\models\MchModel;
- use Yii;
- use yii\data\Pagination;
- use app\modules\client\models\ApiModel;
- class GoodsBrandForm extends ApiModel
- {
- public $id;
- public $store_id;
- public $user_id;
- public $md_id;
- public $brand_name;
- public $brand_cat_id;
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['store_id', 'id', 'brand_cat_id', 'user_id', 'md_id'], 'integer'],
- [['brand_name'], 'string'],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => 'Store ID',
- 'brand_name' => '品牌名称',
- ];
- }
- public function getBrandList() {
- $brand_name = $this->brand_name;
- $query = GoodsBrand::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'is_show' => 1])
- ->select('id, brand_name, brand_logo, brand_desc');
- if (!empty(trim($this->brand_name))) {
- $query->andWhere(['LIKE', 'brand_name', $brand_name]);
- }
- $goods_brand = $query->orderBy('sort DESC')->asArray()->all();
- $new_arr = [];
- foreach ($goods_brand as &$item) {
- $f_char = $this->getFirstCharter($item['brand_name']);
- $new_arr[$f_char][] = $item;
- }
- ksort($new_arr);
- if (key($new_arr) === '#') {
- $first_item = $new_arr['#'];
- array_splice($new_arr, 0, 1);
- $new_arr = array_merge($new_arr, ['#' => $first_item]);
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'list' => $new_arr
- ]
- ];
- }
- public function getList() {
- $brand_name = $this->brand_name;
- $brand_cat_id = $this->brand_cat_id;
- $user_id = $this->user_id;
- $store_id = $this->store_id;
- $query = GoodsBrand::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => 1])
- ->select('id, brand_name, brand_logo, brand_desc');
- if (!empty(trim($this->brand_name))) {
- $query->andWhere(['LIKE', 'brand_name', $brand_name]);
- }
- if ($brand_cat_id > 0) {
- $query->andWhere(['brand_cat_id' => $brand_cat_id]);
- } elseif ($brand_cat_id < 0) {
- $brand_id = GoodsBrandSubscribeLog::getUserSubscribeLog($user_id);
- $query->andWhere(['id' => $brand_id]);
- }
- $query->orderBy('sort DESC');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $item['goods_num'] = count(Goods::getBrandGoodsList($item['id'], $store_id));
- $item['brand_desc'] = $item['brand_desc'] ?: '';
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $list
- ];
- }
- public function getCatList() {
- try {
- $store_id = $this->store_id;
- $user_id = $this->user_id;
- $goodsBrandCatList = GoodsBrandCat::getList($store_id);
- array_unshift($goodsBrandCatList, ['id' => 0, 'brand_cat_name' => '全部'], ['id' => -1, 'brand_cat_name' => '我的订阅']);
- foreach ($goodsBrandCatList as &$item) {
- if ($item['id'] < 0) {
- $item['brand_count'] = count(GoodsBrandSubscribeLog::getUserSubscribeLog($user_id));
- } else {
- $item['brand_count'] = count(GoodsBrand::getList($store_id, $item['id']));
- }
- }
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => [
- 'list' => $goodsBrandCatList
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function userBrandSubscribe() {
- try {
- $user_id = $this->user_id;
- $id = $this->id;
- $store_id = $this->store_id;
- $goodsBrand = GoodsBrand::getBrand($id, $store_id);
- if (!$goodsBrand) {
- throw new \Exception('查询品牌失败');
- }
- //查询是否关注当前品牌
- $userSubscribeLog = GoodsBrandSubscribeLog::getUserSubscribeLog($user_id);
- if (in_array($id, $userSubscribeLog)) {
- //已经关注就取消关注
- GoodsBrandSubscribeLog::deleteAll(['user_id' => $user_id, 'goods_brand_id' => $id]);
- return [
- 'code' => 0,
- 'msg' => '取消订阅成功'
- ];
- }
- $model = new GoodsBrandSubscribeLog();
- $model->user_id = $user_id;
- $model->goods_brand_id = $id;
- if (!$model->save()) {
- throw new \Exception('操作失败');
- }
- return [
- 'code' => 0,
- 'msg' => '订阅成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //获取品牌详情
- public function brandInfo() {
- try {
- $id = $this->id;
- $store_id = $this->store_id;
- $user_id = $this->user_id;
- $md_id = $this->md_id;
- $goods_list = Goods::getBrandGoodsList($id, $store_id);
- $data = [];
- $goodsBrand = GoodsBrand::getBrand($id, $store_id);
- $data['brand'] = [
- 'brand_name' => $goodsBrand->brand_name,
- 'brand_logo' => $goodsBrand->brand_logo,
- 'brand_desc' => $goodsBrand->brand_desc ?: '',
- 'brand_bg' => $goodsBrand->brand_bg ?: '',
- 'goods_num' => count($goods_list),
- 'subscribe_num' => count(GoodsBrandSubscribeLog::getBrandList($id)),
- 'is_subscribe' => GoodsBrandSubscribeLog::isSubscribe($user_id, $id) ? 1 : 0
- ];
- $goods_id = array_column($goods_list, 'id');
- $goods_cat = GoodsCat::find()->where(['goods_id' => $goods_id])->select('cat_id')->column();
- $goods_cat = array_unique($goods_cat);
- $goods_cat_id = [];
- foreach ($goods_cat as $goods_cat_item) {
- $first_cat = Cat::find()->where(['id' => $goods_cat_item, 'is_delete' => 0])->select('parent_id')
- ->asArray()->one();
- if ($first_cat) {
- if (!intval($first_cat['parent_id'])) {
- $goods_cat_id[] = $goods_cat_item;
- } else {
- $second_cat = Cat::find()->where(['id' => $first_cat['parent_id'], 'is_delete' => 0])->select('parent_id')
- ->asArray()->one();
- if ($second_cat) {
- if (!intval($second_cat['parent_id'])) {
- $goods_cat_id[] = $first_cat['parent_id'];
- } else {
- $third_cat = Cat::find()->where(['id' => $second_cat['parent_id'], 'is_delete' => 0])->select('parent_id')
- ->asArray()->one();
- if ($third_cat) {
- $goods_cat_id[] = $second_cat['parent_id'];
- }
- }
- }
- }
- }
- }
- $data['cat_list'] = Cat::find()->where(['id' => $goods_cat_id])->select('id, name')->asArray()->all();
- $cartQuery = Cart::find()->alias('c')->leftJoin(['g' => Goods::tableName()], 'c.goods_id = g.id')
- ->where(['c.store_id' => $store_id, 'c.user_id' => $user_id, 'c.is_delete' => 0, 'g.status' => 1, 'g.is_delete' => 0]);
- if ($md_id < 0) {
- $md_id = [-1, 0];
- }
- $data['cart_count'] = $cartQuery->andWhere(['c.md_id' => $md_id])->select('c.goods_id, c.num')
- ->sum('c.num') ?: 0;
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $data
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function getFirstCharter($str)
- {
- if (empty($str)) {
- return '';
- }
- $f_char = ord($str{0});
- if ($f_char >= ord('A') && $f_char <= ord('z')) return strtoupper($str{0});
- $s1 = iconv('UTF-8', 'gb2312', $str);
- $s2 = iconv('gb2312', 'UTF-8', $s1);
- $s = $s2 == $str ? $s1 : $str;
- $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
- if ($asc >= -20319 && $asc <= -20284) return 'A';
- if ($asc >= -20283 && $asc <= -19776) return 'B';
- if ($asc >= -19775 && $asc <= -19219) return 'C';
- if ($asc >= -19218 && $asc <= -18711) return 'D';
- if ($asc >= -18710 && $asc <= -18527) return 'E';
- if ($asc >= -18526 && $asc <= -18240) return 'F';
- if ($asc >= -18239 && $asc <= -17923) return 'G';
- if ($asc >= -17922 && $asc <= -17418) return 'H';
- if ($asc >= -17417 && $asc <= -16475) return 'J';
- if ($asc >= -16474 && $asc <= -16213) return 'K';
- if ($asc >= -16212 && $asc <= -15641) return 'L';
- if ($asc >= -15640 && $asc <= -15166) return 'M';
- if ($asc >= -15165 && $asc <= -14923) return 'N';
- if ($asc >= -14922 && $asc <= -14915) return 'O';
- if ($asc >= -14914 && $asc <= -14631) return 'P';
- if ($asc >= -14630 && $asc <= -14150) return 'Q';
- if ($asc >= -14149 && $asc <= -14091) return 'R';
- if ($asc >= -14090 && $asc <= -13319) return 'S';
- if ($asc >= -13318 && $asc <= -12839) return 'T';
- if ($asc >= -12838 && $asc <= -12557) return 'W';
- if ($asc >= -12556 && $asc <= -11848) return 'X';
- if ($asc >= -11847 && $asc <= -11056) return 'Y';
- if ($asc >= -11055 && $asc <= -10247) return 'Z';
- return '#';
- }
- }
|