| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- <?php
- namespace app\modules\alliance\models\storeActivity;
- use app\constants\OptionSetting;
- use app\models\ActivityCutPriceGoods;
- use app\models\ActivityCutPriceOrder;
- use app\models\ActivityNewUserGoods;
- use app\models\Coupon;
- use app\models\CouponAutoSend;
- use app\models\District;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\PtActivity;
- use app\models\PtActivityGoods;
- use app\models\SaasUser;
- use app\models\SeckillActivityGoods;
- use app\models\Store;
- use app\models\User;
- use app\models\UserCoupon;
- use app\models\WechatConfig;
- use yii\base\Model;
- class PtStoreActivityForm extends Model
- {
- public $table_model;
- public $goods_model;
- public $price_type;
- public $store_name;
- public $distance;
- public $district_id;
- public $min_price;
- public $max_price;
- public $min_sale_num;
- public $max_sale_num;
- public $sort;
- public $latitude;
- public $longitude;
- public $model_type;
- public $saas_user_id;
- public $page;
- public $pageSize;
- public $store_id;
- public function rules()
- {
- return [
- [['distance', 'store_name'], 'string'],
- [['district_id', 'sort', 'model_type', 'saas_user_id', 'store_id'], 'integer'],
- [['min_sale_num', 'max_sale_num', 'min_price', 'max_price', 'latitude', 'longitude', 'page', 'pageSize'], 'number'],
- ];
- }
- public function activityGoodsList() {
- try {
- $price_type = $this->price_type;
- $model_type = $this->model_type;
- $distance_ = 0;
- $distance = $this->distance;
- $district_id = $this->district_id;
- //查询有活动的店铺
- $latitude = $this->latitude;
- $longitude = $this->longitude;
- $min_price = $this->min_price;
- $max_price = $this->max_price;
- $min_sale_num = $this->min_sale_num;
- $max_sale_num = $this->max_sale_num;
- $sort = (int)$this->sort;
- $store_name = $this->store_name;
- $page = $this->page ?: 1;
- $pageSize = $this->pageSize ?: 10;
- //判断附近
- if (strpos($distance, 'km') !== false) {
- //1-5km
- $km = strpos($distance, 'km');
- $distance_ = substr($distance, 0, $km);
- $distance_ = $distance_ * 1000;
- } else {
- if (strpos($distance, 'm') !== false) {
- //500m
- $km = strpos($distance, 'm');
- $distance_ = substr($distance, 0, $km);
- }
- }
- $sql = $this->getSql($latitude, $longitude);
- //判断距离
- if ($distance_ > 0) {
- $sql .= " AND s_.distance < {$distance_}";
- } else {
- if ($district_id) {
- $sql .= " AND s.district_id = {$district_id}";
- }
- }
- //只有平台运营的商城或者单店铺的无独立小程序才展示商盟的营销活动
- if (\Yii::$app->prod_is_dandianpu()) {
- $store_id = Option::find()->where(['group' => 'store', 'name' => 'self_mini'])->select('store_id')->column();
- $store_id = implode(',', $store_id);
- $sql .= " AND s.id NOT IN ({$store_id})";
- } else {
- $sql .= " AND s.business_model = 2";
- }
- if ($store_name) {
- $sql .= " AND s.name LIKE '%{$store_name}%' ";
- }
- // $sql .= " GROUP BY s.id " ;
- $total_count = \Yii::$app->db->createCommand($sql . " GROUP BY s.id ")->query()->count();
- if (in_array($sort, [0, 1])) {
- $sql .= " ORDER BY s_.distance ASC";
- }
- $array = \Yii::$app->db->createCommand($sql)->queryAll();
- // $sql .= " LIMIT " . $pageSize . " OFFSET " . ($page - 1) * $pageSize ;
- $store_id = array_column($array, 'store_id');
- array_push($store_id, 0);
- $store_id = array_unique($store_id);
- $activity_goods_id = array_column($array, 'id');
- $field_ = "";
- switch (intval($model_type)) {
- case 1:
- $field_ = ", ag.sale_num";
- break;
- case 2:
- $field_ = ", a.coupon_ids, g.goods_num, a.buy_limit";
- break;
- case 3:
- $field_ = ", ag.seckill_num, ag.virtual_sales, ag.sale_num";
- break;
- default:
- $field_ = ", a.party_size, ag.sale_num, ag.virtual_sales";
- break;
- }
- $field = 'g.id, a.id activity_id, ag.id activity_goods_id, a.start_time, a.end_time, ag.attr, g.name, g.cover_pic, s.id store_id, s.name store_name, s.logo, s.province_id, s.city_id, s.district_id, s.address, s.business_model, s_.distance' . $field_;
- $sql = $this->getSql($latitude, $longitude, $field);
- //筛选价格销量
- $a_goods_id = [];
- $this->getGoodsId($min_price, $max_price, $min_sale_num, $max_sale_num, $activity_goods_id, $model_type, $price_type,$a_goods_id);
- //价格 销量筛选
- if ($a_goods_id) {
- $a_goods_id = implode(',', $a_goods_id);
- $sql .= " AND ag.id in ({$a_goods_id})";
- }
- //距离筛选
- $store_id = implode(',', $store_id);
- $sql .= " AND s.id in ({$store_id})";
- // $sql .= " GROUP BY ag.id";
- //距离排序
- switch ($sort) {
- case 1://距离优先
- $sql .= " ORDER BY s_.distance ASC";
- break;
- case 2://好评优先
- $sql .= " ORDER BY s.rank DESC";
- break;
- case 3://销量优先
- if (intval($model_type) !== 2) {
- $sql .= " ORDER BY ag.sale_num DESC";
- }
- break;
- default:
- $sql .= " ORDER BY s_.distance ASC";
- break;
- }
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as &$item) {
- $item['distance'] = $this->distance($item['distance']);
- //获取最低活动价格的规格,填充单价和单卖价格
- $attr = json_decode($item['attr'], true);
- $max_price_arr = array_column($attr, $price_type);
- $current_attr = [
- $price_type => max($max_price_arr)
- ];
- foreach ($attr as $attr_item) {
- if ($attr_item[$price_type] <= $current_attr[$price_type]) {
- $current_attr = $attr_item;
- }
- }
- $item['activity_price'] = $current_attr[$price_type];
- if (in_array($model_type, [1, 2])) {
- //砍价 新人原价处理
- $item['price'] = $current_attr['oldPrice'];
- } else {
- $item['price'] = $current_attr['price'];
- }
- //拼接商城地址信息
- $province = '';
- if ($item['province_id']) {
- $province = District::findOne($item['province_id'])->name ?: '';
- }
- $city = '';
- if ($item['city_id']) {
- $city = District::findOne($item['city_id'])->name ?: '';
- }
- $district = '';
- if ($item['district_id']) {
- $district = District::findOne($item['district_id'])->name ?: '';
- }
- $item['address'] = $province . $city . $district . $item['address'];
- //获取商城logo
- $item['logo'] = $item['logo'] ?: Option::get(OptionSetting::STORE_LOGO, $item['store_id'], 'store', '')['value'];
- if (empty($item['logo'])) {
- $item['logo'] = Option::get('web_log', $item['store_id'], 'web', '')['value'];
- }
- //获取秒杀信息(进度条)
- if (isset($item['seckill_num']) && isset($item['virtual_sales'])&& isset($item['sale_num'])) {
- if (((int)$item['seckill_num'] + $item['virtual_sales']) === 0 || ((int)$item['sale_num'] + $item['virtual_sales']) === 0) {
- $item['progress'] = 0;
- } else {
- $item['progress'] = sprintf('%.2f', (($item['sale_num'] * 1) / ($item['seckill_num'] + $item['sale_num'])) * 100);
- $item['seckill_num'] <= 0 && $item['progress'] = 100;
- if ($item['start_time'] > time()) {
- $item['progress'] = 0;
- $item['virtual_sales'] = 0;
- }
- }
- }
- //砍价信息获取
- if (intval($model_type) === 1) {
- //参与人数
- $userCount = ActivityCutPriceOrder::find()->where(
- [
- "AND",
- [
- 'activity_id' => $item['activity_id'],
- 'is_delete' => 0,
- 'activity_goods_id' => $item['activity_goods_id']
- ], ['AND',
- [
- 'used_order_id' => 0
- ],[
- '>', 'end_time', time()
- ]
- ]
- ]
- )->groupBy(['saas_id'])->count();
- $item['sale_num'] = $userCount;
- //是否正在参与
- $user_join_activity = ActivityCutPriceOrder::find()->where(['AND',
- [
- 'activity_id' => $item['activity_id'],
- 'activity_goods_id' => $item['activity_goods_id'],
- 'is_delete' => 0,
- 'saas_id' => $this->saas_user_id
- ], [
- 'AND',
- [
- 'used_order_id' => 0
- ],[
- '>', 'end_time', time()
- ]
- ]
- ])->asArray()->one();//支付时间 < 活动截止时间
- $item['user_join'] = 0;
- if ($user_join_activity) {
- $item['user_join'] = 1;
- //如果正在参与将price改为已砍价格
- $item['price'] = $user_join_activity['pay_price'];
- }
- }
- //新人专享
- if (intval($model_type) === 2) {
- $item['sale_num'] = OrderDetail::find()->alias('od')->leftJoin(['o' => Order::tableName()], 'o.id = od.order_id')
- ->where(['od.goods_id' => $item['id'], 'o.is_pay' => 1])->select('od.num')->sum('od.num') ?: 0;
- }
- //新人专享
- if (intval($model_type) === 0) {
- $item['sale_num'] += $item['virtual_sales'];
- }
- unset($item['attr'], $item['virtual_sales']);
- }
- $list = array_values($list);
- $store_id_arr = array_column($list, 'store_id');
- $store_id_arr = array_unique($store_id_arr);
- $list = $this->handleData($store_id_arr, $list);
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'list' => $list,
- 'page' => $page,
- 'totalCount' => $total_count
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 0,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function handleData($store_id_arr, $list) {
- $data = [];
- foreach ($store_id_arr as $store_item) {
- foreach ($list as $item_) {
- if ($store_item === $item_['store_id']) {
- $data[$store_item]['coupon_ids'] = '0,';
- if (!empty($item_['coupon_ids'])) {
- $data[$store_item]['coupon_ids'] .= $item_['coupon_ids'] . ',';
- }
- if (!$data[$store_item]['store']) {
- $data[$store_item]['store'] = [
- 'store_name' => $item_['store_name'],
- 'logo' => $item_['logo'],
- 'address' => $item_['address'],
- 'distance' => $item_['distance'],
- 'business_model' => (int)$item_['business_model'],
- 'id' => $item_['store_id'],
- 'wechat_app_id' => '',
- 'gh_wechat_app_id' => '',
- 'alipay_app_id' => ''
- ];
- $wechatInfo = WechatConfig::find()->where(['store_id' => $item_['store_id'], 'is_delete'=>0, 'type'=>1])->asArray()->one();
- if($wechatInfo){
- $data[$store_item]['store']['wechat_app_id'] = $wechatInfo['app_id'];
- $data[$store_item]['store']['gh_wechat_app_id'] = $wechatInfo['gh_wechat_app_id'];
- }
- $alipayInfo = Option::get('alipay_config', $item_['store_id'], 'alipay');
- if($alipayInfo && $alipayInfo['value']){
- $alipayInfo = json_decode($alipayInfo['value'], true);
- $data[$store_item]['store']['alipay_app_id'] = $alipayInfo['app_id'];
- }
- }
- $data[$store_item]['goods_list'][] = $item_;
- }
- }
- //新人专享优惠券
- if (intval($this->model_type) === 2) {
- // $coupon_ids = explode(',', $coupon_ids);
- // $coupon_ids = array_unique($coupon_ids);
- // $coupon = Coupon::find()->alias('c')->leftJoin([
- // 'cu' => UserCoupon::tableName()
- // ], 'c.id = cu.coupon_id')
- // ->where(['c.id' => $coupon_ids, 'c.is_delete' => 0])
- // ->andWhere(['OR', ['cu.is_use' => 0], ['IS', 'cu.id', NULL]])
- // ->select('c.id, c.name, c.discount_type, c.min_price, c.sub_price, c.discount, c.total_count')->asArray()->all();
- // foreach ($coupon as $coupon_index => &$coupon_item) {
- // $coupon_item['discount_type'] = intval($coupon_item['discount_type']);
- // $coupon_item['is_use'] = 0;
- // $saas_user = SaasUser::findOne($this->saas_user_id);
- // if ($saas_user) {
- // $user = User::findOne(['binding' => $saas_user->mobile, 'store_id' => $store_item]);
- // if ($user) {
- // $user_coupon = UserCoupon::findOne(['coupon_id' => $coupon_item['id'], 'user_id' => $user->id, 'is_delete' => 0]);
- // if ($user_coupon) {
- // $coupon_item['is_use'] = 1;
- // }
- // }
- // }
- //
- //
- // $coupon_count = UserCoupon::find()->where(['store_id' => $store_item, 'is_delete' => 0,
- // 'coupon_id' => $coupon_item['id']])->andWhere(['!=', 'type', 3])->count();
- // if ($coupon_item['total_count'] != -1 && $coupon_item['total_count'] <= $coupon_count) {
- // unset($coupon[$coupon_index]);
- // }
- // }
- // $coupon = array_values($coupon);
- // $data[$store_item]['coupon'] = $coupon;
- $data[$store_item]['coupon'] = [];
- $coupons = explode(',', $data[$store_item]['coupon_ids']);
- if($coupons){
- $coupon_list = Coupon::find()->where(['in', 'id', $coupons])->andWhere(['is_delete' => 0])->asArray()->all();
- $auto_sends = CouponAutoSend::find()->where(['store_id' => $store_item, 'is_delete' => 0, 'status' => 1])->asArray()->all();
- if($auto_sends){
- foreach($coupon_list as $index => &$item){
- $item['auto_send'] = [];
- foreach ($auto_sends as $k){
- $cids = json_decode($k['coupon_id'], true);
- if(in_array($item['id'], $cids)){
- unset($coupon_list[$index]);
- }
- }
- }
- $coupon_list = array_values($coupon_list);
- }
- $saas_user = SaasUser::findOne($this->saas_user_id);
- if ($saas_user) {
- $user = User::findOne(['binding' => $saas_user->mobile, 'store_id' => $store_item, 'is_delete' => 0]);
- if($user){
- $user_coupons = UserCoupon::find()->where(['store_id' => $store_item, 'user_id' => $user->id])->asArray()->all();
- foreach($coupon_list as $coupon_index => &$item){
- foreach ($user_coupons as $k) {
- if($item['id'] == $k['coupon_id']){
- unset($coupon_list[$coupon_index]);
- }
- }
- }
- $coupon_list = array_values($coupon_list);
- }
- }
- $data[$store_item]['coupon'] = $coupon_list;
- }
- }
- unset($data[$store_item]['coupon_ids']);
- }
- return array_values($data);
- }
- public function getGoodsId($min_price, $max_price, $min_sale_num, $max_sale_num, $activity_goods_id, $model_type, $price_type, &$a_goods_id = []) {
- switch (intval($model_type)) {
- case 1:
- $ptActivityGoods = ActivityCutPriceGoods::find()->where(['id' => $activity_goods_id])->asArray()->all();
- break;
- case 2:
- $ptActivityGoods = ActivityNewUserGoods::find()->where(['id' => $activity_goods_id])->asArray()->all();
- break;
- case 3:
- $ptActivityGoods = SeckillActivityGoods::find()->where(['id' => $activity_goods_id])->asArray()->all();
- break;
- default:
- $ptActivityGoods = PtActivityGoods::find()->where(['id' => $activity_goods_id])->asArray()->all();
- break;
- }
- foreach ($ptActivityGoods as &$goods_item) {
- $open = false;
- $attr = json_decode($goods_item['attr'], true);
- foreach ($attr as $attr_item) {
- if ($min_price && $max_price) {
- array_push($a_goods_id, 0);
- if ($attr_item[$price_type] >= $min_price && $attr_item[$price_type] <= $max_price) {
- $open = true;
- }
- }
- }
- $num_open = false;
- if ($min_sale_num && $max_sale_num) {
- array_push($a_goods_id, 0);
- //单独处理新人专享
- if (intval($model_type) === 2) {
- $goods_item['sale_num'] = OrderDetail::find()->alias('od')->leftJoin(['o' => Order::tableName()], 'o.id = od.order_id')
- ->where(['od.goods_id' => $goods_item['goods_id'], 'o.is_pay' => 1])->select('od.num')->sum('od.num') ?: 0;
- }
- if ($goods_item['sale_num'] >= $min_sale_num && $goods_item['sale_num'] <= $max_sale_num) {
- $num_open = true;
- }
- }
- if ($min_price && $max_price && $min_sale_num && $max_sale_num) {
- if ($num_open && $open) {
- array_push($a_goods_id, $goods_item['id']);
- }
- } else {
- if ($num_open || $open) {
- array_push($a_goods_id, $goods_item['id']);
- }
- }
- }
- }
- public function getSql($latitude, $longitude, $field = 's.id store_id, ag.id') {
- $table_model = $this->table_model;
- $goods_model = $this->goods_model;
- $store_id = $this->store_id;
- $latitude = $latitude ?: 1;
- $longitude = $longitude ?: 1;
- $time = time();
- $sql = "SELECT
- {$field}
- FROM
- {$goods_model} `ag`
- LEFT JOIN {$table_model} `a` ON a.id = ag.activity_id
- LEFT JOIN `cyy_goods` `g` ON g.id = ag.goods_id
- LEFT JOIN `cyy_store` `s` ON s.id = a.store_id
- LEFT JOIN (select id, acos(cos({$latitude}*pi()/180 )*cos(latitude*pi()/180)*cos({$longitude}*pi()/180 -longitude*pi()/180)+sin({$latitude}*pi()/180 )*sin(latitude*pi()/180))*6370996.81 as
- distance from cyy_store) as s_ ON s_.id = s.id
- WHERE
-
- ( `a`.`is_delete` = 0 )
- AND ( `ag`.`is_delete` = 0 )
- AND ( `g`.`is_delete` = 0 )
- AND ( `s`.`is_delete` = 0 )
- AND (`a`.`start_time` <= {$time} AND `a`.`end_time` > {$time})";//
- if ($store_id > 0) {
- $sql .= " AND ( `a`.`is_platform` = 0 AND `a`.`store_id` = {$store_id})";
- } else {
- $sql .= " AND ( `a`.`status` = 1 )
- AND ( `g`.`status` = 1 )
- AND ( `a`.`is_platform` = 1 )
- AND (`a`.`is_platform_audit` = 1) ";
- }
- return $sql;
- }
- public function distance($distance)
- {
- if ($distance == -1) {
- return -1;
- }
- if ($distance > 1000) {
- $distance = round($distance / 1000, 2) . 'km';
- } else {
- $distance = round($distance, 2);
- $distance .= 'm';
- }
- return $distance;
- }
- }
|