GoodsExtra.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. namespace common\models\goods;
  3. use common\enums\addons\DecorationFieldsLabelEnum;
  4. use common\enums\StatusEnum;
  5. use common\globals\GlobalInvoke;
  6. use common\models\Currency;
  7. use common\models\user\UserLevel;
  8. use Yii;
  9. use common\models\base\BaseActiveRecord;
  10. use Exception;
  11. use yii\db\Expression;
  12. use yii\helpers\Json;
  13. /**
  14. *
  15. * This is the model class for table "{{%goods_extra}}".
  16. *
  17. * @property int $id
  18. * @property int $mall_id 商城id
  19. * @property int $mch_id 商家id
  20. * @property int $goods_id 商品ID
  21. * @property float $full_reduce_money 单品满额减免金额
  22. * @property float $full_reduce_price_limit 单品满额金额
  23. * @property string $score_give_setting 积分赠送设置
  24. * @property string $balance_give_setting 余额赠送设置
  25. * @property string|null $score_deduct_setting 积分营销设置
  26. * @property string|null $currency_deduct_setting 货币营销设置
  27. * @property int $is_level 是否享受会员功能 [0=否,1=是]
  28. * @property int $is_member_price 是否单独设置会员价 [0=否,1=是]
  29. * @property string|null $member_price_setting 会员折扣设置
  30. * @property string|null $member_buy_limit_setting 会员限购设置
  31. * @property int $is_show_price 商品是否显示会员价[0=否, 1是]
  32. * @property int $member_price_switch 商品是否单独显示会员价[0=否, 1是]
  33. * @property int $is_repurchase 是否开启复购价[0=否, 1=是]
  34. * @property string $repurchase_setting 复购价设置
  35. * @property string $digital_give_setting 数字币赠送设置
  36. * @property int $created_at 创建时间
  37. * @property int $updated_at 修改时间
  38. * @property string $symptom_list 疾病列表,药品商品类型使用
  39. * @property int $description_template_id 商品说明模板id
  40. * @property string $description_list 商品说明列表
  41. * @property string $drug_generic_name 药品通用名称
  42. */
  43. class GoodsExtra extends BaseActiveRecord
  44. {
  45. const GIVE_TYPE_MONEY = 1; // 按固定金额赠送
  46. const GIVE_TYPE_PERCENT = 2; // 按售价百分比赠送
  47. const DEDUCT_TYPE_SINGLE = 1; // 单件抵扣
  48. const DEDUCT_TYPE_MULT = 2; // 多件累计抵扣
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public static function tableName()
  53. {
  54. return '{{%goods_extra}}';
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function rules()
  60. {
  61. return [
  62. [['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'],
  63. [['full_reduce_money', 'full_reduce_price_limit'], 'number'],
  64. [['score_give_setting','balance_give_setting', 'score_deduct_setting', 'currency_deduct_setting', 'member_price_setting', 'member_buy_limit_setting', 'repurchase_setting'], 'string'],
  65. [['digital_give_setting'],'string'],
  66. [['drug_generic_name'],'string', 'max' => 120],
  67. [['symptom_list', 'description_list', 'member_alone_buy_limit_setting'], 'safe']
  68. ];
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function attributeLabels()
  74. {
  75. return [
  76. 'id' => 'ID',
  77. 'goods_id' => '商品ID',
  78. 'full_reduce_money' => '单品满额减免金额',
  79. 'full_reduce_price_limit' => '单品满额金额',
  80. 'score_give_setting' => '积分赠送设置',
  81. 'balance_give_setting' => '余额赠送设置',
  82. 'score_deduct_setting' => '积分抵扣设置',
  83. 'currency_deduct_setting' => '货币营销设置',
  84. 'is_level' => '是否享受会员功能 [0=否,1=是]',
  85. 'is_member_price' => '是否单独设置会员价 [0=否,1=是]',
  86. 'is_show_price' => '商品是否显示会员价[0=否, 1是]',
  87. 'member_price_switch' => '商品是否单独显示会员价[0=否, 1是]',
  88. 'member_price_setting' => '会员折扣设置',
  89. 'member_buy_limit_setting' => '会员限购设置',
  90. 'is_repurchase' => '是否开启复购价[0=否, 1=是]',
  91. 'repurchase_setting' => '复购价设置',
  92. 'status' => '状态[1正常 -1删除]',
  93. 'created_at' => '创建时间',
  94. 'updated_at' => '修改时间',
  95. 'symptom_list' => '疾病列表,药品商品类型使用',
  96. 'description_template_id' => '商品说明模板id',
  97. 'description_list' => '商品说明列表',
  98. 'drug_generic_name' => '药品通用名称',
  99. ];
  100. }
  101. /**
  102. * 保存数据
  103. * @param $params
  104. * @return bool
  105. */
  106. public static function setData($goods_id, $params)
  107. {
  108. try {
  109. // 判断参数中是否存在数组,存在则转为json字符串
  110. foreach ($params as $key => &$item) {
  111. $item = is_array($item) ? json_encode($item, JSON_UNESCAPED_UNICODE) : $item;
  112. }
  113. // 根据商品id获取商品信息
  114. $model = self::findOne(['goods_id' => $goods_id, 'status' => StatusEnum::ENABLED]);
  115. if (empty($model)) {
  116. // 初始化商品额外参数
  117. $model = new self();
  118. $model->loadDefaultValues();
  119. }
  120. // 维护商品说明模板使用数量
  121. $beforeDescriptionTemplateId = $model['description_template_id'];
  122. $model->goods_id = $goods_id;
  123. if (!$model->load($params, '') || !$model->save(false)) {
  124. throw new \Exception($model->getErrorMessage());
  125. }
  126. if ($beforeDescriptionTemplateId != $model['description_template_id']) {
  127. $now = time();
  128. if ($beforeDescriptionTemplateId) {
  129. GoodsDescriptionTemplate::updateAll(
  130. [
  131. 'use_num' => new Expression("`use_num` - 1"),
  132. 'updated_at' => $now,
  133. ],
  134. [
  135. 'id' => $beforeDescriptionTemplateId,
  136. 'status' => StatusEnum::ENABLED,
  137. 'mall_id' => $params['mall_id'],
  138. ]
  139. );
  140. }
  141. if ($model['description_template_id']) {
  142. GoodsDescriptionTemplate::updateAll(
  143. [
  144. 'use_num' => new Expression("`use_num` + 1"),
  145. 'updated_at' => $now,
  146. ],
  147. [
  148. 'id' => $model['description_template_id'],
  149. 'status' => StatusEnum::ENABLED,
  150. 'mall_id' => $params['mall_id'],
  151. ]
  152. );
  153. }
  154. }
  155. return true;
  156. } catch (\Exception $e) {
  157. self::$static_error = $e->getMessage();
  158. return false;
  159. }
  160. }
  161. /**
  162. * 购买条件验证
  163. * @Author bing
  164. * @DateTime 2021-09-27 14:20:53
  165. * @copyright: Copyright (c) 2020 广东七件事集团
  166. * @param [type] $user
  167. * @param [type] $limit_setting
  168. * @return void
  169. */
  170. public static function buyLimitValidate($user, &$limit_setting)
  171. {
  172. try {
  173. if (!empty($limit_setting)) {
  174. if (!in_array($user->level, $limit_setting['member_level'])) {
  175. throw new Exception('你的等级不支持购买该商品');
  176. }
  177. foreach ($limit_setting as $key => $val) {
  178. if ($val == 0) continue;
  179. }
  180. }
  181. return true;
  182. } catch (\Exception $e) {
  183. self::$static_error = $e->getMessage();
  184. return false;
  185. }
  186. }
  187. public static function getFieldsDecoration($from, $mall_id)
  188. {
  189. $name = \Yii::$app->services->setting->mall->get($mall_id, 'score.score_name');
  190. $home_page = [
  191. ['label' => DecorationFieldsLabelEnum::SCORE_LABEL, 'name' => $name, 'color' => '', 'showAlpha' => 0, 'field' => 'system_score']
  192. ];
  193. $goods_detail = [
  194. ['name' => $name, 'type' => 'system_score', 'prefix_text' => '']
  195. ];
  196. $permession_fields = [
  197. ['key' => 'system_score', 'label' => DecorationFieldsLabelEnum::SCORE_LABEL, 'name' => $name, 'color' => '', 'prefix_text' => '赠送']
  198. ];
  199. $data = compact('home_page', 'goods_detail', 'permession_fields');
  200. return $data[$from] ?? [];
  201. }
  202. public static function getMarketingScore($mall_id, $source, $data)
  203. {
  204. if (!in_array($source, ['detail', 'list'])) return $data;
  205. //订单满额赠送
  206. $order_config = Yii::$app->services->setting->mall->get($mall_id, 'marketing');
  207. if (!empty($order_config['score_give_setting'])) $order_config['score_give_setting'] = json_decode($order_config['score_give_setting'], true);
  208. $name = \Yii::$app->services->setting->mall->get($mall_id, 'score.score_name');
  209. if ($source == 'list') {
  210. $goods_ids = array_column($data, 'id');
  211. $settings = self::lists([
  212. 'where' => [['goods_id' => $goods_ids], ['status' => StatusEnum::ENABLED]],
  213. 'select' => 'goods_id, score_give_setting',
  214. 'index' => 'goods_id'
  215. ], true);
  216. foreach ($data as &$val) {
  217. $val['score_name'] = $name;
  218. $val['system_score'] = 0;
  219. if (isset($settings[$val['id']])) {
  220. $give_setting = json_decode($settings[$val['id']]['score_give_setting'], true);
  221. if ($give_setting['enable_give_score'] == 1) {
  222. $give_num = $give_setting['give_num'] ?? 0;
  223. $give_type = $give_setting['give_type'] ?? 1;
  224. if ($give_num > 0) {
  225. if ($give_type == 2) {
  226. if ($give_setting['calculation_value'] == 1) {
  227. //商品利润
  228. $give_num = bcmul($val['price']-$val['cost_price'], bcdiv($give_num, 100, 2), 2);
  229. } else {
  230. //商品售价
  231. $give_num = bcmul($val['price'], bcdiv($give_num, 100, 2), 2);
  232. }
  233. }
  234. }
  235. $give_num > 0 && $val['system_score'] = $give_num;
  236. } else {
  237. //设置-订单设置-营销设置,开启营销设置开关并且开启积分赠送开关,商品金额大于等于设置的订单消费金额
  238. if ($order_config['enable_marketing'] == 1 && $order_config['enable_give_score'] == 1 && $val['price'] >= $order_config['total_price']) {
  239. $give_num = $order_config['score_give_setting']['give_num'] ?? 0;
  240. $give_type = $order_config['score_give_setting']['give_type'] ?? 1;
  241. if ($give_type == 2) {
  242. if ($order_config['score_give_setting']['score_giving_calculation'] == 1) {
  243. $give_num = bcmul($val['price']-$val['cost_price'], bcdiv($give_num, 100, 2), 2);
  244. } else {
  245. $give_num = bcmul($val['price'], bcdiv($give_num, 100, 2), 2);
  246. }
  247. }
  248. $give_num > 0 && $val['system_score'] = $give_num;
  249. }
  250. }
  251. }
  252. }
  253. unset($val);
  254. return $data;
  255. }
  256. if ($source == 'detail') {
  257. $goods_ids = $data['id'];
  258. $settings = self::getOne([['goods_id' => $goods_ids], ['status' => StatusEnum::ENABLED]], true, [], false, 'goods_id, score_give_setting');
  259. $data['score_name'] = $name;
  260. $data['system_score'] = 0;
  261. $give_setting = json_decode($settings['score_give_setting'], true);
  262. //商品营销设置积分赠送开关开启
  263. if ($give_setting['enable_give_score'] == 1) {
  264. $give_num = $give_setting['give_num'] ?? 0;
  265. $give_type = $give_setting['give_type'] ?? 1;
  266. if ($give_num > 0) {
  267. if ($give_type == 2) {
  268. if ($give_setting['calculation_value'] == 1) {
  269. //商品利润
  270. $give_num = bcmul($data['price']-$data['cost_price'], bcdiv($give_num, 100, 2), 2);
  271. } else {
  272. //商品售价
  273. $give_num = bcmul($data['price'], bcdiv($give_num, 100, 2), 2);
  274. }
  275. }
  276. }
  277. $give_num > 0 && $data['system_score'] = $give_num;
  278. } else {
  279. //score_giving_calculation
  280. //设置-订单设置-营销设置,开启营销设置开关并且开启积分赠送开关,商品金额大于等于设置的订单消费金额
  281. if ($order_config['enable_marketing'] == 1 && $order_config['enable_give_score'] == 1 && $data['price'] >= $order_config['total_price']) {
  282. $give_num = $order_config['score_give_setting']['give_num'] ?? 0;
  283. $give_type = $order_config['score_give_setting']['give_type'] ?? 1;
  284. if ($give_type == 2) {
  285. if ($order_config['score_give_setting']['score_giving_calculation'] == 1) {
  286. $give_num = bcmul($data['price']-$data['cost_price'], bcdiv($give_num, 100, 2), 2);
  287. } else {
  288. $give_num = bcmul($data['price'], bcdiv($give_num, 100, 2), 2);
  289. }
  290. }
  291. $give_num > 0 && $data['system_score'] = $give_num;
  292. }
  293. }
  294. return $data;
  295. }
  296. }
  297. public static function getExtraFields($mall_id, $from)
  298. {
  299. $list = self::getFieldsDecoration($from, $mall_id);
  300. // 公共调用
  301. $fields = GlobalInvoke::exec(GlobalInvoke::ADDONS_FIELDS_DECORATION, $mall_id, [
  302. 'type' => $from,
  303. 'mall_id' => $mall_id,
  304. 'list' => $list
  305. ]);
  306. return $fields ? $fields : $list;
  307. }
  308. public static function fixGoodsExtraSetting() {
  309. $model = self::find();
  310. $i = 1;
  311. $page_size = 1000;
  312. while ($list = $model->asArray()->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
  313. foreach ($list as $key => $value) {
  314. $data = [];
  315. $score_give_setting = [];
  316. $balance_give_setting = [];
  317. if (!empty($value['score_give_setting'])) {
  318. $score_give_setting = json_decode($value['score_give_setting'],true);
  319. if (!isset($score_give_setting['enable_give_score'])) {
  320. $score_give_setting['enable_give_score'] = 1;
  321. $data['score_give_setting'] = json_encode($score_give_setting);
  322. }
  323. }
  324. if (!empty($value['balance_give_setting'])) {
  325. $balance_give_setting = json_decode($value['balance_give_setting'],true);
  326. if (!isset($balance_give_setting['enable_give_balance'])) {
  327. $balance_give_setting['enable_give_balance'] = 1;
  328. $data['balance_give_setting'] = json_encode($balance_give_setting);
  329. }
  330. }
  331. if (!empty($data)) {
  332. $res = self::updateAll($data,['id' => $value['id']]);
  333. // if ($res) {
  334. // echo 'id:' .$value['id'] .' update success';
  335. // }
  336. }
  337. }
  338. $i++;
  339. }
  340. }
  341. }