| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- <?php
- namespace common\models\goods;
- use common\enums\addons\DecorationFieldsLabelEnum;
- use common\enums\StatusEnum;
- use common\globals\GlobalInvoke;
- use common\models\Currency;
- use common\models\user\UserLevel;
- use Yii;
- use common\models\base\BaseActiveRecord;
- use Exception;
- use yii\db\Expression;
- use yii\helpers\Json;
- /**
- *
- * This is the model class for table "{{%goods_extra}}".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $mch_id 商家id
- * @property int $goods_id 商品ID
- * @property float $full_reduce_money 单品满额减免金额
- * @property float $full_reduce_price_limit 单品满额金额
- * @property string $score_give_setting 积分赠送设置
- * @property string $balance_give_setting 余额赠送设置
- * @property string|null $score_deduct_setting 积分营销设置
- * @property string|null $currency_deduct_setting 货币营销设置
- * @property int $is_level 是否享受会员功能 [0=否,1=是]
- * @property int $is_member_price 是否单独设置会员价 [0=否,1=是]
- * @property string|null $member_price_setting 会员折扣设置
- * @property string|null $member_buy_limit_setting 会员限购设置
- * @property int $is_show_price 商品是否显示会员价[0=否, 1是]
- * @property int $member_price_switch 商品是否单独显示会员价[0=否, 1是]
- * @property int $is_repurchase 是否开启复购价[0=否, 1=是]
- * @property string $repurchase_setting 复购价设置
- * @property string $digital_give_setting 数字币赠送设置
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property string $symptom_list 疾病列表,药品商品类型使用
- * @property int $description_template_id 商品说明模板id
- * @property string $description_list 商品说明列表
- * @property string $drug_generic_name 药品通用名称
- */
- class GoodsExtra extends BaseActiveRecord
- {
- const GIVE_TYPE_MONEY = 1; // 按固定金额赠送
- const GIVE_TYPE_PERCENT = 2; // 按售价百分比赠送
- const DEDUCT_TYPE_SINGLE = 1; // 单件抵扣
- const DEDUCT_TYPE_MULT = 2; // 多件累计抵扣
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%goods_extra}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['goods_id', 'is_level', 'is_member_price', 'is_show_price', 'member_price_switch', 'status', 'created_at', 'updated_at', 'is_repurchase', 'enable_full_reduce', 'is_alone_show_return_btn', 'is_show_return_btn', 'description_template_id', 'mall_id', 'is_member_alone_buy_limit'], 'integer'],
- [['full_reduce_money', 'full_reduce_price_limit'], 'number'],
- [['score_give_setting','balance_give_setting', 'score_deduct_setting', 'currency_deduct_setting', 'member_price_setting', 'member_buy_limit_setting', 'repurchase_setting'], 'string'],
- [['digital_give_setting'],'string'],
- [['drug_generic_name'],'string', 'max' => 120],
- [['symptom_list', 'description_list', 'member_alone_buy_limit_setting'], 'safe']
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'goods_id' => '商品ID',
- 'full_reduce_money' => '单品满额减免金额',
- 'full_reduce_price_limit' => '单品满额金额',
- 'score_give_setting' => '积分赠送设置',
- 'balance_give_setting' => '余额赠送设置',
- 'score_deduct_setting' => '积分抵扣设置',
- 'currency_deduct_setting' => '货币营销设置',
- 'is_level' => '是否享受会员功能 [0=否,1=是]',
- 'is_member_price' => '是否单独设置会员价 [0=否,1=是]',
- 'is_show_price' => '商品是否显示会员价[0=否, 1是]',
- 'member_price_switch' => '商品是否单独显示会员价[0=否, 1是]',
- 'member_price_setting' => '会员折扣设置',
- 'member_buy_limit_setting' => '会员限购设置',
- 'is_repurchase' => '是否开启复购价[0=否, 1=是]',
- 'repurchase_setting' => '复购价设置',
- 'status' => '状态[1正常 -1删除]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- 'symptom_list' => '疾病列表,药品商品类型使用',
- 'description_template_id' => '商品说明模板id',
- 'description_list' => '商品说明列表',
- 'drug_generic_name' => '药品通用名称',
- ];
- }
- /**
- * 保存数据
- * @param $params
- * @return bool
- */
- public static function setData($goods_id, $params)
- {
- try {
- // 判断参数中是否存在数组,存在则转为json字符串
- foreach ($params as $key => &$item) {
- $item = is_array($item) ? json_encode($item, JSON_UNESCAPED_UNICODE) : $item;
- }
- // 根据商品id获取商品信息
- $model = self::findOne(['goods_id' => $goods_id, 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- // 初始化商品额外参数
- $model = new self();
- $model->loadDefaultValues();
- }
- // 维护商品说明模板使用数量
- $beforeDescriptionTemplateId = $model['description_template_id'];
- $model->goods_id = $goods_id;
- if (!$model->load($params, '') || !$model->save(false)) {
- throw new \Exception($model->getErrorMessage());
- }
- if ($beforeDescriptionTemplateId != $model['description_template_id']) {
- $now = time();
- if ($beforeDescriptionTemplateId) {
- GoodsDescriptionTemplate::updateAll(
- [
- 'use_num' => new Expression("`use_num` - 1"),
- 'updated_at' => $now,
- ],
- [
- 'id' => $beforeDescriptionTemplateId,
- 'status' => StatusEnum::ENABLED,
- 'mall_id' => $params['mall_id'],
- ]
- );
- }
- if ($model['description_template_id']) {
- GoodsDescriptionTemplate::updateAll(
- [
- 'use_num' => new Expression("`use_num` + 1"),
- 'updated_at' => $now,
- ],
- [
- 'id' => $model['description_template_id'],
- 'status' => StatusEnum::ENABLED,
- 'mall_id' => $params['mall_id'],
- ]
- );
- }
- }
- return true;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 购买条件验证
- * @Author bing
- * @DateTime 2021-09-27 14:20:53
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param [type] $user
- * @param [type] $limit_setting
- * @return void
- */
- public static function buyLimitValidate($user, &$limit_setting)
- {
- try {
- if (!empty($limit_setting)) {
- if (!in_array($user->level, $limit_setting['member_level'])) {
- throw new Exception('你的等级不支持购买该商品');
- }
- foreach ($limit_setting as $key => $val) {
- if ($val == 0) continue;
- }
- }
- return true;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getFieldsDecoration($from, $mall_id)
- {
- $name = \Yii::$app->services->setting->mall->get($mall_id, 'score.score_name');
- $home_page = [
- ['label' => DecorationFieldsLabelEnum::SCORE_LABEL, 'name' => $name, 'color' => '', 'showAlpha' => 0, 'field' => 'system_score']
- ];
- $goods_detail = [
- ['name' => $name, 'type' => 'system_score', 'prefix_text' => '']
- ];
- $permession_fields = [
- ['key' => 'system_score', 'label' => DecorationFieldsLabelEnum::SCORE_LABEL, 'name' => $name, 'color' => '', 'prefix_text' => '赠送']
- ];
- $data = compact('home_page', 'goods_detail', 'permession_fields');
- return $data[$from] ?? [];
- }
- public static function getMarketingScore($mall_id, $source, $data)
- {
- if (!in_array($source, ['detail', 'list'])) return $data;
- //订单满额赠送
- $order_config = Yii::$app->services->setting->mall->get($mall_id, 'marketing');
- if (!empty($order_config['score_give_setting'])) $order_config['score_give_setting'] = json_decode($order_config['score_give_setting'], true);
- $name = \Yii::$app->services->setting->mall->get($mall_id, 'score.score_name');
- if ($source == 'list') {
- $goods_ids = array_column($data, 'id');
- $settings = self::lists([
- 'where' => [['goods_id' => $goods_ids], ['status' => StatusEnum::ENABLED]],
- 'select' => 'goods_id, score_give_setting',
- 'index' => 'goods_id'
- ], true);
- foreach ($data as &$val) {
- $val['score_name'] = $name;
- $val['system_score'] = 0;
- if (isset($settings[$val['id']])) {
- $give_setting = json_decode($settings[$val['id']]['score_give_setting'], true);
- if ($give_setting['enable_give_score'] == 1) {
- $give_num = $give_setting['give_num'] ?? 0;
- $give_type = $give_setting['give_type'] ?? 1;
- if ($give_num > 0) {
- if ($give_type == 2) {
- if ($give_setting['calculation_value'] == 1) {
- //商品利润
- $give_num = bcmul($val['price']-$val['cost_price'], bcdiv($give_num, 100, 2), 2);
- } else {
- //商品售价
- $give_num = bcmul($val['price'], bcdiv($give_num, 100, 2), 2);
- }
- }
- }
- $give_num > 0 && $val['system_score'] = $give_num;
- } else {
- //设置-订单设置-营销设置,开启营销设置开关并且开启积分赠送开关,商品金额大于等于设置的订单消费金额
- if ($order_config['enable_marketing'] == 1 && $order_config['enable_give_score'] == 1 && $val['price'] >= $order_config['total_price']) {
- $give_num = $order_config['score_give_setting']['give_num'] ?? 0;
- $give_type = $order_config['score_give_setting']['give_type'] ?? 1;
- if ($give_type == 2) {
- if ($order_config['score_give_setting']['score_giving_calculation'] == 1) {
- $give_num = bcmul($val['price']-$val['cost_price'], bcdiv($give_num, 100, 2), 2);
- } else {
- $give_num = bcmul($val['price'], bcdiv($give_num, 100, 2), 2);
- }
- }
- $give_num > 0 && $val['system_score'] = $give_num;
- }
- }
- }
- }
- unset($val);
- return $data;
- }
- if ($source == 'detail') {
- $goods_ids = $data['id'];
- $settings = self::getOne([['goods_id' => $goods_ids], ['status' => StatusEnum::ENABLED]], true, [], false, 'goods_id, score_give_setting');
- $data['score_name'] = $name;
- $data['system_score'] = 0;
- $give_setting = json_decode($settings['score_give_setting'], true);
- //商品营销设置积分赠送开关开启
- if ($give_setting['enable_give_score'] == 1) {
- $give_num = $give_setting['give_num'] ?? 0;
- $give_type = $give_setting['give_type'] ?? 1;
- if ($give_num > 0) {
- if ($give_type == 2) {
- if ($give_setting['calculation_value'] == 1) {
- //商品利润
- $give_num = bcmul($data['price']-$data['cost_price'], bcdiv($give_num, 100, 2), 2);
- } else {
- //商品售价
- $give_num = bcmul($data['price'], bcdiv($give_num, 100, 2), 2);
- }
- }
- }
- $give_num > 0 && $data['system_score'] = $give_num;
- } else {
- //score_giving_calculation
- //设置-订单设置-营销设置,开启营销设置开关并且开启积分赠送开关,商品金额大于等于设置的订单消费金额
- if ($order_config['enable_marketing'] == 1 && $order_config['enable_give_score'] == 1 && $data['price'] >= $order_config['total_price']) {
- $give_num = $order_config['score_give_setting']['give_num'] ?? 0;
- $give_type = $order_config['score_give_setting']['give_type'] ?? 1;
- if ($give_type == 2) {
- if ($order_config['score_give_setting']['score_giving_calculation'] == 1) {
- $give_num = bcmul($data['price']-$data['cost_price'], bcdiv($give_num, 100, 2), 2);
- } else {
- $give_num = bcmul($data['price'], bcdiv($give_num, 100, 2), 2);
- }
- }
- $give_num > 0 && $data['system_score'] = $give_num;
- }
- }
- return $data;
- }
- }
- public static function getExtraFields($mall_id, $from)
- {
- $list = self::getFieldsDecoration($from, $mall_id);
- // 公共调用
- $fields = GlobalInvoke::exec(GlobalInvoke::ADDONS_FIELDS_DECORATION, $mall_id, [
- 'type' => $from,
- 'mall_id' => $mall_id,
- 'list' => $list
- ]);
- return $fields ? $fields : $list;
- }
- public static function fixGoodsExtraSetting() {
- $model = self::find();
-
- $i = 1;
- $page_size = 1000;
- while ($list = $model->asArray()->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
- foreach ($list as $key => $value) {
- $data = [];
- $score_give_setting = [];
- $balance_give_setting = [];
- if (!empty($value['score_give_setting'])) {
- $score_give_setting = json_decode($value['score_give_setting'],true);
- if (!isset($score_give_setting['enable_give_score'])) {
- $score_give_setting['enable_give_score'] = 1;
- $data['score_give_setting'] = json_encode($score_give_setting);
- }
- }
- if (!empty($value['balance_give_setting'])) {
- $balance_give_setting = json_decode($value['balance_give_setting'],true);
- if (!isset($balance_give_setting['enable_give_balance'])) {
- $balance_give_setting['enable_give_balance'] = 1;
- $data['balance_give_setting'] = json_encode($balance_give_setting);
- }
- }
- if (!empty($data)) {
- $res = self::updateAll($data,['id' => $value['id']]);
- // if ($res) {
- // echo 'id:' .$value['id'] .' update success';
- // }
- }
- }
- $i++;
- }
- }
- }
|