DiyForm.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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\Clerk;
  9. use app\models\DeliveryRules;
  10. use app\models\Goods;
  11. use app\models\GoodsBook;
  12. use app\models\GoodsBrowse;
  13. use app\models\GoodsCat;
  14. use app\models\Mch;
  15. use app\models\Md;
  16. use app\models\MdGoods;
  17. use app\models\MdGroupActivities;
  18. use app\models\MdGroupActivitiesGoods;
  19. use app\models\Order;
  20. use app\models\OrderComment;
  21. use app\models\Shop;
  22. use app\models\User;
  23. use app\modules\client\models\ApiModel;
  24. use app\modules\client\models\v1\common\CommonGoods;
  25. use yii\data\Pagination;
  26. use yii\helpers\Json;
  27. use app\models\Cat;
  28. use app\models\Cart;
  29. class DiyForm extends ApiModel
  30. {
  31. /**
  32. * 获取商品列表
  33. */
  34. public static function getGoods($reqeruy)
  35. {
  36. try {
  37. $goods_id = Json::decode($reqeruy['goods_id']);
  38. } catch (\Exception $e) {
  39. $goods_id = explode(',', $reqeruy['goods_id']);
  40. }
  41. if ($goods_id === null) {
  42. $goods_id = [-1];
  43. }
  44. $goods_id = (array)$goods_id;
  45. $userLevel = get_user()->level;
  46. $query = Goods::find()->alias('g')
  47. ->leftJoin(['gb' => GoodsBrowse::tableName()],'g.id=gb.goods_id')
  48. ->where([
  49. 'g.is_delete' => 0,
  50. 'g.store_id' => get_store_id(),
  51. 'g.status' => 1
  52. ])->andWhere(['not like', 'g.name', '当面付']);
  53. // 这里判断商品浏览权限
  54. if(empty($userLevel)){
  55. $query->andWhere(['gb.goods_id' => null]);
  56. }else{
  57. $query->andWhere(['or',['gb.goods_id' => null],['gb.level' => $userLevel]]);
  58. }
  59. if (isset($reqeruy['product_type']) && $reqeruy['product_type'] != null) {
  60. $query->andWhere(['g.product_type' => $reqeruy['product_type']]);
  61. } else {
  62. $query->andWhere(['<>', 'g.product_type', Goods::GOODS_TYPE_INTEGRAL]);
  63. }
  64. if (get_md_id()) {
  65. $md = Md::findOne(get_md_id());
  66. if ($md->is_single) {
  67. $query->leftJoin(['mdg' => MdGoods::tableName()], 'mdg.goods_id=g.id');
  68. //兼容独立运营商品可以搜索到平台的预约商品
  69. $query->andWhere(['OR', [
  70. 'g.product_type' => 0,
  71. 'mdg.status' => 1,
  72. 'mdg.md_id' => get_md_id()
  73. ], [
  74. 'g.product_type' => [
  75. 1, 2
  76. ]
  77. ]]);
  78. }
  79. }
  80. if ($goods_id) {
  81. $query->andWhere([
  82. 'in', 'g.id', $goods_id
  83. ]);
  84. $query->orderBy([new \yii\db\Expression('FIELD(g.id, '. implode(",", $goods_id) .')')]);
  85. // $query->addParams([':ids' => implode(',', array_map(function($id) {
  86. // return (int)$id;
  87. // }, $goods_id))]);
  88. } else {
  89. if ($reqeruy['cat_id']) {
  90. if ($reqeruy['product_type'] == 2) {
  91. $query->andWhere(['g.id' => \app\models\BookingGoodsExt::find()->where(['cat_id' => $reqeruy['cat_id']])->select('goods_id')]);
  92. }else{
  93. $cat_arr = Cat::find()->where(['parent_id' => $reqeruy['cat_id'], 'is_delete' => 0, 'is_show' => 1])->select('id')->column();
  94. $cat_arr = array_merge($cat_arr, [$reqeruy['cat_id']]);
  95. if (!empty($cat_arr)) {
  96. $cat_arr2 = Cat::find()->where(['parent_id' => $cat_arr, 'is_delete' => 0, 'is_show' => 1])->select('id')->column();
  97. $cat_arr = array_merge($cat_arr, $cat_arr2);
  98. }
  99. $goods_cat_arr = GoodsCat::find()->where(['cat_id' => $cat_arr, 'is_delete' => 0])->select('goods_id')->column();
  100. $query->leftJoin(['gc' => GoodsCat::tableName()], 'gc.goods_id=g.id')
  101. ->andWhere(['OR', [
  102. 'gc.cat_id' => Cat::getCatId($reqeruy['cat_id'])
  103. ], ['gc.goods_id' => $goods_cat_arr]]);
  104. }
  105. }
  106. // 团购活动
  107. if($reqeruy['activities_id'] > 0){
  108. $goodsIds = MdGroupActivitiesGoods::find()->where(['activities_id' => $reqeruy['activities_id']])->select('goods_id')->column();
  109. $query->andWhere(['in', 'g.id', $goodsIds]);
  110. }
  111. $count = $query->groupBy('g.id')->count();
  112. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $reqeruy['limit'], 'page' => $reqeruy['page'] - 1]);
  113. $query->offset($pagination->offset)->limit($pagination->limit)
  114. ->orderBy(['g.sort' => SORT_DESC, 'g.id' => SORT_DESC]);
  115. }
  116. $is_show_alliance_coupon = \app\models\Option::get('is_show_alliance_coupon', get_store_id(), 'store', 0)['value'];
  117. $select = ['g.goods_num', 'g.order_min_count', 'g.status', 'g.product_type', 'g.virtual_sales', 'g.name', 'g.id', 'g.service', 'g.attr', 'g.cover_pic', 'g.attr', 'g.use_attr', 'g.price', 'g.original_price', 'g.delivery_rules_id', 'g.is_level', 'g.is_negotiable'];
  118. if ($is_show_alliance_coupon) {
  119. array_push($select, 'g.goods_take_price', 'g.goods_send_profit');
  120. }
  121. if (get_md_id()) {
  122. if ($md->is_single) {
  123. array_push($select, 'mdg.virtual_sales as md_virtual_sales', 'mdg.attr as mdg_attr', 'mdg.goods_num as mdg_goods_num', 'mdg.price as mdg_price', 'mdg.status as mdg_status' );
  124. }
  125. }
  126. $goods_list = $query->select($select)->groupBy('g.id')
  127. ->asArray()->all();
  128. if (get_md_id()) {
  129. if ($md->is_single) {
  130. foreach ($goods_list as $key => $value) {
  131. //独立运营商品可以搜索到平台的预约商品
  132. if (!in_array($value['product_type'], [1, 2])) {
  133. $goods_list[$key]['virtual_sales'] = $value['md_virtual_sales'];
  134. $goods_list[$key]['attr'] = $value['mdg_attr'];
  135. $goods_list[$key]['goods_num'] = $value['mdg_goods_num'];
  136. $goods_list[$key]['price'] = $value['mdg_price'];
  137. $goods_list[$key]['status'] = $value['mdg_status'];
  138. }
  139. }
  140. }
  141. }
  142. foreach ($goods_list as $key => $value) {
  143. $goods = Goods::findOne($value['id']);
  144. $goods_list[$key]['virtual_sales'] = $goods->getSalesVolume() + $value['virtual_sales'];
  145. if ($value['product_type'] == 1) {
  146. $goods_list[$key]['date_book'] = Json::decode(GoodsBook::findOne(['goods_id' => $value['id']])->date_book);
  147. }
  148. if ($value['product_type'] == 2) {
  149. $goods_list[$key]['service_book'] = Json::decode(GoodsBook::findOne(['goods_id' => $value['id']])->service_book);
  150. }
  151. $goods_list[$key]['delivery_rules'] = DeliveryRules::find()->where(['id' => $value['delivery_rules_id'], 'is_delete' => 0, 'status' => 1])
  152. ->select('type, times, days, name')->asArray()->one();
  153. if ($goods_list[$key]['delivery_rules']) {
  154. $days = "下单" . $goods_list[$key]['delivery_rules']['days'] . "天后";
  155. $goods_list[$key]['delivery_rules']['times'] =
  156. (int)$goods_list[$key]['delivery_rules']['type'] === 1 ? $days : date("m月d日 H:i:s", $goods_list[$key]['delivery_rules']['times']);
  157. $goods_list[$key]['delivery_rules']['type'] .= '';
  158. }
  159. $res = CommonGoods::getMemberPrice([
  160. 'attr' => $value['attr'],
  161. 'price' => $value['price'],
  162. 'mch_id' => 0,
  163. 'is_level' => $value['is_level'],
  164. 'use_attr' => isset($value['use_attr']) ? $value['use_attr'] : 0,
  165. ]);
  166. $goodsMemberPrice = $res['min_member_price'] ?: $value['price'];
  167. $goods_list[$key]['price'] = sprintf("%.2f", $goodsMemberPrice);
  168. if (intval($value['is_negotiable']) === 1) {
  169. $goods_list[$key]['price'] = '价格面议';
  170. }
  171. }
  172. // 检测商品是不是有团购活动 因为一个商品只能添加在一个团购活动中
  173. // 团购活动不参与会员折扣!!
  174. foreach($goods_list as $key => $value){
  175. $activityModel = MdGroupActivitiesGoods::find()->alias('mgag')->leftJoin(['mga'=>MdGroupActivities::tableName()],'mga.id=mgag.activities_id')->where(['goods_id'=>$value['id'],'mgag.is_delete'=>0])->andWhere(['>=','mga.end_time',time()])->andWhere(['mga.is_delete'=>0])->asArray()->select('mgag.*,mga.start_time,mga.end_time')->one();
  176. if($activityModel){
  177. if($activityModel['start_time'] > time()){
  178. $goods_list[$key]['status'] = ['status'=>0,'time'=>$activityModel['start_time'] - time()];
  179. }else{
  180. $goods_list[$key]['status'] = ['status'=>1,'time'=>$activityModel['end_time'] - time()];
  181. }
  182. $goods_list[$key]['price'] = sprintf("%.2f", $activityModel['price']);
  183. // 计算购物车的商品数量
  184. $goods_list[$key]['cart_info']['cart_num'] = get_user_id() > 0 ? Cart::find()->where(['type'=>Cart::TYPE_DEFAULT,'goods_id'=>$value['id'],'is_delete'=>0,'user_id'=>get_user_id()])->andWhere(['>','num',0])->sum('num') : 0;
  185. $goods_list[$key]['cart_info']['cart_id'] = get_user_id() > 0 ? Cart::find()->where(['type'=>Cart::TYPE_DEFAULT,'goods_id'=>$value['id'],'is_delete'=>0,'user_id'=>get_user_id()])->andWhere(['>','num',0])->select('id')->one()->id : 0;
  186. }
  187. $goods_list[$key]['take_price'] = 0;
  188. $goods_list[$key]['send_price'] = 0;
  189. if ($is_show_alliance_coupon) {
  190. if ($value['goods_take_price'] > 0) {
  191. $goods_list[$key]['take_price'] = \round($goods_list[$key]['price'] * $value['goods_take_price'] / 100, 2);
  192. }
  193. if ($value['goods_send_profit'] > 0) {
  194. $goods_list[$key]['send_price'] = \round($goods_list[$key]['price'] * $value['goods_send_profit'] / 100, 2);
  195. }
  196. }
  197. }
  198. return [
  199. 'code' => 0,
  200. 'data' => [
  201. 'row_count' => isset($count) ? $count: 0,
  202. 'page_count' => isset($pagination) ? $pagination->pageCount : 0,
  203. 'list' => $goods_list
  204. ]
  205. ];
  206. }
  207. /**
  208. * 获取门店列表
  209. */
  210. public static function getShop($requery)
  211. {
  212. $shop_id = Json::decode($requery['shop_id']);
  213. $query = Shop::find()->where([
  214. 'store_id' => get_store_id(),
  215. 'is_delete' => 0,
  216. ])->andWhere([
  217. '>',
  218. 'user_id',
  219. 0
  220. ]);
  221. if ($shop_id) {
  222. $query->andWhere([
  223. 'id' => $shop_id
  224. ]);
  225. } else {
  226. $count = $query->count();
  227. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $requery['limit'], 'page' => $requery['page']]);
  228. $query->offset($pagination->offset)->limit($pagination->limit)
  229. ->orderBy(['id' => SORT_DESC]);
  230. }
  231. $shop_list = $query->select(['*'])
  232. ->asArray()->all();
  233. return [
  234. 'code' => 0,
  235. 'data' => [
  236. 'list' => $shop_list
  237. ]
  238. ];
  239. }
  240. /**
  241. * 获取门店列表
  242. */
  243. public static function getNearbyShop($requery)
  244. {
  245. $query = Shop::find()->alias('s')
  246. ->leftJoin(['c' => Clerk::tableName()], 's.id=c.shop_id')
  247. ->where([
  248. 's.store_id' => get_store_id(),
  249. 's.is_delete' => 0
  250. ])->andWhere(['>','c.shop_id',0]);
  251. $count = $query->count();
  252. $p = new Pagination(['totalCount' => $count, 'pageSize' => $requery['limit'], 'page' => $requery['page'] - 1]);
  253. $shop_table_name = Shop::tableName();
  254. $user_table_name = Clerk::tableName();
  255. $start = ($requery['page'] - 1) * $requery['limit'];
  256. $store_id = get_store_id();
  257. $sql = "SELECT `s`.*, acos(cos({$requery['latitude']}*pi()/180 )*cos(s.latitude*pi()/180)*cos({$requery['longitude']}*pi()/180 -s.longitude*pi()/180)+sin({$requery['latitude']}*pi()/180 )*sin(s.latitude*pi()/180))*6370996.81 as distance FROM {$shop_table_name} `s` LEFT JOIN {$user_table_name} `c` ON s.id=u.shop_id WHERE ((`s`.`store_id`={$store_id}) AND (`s`.`is_delete`=0)) AND (`c`.`shop_id` > 0) ";
  258. $sql .= "ORDER BY `distance` LIMIT {$start},{$requery['limit']}";
  259. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  260. return [
  261. 'code' => 0,
  262. 'data' => [
  263. 'list' => $list
  264. ]
  265. ];
  266. }
  267. /**
  268. * 获取门店列表
  269. */
  270. public static function getMch($requery)
  271. {
  272. $shop_id = Json::decode($requery['shop_id']);
  273. $query = Mch::find()->alias('m')
  274. ->where([
  275. 'm.is_delete' => 0,
  276. 'm.store_id' => get_store_id()
  277. ])->andWhere([
  278. '>',
  279. 'u.shop_id',
  280. 0
  281. ]);
  282. if ($shop_id) {
  283. $query->andWhere([
  284. 's.id' => $shop_id
  285. ]);
  286. } else {
  287. $count = $query->count();
  288. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $requery['limit'], 'page' => $requery['page']]);
  289. $query->offset($pagination->offset)->limit($pagination->limit)
  290. ->orderBy(['s.id' => SORT_DESC]);
  291. }
  292. $shop_list = $query->select(['s.*'])
  293. ->asArray()->all();
  294. return [
  295. 'code' => 0,
  296. 'data' => [
  297. 'list' => $shop_list
  298. ]
  299. ];
  300. }
  301. /**
  302. * 获取门店列表
  303. * @param $requery
  304. * @return array
  305. */
  306. public static function getNearbyMch($requery)
  307. {
  308. $mch_table = Mch::tableName();
  309. $start = ($requery['page'] - 1) * $requery['limit'];
  310. $store_id = get_store_id();
  311. $sql = "SELECT `id`,`name`,`header_bg`,`longitude`,`latitude`,`logo`, acos(cos({$requery['latitude']}*pi()/180 )*cos(latitude*pi()/180)*cos({$requery['longitude']}*pi()/180 -longitude*pi()/180)+sin({$requery['latitude']}*pi()/180 )*sin(latitude*pi()/180))*6370996.81 as distance FROM {$mch_table} WHERE `store_id`={$store_id} AND `is_delete`=0 AND `is_lock`=0 AND `review_status`=1 AND `is_open`=1 ";
  312. $sql .= "ORDER BY `distance` LIMIT {$start},{$requery['limit']}";
  313. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  314. foreach($list as &$val) {
  315. $val['goods_list'] = Goods::find()->where([
  316. 'mch_id' => $val['id'],
  317. 'status' => 1,
  318. 'is_delete' => 0
  319. ])->select(['cover_pic', 'id', 'name', 'original_price', 'price'])->limit(10)->all();
  320. // 获取评分
  321. $where = [
  322. '>',
  323. 'created_at',
  324. time() - 30 * 24 * 60 * 60
  325. ];
  326. $query = OrderComment::find()->where([
  327. 'mch_id' => $val['id'],
  328. ])->andWhere($where);
  329. $count = $query->count();
  330. if ($count > 0) {
  331. $commit_sum = $query->sum('score');
  332. $val['score'] = round($commit_sum/$count,1);
  333. } else {
  334. $val['score'] = round(5,1);
  335. }
  336. // 统计销量
  337. $order_num = Order::find()->where([
  338. 'mch_id' => $val['id'],
  339. ])->andWhere($where)->count();
  340. $val['order_num'] = self::setNum($order_num);
  341. }
  342. return [
  343. 'code' => 0,
  344. 'data' => [
  345. 'list' => $list
  346. ]
  347. ];
  348. }
  349. private static function setNum($num){
  350. if ($num > 10000) {
  351. return round($num/10000).'万+';
  352. } elseif($num > 1000) {
  353. return round($num/1000).'千+';
  354. } else {
  355. return $num;
  356. }
  357. }
  358. }