| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\constants\OptionSetting;
- use app\models\AboutArticle;
- use app\models\ActivityRebateOrderN;
- use app\models\Address;
- use app\models\Cart;
- use app\models\Cat;
- use app\models\Coupon;
- use app\models\DeliveryRules;
- use app\models\FreeQueue;
- use app\models\FreeQueueGoods;
- use app\models\GoodsBook;
- use app\models\GoodsBrowse;
- use app\models\GoodsCat;
- use app\models\GoodsFullMinus;
- use app\models\IntegralAppreciationGoods;
- use app\models\Level;
- use app\models\material\Material;
- use app\models\MchGoodsCat;
- use app\models\Md;
- use app\models\MdGoods;
- use app\models\MdGroupActivities;
- use app\models\MdGroupActivitiesGoods;
- use app\models\Option;
- use app\models\OrderDetail;
- use app\models\PtActivity;
- use app\models\PtActivityGoods;
- use app\models\PtActivityOrder;
- use app\models\PtActivityOrderDetail;
- use app\models\SaasUser;
- use app\models\SeckillActivity;
- use app\models\SeckillActivityGoods;
- use app\models\Share;
- use app\models\ShareLevel;
- use app\models\User;
- use app\models\Store;
- 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\ActivityNewUser;
- use app\models\ActivityNewUserGoods;
- use app\models\ActivityWechatRoom;
- use app\models\ActivityWechatRoomGoods;
- use app\models\ActivityCutPrice;
- use app\modules\admin\models\ActivityCutPriceForm;
- use app\models\ActivityCutPriceGoods;
- use app\modules\admin\models\givingGifts\GivingGiftsForm;
- use app\models\GoodsLoweringPrice;
- 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\product_traceability\models\ProductBatch;
- use app\utils\Notice\NoticeAction;
- use app\utils\Tools;
- use yii\base\Model;
- use yii\helpers\Json;
- class GoodsForm extends Model
- {
- public $mini_id;
- public $id;
- public $user_id;
- public $store_id;
- public $mch_id;
- public $shop_id;
- public $address_id;
- public $verify_card_id;
- public $activity_new_user_id = 0;
- public $activity_cut_price_id = 0;
- public $is_pt;
- public $is_seckill;
- public $cat_id;
- public $md_id;
- public function rules()
- {
- return [
- [['id'], 'required'],
- [['mch_id', 'shop_id', 'address_id', 'cat_id', 'md_id'], 'integer'],
- [['mini_id', 'user_id', 'activity_new_user_id', 'activity_cut_price_id', 'is_pt', 'is_seckill'], 'safe'],
- ];
- }
- /**
- * 排序类型$sort 1--综合排序 2--销量排序
- */
- public function search()
- {
- $cacheKey = $this->attributes;
- // unset($cacheKey['user_id']);
- // $cacheVal = cache()->get($cacheKey);
- // if($cacheVal){
- // return array_merge($cacheVal, ['_cache' => 1]);
- // }
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $where = [
- 'id' => $this->id,
- 'is_delete' => 0,
- 'status' => 1,
- 'store_id' => $this->store_id
- ];
- if (\Yii::$app->prod_is_dandianpu()) {
- unset($where['store_id']);
- }
- $result = StoreConfigForm::getStoreSwitch($this->store_id);
- if ($result['is_switch_store']) {
- $this->store_id = $where['store_id'] = $result['store_id'];
- }
- $goods = Goods::findOne($where);
- //加价保护start
- $platform_profit_strategy = Option::get('platform_profit_strategy', 0, 'store')['value'];
- $platform_profit_strategy = json_decode($platform_profit_strategy, true);
- if (!empty($platform_profit_strategy) && $goods->cloud_goods_id > 0) {
- if (intval($platform_profit_strategy['is_risk_control'])) {
- //开启风控
- $id = \Yii::$app->cache->get('risk_control_model_id_' . $this->id);
- if ($id && !\Yii::$app->queue->isDone($id)) {//\Yii::$app->queue->status($id)
- \Yii::$app->queue->remove($id);
- }
- $id =\queue_push(new \app\jobs\RiskControlModelJob(['goods_id' => $this->id]));
- \Yii::$app->cache->set('risk_control_model_id_' . $this->id, $id);
- }
- }
- //加价保护end
- $goodsBrowse = GoodsBrowse::find()->where(['goods_id' => $this->id])->select('level')->column();
- if(!empty($goodsBrowse) && (empty(get_user()->level) || !in_array(get_user()->level, $goodsBrowse))){
- return ['code' => 1,'msg' => '指定会员等级用户才能浏览此商品'];
- }
- \app\modules\admin\models\erp\InventoryForm::erp2Goods($this->store_id, $goods);
- \app\modules\admin\models\jushuitan\JuShuiTanForm::syncStoreJstGoodsQty($this->store_id, $goods);
- if (!empty($goods->verify_card_id)) {
- $ids = explode(',', $goods->verify_card_id);
- $verify_card = Tools::getVerifyList($ids);
- } else {
- $verify_card = [];
- }
- $md_is_single = 0;
- if (get_md_id()) {
- $md = Md::findOne(get_md_id());
- if ($md->is_single) {
- $md_is_single = 1;
- $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' => '商品已下架'
- ];
- }
- }
- } 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 && $md_is_single) {
- $is_md = true;
- }
- }
- //展示新人专享活动商品信息
- // if($this->activity_new_user_id){
- $this->activity_new_user($this->activity_new_user_id, $goods, $md_goods);
- // }
- //展示砍价活动商品信息
- $cutGoods = null;
- if($this->activity_cut_price_id){
- $cutGoods = ActivityCutPriceGoods::findOne(['activity_id' => $this->activity_cut_price_id, 'goods_id' => $goods->id, 'is_delete' => 0]);
- // $this->activity_cut_price($this->activity_cut_price_id, $goods, $md_goods);
- }
- //群活动商品信息
- $activityWechatRoom = $this->activity_wechat_room($goods, $md_goods);
- $new_price = $is_md ? $md_goods->price : $goods->price;
- $price_attr = $is_md ? Json::decode($md_goods->attr) : Json::decode($goods->attr);
- $price = [];
- // 是否是团购活动商品
- $activityModel = MdGroupActivitiesGoods::find()->alias('mgag')->leftJoin(['mga'=>MdGroupActivities::tableName()],'mga.id=mgag.activities_id')->where(['goods_id'=>$goods->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();
- if (is_array($price_attr)) {
- if($activityModel){
- $group_price_attr = Json::decode($activityModel['attr']);
- foreach ($group_price_attr as $v) {
- if (is_object($v) && $v->group_price > 0) {
- $price[] = $v->group_price;
- } else {
- $price[] = floatval($activityModel['price']);
- }
- }
- }else{
- foreach ($price_attr as $v) {
- if (is_object($v) && $v->price > 0) {
- $price[] = $v->price;
- } else {
- $price[] = floatval($new_price);
- }
- }
- }
- } else {
- if($activityModel){
- $price = [$activityModel['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' => $activityModel ? $activityModel['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' => $activityModel ? $activityModel['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();
- $seckill_activity_arr = null;
- $seckill_activity_goods = [];
- if ($this->is_seckill) {
- //秒杀活动
- $seckill_activity_goods = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $goods->id])
- ->leftJoin(['sa' => SeckillActivity::tableName()], 'sag.activity_id = sa.id')
- ->andWhere(['AND', ['sa.is_delete' => 0], ['>', 'sa.end_time', time()], ['sag.is_delete' => 0]])
- ->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, sag.virtual_num')->asArray()->all();
- }
- $level = null;
- //获取下一会员等级
- if (get_user()) {
- $level = Level::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => Level::NOT_DELETE
- ])->andWhere(['AND', ['>', 'level', get_user()->level], ['>', 'price', 0]])->select('id, level, name, discount')->orderBy('level ASC')->asArray()->one();
- if ($level) {
- $level['goods_price'] = $res['min_next_member_price'] > 0 ? sprintf('%.2f', $res['min_next_member_price']) : sprintf('%.2f', ($goods->price * ($level['discount'] / 10)));
- //20240816前端要显示下个会员等级预计省多钱
- $min_member_price = $res['min_member_price'];
- if ($min_member_price <= 0) {
- $min_member_price = $goods->price;
- }
- $level['diff_price'] = bcsub($min_member_price, $level['goods_price'], 2);
- if ($level['diff_price'] <= 0) {
- $level = null;
- }
- }
- }
- if (!empty($seckill_activity_goods) && empty($this->activity_new_user_id) && empty($this->activity_cut_price_id) && empty($this->is_pt)) {
- $last_names = array_column($seckill_activity_goods,'start_time');
- array_multisort($last_names, SORT_ASC, $seckill_activity_goods);
- foreach ($seckill_activity_goods as $activity_good) {
- if (($activity_good['seckill_num'] + $activity_good['sale_num'] + $activity_good['virtual_sales']) > 0) {
- $activity_good['progress'] = sprintf('%.2f', (($activity_good['sale_num'] + $activity_good['virtual_sales']) / ($activity_good['seckill_num'] + $activity_good['sale_num'] + $activity_good['virtual_sales'])) * 100);
- } else {
- $activity_good['progress'] = 0;
- }
- $activity_good['seckill_num'] <= 0 && $activity_good['progress'] = 100;
- $activity_good['price'] = $new_price;
- $activity_good['seckill_price'] = sprintf('%.2f', $activity_good['seckill_price']);
- if ($activity_good['start_time'] < time()) { //活动进行中 前端展示距离结束
- $activity_good['status'] = 1;
- $seckill_activity_arr = $activity_good;
- goto seckill_activity_arr;
- }
- if ($activity_good['start_time'] >= time()) {//活动进行中 前端展示距离开始
- $activity_good['status'] = 2;
- $activity_good['progress'] = 0;
- $seckill_activity_arr = $activity_good;
- goto seckill_activity_arr;
- }
- }
- seckill_activity_arr:
- }
- if (!empty($seckill_activity_arr)) {
- $seckill_activity_arr['attr'] = json_decode($seckill_activity_arr['attr'], true);
- if (!empty($seckill_activity_arr['attr'])) {
- foreach ($seckill_activity_arr['attr'] as &$activity_attr) {
- $activity_attr['seckill_price'] = sprintf('%.2f', $activity_attr['seckill_price']);
- }
- }
- }
- //拼团活动信息
- $ptGoodsActivity = PtActivityGoods::find()->alias('pag')->where(['pag.is_delete' => 0, 'pa.is_delete' => 0, 'pa.store_id' => get_store_id()])
- ->leftJoin(['pa' => PtActivity::tableName()], 'pag.activity_id = pa.id')
- ->andWhere(['pag.goods_id' => $goods->id])
- ->select('pag.pt_price, pag.sale_num, pag.virtual_sales, pag.attr, pa.party_size, pa.party_type, pa.party_goods_count, pa.split_time, pa.head_integral, pa.head_is_free')
- ->andWhere(['AND', ['<', 'pa.start_time', time()], ['>', 'pa.end_time', time()]])->asArray()->one();
- if ($ptGoodsActivity && !empty($this->is_pt)) {
- $split_time = $ptGoodsActivity['split_time'];
- $party_size = $ptGoodsActivity['party_size'];
- $party_type = $ptGoodsActivity['party_type'];
- $party_goods_count = $ptGoodsActivity['party_goods_count'];
- //当前商品拼团数据
- $pt_arr = PtActivityOrderDetail::find()->alias('pod')
- ->where(['store_id' => get_store_id(), 'pod.goods_id' => $goods->id])
- ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id')
- ->andWhere(['po.pt_number' => 0, 'po.is_pay' => 1, 'po.is_pt_finish' => 0])
- ->select('po.id, po.pay_time')->orderBy('po.created_at desc')->asArray()->all();
- $arr = [];
- foreach ($pt_arr as $index => $item) {
- $arr[$index]['id'] = $item['id'];
- $end_time = ($item['pay_time'] + ($split_time * 60 * 60)) - time();
- $arr[$index]['end_time'] = $end_time > 0 ? $end_time : 0;
- $arr[$index]['user_list'] = PtActivityOrder::find()->alias('o')->where(['o.is_pay' => 1])
- ->leftJoin(['u' => User::tableName()], 'o.user_id = u.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
- ->andWhere(['OR' , ['o.id' => $item['id']], ['o.pt_number' => $item['id']]])
- ->select('su.avatar, su.name')->limit(2)->asArray()->all();
- $arr[$index]['surplus_num'] = $party_size - count($arr[$index]['user_list']);
- $arr[$index]['party_type'] = $party_type;
- $arr[$index]['surplus_num_goods'] = 0;
- if($party_type == 1){
- $goodsCountQuery = PtActivityOrderDetail::find()->alias('pod')
- ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id')
- ->where(['po.is_pay' => 1, 'pod.is_delete' => 0])
- ->andWhere(['OR' , ['po.id' => $item['id']], ['po.pt_number' => $item['id']]]);
- $goodsCount = $goodsCountQuery->sum('pod.num');
- $arr[$index]['surplus_num_goods'] = $party_goods_count - (int)$goodsCount;
- }
- }
- //拼团商品拼团数据
- $order_detail = PtActivityOrderDetail::find()->alias('pod')->where(['store_id' => get_store_id()])
- ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id')
- ->andWhere(['po.pt_number' => 0, 'po.is_pay' => 1, 'po.is_pt_finish' => 0])
- ->select('pod.goods_id, pod.goods_name, pod.pic, pod.total_price pt_price, po.pay_time, pod.activity_id, po.user_id, po.id')
- ->orderBy('num desc')->asArray()->all();
- // $current_activity_num = count($order_detail);
- // $order_detail = $order_detail[0];
- $goods_activity = null;
- if ($order_detail) {
- foreach ($order_detail as $index => $value) {
- $activity = PtActivity::findOne($value['activity_id']);
- $goods_activity[$index]['name'] = $value['goods_name'];
- $goods_activity[$index]['cover_pic'] = $value['pic'];
- $saas_user = User::find()->alias('u')->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
- ->select('su.name, su.avatar')->where(['u.id' => $value['user_id']])->asArray()->one();
- $goods_activity[$index]['avatar'] = $saas_user['avatar'];
- $goods_activity[$index]['end_time'] = 0;
- $goods_activity[$index]['pt_price'] = $value['pt_price'];
- if (($value['pay_time'] + ($activity->split_time * 60 * 60)) > time()) {
- $goods_activity[$index]['end_time'] = ($value['pay_time'] + ($activity->split_time * 60 * 60)) - time();
- }
- $goods_activity[$index]['goods_id'] = $value['goods_id'];
- $goods_activity[$index]['pt_number'] = $value['id'];
- }
- }
- //获取活动规则
- $aboutArticle = AboutArticle::findOne(['store_id' => get_store_id(), 'is_delete' => 0, 'type' => 1]);
- $pt_activity = [
- 'party_type' => (int)$party_type,
- 'is_join_pt' => (bool)$ptGoodsActivity,
- 'current_activity' => $arr,
- 'pt_price' => (string)$ptGoodsActivity['pt_price'],
- 'sale_num' => ((int)$ptGoodsActivity['sale_num'] + (int)$ptGoodsActivity['virtual_sales']),
- 'current_activity_num' => count($arr),
- 'goods_activity' => $goods_activity,
- 'rule' => $aboutArticle->desc ?? '',
- 'head_integral' => (int)$ptGoodsActivity['head_integral'],
- 'head_is_free' => (int)$ptGoodsActivity['head_is_free']
- ];
- } else {
- $pt_activity = null;
- }
- if($activityModel){
- $goods_attr = $activityModel['attr'];
- }else{
- $goods_attr = $goods->attr;
- if ($is_md && !in_array($goods->product_type, [4])) {
- $goods_attr = $md_goods->attr;
- }
- }
- if (!empty($ptGoodsActivity) && !empty($this->is_pt)) {
- $goods_attr = json_decode($goods_attr, true);
- $pt_attr = json_decode($ptGoodsActivity['attr'], true);
- if (!$goods->use_attr) {
- $goods_attr = [$goods_attr[0]];
- $pt_attr = [$pt_attr[0]];
- }
- foreach ($goods_attr as &$g_attr) {
- $g_attr_id = array_column($g_attr['attr_list'], 'attr_id');
- sort($g_attr_id);
- foreach ($pt_attr as $p_attr) {
- $p_attr_id = array_column($p_attr['attr_list'], 'attr_id');
- sort($p_attr_id);
- if (!array_diff($g_attr_id, $p_attr_id)) {
- $attr_name = $ptGoodsActivity['party_size'] . "人团";
- if($ptGoodsActivity['party_type'] == 1){
- $attr_name = $ptGoodsActivity['party_goods_count'] . "个商品成团";
- }
- $g_attr['attr_list'] = array_merge([
- [
- "attr_id" => -1,
- "attr_name" => $attr_name,
- ]
- ], $g_attr['attr_list']);
- $g_attr['price'] = $p_attr['pt_price'];
- }
- }
- }
- $last_names = array_column($pt_attr,'pt_price');
- array_multisort($last_names,SORT_DESC, $pt_attr);
- $pt_activity['pt_price'] = (string)($pt_attr[0]['pt_price']);
- $goods_attr = json_encode($goods_attr);
- }
- //配送时间展示
- $delivery_rules = DeliveryRules::find()->where(['id' => $goods->delivery_rules_id, 'is_delete' => 0, 'status' => 1])->select('id, times, type, days, name')->one();
- if ($delivery_rules) {
- $days = "下单" . $delivery_rules['days'] . "天后";
- $delivery_rules['times'] =
- (int)$delivery_rules['type'] === 1 ? $days : date("m月d日 H:i:s", $delivery_rules['times']);
- $delivery_rules['type'] .= '';
- }
- if($goods->goods_send_profit > 0 || $goods->goods_take_price > 0){
- $is_league = 1;
- }else{
- $is_league = 0;
- }
- // $send_profit_model = Option::findOne(['name' => 'store_send_profit']);//平台让利比例
- // $this_store_send_profit_model= Option::findOne(['name' => 'store_this_profit','store_id' => get_store_id()]);
- // if($this_store_send_profit_model->value > 0){
- // $basic_send_profit = $this_store_send_profit_model->value;
- // }else{
- // $basic_send_profit = $send_profit_model->value;
- // }
- // $basic_send_profit = (float)$basic_send_profit;
- $goods_send_profit = $goods->goods_send_profit * 0.01;
- $goods_take_profit = $goods->goods_take_price * 0.01;
- if($goods->product_type == Goods::GOODS_TYPE_VIEW && $this->mini_id){
- $aweme_id = \app\utils\Douyin\DouyinMini::query_aweme_user_bind_ok($this->mini_id);
- }
- $activityOrderRebateSelf = \app\models\ActivityOrderRebateSelf::activityAt($goods->store_id, $goods->id);
- //需要判断当前商品是否是排队免单商品
- $cashback_bili = 0;
- $is_queue = 0;
- $single_price = 0;
- $single_bili = 0;
- $queue_goods = FreeQueueGoods::find()->where(['goods_id' => $goods->id, 'is_delete' => 0,'store_id' => $goods->store_id])->one();
- if ($queue_goods) {
- $is_queue = 1;
- $queue_free = FreeQueue::find()->where(['id'=>$queue_goods->activity_id])->one();
- $cashback_bili = $queue_free->cashback_bili;
- if($queue_free->cashback_type){
- $cashback_bili = $goodsPrice <= 0 ? 0 : ($queue_free->cashback_money * $queue_free->cashback_money_bili / 100) / $goodsPrice * 100;
- }
- $single_bili = $goods->queue_rate > 0 ? $goods->queue_rate : $queue_free->single_bili;
- $single_price = round($goodsPrice * $cashback_bili /100 * $single_bili /100,2);
- }
- $is_show_alliance_coupon = \app\models\Option::get('is_show_alliance_coupon', get_store_id(), 'store', 0)['value'];
- $goods_integral = json_decode($goods->integral, true) ?: ['give' => 0, 'forehead' => 0, 'forehead_integral' => 0];
- // if ($goods_integral['give'] == 0) {
- // $goods_integral['give'] = Option::get(OptionSetting::SEND_INTEGRAL_PROFIT, $goods->store_id, 'store', 0)['value'];
- // }
- $materialId = 0;
- if(in_array('material',\yii::$app->getAttr('plugin')['store'])){
- $material = Material::find()->where(['goods_id' => $goods->id,'status'=>1])->andWhere(['OR', ['delete_time' => 0], ['IS', 'delete_time', NULL]])->orderBy('is_top DESC,create_time DESC')->one();
- if($material){
- $materialId = $material->id;
- }
- }
- $data = array_merge($goods->attributes, [
- 'goods' => $goods,
- 'is_queue'=>$is_queue,
- 'cashback_bili'=>round($cashback_bili, 2),
- 'single_price'=>$single_price,
- 'single_bili'=>$single_bili,
- 'activityOrderRebateSelf' => $activityOrderRebateSelf,
- 'aweme_id' => $aweme_id ?? '',
- 'cutGoods' => $cutGoods,
- 'activityWechatRoom' => $activityWechatRoom,
- 'activityPrice' => sprintf('%.2f', $goods->activityPrice),
- 'id' => $goods->id,
- 'is_league' => $is_league,
- 'goods_send_profit' => $goods_send_profit,
- 'goods_take_profit' => $is_show_alliance_coupon ? $goods_take_profit : 0,
- 'status' => $goods->status,
- 'pic_list' => $pic_list,
- '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,
- 'sub_name' => $goods->sub_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($this->is_pt, $goods->use_attr),
- 'num' => $goods->getNum(),
- 'goods_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' => $this->is_pt ? 1 : 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,
- 'confine_count' => $goods->confine_count ?: 0,
- 'integral' => $goods_integral,
- 'integral_price' => $goods->integral_price,
- 'delivery_rules' => $delivery_rules ?: null,
- 'seckill_activity_arr' => $seckill_activity_arr ?: null,
- 'next_level' => $level,
- 'pt_activity' => $pt_activity,
- 'parameter_list' =>json_decode($goods->parameter_list, true),
- 'accessories_image_status' => empty($goods->accessories_image) ? 0 : 1,
- // 商品所挂接的素材ID
- 'material_id' => $materialId
- ]);
- //分销最高可赚
- //获取商品中最大金额
- $attr = json_decode($goods->attr, true);
- $price_arr = array_column($attr, 'price');
- $max_price = max($price_arr);
- //获取会员价
- $user_level = get_user()->level;
- $level = Level::findOne(['store_id' => get_store_id(), 'level' => $user_level]);
- $discount = 10;
- if ($level) {
- $discount = $level->discount;
- }
- $level_price = $max_price * ($discount / 10);
- if ($user_level > 0) {
- $price_arr = array_column($attr, 'member_' . $user_level);
- $min_level_price = is_array($price_arr) ? floatval(min($price_arr ?: [$level_price])) : $level_price;
- if ($min_level_price > 0) {
- $level_price = $min_level_price;
- }
- }
- //获取分销配置
- $share_setting = Option::get('share_basic_setting', get_store_id());
- $share_setting = $share_setting ? Json::decode($share_setting['value']) : [];
- $rebate = 0;
- //判断是否开启三级分销且自购返利
- // if (!isset($share_setting['is_rebate']['value']) || (isset($share_setting['is_rebate']['value']) && $share_setting['is_rebate']['value'] >= 1)) {// && get_user()->is_distributor == 1
- $share_commission_money_first = 0;
- //获取最高分销比例
- $rebate = $this->getShareCommissionMoney($goods, get_user_id());
- // $setting_shara = Option::get('share_money_setting', get_store_id(), 'share');
- // $setting_shara = $setting_shara ? Json::decode($setting_shara['value']) : [];
- // $rate_first = doubleval($setting_shara['level_one']['value']);
- // if ($rate_first > 0) {
- // //计算比例
- // $share_commission_money_first += ($level_price * $rate_first / 100);
- // $rebate = sprintf("%.2f",$share_commission_money_first < 0.01 ? 0 : $share_commission_money_first);
- // }
- // }
- $data['rebate'] = floatval(sprintf('%.2f', $rebate));
- if ($data['integral']['forehead'] > 0) {
- $store_integral = Option::get(OptionSetting::STORE_INTEGRAL, get_store_id(), 'gift', Option::get(OptionSetting::STORE_INTEGRAL, get_store_id(), 'store')['value'])['value'];
- $data['integral']['forehead_integral'] = round($store_integral * $data['integral']['forehead']);
- }
- if ($data['integral']['give'] <= 0) {
- $send_integral_profit = Option::get(OptionSetting::SEND_INTEGRAL_PROFIT, get_store_id(), 'gift', Option::get(OptionSetting::SEND_INTEGRAL_PROFIT, get_store_id(), 'store')['value'])['value'];
- if ($send_integral_profit > 0) {
- $data['integral']['give'] = (int)(floatval($data['price']) * ($send_integral_profit / 100));
- }
- }
- // 处理app上不显示没加协议的图片
- $data['detail'] = str_replace('src="//', 'src="https://', $data['detail']);
- $now_date = strtotime(date('Y-m-d'));
- $goods_book = GoodsBook::findOne(['goods_id' => $goods->id]);
- // 预约参数配置
- if ($data['product_type'] == Goods::GOODS_TYPE_DATE) {
- $data['num'] = Goods::getGoodsNum($goods)['data'];
- $data['date_book'] = [
- 'date_book' => $data['num'] ,
- 'date' => count($data['num'])
- ];
- 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)) {
- $new_arr = $this->goodsBookingTime($goods['id'], 7);
- $data['service_book'] = $new_arr;
- } else {
- return [
- 'code' => 1,
- 'msg' => '商品服务预约配置有误'
- ];
- }
- }
- $data['givingGifts'] = GivingGiftsForm::isopenGoods($goods['store_id'], $goods['id']) ? 1 : 0;
- $ProductBatch = ProductBatch::find()->where(['goods_id' => $goods->id, 'is_delete' => 0, 'state' => 1])->one();
- $data['have_product_traceability'] = $ProductBatch ? 1 : 0;
- $data['product_batch_id'] = $ProductBatch ? $ProductBatch->id : 0;
- $ruleBuyerLoc = \app\models\OrderRulesBuyerLocation::findOne($goods->order_rules_buyer_location_id);
- $data['order_rules_buyer_location'] = $ruleBuyerLoc;
- /* begin 2024/03/13 11:36:22 降价通知订阅消息相关 WPing丶 */
- $data['template_id'] = NoticeAction::getSendTamplateId(['lowering_price']);
- $data['is_lowering'] = GoodsLoweringPrice::find()->where(['user_id' => get_user_id(), 'goods_id' => $goods->id, 'store_id' => get_user_id(), 'is_delete' => 0])->exists();//判断当前用户是否订阅此商品的降价通知
- /* end */
- $goodsInfo = new Goods();
- $goodsInfo->id = $goods->id;
- //前端显示最近购买这个产品用户的头像
- $data['recently_purchased_user_info'] = [
- 'user_list' => [],
- 'sales_volume' => $goodsInfo->getSalesVolume()
- ];
- $limit = 10;
- $virtual_sales = $is_md ? $md_goods->virtual_sales : $goods->virtual_sales;
- if ($virtual_sales > 0) {
- $data['recently_purchased_user_info']['sales_volume'] = $virtual_sales;
- if ($limit > $virtual_sales) {
- $limit = $virtual_sales;
- }
- } else {
- if ($data['recently_purchased_user_info']['sales_volume'] <= 10) {
- $limit = $data['recently_purchased_user_info']['sales_volume'];
- }
- }
- $data['recently_purchased_user_info']['user_list'] = OrderDetail::find()->alias('od')
- ->leftJoin(['o' => Order::tableName()], 'od.order_id = o.id')
- ->leftJoin(['u' => User::tableName()], 'o.user_id = u.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
- ->where(['od.goods_id' => $goods->id, 'od.is_delete' => 0, 'o.is_pay' => 1,
- 'o.trade_status' => [
- Order::ORDER_FLOW_NO_SEND,
- Order::ORDER_FLOW_SEND,
- Order::ORDER_FLOW_CONFIRM
- ]])->groupBy('o.user_id')->orderBy('o.id desc')->select('su.avatar')->limit($limit)->column();
- if ($virtual_sales <= 0 && empty($data['recently_purchased_user_info']['user_list'])) {
- $limit = 0;
- }
- if (count($data['recently_purchased_user_info']['user_list']) < $limit) {
- $limit = $limit - count($data['recently_purchased_user_info']['user_list']);
- $data['recently_purchased_user_info']['user_list'] = array_merge($data['recently_purchased_user_info']['user_list'], SaasUser::find()
- ->where(['AND', ['<>', 'avatar', ''], ['IS NOT', 'avatar', null], ['NOT LIKE', 'avatar', 'avatar.png']])
- ->orderBy(new \yii\db\Expression("RAND()"))
- ->select('avatar')->limit($limit)->column());
- }
- //是否是增值积分商品
- $integralAppreciationGoods = IntegralAppreciationGoods::findOne(['goods_id' => $goods->id, 'is_delete' => 0]);
- $data['is_integral_appreciation_goods'] = $integralAppreciationGoods ? 1 : 0;
- $data['reflux_amount'] = $integralAppreciationGoods->reflux_amount ?: '0.00';
- $res = [
- 'code' => 0,
- 'data' => $data
- ];
- $sqlDependency = new \yii\caching\DbDependency(['sql'=>Goods::find()->where(['id' => $this->id])->select('updated_at')->createCommand()->getRawSql()]);
- if (empty($pt_activity) || empty($this->activity_cut_price_id)) {
- cache()->set($cacheKey, $res, 600, $sqlDependency);
- }
- return $res;
- }
- public function getShareCommissionMoney($goods, $user_id) {
- $goods = Goods::findOne($goods->id);
- $store_id = $goods->store_id;
- $share = Share::findOne(['user_id' => $user_id, 'status' => Share::SHARE_AUDIT_PASS, 'is_delete' => Share::SHARE_NOT_DELETE]);
- $share_commission_money = 0;
- $attr = json_decode($goods->attr, true);
- $last_names = array_column($attr, 'price');
- array_multisort($last_names, SORT_DESC, $attr);
- $max_attr = $attr[0];
- if ($share) {
- $share_level_id = $share->level;
- $share_level = ShareLevel::findOne(['level' => $share_level_id, 'store_id' => $store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE,
- 'status' => ShareLevel::STATUS_ON]);
- if(ActivityRebateOrderN::passBaseRebate($store_id, $goods->id)){
- return 0;
- }
- // TODO: 价格
- $item_price = doubleval($max_attr['price']);
- // $item_price = doubleval($orderShare->pay_price);
- //
- $rate_first = 0;
- //商品开启独立分销
- if ($goods->individual_share == 1) {
- $share_commission = $this->goodsAttrSettingType($goods, $share_level_id);
- $current_rebate_price = 0;
- foreach ($share_commission as $share_c_index => $share_c_item) {
- if ($current_rebate_price === 0 || $current_rebate_price < $share_c_item['share_commission_level_' . $share_level_id] ?? 0) {
- $current_rebate_price = $share_c_item['share_commission_level_' . $share_level_id] ?? 0;
- }
- }
- $rate_first = $current_rebate_price;
- $shareType = $goods->share_type;
- } else {
- $setting_shara = Option::get('share_money_setting', $store_id, 'share');
- $setting_shara = $setting_shara ? Json::decode($setting_shara['value']) : [];
- $rate_first = $share_level->first_profit ?? ($setting_shara['level_one']['value'] ?? 0);
- if ($rate_first < $share_level->second_profit ?? ($setting_shara['level_two']['value'] ?? 0)) {
- $rate_first = $share_level->second_profit ?? ($setting_shara['level_two']['value'] ?? 0);
- }
- if ($rate_first < $share_level->third_profit ?? ($setting_shara['level_three']['value'] ?? 0)) {
- $rate_first = $share_level->third_profit ?? ($setting_shara['level_three']['value'] ?? 0);
- }
- if ($share_level) {
- $shareType = intval($share_level->profit_type);
- } else {
- $shareType = 0;
- }
- }
- if ($shareType == 1) { // 金钱
- $share_commission_money += $rate_first ;
- } else { // 比例
- $share_commission_money += $item_price * $rate_first / 100;
- }
- return sprintf("%.2f", $share_commission_money < 0.01 ? 0 : $share_commission_money);
- }
- return 0;
- }
- //获取商品详细模式下的佣金最高可得
- private function goodsAttrSettingType($goods, $share_level) {
- $goods = Goods::findOne($goods->id);
- if ($goods->attr_setting_type) {
- $attr = json_decode($goods->attr, true);
- $share_commission_level = 'share_commission_level_' . $share_level;
- $current_attr_max_rebate_price = 0;
- $current_attr = [];
- foreach ($attr as $attr_item) {
- if (empty($current_attr)) {
- $current_attr = $attr_item;
- }
- if (isset($attr_item['share_commission_new_first'][$share_commission_level])) {
- $current_attr_rebate_price = bcmul($attr_item['price'], $attr_item['share_commission_new_first'][$share_commission_level], 2);
- if ($current_attr_rebate_price > $current_attr_max_rebate_price) {
- $current_attr_max_rebate_price = $current_attr_rebate_price;
- $current_attr = $attr_item;
- }
- }
- if (isset($attr_item['share_commission_new_second'][$share_commission_level])) {
- $current_attr_rebate_price = bcmul($attr_item['price'], $attr_item['share_commission_new_second'][$share_commission_level], 2);
- if ($current_attr_rebate_price > $current_attr_max_rebate_price) {
- $current_attr_max_rebate_price = $current_attr_rebate_price;
- $current_attr = $attr_item;
- }
- }
- if (isset($attr_item['share_commission_new_third'][$share_commission_level])) {
- $current_attr_rebate_price = bcmul($attr_item['price'], $attr_item['share_commission_new_third'][$share_commission_level], 2);
- if ($current_attr_rebate_price > $current_attr_max_rebate_price) {
- $current_attr_max_rebate_price = $current_attr_rebate_price;
- $current_attr = $attr_item;
- }
- }
- }
- return [
- 'share_commission_new_first' => $current_attr['share_commission_new_first'],
- 'share_commission_new_second' => $current_attr['share_commission_new_second'],
- 'share_commission_new_third' => $current_attr['share_commission_new_third'],
- ];
- } else {
- $share_commission_new_first = json_decode($goods->share_commission_new_first, true);
- $share_commission_new_second = json_decode($goods->share_commission_new_second, true);
- $share_commission_new_third = json_decode($goods->share_commission_new_third, true);
- return [
- 'share_commission_new_first' => $share_commission_new_first,
- 'share_commission_new_second' => $share_commission_new_second,
- 'share_commission_new_third' => $share_commission_new_third,
- ];
- }
- }
- // public function activity_cut_price($activity_cut_price_id, &$goods, &$md_goods = null) {
- // //展示活动商品信息
- // if($activity_cut_price_id){
- // $activityAt = ActivityCutPrice::activityAt($activity_cut_price_id);
- // $goods_ext = ActivityCutPriceGoods::findOne(['activity_id' => $activity_cut_price_id, 'goods_id' => $goods->id, 'is_delete' => 0]);
- // //商品属于当前进行中活动
- // if($activityAt && $activityAt->id == $activity_cut_price_id && $goods_ext){
- // $goods->virtual_sales += $goods_ext->virtual_sales;
- // $md_goods && $md_goods->virtual_sales += $goods_ext->virtual_sales;
- // if($goods_ext->price){
- // $goods->price = $goods_ext->price;
- // $md_goods && $md_goods->price = $goods_ext->price;
- // }
- // $ext_attrs = json_decode($goods_ext->attr, true);
- // $attrs = json_decode($goods->attr, true);
- // if($md_goods){
- // $md_attrs = json_decode($md_goods->attr, true);
- // }
- // foreach($ext_attrs as $ext_attr){
- // $ext_attr_list = json_encode($ext_attr['attr_list']);
- // foreach($attrs as &$attr){
- // $attr_list = json_encode($attr['attr_list']);
- // if($attr_list == $ext_attr_list){
- // $attr['price'] = $ext_attr['price'];
- // $attr['num'] = $ext_attr['num'];
- // }
- // }
- // if($md_goods){
- // foreach($md_attrs as &$md_attr){
- // $md_attr_list = json_encode($md_attr['attr_list']);
- // if($md_attr_list == $ext_attr_list){
- // $md_attr['price'] = $ext_attr['price'];
- // $md_attr['num'] = $ext_attr['num'];
- // }
- // }
- // }
- // }
- // $goods->attr = json_encode($attrs);
- // $md_goods && $md_goods->attr = json_encode($md_attrs);
- // }
- // }
- // }
- public function activity_new_user($activity_new_user_id, &$goods, &$md_goods = null) {
- // $activityAt = ActivityNewUser::activityAt($goods->store_id);
- // $activityAt && $this->activity_new_user_id = $activityAt->id;
- //展示活动商品信息
- $activityAt = ActivityNewUser::activityAt($goods->store_id);
- if ($activityAt) {
- $goods_ext = ActivityNewUserGoods::findOne(['activity_id' => $activityAt->id, 'goods_id' => $goods->id, 'is_delete' => 0]);
- //商品属于当前进行中活动
- if($goods_ext){
- if($goods_ext->price){
- $goods->price = $goods_ext->price;
- $md_goods && $md_goods->price = $goods_ext->price;
- }
- $ext_attrs = json_decode($goods_ext->attr, true);
- $attrs = json_decode($goods->attr, true);
- if($md_goods){
- $md_attrs = json_decode($md_goods->attr, true);
- }
- foreach($ext_attrs as $ext_attr){
- $ext_attr_list = json_encode($ext_attr['attr_list']);
- foreach($attrs as &$attr){
- $attr_list = json_encode($attr['attr_list']);
- if($attr_list == $ext_attr_list){
- $attr['price'] = $ext_attr['price'];
- }
- }
- if($md_goods){
- foreach($md_attrs as &$md_attr){
- $md_attr_list = json_encode($md_attr['attr_list']);
- if($md_attr_list == $ext_attr_list){
- $md_attr['price'] = $ext_attr['price'];
- }
- }
- }
- }
- $goods->attr = json_encode($attrs);
- $md_goods && $md_goods->attr = json_encode($md_attrs);
- }
- }
- }
- public function activity_wechat_room(&$goods, &$md_goods = null) {
- if($this->is_pt || $this->is_seckill || $this->activity_new_user_id || $this->activity_cut_price_id){
- return null;
- }
- $goods_ext = null;
- $activityAt = ActivityWechatRoom::activityAtByGoodsId($goods->id, $goods_ext);
- //展示活动商品信息
- if ($activityAt) {
- $inRoom = false;
- if($this->user_id){
- $wechatRoomIds = explode(',', $activityAt['wechat_room_id']);
- foreach($wechatRoomIds as $roomId){
- if(\app\modules\admin\models\mochat\MochatForm::userInRoom($this->store_id, $roomId, $this->user_id)){
- $inRoom = true;
- break;
- }
- }
- }
- $activityAt['inRoom'] = $inRoom;
- $activityAt['inTime'] = $activityAt['end_time'] - time();
- $minPrice = $goods->price;
- //商品属于当前进行中活动
- if($goods_ext){
- // if($goods_ext->price){
- // $goods->price = $goods_ext->price;
- // $md_goods && $md_goods->price = $goods_ext->price;
- // }
- $ext_attrs = json_decode($goods_ext->attr, true);
- $attrs = json_decode($goods->attr, true);
- if($md_goods){
- $md_attrs = json_decode($md_goods->attr, true);
- }
- foreach($ext_attrs as $ext_attr){
- $ext_attr_list = json_encode($ext_attr['attr_list']);
- foreach($attrs as &$attr){
- $attr_list = json_encode($attr['attr_list']);
- if($attr_list == $ext_attr_list){
- $attr['price'] = $ext_attr['price'];
- $minPrice = $attr['price'] < $minPrice ? $attr['price'] : $minPrice;
- }
- }
- if($md_goods){
- foreach($md_attrs as &$md_attr){
- $md_attr_list = json_encode($md_attr['attr_list']);
- if($md_attr_list == $ext_attr_list){
- $md_attr['price'] = $ext_attr['price'];
- $minPrice = $md_attr['price'] < $minPrice ? $md_attr['price'] : $minPrice;
- }
- }
- }
- }
- $goods->attr = json_encode($attrs);
- $md_goods && $md_goods->attr = json_encode($md_attrs);
- }
- }
- $goods->activityPrice = $minPrice;
- $md_goods && $md_goods->activityPrice = $minPrice;
- return $activityAt;
- }
- // 快速给购买商品
- 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) {
- $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)
- {
- $mch = \app\models\Mch::find()->where(['id' => $goods->mch_id, 'is_delete' => 0, 'is_open' => 1])->one();
- if (!$mch) {
- return null;
- }
- $mch->kefu_info = json_decode($mch->kefu_info ?: '', true) ?: [
- 'kefu_id' => ''
- ];
- return $mch;
- }
- public function getDateByInterval(int $num) :array
- {
- //var_dump($st, $et);die;
- $returnData = [];
- $i = 0;
- do {
- $temp = date('Y-m-d', strtotime('+' . $i . ' day', strtotime(date('Y-m-d'))));
- $returnData[] = $temp;
- $i++;
- } while ($i < $num);
- return $returnData;
- }
- public function goodsBookingTime($goods_id, $dayCount = 7) {
- $now_date = strtotime(date('Y-m-d'));
- $goods_book = GoodsBook::findOne(['goods_id' => $goods_id]);
- $service_book = Json::decode($goods_book->service_book);
- $date_data = $this->getDateByInterval($dayCount);
- $new_arr = [
- 'data' => []
- ];
- $order = Order::find()->alias('o')
- ->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id')
- ->where(['o.trade_status' => [0, 2, 3], 'o.is_delete' => 0, 'o.order_type' => 2, 'goods_id' => $goods_id])
- ->andWhere(['>', 'o.created_at', time() - 3600 - $dayCount * 86400])
- ->select('od.attr, od.num')->asArray()->all();
- foreach ($date_data as $index => $datum) {
- $new_arr['data'][$index]['date'] = $datum;
- $new_arr['data'][$index]['time'] = $service_book['data'][0]['time'];
- }
- foreach ($new_arr['data'] as &$value) {
- if (strtotime($value['date']) >= $now_date) {
- foreach ($value['time'] as &$m) {
- foreach ($m['times'] as &$n) {
- $times = explode('-', $n['time']);
- foreach ($order as $item) {
- if ($item['attr']) {
- $item['attr'] = json_decode($item['attr'], true);
- $order_time = explode('-', $item['attr']['time']);
- if (
- strtotime($item['attr']['date'] . ' ' . $times[0]) === strtotime($value['date'] . ' ' . $order_time[0]) &&
- strtotime($item['attr']['date'] . ' ' . $times[1]) === strtotime($value['date'] . ' ' . $order_time[1])
- ) {
- $n['num'] = $n['num'] - $item['num'];
- }
- }
- }
- }
- }
- }
- }
- return $new_arr;
- }
- public function goodsPurchaseCatList() {
- $user_id = $this->user_id;
- $md_id = $this->md_id;
- if ($md_id <= 0) {
- $md_id = [-1, 0];
- }
- $query = OrderDetail::find()->alias('od')
- ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
- ->leftJoin(['o' => Order::tableName()], 'od.order_id = o.id')
- ->leftJoin(['odg' => OrderDetail::find()->alias('od_')
- ->leftJoin(['g_' => Goods::tableName()], 'od_.goods_id = g_.id')
- ->leftJoin(['o_' => Order::tableName()], 'od_.order_id = o_.id')
- ->where(['o_.user_id' => $user_id, 'g_.status' => 1, 'g_.is_delete' => 0])
- ->andWhere(['or', ['o_.is_pay' => 1], ['o_.pay_type' => 2]])
- ->andWhere(['<>', 'o_.trade_status', 1])
- ->groupBy('od_.goods_id')
- ->select('goods_id, sum(od_.num) as num, o_.id as order_id')],
- 'od.goods_id = odg.goods_id')
- ->where(['o.user_id' => $user_id, 'g.status' => 1, 'g.is_delete' => 0])
- ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]])
- ->andWhere(['<>', 'o.trade_status', 1]);
- $data = $query->orderBy('odg.num DESC, g.id DESC')->groupBy('od.goods_id')->select('od.goods_id, o.id order_id')->asArray()->all();
- $cat_data = [];
- foreach ($data as $item) {
- $goods_cat_info = GoodsCat::find()->alias('gc')->leftJoin(['c' => Cat::tableName()], 'gc.cat_id = c.id')
- ->where(['gc.goods_id' => $item['goods_id'], 'gc.is_delete' => 0, 'c.is_delete' => 0, 'c.is_show' => 1])
- ->select(' c.name, c.id')->asArray()->all();
- foreach ($goods_cat_info as $goods_cat_item) {
- if (isset($cat_data[$goods_cat_item['id']])) {
- $cat_data[$goods_cat_item['id']]['goods_id'][] = $item['goods_id'];
- } else {
- $cat_data[$goods_cat_item['id']] = [
- 'id' => $goods_cat_item['id'],
- 'name' => $goods_cat_item['name'],
- 'goods_id' => [$item['goods_id']]
- ];
- }
- }
- }
- $cat_data = array_values($cat_data);
- foreach ($cat_data as &$item) {
- $item['cart_num'] = intval(Cart::find()->where(['user_id' => $user_id, 'goods_id' => $item['goods_id'], 'is_delete' => 0, 'md_id' => $md_id])
- ->select('num')->sum('num') ?? 0);
- }
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => [
- 'list' => $cat_data
- ]
- ];
- }
- //获取用户常购商品
- public function goodsPurchaseList() {
- $store_id = $this->store_id;
- $user_id = $this->user_id;
- $cat_id = intval($this->cat_id);
- $md_id = $this->md_id;
- if ($md_id <= 0) {
- $md_id = [-1, 0];
- }
- if ($cat_id <= 0) {
- return [
- 'code' => 1,
- 'msg' => '参数错误',
- ];
- }
- $query = OrderDetail::find()->alias('od')
- ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
- ->leftJoin(['o' => Order::tableName()], 'od.order_id = o.id')
- ->leftJoin(['odg' => OrderDetail::find()->alias('od_')
- ->leftJoin(['g_' => Goods::tableName()], 'od_.goods_id = g_.id')
- ->leftJoin(['o_' => Order::tableName()], 'od_.order_id = o_.id')
- ->where(['o_.user_id' => $user_id, 'g_.status' => 1, 'g_.is_delete' => 0])
- ->andWhere(['or', ['o_.is_pay' => 1], ['o_.pay_type' => 2]])
- ->andWhere(['<>', 'o_.trade_status', 1])
- ->groupBy('od_.goods_id')
- ->select('goods_id, sum(od_.num) as num')],
- 'od.goods_id = odg.goods_id')
- ->where(['o.user_id' => $user_id, 'g.status' => 1, 'g.is_delete' => 0])
- ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]])
- ->andWhere(['<>', 'o.trade_status', 1]);
- $query->leftJoin(['gc' => GoodsCat::tableName()], 'od.goods_id = gc.goods_id')->andWhere([
- 'gc.cat_id' => $cat_id
- ])->orderBy('odg.num DESC');
- $query->orderBy('odg.num DESC, g.id DESC')->groupBy('gc.goods_id')->select('odg.*');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $goods_id = $item['goods_id'];
- $goods = Goods::findOne($goods_id);
- $item['name'] = $goods->name;
- $item['cover_pic'] = $goods->cover_pic;
- $item['price'] = $goods->price;
- $item['original_price'] = $goods->original_price;
- $item['use_attr'] = intval($goods->use_attr);
- $item['attr'] = json_decode($goods->attr, true);
- $item['sales'] = $goods->getSalesVolume();
- $item['goods_num'] = $goods->goods_num;
- $item['cart_num'] = Cart::find()->where(['user_id' => $user_id, 'goods_id' => $goods_id, 'is_delete' => 0, 'md_id' => $md_id])
- ->select('num')->sum('num') ?? 0;
- $item['cart_id'] = Cart::find()->where(['user_id' => $user_id, 'goods_id' => $goods_id, 'is_delete' => 0, 'md_id' => $md_id])
- ->select('id')->scalar() ?: 0;
- }
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => $pagination
- ];
- }
- }
|