| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- namespace addons\BlindBox\backend\controllers;
- use addons\BlindBox\common\models\BlindBoxCate;
- use addons\BlindBox\common\models\BlindBoxGoods;
- use common\enums\GoodsTypeEnum;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use Yii;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\BlindBox\backend\controllers
- */
- class GoodsController extends BaseController
- {
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $is_true = false;
- $goods_select = 'id,goods_name,goods_type,is_on_sale';
- $goods_ids = [];
- if (!empty($params['name'])) {
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $where[] = ['like', 'goods_name', trim($params['name'])];
- $goods_list = Goods::lists(['where' => $where, 'index' => 'id', 'select' => $goods_select]);
- $goods_ids = array_column($goods_list, 'id');
- $is_true = true;
- }
- if (isset($params['status'])) {
- $params['where'][] = ['status' => $params['status']];
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['mch_id' => 0];
- $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $where[] = ['is_on_sale' => $params['status']];
- $goods_list = Goods::lists(['where' => $where, 'index' => 'id', 'select' => $goods_select]);
- $goods_id_arr = array_column($goods_list, 'id');
- if ($is_true) {
- $goods_ids = array_intersect($goods_ids, $goods_id_arr);
- } else {
- $goods_ids = array_column($goods_list, 'id');
- $is_true = true;
- }
- }
- $where = [];
- if ($is_true) $where[] = ['goods_id' => $goods_ids];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- if (!empty($params['start'])) $where[] = ['>=', 'created_at', strtotime($params['start'])];
- if (!empty($params['end'])) $where[] = ['<=', 'created_at', strtotime($params['end'])];
- $params['where'] = $where;
- $params['limit'] = 10;
- $params['order'] = 'id DESC';
- $params['select'] = 'id,mall_id,goods_id,cate_id,contribution,contribution_percent,profit,profit_percent,status,created_at';
- $pageData = BlindBoxGoods::listPage($params, false);
- if ($pageData === false) return $this->error(BlindBoxCate::getStaticError());
- if (!empty($pageData['list'])) {
- $goods_ids = array_column($pageData['list'], 'goods_id');
- $cate_ids = array_column($pageData['list'], 'cate_id');
- if (!isset($goods_list)) {
- $goods_list = Goods::lists(['where' => [['id' => $goods_ids]], 'index' => 'id', 'select' => $goods_select]);
- }
- $cate_list = BlindBoxCate::lists(['where' => [['id' => $cate_ids]], 'index' => 'id', 'select' => 'id,name']);
- foreach ($pageData['list'] as &$item) {
- $item['cate_name'] = $item['goods_name'] = $item['goods_type'] = '';
- if (isset($goods_list[$item['goods_id']])) {
- $item['goods_name'] = $goods_list[$item['goods_id']]['goods_name'];
- $item['goods_type'] = $goods_list[$item['goods_id']]['goods_type'];
- $item['status'] = $goods_list[$item['goods_id']]['is_on_sale'];
- }
- if (isset($cate_list[$item['cate_id']])) {
- $item['cate_name'] = $cate_list[$item['cate_id']]['name'];
- }
- }
- }
- $pageData['status_map'] = ['下架', '上架'];
- $pageData['goods_type_map'] = GoodsTypeEnum::getMap();
- return $this->success('操作成功', $pageData);
- }
- }
- return $this->render('index', []);
- }
- //添加
- public function actionAdd()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['goods_id'], 'required','message' => '请选择商品'],
- [['contribution_percent'], 'required', 'message' => '请选择贡献值类型'],
- [['contribution'], 'required', 'message' => '请填写贡献值'],
- [['profit_percent'], 'required', 'message' => '请选择利润值类型'],
- [['profit'], 'required', 'message' => '请填写利润值'],
- [['cate_id'], 'required', 'message' => '请选择分类'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $res = BlindBoxGoods::addData($params);
- if ($res === false) return $this->error(BlindBoxCate::getStaticError());
- return $this->success('操作成功');
- }
- }
- }
- //编辑
- public function actionEdit()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required', 'message' => '请选择商品'],
- [['contribution_percent'], 'required', 'message' => '请选择贡献值类型'],
- [['contribution'], 'required', 'message' => '请填写贡献值'],
- [['profit_percent'], 'required', 'message' => '请选择利润值类型'],
- [['profit'], 'required', 'message' => '请填写利润值'],
- [['cate_id'], 'required', 'message' => '请选择分类'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $res = BlindBoxGoods::setData($params);
- if ($res === false) return $this->error(BlindBoxCate::getStaticError());
- return $this->success('操作成功');
- }
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required', 'message' => '请选择商品'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $data = BlindBoxGoods::getOne([
- ['id' => $params['id']],
- ['mall_id' => $this->mall_id],
- ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
- ],
- true, [], false, 'id,contribution_percent,contribution,profit_percent,profit,cate_id');
- return $this->success('操作成功', $data);
- }
- }
- }
- //删除
- public function actionDel()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required', 'message' => '请选择商品'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $ids = $params['id'];
- $res = BlindBoxGoods::del($this->mall_id, $ids);
- if ($res == false) return $this->error(BlindBoxGoods::getStaticError());
- return $this->success('操作成功');
- }
- }
- }
- }
|