| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\adopt\models\client;
- use app\models\Address;
- use app\plugins\adopt\models\AdoptGoodsDevice;
- use app\models\Coupon;
- use app\models\GoodsBook;
- use app\models\GoodsCat;
- use app\models\GoodsFullMinus;
- use app\models\MchGoodsCat;
- use app\models\MdGoods;
- use app\models\Option;
- use app\models\VerifyCard;
- use app\models\Favorite;
- use app\models\Goods;
- use app\models\GoodsPic;
- use app\models\Order;
- use app\models\PostageRules;
- use app\models\Shop;
- use app\models\TerritorialLimitation;
- use app\modules\client\models\v1\common\CommonGoods;
- use app\modules\client\models\v1\diy\GetInfo;
- use app\modules\client\models\v1\mch\ShopDataForm;
- use app\plugins\adopt\models\AdoptGoods;
- use app\utils\Tools;
- use yii\base\Model;
- use yii\helpers\Json;
- class AdoptGoodsForm extends Model
- {
- public $id;
- public $user_id;
- public $store_id;
- public $mch_id;
- public $shop_id;
- public $address_id;
- public $verify_card_id;
- public function rules()
- {
- return [
- [['id'], 'required'],
- [['mch_id', 'shop_id', 'address_id'], 'integer'],
- [['user_id'], 'safe'],
- ];
- }
- /**
- * 排序类型$sort 1--综合排序 2--销量排序
- */
- public function search()
- {
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $goods = Goods::findOne([
- 'id' => $this->id,
- 'is_delete' => 0,
- 'status' => 1,
- 'store_id' => $this->store_id,
- 'product_type' => Goods::GOODS_TYPE_ADOPT,
- ]);
- $verify_card = [];
- if (!empty($goods->verify_card_id)) {
- $ids = explode(',', $goods->verify_card_id);
- $verify_card = Tools::getVerifyList($ids);
- } else {
- $verify_card = [];
- }
- if (get_md_id()) {
- $md_goods = MdGoods::findOne(['md_id' => get_md_id(), 'goods_id' => $this->id]);
- if ($md_goods) {
- if ($md_goods->status == 0) {
- return [
- 'code' => 1,
- 'msg' => '商品已下架'
- ];
- }
- } else {
- if (!$goods) {
- return [
- 'code' => 1,
- 'msg' => '商品已下架'
- ];
- }
- }
- } else {
- if (!$goods) {
- return [
- 'code' => 1,
- 'msg' => '商品已下架'
- ];
- }
- }
- $mch = null;
- if ($goods->mch_id) {
- $mch = $this->getMch($goods);
- if (!$mch) {
- return [
- 'code' => 1,
- 'msg' => '店铺已经打烊了哦~'
- ];
- }
- }
- $pic_list = GoodsPic::find()->select('pic_url')->where(['goods_id' => $goods->id, 'is_delete' => 0])->asArray()->all();
- $is_favorite = 0;
- if ($this->user_id) {
- $exist_favorite = Favorite::find()->where(['user_id' => $this->user_id, 'goods_id' => $goods->id, 'is_delete' => 0])->exists();
- if ($exist_favorite) {
- $is_favorite = 1;
- }
- }
- $service_list = explode(',', $goods->service);
- // 默认商品服务
- if (!$goods->service) {
- $option = Option::get('good_services', $this->store_id, 'admin', []);
- foreach ($option as $item) {
- if (!empty($item['is_default'])) {
- $service_list = explode(',', $item['service']);
- break;
- }
- }
- }
- $new_service_list = [];
- if (is_array($service_list)) {
- foreach ($service_list as $item) {
- $item = trim($item);
- if ($item) {
- $new_service_list[] = $item;
- }
- }
- }
- // 门店逻辑
- $is_md = false;
- if (get_md_id()) {
- $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => get_md_id()]);
- if ($md_goods) {
- $is_md = true;
- }
- }
- $new_price = $is_md ? $md_goods->price : $goods->price;
- $price_attr = $is_md ? Json::decode($md_goods->attr) : Json::decode($goods->attr);
- $price = [];
- if (is_array($price_attr)) {
- foreach ($price_attr as $v) {
- if (is_object($v) && $v->price > 0) {
- $price[] = $v->price;
- } else {
- $price[] = floatval($new_price);
- }
- }
- } else {
- $price = [$goods->price];
- }
- $res_url = GetInfo::getVideoInfo($goods->video_url);
- $goods->video_url = $res_url['url'];
- if ($goods->is_negotiable) {
- $min_price = Goods::GOODS_NEGOTIABLE;
- } else {
- $min_price = sprintf('%.2f', min($price));
- }
- $res = CommonGoods::getMMPrice([
- 'attr' => $is_md ? $md_goods->attr : $goods->attr,
- 'attr_setting_type' => $goods->attr_setting_type,
- 'share_type' => $goods->share_type,
- 'share_commission_first' => $goods->share_commission_first,
- 'price' => $new_price,
- 'individual_share' => $goods->individual_share,
- 'mch_id' => $goods->mch_id,
- 'is_level' => $goods->is_level,
- 'use_attr' => $goods->use_attr,
- ]);
- $attr = $price_attr;
- $goodsPrice = $new_price;
- $isMemberPrice = false;
- if ($res['user_is_member'] === true && count($attr) === 1 && $attr[0]['attr_list'][0]['attr_name'] == '默认') {
- $goodsPrice = $res['min_member_price'] ? $res['min_member_price'] : $new_price;
- $isMemberPrice = true;
- }
- $GoodsFullMinus = GoodsFullMinus::find()
- ->select('*')
- ->where(['store_id' => $this->store_id, 'goods_id' => $goods->id])
- ->orderBy('full_minus_num ASC')->asArray()->all();
- $adopt_goods = AdoptGoods::findOne([
- 'goods_id' => $this->id,
- 'is_delete' => 0,
- 'store_id' => $this->store_id,
- ]);
- $device_id = AdoptGoodsDevice::find()->where(['goods_id'=>$goods['id'],'is_delete'=>0])->select('device_id')->column();
- $data = [
- 'id' => $goods->id,
- 'status' => $goods->status,
- 'pic_list' => $pic_list,
- 'attr' => $is_md ? $md_goods->attr : $goods->attr,
- 'cover_pic' => $goods->cover_pic,
- 'is_negotiable' => $goods->is_negotiable,
- 'max_price' => sprintf('%.2f', max($price)),
- 'min_price' => $min_price,
- 'name' => $goods->name,
- 'cat_id' => $goods->cat_id,
- 'price' => sprintf('%.2f', $goodsPrice),
- 'detail' => $goods->detail,
- 'sales' => $goods->getSalesVolume() + ($is_md ? $md_goods->virtual_sales : $goods->virtual_sales),
- 'attr_group_list' => $goods->getAttrGroupList(),
- 'num' => $goods->getNum(),
- 'is_favorite' => $is_favorite,
- 'service_list' => $new_service_list,
- 'original_price' => sprintf('%.2f', $goods->original_price),
- 'video_url' => $goods->video_url,
- 'unit' => $goods->unit,
- 'use_attr' => intval($goods->use_attr),
- 'mch' => $mch,
- 'max_share_price' => sprintf('%.2f', $res['max_share_price']),
- 'min_member_price' => sprintf('%.2f', $res['min_member_price']),
- 'is_share' => $res['is_share'],
- 'is_level' => $res['is_level'],
- 'is_member_price' => $isMemberPrice,
- 'full_minus' => $GoodsFullMinus,
- 'verify_card_id' => $verify_card,
- 'goods_share_title' => $goods->goods_share_title,
- 'goods_share_desc' => $goods->goods_share_desc,
- 'goods_share_logo' => $goods->goods_share_logo,
- 'product_type' => $goods->product_type,
- 'growth_cycle' => $adopt_goods->growth_cycle,
- 'desc' => $adopt_goods->desc,
- 'yield' => $adopt_goods->yield,
- 'varieties' => $adopt_goods->varieties,
- 'adopt_income' => Json::decode($adopt_goods->adopt_income),
- 'self_pick_date' => Json::decode($adopt_goods->self_pick_date),
- 'self_pick_times' => Json::decode($adopt_goods->self_pick_times),
- 'remaining_num' => $goods->getNum() - ($goods->getSalesVolume() + ($is_md ? $md_goods->virtual_sales : $goods->virtual_sales)),
- 'device_id' => $device_id,
- 'integral' => json_decode($goods->integral, true),
- ];
- $now_date = strtotime(date('Y-m-d'));
- $goods_book = GoodsBook::findOne(['goods_id' => $goods->id]);
- // 预约参数配置
- if ($data['product_type'] == Goods::GOODS_TYPE_DATE) {
- if ($goods_book && !empty($goods_book->date_book)) {
- $date_book = Json::decode($goods_book->date_book);
- $count = 0;
- foreach ($date_book as $value) {
- if (strtotime($value['date']) >= $now_date) {
- $count++;
- }
- }
- if ($count <= 0) {
- return [
- 'code' => 1,
- 'msg' => '商品预约数据不存在'
- ];
- }
- $data['date_book'] = [
- 'date_book' => $date_book,
- 'date' => count($date_book)
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '该商品暂无可预约数据'
- ];
- }
- }
- if ($data['product_type'] == Goods::GOODS_TYPE_TIME) {
- if ($goods_book && !empty($goods_book->service_book)) {
- $service_book = Json::decode($goods_book->service_book);
- $count = 0;
- foreach ($service_book['data'] as &$value) {
- if (strtotime($value['date']) >= $now_date) {
- foreach ($value['time'] as &$m) {
- foreach ($m['times'] as &$n) {
- if (strtotime(date('Y-m-d H:i:s')) <= strtotime(date('Y-m-d ' . $n['time']))) {
- $count++;
- }
- }
- }
- }
- }
- if ($count <= 0) {
- return [
- 'code' => 1,
- 'msg' => '该商品暂无可预约数据'
- ];
- }
- $data['service_book'] = $service_book;
- } else {
- return [
- 'code' => 1,
- 'msg' => '商品服务预约配置有误'
- ];
- }
- }
- return [
- 'code' => 0,
- 'data' => $data
- ];
- }
- // 快速给购买商品
- public function quickGoods($twocatid)
- {
- $goods = Goods::find()
- ->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'status' => 1,
- 'quick_purchase' => 1
- ])
- ->andWhere([
- 'in', 'cat_id', $twocatid
- ])->asArray()
- ->all();
- foreach ($goods as $key => &$value) {
- $value['attr'] = json_decode($value['attr']);
- foreach ($value['attr'] as $key2 => $value2) {
- foreach ($value2->attr_list as $key3 => $value3) {
- $value['attr_name'] = $value3->attr_name;
- }
- $value['num'] = 0;
- }
- }
- return [
- 'code' => 0,
- 'data' => [
- 'list' => $goods,
- ],
- ];
- }
- /**
- * 获取优惠详情
- */
- public function getYouhui()
- {
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $data = [];
- $goods = Goods::findOne($this->id);
- // 是否赠送积分
- $integral = json_decode($goods->integral, true);
- if ($integral['give'] > 0) {
- $data[] = [
- 'name' => '赠送积分',
- 'content' => '购买赠送' . $integral['give'] . '积分'
- ];
- }
- if ($integral['forehead'] > 0) {
- $data[] = [
- 'name' => '积分抵扣',
- 'content' => '最高可抵扣' . $integral['forehead'] . '元'
- ];
- }
- $goods_card = Goods::getGoodsCard($goods->id);
- foreach ($goods_card as $card) {
- $data[] = [
- 'name' => '卡券',
- 'content' => $card['name']
- ];
- }
- // 核销卡列表获取
- // 获取商品已添加核销卡
- if (empty($goods->verify_card_id)) {
- $goods_verify_card_list = [];
- } else {
- $verify_cards = explode(',', $goods['verify_card_id']);
- foreach ($verify_cards as $k => $v) {
- $verifyCard = VerifyCard::find()->select(['id', 'name'])->where(['id' => $v, 'is_delete' => 0])->asArray()->one();
- if ($verifyCard) {
- $goods_verify_card_list[] = $verifyCard;
- }
- }
- }
- foreach ((array)$goods_verify_card_list as $verify) {
- $data[] = [
- 'name' => '核销卡',
- 'content' => $verify['name']
- ];
- }
- $coupon_list = Coupon::getList($goods->mch_id);
- $coupon_list_res = [];
- foreach ($coupon_list as $coupon) {
- if ($coupon['appoint_type'] !== 3) {
- if ($coupon['appoint_type'] == 0) {
- $coupon_list_res[] = $coupon;
- } else {
- if ($coupon['appoint_type'] == 2) {
- $goods_id_arr = array_column($coupon['goods'], 'id');
- if (in_array($goods->id, $goods_id_arr) || empty($goods_id_arr)) {
- $coupon_list_res[] = $coupon;
- }
- } else {
- if ($coupon['appoint_type'] == 1) {
- if ($goods->mch_id > 0) {
- $cat_list = MchGoodsCat::find()->where([
- 'goods_id' => $goods->id,
- ])->all();
- } else {
- $cat_list = GoodsCat::find()->where([
- 'goods_id' => $goods->id,
- 'is_delete' => 0,
- 'store_id' => get_store_id()
- ])->all();
- }
- $cat_id_arr = array_column($coupon['cat'], 'id');
- if (empty($cat_id_arr)) {
- $coupon_list_res[] = $coupon;
- } else {
- foreach ($cat_list as $cat) {
- if (in_array($cat->cat_id, $cat_id_arr) || empty($cat_id_arr)) {
- $coupon_list_res[] = $coupon;
- break;
- }
- }
- }
- }
- }
- }
- }
- }
- $dataRes = [
- 'data' => $data,
- 'coupon' => $coupon_list_res
- ];
- return [
- 'code' => 0,
- 'data' => $dataRes
- ];
- }
- /**
- * @description: 获取商品收货地址
- * @param {*}
- * @return {*}
- */
- public function getAddress()
- {
- if ($this->address_id) {
- $query = Address::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'id' => $this->address_id]);
- if (is_platform()) {
- $address = $query->andWhere([
- 'user_id' => get_saas_user_id()
- ])->one();
- } else {
- $address = $query->andWhere([
- 'user_id' => get_user_id()
- ])->one();
- }
- } else {
- $query = Address::find()->where(['store_id' => get_store_id(), 'is_delete' => 0]);
- if (is_platform()) {
- $query->andWhere([
- 'user_id' => get_saas_user_id()
- ]);
- } else {
- $query->andWhere([
- 'user_id' => get_user_id()
- ]);
- }
- $address = $query->orderBy(['is_default' => SORT_DESC])->one();
- }
- $goods = Goods::findOne($this->id);
- // 计算运费
- $express_price = $this->getExpressPrice($address, $goods);
- // 计算区域限制购买
- $is_area = $this->getTerritorialLimitation($address, $goods);
- $shop_id = null;
- $shop = null;
- if ($this->shop_id) {
- $shop_id = $this->shop_id;
- } else {
- $order = Order::find()->where([
- 'is_offline' => 1,
- 'user_id' => get_user_id()
- ])->orderBy(['id' => SORT_DESC])->one();
- if ($order) {
- $shop_id = $order->shop_id;
- }
- }
- if ($shop_id) {
- $shop = Shop::findOne($shop_id);
- }
- return [
- 'code' => 0,
- 'data' => [
- 'address' => $address,
- 'shop' => $shop,
- 'express_price' => $express_price,
- 'is_area' => $is_area
- ]
- ];
- }
- /**
- * 门店推荐
- */
- public function mchRecommend()
- {
- $goods_list = Goods::find()->where([
- 'mch_id' => $this->mch_id,
- 'status' => 1,
- 'is_delete' => 0
- ])->orderBy(['sort' => SORT_ASC])
- ->select(['id', 'name', 'cover_pic', 'price'])->limit(6)->all();
- return [
- 'code' => 0,
- 'data' => [
- 'list' => $goods_list
- ]
- ];
- }
- /**
- * 猜你喜欢
- */
- public function guessLike()
- {
- $cat_list = GoodsCat::find()->where(['goods_id' => $this->id, 'is_delete' => 0])->all();
- $cat_id = [];
- foreach ($cat_list as $val) {
- $cat_id[] = $val->cat_id;
- }
- $goods_list = Goods::find()->alias('g')
- ->leftJoin(['gc' => GoodsCat::tableName()], 'gc.goods_id=g.id')
- ->where([
- 'g.status' => 1,
- 'g.is_delete' => 0,
- 'gc.cat_id' => $cat_id
- ])->orderBy(['g.sort' => SORT_DESC])
- ->select(['g.id', 'g.name', 'g.cover_pic', 'g.price'])->groupBy('gc.goods_id')->limit(6)->all();
- return [
- 'code' => 0,
- 'data' => [
- 'list' => $goods_list
- ]
- ];
- }
- /**
- * 计算单个商品的运费
- */
- protected function getExpressPrice($address, $goods)
- {
- $expressPrice = 0;
- if ($address) {
- //再通过运费规则计算运费
- $expressPrice = PostageRules::getExpressPrice($goods->store_id, $address->city_id, $goods, 1, $address->province_id);
- }
- return $expressPrice >= 0 ? $expressPrice : 0;
- }
- /**
- * 计算单个商品是否区域限制购买
- */
- protected function getTerritorialLimitation($address, $goods)
- {
- $isArea = 0;
- if ($address) {
- $area = TerritorialLimitation::findOne([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- 'is_enable' => 1,
- ]);
- if ($area) {
- $city_id = []; //限制的地区ID
- $detail = json_decode($area->detail);
- if (!is_array($detail)) {
- $detail = [];
- }
- foreach ($detail as $key => $value) {
- foreach ($value->province_list as $key2 => $value2) {
- $city_id[] = $value2->id;
- }
- }
- $addressArr = [
- $address['province_id'],
- $address['city_id'],
- $address['district_id']
- ];
- $addressArray = array_intersect($addressArr, $city_id);
- if (empty($addressArray)) {
- $isArea = 1;
- }
- }
- }
- return $isArea;
- }
- /**
- * User: chiyanying
- * Date: 2020/9/27
- * Time: 17:27
- * Notes:获取单个商品信息
- */
- public static function getGoods($goods_id = 0, $tore_id = 0, $type = 0)
- {
- if ($type == 0) {
- $form = new GoodsForm();
- $form->id = $goods_id;
- $form->store_id = $tore_id;
- return $form->search()['data'];
- }
- }
- /**
- * 获取店铺信息
- */
- public function getMch($goods)
- {
- $f = new ShopDataForm();
- $f->mch_id = $goods->mch_id;
- $shop = $f->getShop();
- if (isset($shop['code']) && $shop['code'] == 1) {
- return null;
- }
- return $shop;
- }
- }
|