Sale.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/6
  6. * Time: 上午11:45
  7. */
  8. namespace app\backend\modules\goods\models;
  9. use Illuminate\Support\Facades\DB;
  10. class Sale extends \app\common\models\Sale
  11. {
  12. public $timestamps = false;
  13. static protected $needLog = true;
  14. /**
  15. * Author:blank
  16. * UpdateTime:2017/12/13
  17. * @param [int] $goodsId 商品id
  18. * @return object $saleData 商品营销对象
  19. */
  20. public static function getList($goodsId)
  21. {
  22. $saleData = self::where('goods_id', $goodsId)
  23. ->first();
  24. if ($saleData->is_push == 1) {
  25. $arr = explode('-', $saleData->push_goods_ids);
  26. if ($arr) {
  27. $goods = \app\common\models\Goods::getPushGoods($arr);
  28. foreach ($goods as $k=>$good) {
  29. $goods[$k]['thumb_url'] = yz_tomedia($good['thumb']);
  30. }
  31. $saleData->push_goods_ids = $goods;
  32. } else {
  33. $saleData->push_goods_ids = [];
  34. }
  35. // Goods::select('id','title')->whereIn('id', $arr)->where('status', 1)->get()->toArray();
  36. }
  37. //todo 兼容商品独立设置积分抵扣比例,这样写的原因是其他插件有写通知 % 判断的如果这边改了插件也要修改
  38. if ($saleData) {
  39. if (strexists($saleData->max_point_deduct, '%') || strexists($saleData->min_point_deduct, '%')) {
  40. $saleData->point_deduct_type = 1;
  41. } else {
  42. $saleData->point_deduct_type = 0;
  43. }
  44. $saleData->max_point_deduct = str_replace('%', '', $saleData->max_point_deduct);
  45. $saleData->min_point_deduct = str_replace('%', '', $saleData->min_point_deduct);
  46. }
  47. return $saleData;
  48. }
  49. public static function relationSave($goodsId, $data, $operate)
  50. {
  51. if (!$goodsId) {
  52. return false;
  53. }
  54. if (!$data) {
  55. return false;
  56. }
  57. $saleModel = self::getModel($goodsId, $operate);
  58. //判断deleted
  59. if ($operate == 'deleted') {
  60. return $saleModel->delete();
  61. }
  62. $data['goods_id'] = $goodsId;
  63. if (isset($data['ed_full'])) {
  64. $data['ed_full'] = empty($data['ed_full']) ? 0 : $data['ed_full'];
  65. }
  66. if (isset($data['ed_reduction'])) {
  67. $data['ed_reduction'] = empty($data['ed_reduction']) ? 0 : $data['ed_reduction'];
  68. }
  69. if (isset($data['point'])) {
  70. $data['point'] = trim($data['point']);
  71. }
  72. if (isset($data['first_parent_point'])) {
  73. $data['first_parent_point'] = trim($data['first_parent_point']);
  74. }
  75. if (isset($data['second_parent_point'])) {
  76. $data['second_parent_point'] = trim($data['second_parent_point']);
  77. }
  78. if (isset($data['award_balance'])) {
  79. $data['award_balance'] = trim($data['award_balance']);
  80. }
  81. if (isset($data['pay_reward_balance'])) {
  82. $data['pay_reward_balance'] = trim($data['pay_reward_balance']);
  83. }
  84. if ($data['point_deduct_type'] == 1) {
  85. if ($data['max_point_deduct'] !== '0' && !empty($data['max_point_deduct'])) {
  86. $data['max_point_deduct'] = trim($data['max_point_deduct']).'%';
  87. }
  88. if ($data['min_point_deduct'] !== '0' && !empty($data['min_point_deduct'])) {
  89. $data['min_point_deduct'] = trim($data['min_point_deduct']).'%';
  90. }
  91. }
  92. /**
  93. * Author:blank
  94. * UpdateTime:2017/12/13
  95. */
  96. if (isset($data['is_push'])) {
  97. if ($data['is_push'] == 1 && $data['push_goods_ids']) {
  98. $push_goods_ids = array_column($data['push_goods_ids'],'id');
  99. $data['push_goods_ids'] = implode('-', $push_goods_ids);
  100. } else {
  101. $data['push_goods_ids'] = '';
  102. }
  103. }
  104. //为了保存不报错添加过滤,0和空字符串不能过滤掉
  105. $data = array_map(function ($item) {
  106. if ($item === null || $item === "null") {
  107. return '';
  108. }
  109. return $item;
  110. },$data);
  111. $saleModel->fill($data);
  112. return $saleModel->save();
  113. }
  114. public static function getModel($goodsId, $operate)
  115. {
  116. $model = false;
  117. if ($operate != 'created') {
  118. $model = static::where(['goods_id' => $goodsId])->first();
  119. }
  120. !$model && $model = new static;
  121. return $model;
  122. }
  123. }