TeamGoodsForm.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\modules\admin\models\team_grades;
  3. use app\models\Cat;
  4. use app\models\Goods;
  5. use app\models\GoodsCat;
  6. use app\models\TeamGradesGoods;
  7. use yii\base\Model;
  8. class TeamGoodsForm extends Model
  9. {
  10. public $id;
  11. public $ids;
  12. public $store_id;
  13. public $goods_id;
  14. public $goods_team_grades;
  15. public $cat_id;
  16. public $goods_name;
  17. public function rules()
  18. {
  19. return [
  20. [['store_id', 'goods_id', 'goods_team_grades', 'id', 'cat_id'], 'integer'],
  21. [['goods_name', 'ids'], 'string']
  22. ];
  23. }
  24. //设置商品业绩分红值
  25. public function setGoods() {
  26. try {
  27. $id = $this->id;
  28. $store_id = $this->store_id;
  29. $goods_id = $this->goods_id;
  30. $goods_team_grades = $this->goods_team_grades;
  31. $goods = \app\models\Goods::findOne(['id' => $goods_id, 'is_delete' => 0]);
  32. if (!$goods) {
  33. throw new \Exception('商品不存在');
  34. }
  35. $form = TeamGradesGoods::findOne($id);
  36. if (!$form) {
  37. $form = TeamGradesGoods::findOne(['goods_id' => $goods_id, 'is_delete' => 0]);
  38. }
  39. $form = $form ?: new TeamGradesGoods();
  40. $form->goods_team_grades = $goods_team_grades;
  41. $form->goods_id = $goods_id;
  42. $form->store_id = $store_id;
  43. if (!$form->save()) {
  44. throw new \Exception('商品不存在');
  45. }
  46. return [
  47. 'code' => 0,
  48. 'msg' => '保存成功'
  49. ];
  50. } catch (\Exception $e) {
  51. return [
  52. 'code' => 1,
  53. 'msg' => $e->getMessage()
  54. ];
  55. }
  56. }
  57. public function list() {
  58. $store_id = $this->store_id;
  59. $cat_id = $this->cat_id;
  60. $goods_name = $this->goods_name;
  61. $query = TeamGradesGoods::find()->alias('tg')
  62. ->leftJoin(['g' => Goods::tableName()], 'g.id=tg.goods_id')
  63. ->where(['tg.is_delete' => 0, 'tg.store_id' => $store_id, 'g.is_delete' => 0]);
  64. if (!empty(trim($goods_name))) {
  65. $query->andWhere(['LIKE', 'g.name', trim($goods_name)]);
  66. }
  67. if (isset($cat_id) && $cat_id > 0) {
  68. $query->leftJoin(['gc' => GoodsCat::tableName()], 'g.id = gc.goods_id')->andWhere([
  69. 'gc.cat_id' => Cat::getCatId($cat_id)
  70. ])->groupBy('gc.goods_id');
  71. }
  72. $query->orderBy('tg.id DESC')->select('tg.id, tg.goods_team_grades, g.id goods_id, g.name, g.cover_pic');
  73. $pagination = pagination_make($query);
  74. foreach ($pagination['list'] as &$list) {
  75. $goods_cat = GoodsCat::find()->alias('gc')
  76. ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id')
  77. ->where([ 'gc.goods_id' => $list['goods_id'], 'c.is_delete' => 0, 'gc.is_delete' => 0, 'c.store_id' => $store_id ])
  78. ->select(['c.name', 'c.id'])
  79. ->asArray()
  80. ->all();
  81. $list['cat'] = $goods_cat;
  82. }
  83. return [
  84. 'code' => 0,
  85. 'msg' => 'success',
  86. 'data' => [
  87. 'data' => $pagination['list'],
  88. 'pageNo' => $pagination['pageNo'],
  89. 'totalCount' => $pagination['totalCount'],
  90. ]
  91. ];
  92. }
  93. //删除商品业绩分红值
  94. public function del() {
  95. try {
  96. $ids = $this->ids;
  97. $ids = explode(',', $ids);
  98. foreach ($ids as $id) {
  99. $form = TeamGradesGoods::findOne($id);
  100. if (!$form) {
  101. throw new \Exception('数据不存在');
  102. }
  103. $form->is_delete = 1;
  104. if (!$form->save()) {
  105. throw new \Exception('删除失败');
  106. }
  107. }
  108. return [
  109. 'code' => 0,
  110. 'msg' => '删除成功'
  111. ];
  112. }catch (\Exception $e) {
  113. return [
  114. 'code' => 1,
  115. 'msg' => $e->getMessage()
  116. ];
  117. }
  118. }
  119. }