| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- <?php
- namespace addons\ScoreMall\common\models;
- use addons\ScoreMall\common\enums\ScoreMallEnum;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\common\AddonsMarketingSetting;
- use common\models\goods\Goods;
- use common\models\goods\GoodsExtra;
- use common\models\user\UserWallet;
- use common\models\goods\GoodsMarketing;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%addons_score_mall_goods}}".
- * @property int $id
- * @property int $mall_id
- * @property int $goods_id 商品ID
- * @property int $goods_attr_id 属性ID
- * @property int $num 已兑换数量
- * @property int $type 类型
- * @property int $use_coupon 是否使用
- * @property int $status
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class ScoreMallGoods extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_score_mall_goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id'], 'required'],
- [['mall_id', 'status', 'created_at', 'updated_at', 'goods_id', 'type', 'num', 'use_coupon', 'cate_id'], 'integer'],
- [['money', 'price', 'score'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '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;
- }
- }
|