GoodsController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace addons\BlindBox\backend\controllers;
  3. use addons\BlindBox\common\models\BlindBoxCate;
  4. use addons\BlindBox\common\models\BlindBoxGoods;
  5. use common\enums\GoodsTypeEnum;
  6. use common\enums\StatusEnum;
  7. use common\models\goods\Goods;
  8. use Yii;
  9. /**
  10. * 默认控制器
  11. *
  12. * Class DefaultController
  13. * @package addons\BlindBox\backend\controllers
  14. */
  15. class GoodsController extends BaseController
  16. {
  17. /**
  18. * 首页
  19. *
  20. * @return string
  21. */
  22. public function actionIndex()
  23. {
  24. if (\Yii::$app->request->isAjax) {
  25. if (\Yii::$app->request->isGet) {
  26. $params = \Yii::$app->request->get();
  27. $is_true = false;
  28. $goods_select = 'id,goods_name,goods_type,is_on_sale';
  29. $goods_ids = [];
  30. if (!empty($params['name'])) {
  31. $where[] = ['mall_id' => $this->mall_id];
  32. $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  33. $where[] = ['like', 'goods_name', trim($params['name'])];
  34. $goods_list = Goods::lists(['where' => $where, 'index' => 'id', 'select' => $goods_select]);
  35. $goods_ids = array_column($goods_list, 'id');
  36. $is_true = true;
  37. }
  38. if (isset($params['status'])) {
  39. $params['where'][] = ['status' => $params['status']];
  40. $where[] = ['mall_id' => $this->mall_id];
  41. $where[] = ['mch_id' => 0];
  42. $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  43. $where[] = ['is_on_sale' => $params['status']];
  44. $goods_list = Goods::lists(['where' => $where, 'index' => 'id', 'select' => $goods_select]);
  45. $goods_id_arr = array_column($goods_list, 'id');
  46. if ($is_true) {
  47. $goods_ids = array_intersect($goods_ids, $goods_id_arr);
  48. } else {
  49. $goods_ids = array_column($goods_list, 'id');
  50. $is_true = true;
  51. }
  52. }
  53. $where = [];
  54. if ($is_true) $where[] = ['goods_id' => $goods_ids];
  55. $where[] = ['=', 'mall_id', $this->mall_id];
  56. $where[] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  57. if (!empty($params['start'])) $where[] = ['>=', 'created_at', strtotime($params['start'])];
  58. if (!empty($params['end'])) $where[] = ['<=', 'created_at', strtotime($params['end'])];
  59. $params['where'] = $where;
  60. $params['limit'] = 10;
  61. $params['order'] = 'id DESC';
  62. $params['select'] = 'id,mall_id,goods_id,cate_id,contribution,contribution_percent,profit,profit_percent,status,created_at';
  63. $pageData = BlindBoxGoods::listPage($params, false);
  64. if ($pageData === false) return $this->error(BlindBoxCate::getStaticError());
  65. if (!empty($pageData['list'])) {
  66. $goods_ids = array_column($pageData['list'], 'goods_id');
  67. $cate_ids = array_column($pageData['list'], 'cate_id');
  68. if (!isset($goods_list)) {
  69. $goods_list = Goods::lists(['where' => [['id' => $goods_ids]], 'index' => 'id', 'select' => $goods_select]);
  70. }
  71. $cate_list = BlindBoxCate::lists(['where' => [['id' => $cate_ids]], 'index' => 'id', 'select' => 'id,name']);
  72. foreach ($pageData['list'] as &$item) {
  73. $item['cate_name'] = $item['goods_name'] = $item['goods_type'] = '';
  74. if (isset($goods_list[$item['goods_id']])) {
  75. $item['goods_name'] = $goods_list[$item['goods_id']]['goods_name'];
  76. $item['goods_type'] = $goods_list[$item['goods_id']]['goods_type'];
  77. $item['status'] = $goods_list[$item['goods_id']]['is_on_sale'];
  78. }
  79. if (isset($cate_list[$item['cate_id']])) {
  80. $item['cate_name'] = $cate_list[$item['cate_id']]['name'];
  81. }
  82. }
  83. }
  84. $pageData['status_map'] = ['下架', '上架'];
  85. $pageData['goods_type_map'] = GoodsTypeEnum::getMap();
  86. return $this->success('操作成功', $pageData);
  87. }
  88. }
  89. return $this->render('index', []);
  90. }
  91. //添加
  92. public function actionAdd()
  93. {
  94. if (\Yii::$app->request->isAjax) {
  95. if (\Yii::$app->request->isPost) {
  96. $params = \Yii::$app->request->post();
  97. $res = Yii::$app->services->validate->validate($params, [
  98. [['goods_id'], 'required','message' => '请选择商品'],
  99. [['contribution_percent'], 'required', 'message' => '请选择贡献值类型'],
  100. [['contribution'], 'required', 'message' => '请填写贡献值'],
  101. [['profit_percent'], 'required', 'message' => '请选择利润值类型'],
  102. [['profit'], 'required', 'message' => '请填写利润值'],
  103. [['cate_id'], 'required', 'message' => '请选择分类'],
  104. ]);
  105. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  106. $params['mall_id'] = $this->mall_id;
  107. $res = BlindBoxGoods::addData($params);
  108. if ($res === false) return $this->error(BlindBoxCate::getStaticError());
  109. return $this->success('操作成功');
  110. }
  111. }
  112. }
  113. //编辑
  114. public function actionEdit()
  115. {
  116. if (\Yii::$app->request->isAjax) {
  117. if (\Yii::$app->request->isPost) {
  118. $params = \Yii::$app->request->post();
  119. $res = Yii::$app->services->validate->validate($params, [
  120. [['id'], 'required', 'message' => '请选择商品'],
  121. [['contribution_percent'], 'required', 'message' => '请选择贡献值类型'],
  122. [['contribution'], 'required', 'message' => '请填写贡献值'],
  123. [['profit_percent'], 'required', 'message' => '请选择利润值类型'],
  124. [['profit'], 'required', 'message' => '请填写利润值'],
  125. [['cate_id'], 'required', 'message' => '请选择分类'],
  126. ]);
  127. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  128. $params['mall_id'] = $this->mall_id;
  129. $res = BlindBoxGoods::setData($params);
  130. if ($res === false) return $this->error(BlindBoxCate::getStaticError());
  131. return $this->success('操作成功');
  132. }
  133. if (\Yii::$app->request->isGet) {
  134. $params = \Yii::$app->request->get();
  135. $res = Yii::$app->services->validate->validate($params, [
  136. [['id'], 'required', 'message' => '请选择商品'],
  137. ]);
  138. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  139. $data = BlindBoxGoods::getOne([
  140. ['id' => $params['id']],
  141. ['mall_id' => $this->mall_id],
  142. ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
  143. ],
  144. true, [], false, 'id,contribution_percent,contribution,profit_percent,profit,cate_id');
  145. return $this->success('操作成功', $data);
  146. }
  147. }
  148. }
  149. //删除
  150. public function actionDel()
  151. {
  152. if (\Yii::$app->request->isAjax) {
  153. if (\Yii::$app->request->isPost) {
  154. $params = \Yii::$app->request->post();
  155. $res = Yii::$app->services->validate->validate($params, [
  156. [['id'], 'required', 'message' => '请选择商品'],
  157. ]);
  158. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  159. $ids = $params['id'];
  160. $res = BlindBoxGoods::del($this->mall_id, $ids);
  161. if ($res == false) return $this->error(BlindBoxGoods::getStaticError());
  162. return $this->success('操作成功');
  163. }
  164. }
  165. }
  166. }