GoodsForm.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\cashier;
  8. use app\constants\OptionSetting;
  9. use app\models\Address;
  10. use app\models\Coupon;
  11. use app\models\DeliveryRules;
  12. use app\models\GoodsBook;
  13. use app\models\GoodsCat;
  14. use app\models\GoodsFullMinus;
  15. use app\models\MchGoodsCat;
  16. use app\models\MdGoods;
  17. use app\models\Option;
  18. use app\models\OrderDetail;
  19. use app\models\SeckillActivity;
  20. use app\models\SeckillActivityGoods;
  21. use app\models\VerifyCard;
  22. use app\models\Favorite;
  23. use app\models\Goods;
  24. use app\models\GoodsPic;
  25. use app\models\Order;
  26. use app\models\PostageRules;
  27. use app\models\Shop;
  28. use app\models\TerritorialLimitation;
  29. use app\modules\client\models\v1\common\CommonGoods;
  30. use app\modules\client\models\v1\diy\GetInfo;
  31. use app\modules\client\models\v1\mch\ShopDataForm;
  32. use app\plugins\product_traceability\models\ProductBatch;
  33. use app\utils\Tools;
  34. use yii\base\Model;
  35. use yii\helpers\Json;
  36. class GoodsForm extends Model
  37. {
  38. public $id;
  39. public $user_id;
  40. public $store_id;
  41. public $mch_id;
  42. public $shop_id;
  43. public $address_id;
  44. public $verify_card_id;
  45. public function rules()
  46. {
  47. return [
  48. [['id'], 'required'],
  49. [['mch_id', 'shop_id', 'address_id'], 'integer'],
  50. [['user_id'], 'safe'],
  51. ];
  52. }
  53. /**
  54. * 排序类型$sort 1--综合排序 2--销量排序
  55. */
  56. public function search()
  57. {
  58. if (!$this->validate()) {
  59. return $this->getErrorSummary(false)[0];
  60. }
  61. $where = [
  62. 'id' => $this->id,
  63. 'is_delete' => 0,
  64. 'status' => 1,
  65. 'store_id' => $this->store_id
  66. ];
  67. if (\Yii::$app->prod_is_dandianpu()) {
  68. unset($where['store_id']);
  69. }
  70. $goods = Goods::findOne($where);
  71. \app\modules\admin\models\erp\InventoryForm::erp2Goods($this->store_id, $goods);
  72. \app\modules\admin\models\jushuitan\JuShuiTanForm::syncStoreJstGoodsQty($this->store_id, $goods);
  73. if (!empty($goods->verify_card_id)) {
  74. $ids = explode(',', $goods->verify_card_id);
  75. $verify_card = Tools::getVerifyList($ids);
  76. } else {
  77. $verify_card = [];
  78. }
  79. if (get_md_id()) {
  80. $md_goods = MdGoods::findOne(['md_id' => get_md_id(), 'goods_id' => $this->id]);
  81. if ($md_goods) {
  82. if ($md_goods->status == 0) {
  83. return [
  84. 'code' => 1,
  85. 'msg' => '商品已下架'
  86. ];
  87. }
  88. } else {
  89. if (!$goods) {
  90. return [
  91. 'code' => 1,
  92. 'msg' => '商品已下架'
  93. ];
  94. }
  95. }
  96. } else {
  97. if (!$goods) {
  98. return [
  99. 'code' => 1,
  100. 'msg' => '商品已下架'
  101. ];
  102. }
  103. }
  104. $mch = null;
  105. if ($goods->mch_id) {
  106. $mch = $this->getMch($goods);
  107. if (!$mch) {
  108. return [
  109. 'code' => 1,
  110. 'msg' => '店铺已经打烊了哦~'
  111. ];
  112. }
  113. }
  114. $pic_list = GoodsPic::find()->select('pic_url')->where(['goods_id' => $goods->id, 'is_delete' => 0])->asArray()->all();
  115. $is_favorite = 0;
  116. if ($this->user_id) {
  117. $exist_favorite = Favorite::find()->where(['user_id' => $this->user_id, 'goods_id' => $goods->id, 'is_delete' => 0])->exists();
  118. if ($exist_favorite) {
  119. $is_favorite = 1;
  120. }
  121. }
  122. $service_list = explode(',', $goods->service);
  123. // 默认商品服务
  124. if (!$goods->service) {
  125. $option = Option::get('good_services', $this->store_id, 'admin', []);
  126. foreach ($option as $item) {
  127. if (!empty($item['is_default'])) {
  128. $service_list = explode(',', $item['service']);
  129. break;
  130. }
  131. }
  132. }
  133. $new_service_list = [];
  134. if (is_array($service_list)) {
  135. foreach ($service_list as $item) {
  136. $item = trim($item);
  137. if ($item) {
  138. $new_service_list[] = $item;
  139. }
  140. }
  141. }
  142. // 门店逻辑
  143. $is_md = false;
  144. if (get_md_id()) {
  145. $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => get_md_id()]);
  146. if ($md_goods) {
  147. $is_md = true;
  148. }
  149. }
  150. $new_price = $is_md ? $md_goods->price : $goods->price;
  151. $price_attr = $is_md ? Json::decode($md_goods->attr) : Json::decode($goods->attr);
  152. $price = [];
  153. if (is_array($price_attr)) {
  154. foreach ($price_attr as $v) {
  155. if (is_object($v) && $v->price > 0) {
  156. $price[] = $v->price;
  157. } else {
  158. $price[] = floatval($new_price);
  159. }
  160. }
  161. } else {
  162. $price = [$goods->price];
  163. }
  164. $res_url = GetInfo::getVideoInfo($goods->video_url);
  165. $goods->video_url = $res_url['url'];
  166. if ($goods->is_negotiable) {
  167. $min_price = Goods::GOODS_NEGOTIABLE;
  168. } else {
  169. $min_price = sprintf('%.2f', min($price));
  170. }
  171. $res = CommonGoods::getMMPrice([
  172. 'attr' => $is_md ? $md_goods->attr : $goods->attr,
  173. 'attr_setting_type' => $goods->attr_setting_type,
  174. 'share_type' => $goods->share_type,
  175. 'share_commission_first' => $goods->share_commission_first,
  176. 'price' => $new_price,
  177. 'individual_share' => $goods->individual_share,
  178. 'mch_id' => $goods->mch_id,
  179. 'is_level' => $goods->is_level,
  180. 'use_attr' => $goods->use_attr,
  181. ]);
  182. $attr = $price_attr;
  183. $goodsPrice = $new_price;
  184. $isMemberPrice = false;
  185. if ($res['user_is_member'] === true && count($attr) === 1 && $attr[0]['attr_list'][0]['attr_name'] == '默认') {
  186. $goodsPrice = $res['min_member_price'] ? $res['min_member_price'] : $new_price;
  187. $isMemberPrice = true;
  188. }
  189. $GoodsFullMinus = GoodsFullMinus::find()
  190. ->select('*')
  191. ->where(['store_id' => $this->store_id, 'goods_id' => $goods->id])
  192. ->orderBy('full_minus_num ASC')->asArray()->all();
  193. //秒杀活动
  194. $seckill_activity_goods = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $goods->id])
  195. ->leftJoin(['sa' => SeckillActivity::tableName()], 'sag.activity_id = sa.id')
  196. ->andWhere(['AND', ['sa.is_delete' => 0], ['>', 'sa.end_time', time()], ['sag.is_delete' => 0]])
  197. ->select('sa.id, sa.self_limit_num, sa.order_limit_num, sa.start_time, sa.end_time, sag.attr, sag.seckill_num, sag.seckill_price, sag.use_attr, sag.sale_num, sag.virtual_sales')->asArray()->all();
  198. $seckill_activity_arr = [];
  199. if (!empty($seckill_activity_goods)) {
  200. $last_names = array_column($seckill_activity_goods,'start_time');
  201. array_multisort($last_names, SORT_ASC, $seckill_activity_goods);
  202. foreach ($seckill_activity_goods as $activity_good) {
  203. $activity_good['progress'] = sprintf('%.2f', (($activity_good['sale_num'] + $activity_good['virtual_sales']) / ($activity_good['seckill_num'] + $activity_good['virtual_sales'])) * 100);
  204. $activity_good['price'] = $goods->price;
  205. $activity_good['seckill_price'] = sprintf('%.2f', $activity_good['seckill_price']);
  206. if ($activity_good['start_time'] < time()) { //活动进行中 前端展示距离结束
  207. $activity_good['status'] = 1;
  208. $seckill_activity_arr = $activity_good;
  209. goto seckill_activity_arr;
  210. }
  211. if ($activity_good['start_time'] >= time()) {//活动进行中 前端展示距离开始
  212. $activity_good['status'] = 2;
  213. $activity_good['progress'] = 0;
  214. $seckill_activity_arr = $activity_good;
  215. goto seckill_activity_arr;
  216. }
  217. }
  218. seckill_activity_arr:
  219. }
  220. if (!empty($seckill_activity_arr)) {
  221. $seckill_activity_arr['attr'] = json_decode($seckill_activity_arr['attr'], true);
  222. if (!empty($seckill_activity_arr['attr'])) {
  223. foreach ($seckill_activity_arr['attr'] as &$activity_attr) {
  224. $activity_attr['seckill_price'] = sprintf('%.2f', $activity_attr['seckill_price']);
  225. }
  226. }
  227. }
  228. //配送时间展示
  229. $delivery_rules = DeliveryRules::find()->where(['id' => $goods->delivery_rules_id, 'is_delete' => 0, 'status' => 1])->select('id, times, type, days, name')->one();
  230. if ($delivery_rules) {
  231. $days = "下单" . $delivery_rules['days'] . "天后";
  232. $delivery_rules['times'] =
  233. (int)$delivery_rules['type'] === 1 ? $days : date("m月d日 H:i:s", $delivery_rules['times']);
  234. $delivery_rules['type'] .= '';
  235. }
  236. $data = [
  237. 'id' => $goods->id,
  238. 'status' => $goods->status,
  239. 'pic_list' => $pic_list,
  240. 'attr' => $is_md ? $md_goods->attr : $goods->attr,
  241. 'cover_pic' => $goods->cover_pic,
  242. 'is_negotiable' => $goods->is_negotiable,
  243. 'max_price' => sprintf('%.2f', max($price)),
  244. 'min_price' => $min_price,
  245. 'name' => $goods->name,
  246. 'cat_id' => $goods->cat_id,
  247. 'price' => sprintf('%.2f', $goodsPrice),
  248. 'detail' => $goods->detail,
  249. 'sales' => $goods->getSalesVolume() + ($is_md ? $md_goods->virtual_sales : $goods->virtual_sales),
  250. 'attr_group_list' => $goods->getAttrGroupList(),
  251. 'num' => $goods->getNum(),
  252. 'is_favorite' => $is_favorite,
  253. 'service_list' => $new_service_list,
  254. 'original_price' => sprintf('%.2f', $goods->original_price),
  255. 'video_url' => $goods->video_url,
  256. 'unit' => $goods->unit,
  257. 'use_attr' => intval($goods->use_attr),
  258. 'mch' => $mch,
  259. 'max_share_price' => sprintf('%.2f', $res['max_share_price']),
  260. 'min_member_price' => sprintf('%.2f', $res['min_member_price']),
  261. 'is_share' => $res['is_share'],
  262. 'is_level' => $res['is_level'],
  263. 'is_member_price' => $isMemberPrice,
  264. 'full_minus' => $GoodsFullMinus,
  265. 'verify_card_id' => $verify_card,
  266. 'goods_share_title' => $goods->goods_share_title,
  267. 'goods_share_desc' => $goods->goods_share_desc,
  268. 'goods_share_logo' => $goods->goods_share_logo,
  269. 'product_type' => $goods->product_type,
  270. 'confine_count' => $goods->confine_count ?: 0,
  271. 'integral' => json_decode($goods->integral, true) ?: ['give' => 0, 'forehead' => 0, 'forehead_integral' => 0],
  272. 'integral_price' => $goods->integral_price,
  273. 'delivery_rules' => $delivery_rules ?: null,
  274. 'seckill_activity_arr' => $seckill_activity_arr ?: null
  275. ];
  276. $store_integral = Option::get(OptionSetting::STORE_INTEGRAL, get_store_id(), 'gift', Option::get(OptionSetting::STORE_INTEGRAL, get_store_id(), 'store')['value'])['value'];
  277. if ($goods->integral) {
  278. $goods_integral = Json::decode($goods->integral);
  279. if ($goods_integral['forehead'] > 0) {
  280. $data['integral'] = [
  281. 'give' => $goods_integral['give'],
  282. 'forehead' => $goods_integral['forehead'],
  283. 'forehead_integral' => round($store_integral * $goods_integral['forehead'])
  284. ];
  285. }
  286. }
  287. // 处理app上不显示没加协议的图片
  288. $data['detail'] = str_replace('src="//', 'src="https://', $data['detail']);
  289. $now_date = strtotime(date('Y-m-d'));
  290. $goods_book = GoodsBook::findOne(['goods_id' => $goods->id]);
  291. // 预约参数配置
  292. if ($data['product_type'] == Goods::GOODS_TYPE_DATE) {
  293. $data['num'] = Goods::getGoodsNum($goods)['data'];
  294. $data['date_book'] = [
  295. 'date_book' => $data['num'] ,
  296. 'date' => count($data['num'])
  297. ];
  298. if ($goods_book && !empty($goods_book->date_book)) {
  299. $date_book = Json::decode($goods_book->date_book);
  300. $count = 0;
  301. foreach ($date_book as $value) {
  302. if (strtotime($value['date']) >= $now_date) {
  303. $count++;
  304. }
  305. }
  306. // if ($count <= 0) {
  307. // return [
  308. // 'code' => 1,
  309. // 'msg' => '商品预约数据不存在'
  310. // ];
  311. // }
  312. $data['date_book'] = [
  313. 'date_book' => $date_book,
  314. 'date' => count($date_book)
  315. ];
  316. } else {
  317. // return [
  318. // 'code' => 1,
  319. // 'msg' => '该商品暂无可预约数据'
  320. // ];
  321. }
  322. }
  323. if ($data['product_type'] == Goods::GOODS_TYPE_TIME) {
  324. if ($goods_book && !empty($goods_book->service_book)) {
  325. $service_book = Json::decode($goods_book->service_book);
  326. $date_data = $this->getDateByInterval(7);
  327. $count = 0;
  328. $new_arr = [
  329. 'data' => []
  330. ];
  331. $order = Order::find()->alias('o')
  332. ->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id')
  333. ->where(['o.trade_status' => [0, 2, 3], 'o.is_delete' => 0, 'o.order_type' => 2, 'goods_id' => $goods['id']])
  334. ->select('od.attr, od.num')->asArray()->all();
  335. foreach ($date_data as $index => $datum) {
  336. $new_arr['data'][$index]['date'] = $datum;
  337. $new_arr['data'][$index]['time'] = $service_book['data'][0]['time'];
  338. }
  339. foreach ($new_arr['data'] as &$value) {
  340. if (strtotime($value['date']) >= $now_date) {
  341. foreach ($value['time'] as &$m) {
  342. foreach ($m['times'] as &$n) {
  343. $times = explode('-', $n['time']);
  344. if (strtotime(date('Y-m-d H:i:s')) <= strtotime($value['date'] . $times[1])) {
  345. $count++;
  346. }
  347. foreach ($order as $item) {
  348. if ($item['attr']) {
  349. $item['attr'] = json_decode($item['attr'], true);
  350. $order_time = explode('-', $item['attr']['time']);
  351. if (
  352. strtotime($item['attr']['date'] . ' ' . $times[0]) === strtotime($value['date'] . ' ' . $order_time[0]) &&
  353. strtotime($item['attr']['date'] . ' ' . $times[1]) === strtotime($value['date'] . ' ' . $order_time[1])
  354. ) {
  355. $n['num'] = $n['num'] - $item['num'];
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }
  362. }
  363. if ($count <= 0) {
  364. return [
  365. 'code' => 1,
  366. 'msg' => '该商品暂无可预约数据'
  367. ];
  368. }
  369. $data['service_book'] = $new_arr;
  370. } else {
  371. return [
  372. 'code' => 1,
  373. 'msg' => '商品服务预约配置有误'
  374. ];
  375. }
  376. }
  377. $ProductBatch = ProductBatch::find()->where(['goods_id' => $goods->id, 'is_delete' => 0, 'state' => 1])->one();
  378. $data['have_product_traceability'] = $ProductBatch ? 1 : 0;
  379. $data['product_batch_id'] = $ProductBatch ? $ProductBatch->id : 0;
  380. $ruleBuyerLoc = \app\models\OrderRulesBuyerLocation::findOne($goods->order_rules_buyer_location_id);
  381. $data['order_rules_buyer_location'] = $ruleBuyerLoc;
  382. return [
  383. 'code' => 0,
  384. 'data' => $data
  385. ];
  386. }
  387. // 快速给购买商品
  388. public function quickGoods($twocatid)
  389. {
  390. $goods = Goods::find()
  391. ->where([
  392. 'store_id' => $this->store_id,
  393. 'is_delete' => 0,
  394. 'status' => 1,
  395. 'quick_purchase' => 1
  396. ])
  397. ->andWhere([
  398. 'in', 'cat_id', $twocatid
  399. ])->asArray()
  400. ->all();
  401. foreach ($goods as $key => &$value) {
  402. $value['attr'] = json_decode($value['attr']);
  403. foreach ($value['attr'] as $key2 => $value2) {
  404. foreach ($value2->attr_list as $key3 => $value3) {
  405. $value['attr_name'] = $value3->attr_name;
  406. }
  407. $value['num'] = 0;
  408. }
  409. }
  410. return [
  411. 'code' => 0,
  412. 'data' => [
  413. 'list' => $goods,
  414. ],
  415. ];
  416. }
  417. /**
  418. * 获取优惠详情
  419. */
  420. public function getYouhui()
  421. {
  422. if (!$this->validate()) {
  423. return $this->getErrorSummary(false)[0];
  424. }
  425. $data = [];
  426. $goods = Goods::findOne($this->id);
  427. // 是否赠送积分
  428. $integral = json_decode($goods->integral, true);
  429. if ($integral['give'] > 0) {
  430. $data[] = [
  431. 'name' => '赠送积分',
  432. 'content' => '购买赠送' . $integral['give'] . '积分'
  433. ];
  434. }
  435. if ($integral['forehead'] > 0) {
  436. $data[] = [
  437. 'name' => '积分抵扣',
  438. 'content' => '最高可抵扣' . $integral['forehead'] . '元'
  439. ];
  440. }
  441. $goods_card = Goods::getGoodsCard($goods->id);
  442. foreach ($goods_card as $card) {
  443. $data[] = [
  444. 'name' => '卡券',
  445. 'content' => $card['name']
  446. ];
  447. }
  448. // 核销卡列表获取
  449. // 获取商品已添加核销卡
  450. if (empty($goods->verify_card_id)) {
  451. $goods_verify_card_list = [];
  452. } else {
  453. $verify_cards = explode(',', $goods['verify_card_id']);
  454. foreach ($verify_cards as $k => $v) {
  455. $verifyCard = VerifyCard::find()->select(['id', 'name'])->where(['id' => $v, 'is_delete' => 0])->asArray()->one();
  456. if ($verifyCard) {
  457. $goods_verify_card_list[] = $verifyCard;
  458. }
  459. }
  460. }
  461. foreach ((array)$goods_verify_card_list as $verify) {
  462. $data[] = [
  463. 'name' => '核销卡',
  464. 'content' => $verify['name']
  465. ];
  466. }
  467. $coupon_list = Coupon::getList();
  468. $coupon_list_res = [];
  469. foreach ($coupon_list as $coupon) {
  470. if ($coupon['appoint_type'] !== 3) {
  471. if ($coupon['appoint_type'] == 0) {
  472. $coupon_list_res[] = $coupon;
  473. } else {
  474. if ($coupon['appoint_type'] == 2) {
  475. $goods_id_arr = array_column($coupon['goods'], 'id');
  476. if (in_array($goods->id, $goods_id_arr) || empty($goods_id_arr)) {
  477. $coupon_list_res[] = $coupon;
  478. }
  479. } else {
  480. if ($coupon['appoint_type'] == 1) {
  481. $cat_list = GoodsCat::find()->where([
  482. 'goods_id' => $goods->id,
  483. 'is_delete' => 0,
  484. 'store_id' => get_store_id()
  485. ])->all();
  486. $cat_id_arr = array_column($coupon['cat'], 'id');
  487. if (empty($cat_id_arr)) {
  488. $coupon_list_res[] = $coupon;
  489. } else {
  490. foreach ($cat_list as $cat) {
  491. if (in_array($cat->cat_id, $cat_id_arr) || empty($cat_id_arr)) {
  492. $coupon_list_res[] = $coupon;
  493. break;
  494. }
  495. }
  496. }
  497. }
  498. }
  499. }
  500. }
  501. }
  502. $dataRes = [
  503. 'data' => $data,
  504. 'coupon' => $coupon_list_res
  505. ];
  506. return [
  507. 'code' => 0,
  508. 'data' => $dataRes
  509. ];
  510. }
  511. /**
  512. * @description: 获取商品收货地址
  513. * @param {*}
  514. * @return {*}
  515. */
  516. public function getAddress()
  517. {
  518. if ($this->address_id) {
  519. $query = Address::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'id' => $this->address_id]);
  520. if (is_platform()) {
  521. $address = $query->andWhere([
  522. 'user_id' => get_saas_user_id()
  523. ])->one();
  524. } else {
  525. $address = $query->andWhere([
  526. 'user_id' => get_user_id()
  527. ])->one();
  528. }
  529. } else {
  530. $query = Address::find()->where(['store_id' => get_store_id(), 'is_delete' => 0]);
  531. if (is_platform()) {
  532. $query->andWhere([
  533. 'user_id' => get_saas_user_id()
  534. ]);
  535. } else {
  536. $query->andWhere([
  537. 'user_id' => get_user_id()
  538. ]);
  539. }
  540. $address = $query->orderBy(['is_default' => SORT_DESC])->one();
  541. }
  542. $goods = Goods::findOne($this->id);
  543. // 计算运费
  544. $express_price = $this->getExpressPrice($address, $goods);
  545. // 计算区域限制购买
  546. $is_area = $this->getTerritorialLimitation($address, $goods);
  547. $shop_id = null;
  548. $shop = null;
  549. if ($this->shop_id) {
  550. $shop_id = $this->shop_id;
  551. } else {
  552. $order = Order::find()->where([
  553. 'is_offline' => 1,
  554. 'user_id' => get_user_id()
  555. ])->orderBy(['id' => SORT_DESC])->one();
  556. if ($order) {
  557. $shop_id = $order->shop_id;
  558. }
  559. }
  560. if ($shop_id) {
  561. $shop = Shop::findOne($shop_id);
  562. }
  563. return [
  564. 'code' => 0,
  565. 'data' => [
  566. 'address' => $address,
  567. 'shop' => $shop,
  568. 'express_price' => $express_price,
  569. 'is_area' => $is_area
  570. ]
  571. ];
  572. }
  573. /**
  574. * 门店推荐
  575. */
  576. public function mchRecommend()
  577. {
  578. $goods_list = Goods::find()->where([
  579. 'mch_id' => $this->mch_id,
  580. 'status' => 1,
  581. 'is_delete' => 0
  582. ])->orderBy(['sort' => SORT_ASC])
  583. ->select(['id', 'name', 'cover_pic', 'price'])->limit(6)->all();
  584. return [
  585. 'code' => 0,
  586. 'data' => [
  587. 'list' => $goods_list
  588. ]
  589. ];
  590. }
  591. /**
  592. * 猜你喜欢
  593. */
  594. public function guessLike()
  595. {
  596. $cat_list = GoodsCat::find()->where(['goods_id' => $this->id, 'is_delete' => 0])->all();
  597. $cat_id = [];
  598. foreach ($cat_list as $val) {
  599. $cat_id[] = $val->cat_id;
  600. }
  601. $goods_list = Goods::find()->alias('g')
  602. ->leftJoin(['gc' => GoodsCat::tableName()], 'gc.goods_id=g.id')
  603. ->where([
  604. 'g.status' => 1,
  605. 'g.is_delete' => 0,
  606. 'gc.cat_id' => $cat_id
  607. ])->orderBy(['g.sort' => SORT_DESC])
  608. ->select(['g.id', 'g.name', 'g.cover_pic', 'g.price'])->groupBy('gc.goods_id')->limit(6)->all();
  609. return [
  610. 'code' => 0,
  611. 'data' => [
  612. 'list' => $goods_list
  613. ]
  614. ];
  615. }
  616. /**
  617. * 计算单个商品的运费
  618. */
  619. protected function getExpressPrice($address, $goods)
  620. {
  621. $expressPrice = 0;
  622. if ($address) {
  623. //再通过运费规则计算运费
  624. $expressPrice = PostageRules::getExpressPrice($goods->store_id, $address->city_id, $goods, 1, $address->province_id);
  625. }
  626. return $expressPrice >= 0 ? $expressPrice : 0;
  627. }
  628. /**
  629. * 计算单个商品是否区域限制购买
  630. */
  631. protected function getTerritorialLimitation($address, $goods)
  632. {
  633. $isArea = 0;
  634. if ($address) {
  635. $area = TerritorialLimitation::findOne([
  636. 'store_id' => get_store_id(),
  637. 'is_delete' => 0,
  638. 'is_enable' => 1,
  639. ]);
  640. if ($area) {
  641. $city_id = []; //限制的地区ID
  642. $detail = json_decode($area->detail);
  643. if (!is_array($detail)) {
  644. $detail = [];
  645. }
  646. foreach ($detail as $key => $value) {
  647. foreach ($value->province_list as $key2 => $value2) {
  648. $city_id[] = $value2->id;
  649. }
  650. }
  651. $addressArr = [
  652. $address['province_id'],
  653. $address['city_id'],
  654. $address['district_id']
  655. ];
  656. $addressArray = array_intersect($addressArr, $city_id);
  657. if (empty($addressArray)) {
  658. $isArea = 1;
  659. }
  660. }
  661. }
  662. return $isArea;
  663. }
  664. /**
  665. * User: chiyanying
  666. * Date: 2020/9/27
  667. * Time: 17:27
  668. * Notes:获取单个商品信息
  669. */
  670. public static function getGoods($goods_id = 0, $tore_id = 0, $type = 0)
  671. {
  672. if ($type == 0) {
  673. $form = new GoodsForm();
  674. $form->id = $goods_id;
  675. $form->store_id = $tore_id;
  676. return $form->search()['data'];
  677. }
  678. }
  679. /**
  680. * 获取店铺信息
  681. */
  682. public function getMch($goods)
  683. {
  684. $f = new ShopDataForm();
  685. $f->mch_id = $goods->mch_id;
  686. $shop = $f->getShop();
  687. if (isset($shop['code']) && $shop['code'] == 1) {
  688. return null;
  689. }
  690. return $shop;
  691. }
  692. public function getDateByInterval(int $num) :array
  693. {
  694. //var_dump($st, $et);die;
  695. $returnData = [];
  696. $i = 0;
  697. do {
  698. $temp = date('Y-m-d', strtotime('+' . $i . ' day', strtotime(date('Y-m-d'))));
  699. $returnData[] = $temp;
  700. $i++;
  701. } while ($i < $num);
  702. return $returnData;
  703. }
  704. }