GoodsForm.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace app\modules\admin\models\integralAppreciation;
  3. use app\models\Goods;
  4. use app\models\IntegralAppreciationGoods;
  5. use yii\base\Model;
  6. class GoodsForm extends Model
  7. {
  8. public $store_id;
  9. public $goods_id;
  10. public $reflux_amount;
  11. public $integral_amount;
  12. public $goods_name;
  13. public $start_time;
  14. public $end_time;
  15. public $id;
  16. public $ids;
  17. public function rules()
  18. {
  19. return [
  20. [['store_id', 'goods_id', 'id'], 'integer'],
  21. [['reflux_amount', 'integral_amount'], 'number'],
  22. [['ids', 'goods_name', 'start_time', 'end_time'], 'string']
  23. ];
  24. }
  25. public function attributeLabels()
  26. {
  27. return [
  28. 'store_id' => '商城ID',
  29. 'goods_id' => '商品ID',
  30. 'reflux_amount' => '回流奖金池金额',
  31. 'integral_amount' => '增值积分金额',
  32. 'id' => 'ID',
  33. 'ids' => 'ID',
  34. ];
  35. }
  36. /**
  37. * 保存
  38. */
  39. public function save() {
  40. try {
  41. $id = $this->id;
  42. $goods_id = $this->goods_id;
  43. $store_id = $this->store_id;
  44. $reflux_amount = $this->reflux_amount;
  45. $integral_amount = $this->integral_amount;
  46. $goods = Goods::findOne(['id' => $goods_id, 'is_delete' => 0, 'store_id' => $store_id]);
  47. if (!$goods) {
  48. throw new \Exception('商品不存在');
  49. }
  50. if ($reflux_amount <= 0) {
  51. throw new \Exception('回流奖金池金额必须大于0');
  52. }
  53. // if ($integral_amount <= 0) {
  54. // throw new \Exception('增值积分金额必须大于0');
  55. // }
  56. if ($id) {
  57. $model = IntegralAppreciationGoods::findOne(['id' => $id, 'is_delete' => 0]);
  58. } else {
  59. $model = IntegralAppreciationGoods::findOne(['goods_id' => $goods_id, 'is_delete' => 0]);
  60. if ($model) {
  61. throw new \Exception('该商品已存在');
  62. }
  63. $model = new IntegralAppreciationGoods();
  64. $model->store_id = $store_id;
  65. }
  66. $model->goods_id = $goods_id;
  67. $model->reflux_amount = $reflux_amount;
  68. $model->integral_amount = $integral_amount;
  69. if (!$model->save()) {
  70. throw new \Exception(implode(',', $model->getFirstErrors()));
  71. }
  72. return [
  73. 'code' => 0,
  74. 'msg' => '保存成功'
  75. ];
  76. } catch (\Exception $e) {
  77. return [
  78. 'code' => 1,
  79. 'msg' => $e->getMessage()
  80. ];
  81. }
  82. }
  83. /**
  84. * 列表
  85. */
  86. public function getList() {
  87. $goods_name = $this->goods_name;
  88. $start_time = $this->start_time;
  89. $end_time = $this->end_time;
  90. $store_id = $this->store_id;
  91. $query = IntegralAppreciationGoods::find()->alias('ig')
  92. ->leftJoin(['g' => Goods::tableName()], 'ig.goods_id = g.id')
  93. ->where(['ig.is_delete' => 0, 'g.is_delete' => 0, 'ig.store_id' => $store_id]);
  94. if ($goods_name) {
  95. $query->andWhere(['like', 'g.name', $goods_name]);
  96. }
  97. if ($start_time) {
  98. $start_time = strtotime($start_time);
  99. $query->andWhere(['>=', 'ig.created_at', $start_time]);
  100. }
  101. if ($end_time) {
  102. $end_time = strtotime($end_time);
  103. $query->andWhere(['<=', 'ig.created_at', $end_time]);
  104. }
  105. $query->orderBy('ig.id DESC')
  106. ->select('ig.id, ig.goods_id, ig.reflux_amount, ig.integral_amount, ig.created_at, g.name, g.cover_pic, g.price');
  107. $list = pagination_make($query);
  108. foreach ($list['list'] as &$item) {
  109. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  110. }
  111. return [
  112. 'code' => 0,
  113. 'msg' => '',
  114. 'data' => $list
  115. ];
  116. }
  117. /**
  118. * 删除 / 批量
  119. */
  120. public function del() {
  121. try {
  122. $ids = $this->ids;
  123. $ids = explode(',', $ids);
  124. if (!$ids) {
  125. throw new \Exception('请选择要删除的项');
  126. }
  127. foreach ($ids as $id) {
  128. $goods = IntegralAppreciationGoods::findOne($id);
  129. $goods->is_delete = 1;
  130. if (!$goods->save()) {
  131. throw new \Exception(implode(',', $goods->getFirstErrors()));
  132. }
  133. }
  134. return [
  135. 'code' => 0,
  136. 'msg' => '删除成功'
  137. ];
  138. } catch (\Exception $e) {
  139. return [
  140. 'code' => 1,
  141. 'msg' => $e->getMessage()
  142. ];
  143. }
  144. }
  145. }