'ID', 'mall_id' => 'Mall ID', 'goods_id' => '商品ID', 'cate_id' => '分类ID', 'num' => '已兑换数量', 'money' => '商品最低价', 'price' => '商品售价', 'score' => '积分', 'type' => '兑换类型', 'use_coupon' => '是否不参加优惠券抵扣', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => '创建时间', 'updated_at' => '修改时间' ]; } /** * 关联积分商品规格表 * @Author: lun * @DateTime: 2022/1/7 0007 14:40 * @Copyright: copyright (c) 2021 广东七件事集团 * @return \yii\db\ActiveQuery */ public function getAttr() { return $this->hasMany(ScoreMallGoodsAttr::class, ['goods_id' => 'goods_id'])->where(['status' => StatusEnum::ENABLED]); } /** * 关联商品表 * @Author: lun * @DateTime: 2022/1/7 0007 16:28 * @Copyright: copyright (c) 2021 广东七件事集团 * @return \yii\db\ActiveQuery */ public function getGoods() { return $this->hasOne(Goods::class, ['id' => 'goods_id']); } /** * 获取积分商品兑换价 * @Author: lun * @DateTime: 2022/1/7 0007 16:24 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $type * @param $attr * @param $score_config * @param $num * @param $user_id * @return array */ public static function getExchangePrice($type, $attr, $score_config, $num = 1, $user_id = 0) { $data = [ 'exchange_price' => '', 'pay_money' => 0, 'pay_score' => 0, 'deduct_money' => 0, ]; $score_name = $score_config['score_name'] ?? '积分'; $price = $total_price = 0; switch ($type) { case ScoreMallEnum::TYPE_SCORE: $data['exchange_price'] = $total_price = ($attr['score'] * $num) . $score_name; $price = $attr['score'] . $score_name; $data['pay_score'] = $attr['score'] * $num; $data['deduct_money'] = ($attr['price'] ?? 0) * $num; break; case ScoreMallEnum::TYPE_SCORE_MONEY: $data['exchange_price'] = $total_price = ($attr['score'] * $num) . $score_name . '+¥' . ($attr['money'] * $num); $price = '¥' . $attr['money'] . '+' . $attr['score'] . $score_name; $data['pay_money'] = $attr['money'] * $num; $data['pay_score'] = $attr['score'] * $num; $data['deduct_money'] = ($attr['price'] ?? 0) * $num - $data['pay_money']; break; case ScoreMallEnum::TYPE_LIMIT: $deduct_ratio = empty((float)$score_config['score_deduct_ratio']) ? 1 : $score_config['score_deduct_ratio']; $deduct_limit = Json::decode($attr['deduction_limit'], true); $low = $deduct_limit[0]; $high = $deduct_limit[1]; $data['exchange_price'] = '¥' . ($attr['price'] ?? 0); // 默认售价 // 最低抵扣不做限制,则最低零点抵扣 if ($low['limit'] == 0) { $low_score = 0; // 最低积分 } else { $low_score = $low['score'] * $num; // 最低积分 } // 最高抵扣不做限制,则最高抵扣为商品原价 if ($high['limit'] == 0) { $high_score = ceil($attr['price'] * $deduct_ratio * $num); // 最高积分 } else { $high_price = ceil($high['score'] / $deduct_ratio) * $num; // 最高价 if ($high_price >= $attr['price'] * $num) { // 设置的最高价若大于等级商品售价则显示商品售价 $high_score = ceil($attr['price'] * $deduct_ratio * $num); // 最高积分 } else { $high_score = $high['score'] * $num; // 最高积分 } } // 获取用户的现有积分 if ($user_id) { $score = UserWallet::find()->select("score")->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->scalar() ?? 0; $score = floatval($score); if ($score < $low_score) { // 不够积分情况 $data['pay_score'] = $low_score; // 支付的积分 $data['pay_money'] = ceil(($attr['price'] * $num) - ($low_score / $deduct_ratio)); // 支付的金额 $data['exchange_price'] = $low_score . $score_name . '+¥' . $data['pay_money']; } elseif ($score >= $low_score && $score < $high_score) { // 积分足够情况 $data['pay_score'] = $score; // 支付的积分 $data['pay_money'] = ceil(($attr['price'] * $num) - ($score / $deduct_ratio)); // 支付的金额 $data['exchange_price'] = empty($data['pay_money']) ? $score . $score_name : $score . $score_name . '+¥' . $data['pay_money']; } else { // 积分足够并且超出最大设置 $data['pay_score'] = $high_score; // 支付的积分 $data['pay_money'] = ceil(($attr['price'] * $num) - ($high_score / $deduct_ratio)); // 支付的金额 $data['exchange_price'] = $data['pay_score'] . $score_name; !empty($data['pay_money']) && $data['exchange_price'] .= '+¥' . $data['pay_money']; } $price_arr = []; // 单价 !empty($data['pay_money']) && $price_arr[] = '¥' . bcdiv($data['pay_money'], $num, 2); !empty($data['pay_score']) && $price_arr[] = bcdiv($data['pay_score'], $num, 2) . $score_name; $price = implode('+', $price_arr); $exchange_price = []; !empty($data['pay_money']) && $exchange_price[] = '¥' . $data['pay_money']; !empty($data['pay_score']) && $exchange_price[] = $data['pay_score'] . $score_name; $total_price = implode('+', $exchange_price); // 总价 } $data['deduct_money'] = $attr['price'] * $num - $data['pay_money']; break; } $data['extra_info'] = ['price' => $price, 'total_price' => $total_price]; return $data; } /** * 装修选择链接获取积分商品 * @Author: lun * @DateTime: 2022/1/10 0010 19:13 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $mall_id * @param $post * @return array|bool|mixed|void */ public static function getGoodsListByDiy($mall_id, $post) { $where[] = ['mall_id' => $mall_id]; $where[] = ['status' => StatusEnum::ENABLED]; if (!empty($post['keyword'])) { $goods_list = Goods::lists(['where' => [['mall_id' => $mall_id], ['like', 'goods_name', trim($post['keyword']) . '%', false], ['status' => StatusEnum::ENABLED]], 'select' => 'id']); $goods_id_arr = array_column($goods_list, 'id'); $where[] = ['goods_id' => $goods_id_arr]; } $order = 'id desc'; $select = "id,goods_id,cate_id,score,money,type,created_at"; $with = [ 'goods' => function ($query) { $query->select("id,goods_name,price,cover_pic,subtitle"); }, 'attr' => function ($query) { $query->select("goods_id,goods_attr_id,score,money,deduction_limit,price"); } ]; $params = compact('where', 'order', 'select', 'with'); $list = self::listPage($params, false, true); if (!empty($list['list'])) { $addons_fields = []; if (isset($post['parameter']) && isset($post['parameter']['addonsFields'])) { $addons_fields = $post['parameter']['addonsFields']; } if ($addons_fields) { $goods_list_info = []; foreach ($list['list'] as $value) { $value['goods']['id'] = $value['goods_id']; $goods_list_info[$value['goods_id']] = $value['goods']; } $goods_list_info = Goods::getAddonsGoodsSetting($mall_id, $addons_fields, $goods_list_info, 'list'); foreach ($list['list'] as &$v) { if (!isset($goods_list_info[$v['goods_id']])) continue; $v = array_merge($goods_list_info[$v['goods_id']], $v); } unset($v); } // 获取积分配置 $score_config = Yii::$app->services->setting->mall->get($mall_id, 'score'); foreach ($list['list'] as &$item) { $item['id'] = $item['goods_id']; $item['img'] = $item['goods']['cover_pic']; $item['title'] = $item['goods']['goods_name']; $item['subtitle'] = $item['goods']['subtitle']; $item['price'] = $item['goods']['price']; $exchange_price = ScoreMallGoods::getExchangePrice($item['type'], $item['attr'][0], $score_config, 1, 0); $item['exchange_price'] = $exchange_price['exchange_price']; unset($item['goods']); unset($item['goods_id']); } } return $list; } /** * 获取分类 * @Author: lun * @DateTime: 2022/1/10 0010 19:16 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $mall_id * @param $post * @return array|bool|mixed|void */ public static function getCateListByDiy($mall_id, $post) { $res = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_MALL, 'basic'); $list = !empty($res) && isset($res['classification']) ? json_decode($res['classification'], true) : []; if ($list) { foreach ($list as $key => &$item) { if ($item['status'] == 0) unset($list[$key]); if ($post['keyword'] && stristr($item['name'], $post['keyword']) == false) unset($list[$key]); $item['title'] = $item['name']; $item['created_at'] = strtotime($item['created_at']); } } return ['list' => array_values($list)]; } /** * 保存数据 * @param int $goods_id * @param int $cate_id * @param int $score * @param int $money type=2时 商品价格 * @param int $price 对应商品表price * @param int $type 1:全积分兑换,2:积分加金额,3:积分抵扣 * @param int $num 已兑换数量 * @param int $use_coupon 0:参数优惠券抵扣,1:不参加优惠券抵扣 * @param string action edit时不允许变更goods_id * @author hpei * @return array */ public static function setData($params) { $goods_id = $params['goods_id']; try { $scoreMdl = self::findOne(['goods_id' => $goods_id]); if ($scoreMdl && isset($params['action']) && $params['action'] == 'add' && $scoreMdl->status != StatusEnum::DELETE) { self::$static_error = '该商品已存在'; return false; } if (empty($scoreMdl)) { if (isset($params['action']) && $params['action'] == 'edit') { self::$static_error = '该商品不存在'; return false; } $scoreMdl = new self(); $scoreMdl->loadDefaultValues(); } if (!$scoreMdl->load($params, '') || !$scoreMdl->save()) { self::$static_error = $scoreMdl->getErrorMessage(); return false; } if ($params['action'] != 'edit') { $installData = [ 'mall_id' => $scoreMdl->mall_id, 'goods_id' => $scoreMdl->goods_id, 'marketing_id' => $scoreMdl->id, 'status' => StatusEnum::ENABLED, 'marketing_type' => ScoreMallEnum::ADDONS_NAME ]; GoodsMarketing::setData($installData); } return $scoreMdl->toArray(); } catch (Exception $e) { self::$static_error = $e->getMessage(); return false; } } /** * 积分商品海报分享内容 * @Author: lun * @DateTime: 2022/1/17 0017 15:20 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $mall_id * @param $goods_id * @return array|bool */ public static function getPosterScoreGoods($mall_id, $goods_id) { $score_goods = ScoreMallGoods::getOne([['goods_id' => $goods_id, 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]], true, ['goods' => function ($query) { $query->select("id,goods_name,price,original_price,cover_pic"); }, 'attr' => function ($query) { $query->select("goods_id,score,money,price,deduction_limit"); }], false, 'goods_id,score,money,type,price'); if (empty($score_goods)) return false; $score_config = Yii::$app->services->setting->mall->get($mall_id, 'score'); $exchange = self::getExchangePrice($score_goods['type'], $score_goods['attr'][0], $score_config); return [ 'item_id' => $goods_id, 'share_title' => $score_goods['goods']['goods_name'], 'price' => ltrim($exchange['exchange_price'], '¥'), 'original_price' => $score_goods['price'], 'share_img' => $score_goods['goods']['cover_pic'], ]; } public static function getScoreMallExtraFields($mall_id, $list){ if (!empty($list['list'])) { $data = AddonsMarketingSetting::getOne([['mall_id' => $mall_id], ['addons_name' => ScoreMallEnum::ADDONS_NAME], ['status' => StatusEnum::ENABLED]]); $content = json_decode($data['content'], true); // 勾选的字段 $selected_fields = []; if(isset($content[3]['data']['showContent'])&&is_array($content[3]['data']['showContent'])){ $show_content = $content[3]['data']['showContent']; $selected_fields = array_column($show_content, 'field'); } $exclude_fields = ['goods_name', 'price', 'original_price']; $need_fields = []; foreach($selected_fields as $field) { !in_array($field, $exclude_fields) && $need_fields[] = $field; } if ($need_fields) { $goods_list_info = []; foreach ($list['list'] as $value) { $value['goods']['id'] = $value['goods_id']; $goods_list_info[$value['goods_id']] = $value['goods']; } $goods_list_info = Goods::getAddonsGoodsSetting($mall_id, $need_fields, $goods_list_info, 'list'); foreach ($list['list'] as &$v) { if (!isset($goods_list_info[$v['goods_id']])) continue; $v['goods'] = array_merge($goods_list_info[$v['goods_id']], $v['goods']); } unset($v); } } return $list; } }