GoodsBrandForm.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\Cart;
  9. use app\models\Cat;
  10. use app\models\common\admin\log\CommonActionLog;
  11. use app\models\Goods;
  12. use app\models\GoodsBrand;
  13. use app\models\GoodsBrandCat;
  14. use app\models\GoodsBrandSubscribeLog;
  15. use app\models\GoodsCat;
  16. use app\modules\mch\models\MchModel;
  17. use Yii;
  18. use yii\data\Pagination;
  19. use app\modules\client\models\ApiModel;
  20. class GoodsBrandForm extends ApiModel
  21. {
  22. public $id;
  23. public $store_id;
  24. public $user_id;
  25. public $md_id;
  26. public $brand_name;
  27. public $brand_cat_id;
  28. /**
  29. * @inheritdoc
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['store_id', 'id', 'brand_cat_id', 'user_id', 'md_id'], 'integer'],
  35. [['brand_name'], 'string'],
  36. ];
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => 'ID',
  45. 'store_id' => 'Store ID',
  46. 'brand_name' => '品牌名称',
  47. ];
  48. }
  49. public function getBrandList() {
  50. $brand_name = $this->brand_name;
  51. $query = GoodsBrand::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'is_show' => 1])
  52. ->select('id, brand_name, brand_logo, brand_desc');
  53. if (!empty(trim($this->brand_name))) {
  54. $query->andWhere(['LIKE', 'brand_name', $brand_name]);
  55. }
  56. $goods_brand = $query->orderBy('sort DESC')->asArray()->all();
  57. $new_arr = [];
  58. foreach ($goods_brand as &$item) {
  59. $f_char = $this->getFirstCharter($item['brand_name']);
  60. $new_arr[$f_char][] = $item;
  61. }
  62. ksort($new_arr);
  63. if (key($new_arr) === '#') {
  64. $first_item = $new_arr['#'];
  65. array_splice($new_arr, 0, 1);
  66. $new_arr = array_merge($new_arr, ['#' => $first_item]);
  67. }
  68. return [
  69. 'code' => 0,
  70. 'msg' => '获取成功',
  71. 'data' => [
  72. 'list' => $new_arr
  73. ]
  74. ];
  75. }
  76. public function getList() {
  77. $brand_name = $this->brand_name;
  78. $brand_cat_id = $this->brand_cat_id;
  79. $user_id = $this->user_id;
  80. $store_id = $this->store_id;
  81. $query = GoodsBrand::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => 1])
  82. ->select('id, brand_name, brand_logo, brand_desc');
  83. if (!empty(trim($this->brand_name))) {
  84. $query->andWhere(['LIKE', 'brand_name', $brand_name]);
  85. }
  86. if ($brand_cat_id > 0) {
  87. $query->andWhere(['brand_cat_id' => $brand_cat_id]);
  88. } elseif ($brand_cat_id < 0) {
  89. $brand_id = GoodsBrandSubscribeLog::getUserSubscribeLog($user_id);
  90. $query->andWhere(['id' => $brand_id]);
  91. }
  92. $query->orderBy('sort DESC');
  93. $list = pagination_make($query);
  94. foreach ($list['list'] as &$item) {
  95. $item['goods_num'] = count(Goods::getBrandGoodsList($item['id'], $store_id));
  96. $item['brand_desc'] = $item['brand_desc'] ?: '';
  97. }
  98. return [
  99. 'code' => 0,
  100. 'msg' => '获取成功',
  101. 'data' => $list
  102. ];
  103. }
  104. public function getCatList() {
  105. try {
  106. $store_id = $this->store_id;
  107. $user_id = $this->user_id;
  108. $goodsBrandCatList = GoodsBrandCat::getList($store_id);
  109. array_unshift($goodsBrandCatList, ['id' => 0, 'brand_cat_name' => '全部'], ['id' => -1, 'brand_cat_name' => '我的订阅']);
  110. foreach ($goodsBrandCatList as &$item) {
  111. if ($item['id'] < 0) {
  112. $item['brand_count'] = count(GoodsBrandSubscribeLog::getUserSubscribeLog($user_id));
  113. } else {
  114. $item['brand_count'] = count(GoodsBrand::getList($store_id, $item['id']));
  115. }
  116. }
  117. return [
  118. 'code' => 0,
  119. 'msg' => '',
  120. 'data' => [
  121. 'list' => $goodsBrandCatList
  122. ]
  123. ];
  124. } catch (\Exception $e) {
  125. return [
  126. 'code' => 1,
  127. 'msg' => $e->getMessage()
  128. ];
  129. }
  130. }
  131. public function userBrandSubscribe() {
  132. try {
  133. $user_id = $this->user_id;
  134. $id = $this->id;
  135. $store_id = $this->store_id;
  136. $goodsBrand = GoodsBrand::getBrand($id, $store_id);
  137. if (!$goodsBrand) {
  138. throw new \Exception('查询品牌失败');
  139. }
  140. //查询是否关注当前品牌
  141. $userSubscribeLog = GoodsBrandSubscribeLog::getUserSubscribeLog($user_id);
  142. if (in_array($id, $userSubscribeLog)) {
  143. //已经关注就取消关注
  144. GoodsBrandSubscribeLog::deleteAll(['user_id' => $user_id, 'goods_brand_id' => $id]);
  145. return [
  146. 'code' => 0,
  147. 'msg' => '取消订阅成功'
  148. ];
  149. }
  150. $model = new GoodsBrandSubscribeLog();
  151. $model->user_id = $user_id;
  152. $model->goods_brand_id = $id;
  153. if (!$model->save()) {
  154. throw new \Exception('操作失败');
  155. }
  156. return [
  157. 'code' => 0,
  158. 'msg' => '订阅成功'
  159. ];
  160. } catch (\Exception $e) {
  161. return [
  162. 'code' => 1,
  163. 'msg' => $e->getMessage()
  164. ];
  165. }
  166. }
  167. //获取品牌详情
  168. public function brandInfo() {
  169. try {
  170. $id = $this->id;
  171. $store_id = $this->store_id;
  172. $user_id = $this->user_id;
  173. $md_id = $this->md_id;
  174. $goods_list = Goods::getBrandGoodsList($id, $store_id);
  175. $data = [];
  176. $goodsBrand = GoodsBrand::getBrand($id, $store_id);
  177. $data['brand'] = [
  178. 'brand_name' => $goodsBrand->brand_name,
  179. 'brand_logo' => $goodsBrand->brand_logo,
  180. 'brand_desc' => $goodsBrand->brand_desc ?: '',
  181. 'brand_bg' => $goodsBrand->brand_bg ?: '',
  182. 'goods_num' => count($goods_list),
  183. 'subscribe_num' => count(GoodsBrandSubscribeLog::getBrandList($id)),
  184. 'is_subscribe' => GoodsBrandSubscribeLog::isSubscribe($user_id, $id) ? 1 : 0
  185. ];
  186. $goods_id = array_column($goods_list, 'id');
  187. $goods_cat = GoodsCat::find()->where(['goods_id' => $goods_id])->select('cat_id')->column();
  188. $goods_cat = array_unique($goods_cat);
  189. $goods_cat_id = [];
  190. foreach ($goods_cat as $goods_cat_item) {
  191. $first_cat = Cat::find()->where(['id' => $goods_cat_item, 'is_delete' => 0])->select('parent_id')
  192. ->asArray()->one();
  193. if ($first_cat) {
  194. if (!intval($first_cat['parent_id'])) {
  195. $goods_cat_id[] = $goods_cat_item;
  196. } else {
  197. $second_cat = Cat::find()->where(['id' => $first_cat['parent_id'], 'is_delete' => 0])->select('parent_id')
  198. ->asArray()->one();
  199. if ($second_cat) {
  200. if (!intval($second_cat['parent_id'])) {
  201. $goods_cat_id[] = $first_cat['parent_id'];
  202. } else {
  203. $third_cat = Cat::find()->where(['id' => $second_cat['parent_id'], 'is_delete' => 0])->select('parent_id')
  204. ->asArray()->one();
  205. if ($third_cat) {
  206. $goods_cat_id[] = $second_cat['parent_id'];
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. $data['cat_list'] = Cat::find()->where(['id' => $goods_cat_id])->select('id, name')->asArray()->all();
  214. $cartQuery = Cart::find()->alias('c')->leftJoin(['g' => Goods::tableName()], 'c.goods_id = g.id')
  215. ->where(['c.store_id' => $store_id, 'c.user_id' => $user_id, 'c.is_delete' => 0, 'g.status' => 1, 'g.is_delete' => 0]);
  216. if ($md_id < 0) {
  217. $md_id = [-1, 0];
  218. }
  219. $data['cart_count'] = $cartQuery->andWhere(['c.md_id' => $md_id])->select('c.goods_id, c.num')
  220. ->sum('c.num') ?: 0;
  221. return [
  222. 'code' => 0,
  223. 'msg' => '获取成功',
  224. 'data' => $data
  225. ];
  226. } catch (\Exception $e) {
  227. return [
  228. 'code' => 1,
  229. 'msg' => $e->getMessage()
  230. ];
  231. }
  232. }
  233. public function getFirstCharter($str)
  234. {
  235. if (empty($str)) {
  236. return '';
  237. }
  238. $f_char = ord($str{0});
  239. if ($f_char >= ord('A') && $f_char <= ord('z')) return strtoupper($str{0});
  240. $s1 = iconv('UTF-8', 'gb2312', $str);
  241. $s2 = iconv('gb2312', 'UTF-8', $s1);
  242. $s = $s2 == $str ? $s1 : $str;
  243. $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
  244. if ($asc >= -20319 && $asc <= -20284) return 'A';
  245. if ($asc >= -20283 && $asc <= -19776) return 'B';
  246. if ($asc >= -19775 && $asc <= -19219) return 'C';
  247. if ($asc >= -19218 && $asc <= -18711) return 'D';
  248. if ($asc >= -18710 && $asc <= -18527) return 'E';
  249. if ($asc >= -18526 && $asc <= -18240) return 'F';
  250. if ($asc >= -18239 && $asc <= -17923) return 'G';
  251. if ($asc >= -17922 && $asc <= -17418) return 'H';
  252. if ($asc >= -17417 && $asc <= -16475) return 'J';
  253. if ($asc >= -16474 && $asc <= -16213) return 'K';
  254. if ($asc >= -16212 && $asc <= -15641) return 'L';
  255. if ($asc >= -15640 && $asc <= -15166) return 'M';
  256. if ($asc >= -15165 && $asc <= -14923) return 'N';
  257. if ($asc >= -14922 && $asc <= -14915) return 'O';
  258. if ($asc >= -14914 && $asc <= -14631) return 'P';
  259. if ($asc >= -14630 && $asc <= -14150) return 'Q';
  260. if ($asc >= -14149 && $asc <= -14091) return 'R';
  261. if ($asc >= -14090 && $asc <= -13319) return 'S';
  262. if ($asc >= -13318 && $asc <= -12839) return 'T';
  263. if ($asc >= -12838 && $asc <= -12557) return 'W';
  264. if ($asc >= -12556 && $asc <= -11848) return 'X';
  265. if ($asc >= -11847 && $asc <= -11056) return 'Y';
  266. if ($asc >= -11055 && $asc <= -10247) return 'Z';
  267. return '#';
  268. }
  269. }