Discount.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * 商品折扣关联表数据操作
  4. * Created by PhpStorm.
  5. * Author: 芸众商城 www.yunzshop.com
  6. * Date: 2017/2/28
  7. * Time: 上午11:01
  8. */
  9. namespace app\backend\modules\goods\models;
  10. use app\backend\modules\goods\services\DiscountService;
  11. use app\common\traits\MessageTrait;
  12. class Discount extends \app\common\models\goods\Discount
  13. {
  14. static protected $needLog = true;
  15. use MessageTrait;
  16. //public $timestamps = false;
  17. public $attributes = [
  18. 'level_discount_type' => 1,
  19. 'discount_method' => 1
  20. ];
  21. /**
  22. * 获取商品折扣数据
  23. * @param int $goodsId
  24. * @return array
  25. */
  26. public static function getList($goodsId)
  27. {
  28. return self::getGoodsDiscountList($goodsId);
  29. }
  30. public static function relationSave($goodsId, $data, $operate = '')
  31. {
  32. if (!$goodsId) {
  33. return false;
  34. }
  35. if (!$data) {
  36. return false;
  37. }
  38. self::deletedDiscount($goodsId);
  39. $discount_data = [];
  40. if (!empty($data['discount_value'])) {
  41. foreach ($data['discount_value'] as $key => $value) {
  42. $discount_data[] = [
  43. 'level_discount_type' => !empty($data['level_discount_type']) ? $data['level_discount_type'] : '1',
  44. 'discount_method' => !empty($data['discount_method']) ? $data['discount_method'] : '1',
  45. 'level_id' => $key,
  46. 'discount_value' => $value,
  47. 'goods_id' => $goodsId
  48. ];
  49. }
  50. return self::addByGoodsId($discount_data);
  51. }
  52. return true;
  53. }
  54. public static function addByGoodsId($discount_data)
  55. {
  56. foreach ($discount_data as $discount) {
  57. $discountModel = new static;
  58. $discountModel->setRawAttributes($discount);
  59. $discountModel->save();
  60. }
  61. return true;
  62. }
  63. public static function getModel($goodsId, $operate)
  64. {
  65. $model = false;
  66. if ($operate != 'created') {
  67. $model = static::where(['goods_id' => $goodsId])->first();
  68. }
  69. !$model && $model = new static;
  70. return $model;
  71. }
  72. /**
  73. * 商品折扣数据添加
  74. * @param array $DiscountInfo
  75. * @return bool
  76. */
  77. public static function createdDiscount($DiscountInfo)
  78. {
  79. return self::insert($DiscountInfo);
  80. }
  81. /**
  82. * 商品折扣数据更新
  83. * @param array $DiscountInfo
  84. * @return mixed
  85. */
  86. public static function updatedDiscount($goodsId, $DiscountInfo)
  87. {
  88. return self::where('goods_id', $goodsId)->update($DiscountInfo);
  89. }
  90. /**
  91. * 商品折扣数据删除
  92. * @param int $goodsId
  93. * @return mixed
  94. */
  95. public static function deletedDiscount($goodsId)
  96. {
  97. return self::where('goods_id', $goodsId)->delete();
  98. }
  99. public static function getDetail()
  100. {
  101. return self::hasMany('app\backend\modules\goods\models\DiscountDetail', 'goods_id');
  102. }
  103. }