cxfoot/modules/admin/controllers/mall/GoodsController.php
2023-10-27 14:25:12 +08:00

314 lines
10 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2022年6月1日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\admin\controllers\mall;
use app\models\Model;
use yii\helpers\VarDumper;
use app\modules\admin\controllers\Controller;
use app\modules\admin\behaviors\LoginBehavior;
use app\models\common\CommonGoodsActionForm;
use app\models\common\CommonGoodsListForm;
use app\models\common\CommonGoodsEditForm;
use app\models\common\CommonCatActionForm;
use app\models\common\CommonCatListForm;
use app\models\common\CommonCatEditForm;
use app\components\SysConst;
use app\models\Cat;
use app\models\Goods;
use app\models\GoodsAttr;
use app\models\GoodsCat;
class GoodsController extends Controller
{
public function behaviors()
{
return array_merge(parent::behaviors(), [
'login' => [
'class' => LoginBehavior::className(),
],
]);
}
public function actionIndex()
{
if (\Yii::$app->request->isAjax) {
$form = new CommonGoodsListForm();
$form->attributes = \Yii::$app->request->get();
$form->cx_mch_id = $this->cx_mch_id;
// $form->plugin_sign = SysConst::$cxPluginSceneMall;
$form->setFields('g.id,g.goods_hub_id,g.status,g.price,g.goods_stock,g.virtual_sales,g.sort,g.created_at,g.payment_people,g.payment_amount,g.payment_num,g.payment_order,g.sales,g.viewed_count,gh.name,gh.cover_pic,gh.unit,gh.type');
$data = $form->search();
return $this->responseHandler($data);
}
$cat_list = Cat::find()
->where([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'type' => Cat::TYPE_GOODS,
])
->orderBy(['sort' => SORT_ASC, 'created_at' => SORT_DESC])
->select('id,name')->asArray()->all();
return $this->render('index', [
'cat_list' => $cat_list
]);
}
public function actionEdit($id = 0)
{
$model = Goods::findOne([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'id' => $id,
// 'plugin_sign' => SysConst::$cxPluginSceneMall
]);
if ($model == null)
$model = new Goods();
if (\Yii::$app->request->isPost) {
$form = new CommonGoodsEditForm();
$post = \Yii::$app->request->post();
// $signArr = Goods::findOne(['plugin_sign' => $post['plugin_sign']]);
// if ($signArr) {
// return $this->responseHandler(['code' => 1, 'msg' => "服务已存在,请勿重复添加"]);
// }
switch ($post['plugin_sign']) {
case 'ball_arm':
$post['name'] = '检测球杆';
break;
case "hit_ball":
$post['name'] = '击球检测';
break;
case "member":
$post['name'] = '购买会员';
break;
default:
$post['name'] = '';
}
$post['type'] = 0;
$form->attributes = $post;
$form->cx_mch_id = $this->cx_mch_id;
$form->model = $model;
$form->type = 0;//默认实体商品
$data = $form->save();
return $this->responseHandler($data);
}
$model = Goods::findOne([
'id' => $id,
]);
$return_url = \Yii::$app->request->referrer;
return $this->render('edit', [
'model' => $model,
'return_url' => $return_url,
]);
}
private function getAttrInfo($attr_id, $attr_groups)
{
$attr_info = [];
foreach ($attr_groups as $index => $attr_group) {
foreach ($attr_group['attr_list'] as $j => $attr) {
if ($attr['attr_id'] == $attr_id) {
$attr_info['attr_group_id'] = $attr_group['attr_group_id'];
$attr_info['attr_group_name'] = $attr_group['attr_group_name'];
$attr_info['attr_id'] = $attr['attr_id'];
$attr_info['attr_name'] = $attr['attr_name'];
return $attr_info;
}
}
}
return $attr_info;
}
public function actionDelete()
{
if (!\Yii::$app->request->isPost) {
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonGoodsActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->plugin_sign = SysConst::$cxPluginSceneMall;
$data = $form->delete();
return $this->responseHandler($data);
}
public function actionOnline()
{
if (!\Yii::$app->request->isPost) {
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonGoodsActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
// $form->plugin_sign = SysConst::$cxPluginSceneMall;
$data = $form->online();
return $this->responseHandler($data);
}
public function actionOffline()
{
if (!\Yii::$app->request->isPost) {
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonGoodsActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->plugin_sign = SysConst::$cxPluginSceneMall;
$data = $form->offline();
return $this->responseHandler($data);
}
public function actionCat()
{
if (\Yii::$app->request->isAjax) {
$form = new CommonCatListForm();
$form->attributes = \Yii::$app->request->get();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = Cat::TYPE_GOODS;
$data = $form->search();
return $this->responseHandler($data);
}
return $this->render('cat');
}
public function actionCatEdit($id = 0)
{
$model = Cat::findOne([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'id' => $id,
'type' => Cat::TYPE_GOODS,
]);
if ($model == null) {
$model = new Cat();
$model->status = 1;
}
if (\Yii::$app->request->isPost) {
$form = new CommonCatEditForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->model = $model;
$form->type = Cat::TYPE_GOODS;
$data = $form->save();
return $this->responseHandler($data);
}
$cat_ids = [];
$cat_ids[] = $model->isNewRecord ? 0 : $model->id;
$parent_list = Cat::find()
->where([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'type' => Cat::TYPE_GOODS,
])
->andWhere([
'NOT IN',
'id',
$cat_ids
])
->select('id,name')->asArray()->all();
$is_has_parent = false;//开启是否有父节点
$parent_list = $is_has_parent ? $parent_list : [];
array_unshift($parent_list, ['id' => 0, 'name' => '无上级分类']);
$return_url = \Yii::$app->request->referrer;
return $this->render('cat-edit', [
'model' => $model,
'return_url' => $return_url,
'parent_list' => $parent_list
]);
}
public function actionCatDelete()
{
if (!\Yii::$app->request->isPost) {
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonCatActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = Cat::TYPE_GOODS;
$data = $form->delete();
return $this->responseHandler($data);
}
public function actionCatShow()
{
if (!\Yii::$app->request->isPost) {
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonCatActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = Cat::TYPE_GOODS;
$data = $form->show();
return $this->responseHandler($data);
}
public function actionCatHide()
{
if (!\Yii::$app->request->isPost) {
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonCatActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = Cat::TYPE_GOODS;
$data = $form->hide();
return $this->responseHandler($data);
}
public function actionCatOpen()
{
if (!\Yii::$app->request->isPost) {
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonCatActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = Cat::TYPE_GOODS;
$data = $form->open();
return $this->responseHandler($data);
}
public function actionCatClose()
{
if (!\Yii::$app->request->isPost) {
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonCatActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = Cat::TYPE_GOODS;
$data = $form->close();
return $this->responseHandler($data);
}
}