| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\integral\controllers;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Goods;
- use app\models\SaasIntegralCat;
- use app\plugins\integral\models\form\GoodsForm;
- use app\plugins\integral\models\SaasIntegralGoods;
- use yii\base\BaseObject;
- class GoodsController
- {
- public function actionList() {
- $status = get_params('status', -1);
- $name = get_params('name');
- $query = SaasIntegralGoods::find()->alias('ig')->where(['ig.store_id' => get_store_id(), 'ig.is_delete' => 0])->leftJoin(['g' => Goods::tableName()],'g.id=ig.goods_id');
- if (!empty($name)) {
- $query->andWhere([
- 'or',
- ['like', 'g.name', $name],
- ['like', 'ig.name', $name]
- ]);
- }
- if ($status >= 0) {
- $query->andWhere(['ig.status' => $status]);
- }
- $query->select(['ig.*'])->orderBy(['ig.sort' => SORT_ASC, 'ig.created_at' => SORT_DESC]);
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- $val['cat_name'] = SaasIntegralCat::findOne($val['cat_id'])->name;
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- $val['goods'] = SaasIntegralGoods::getGoods($val['goods_id']);
- $attr = json_decode($val['attr'], true);
- $count = count($attr);
- $str = '';
- foreach ($attr as $key => &$value) {
- $attr_group = GoodsForm::getAttrGroupByAttId($value['attr_id']);
- $t = $value['attr_name'];
- unset($value['attr_name']);
- $value['attr_group_name'] = $attr_group ? $attr_group->attr_group_name : null;
- $value['attr_name'] = $t;
- $str .= $value['attr_group_name'] . ':' . $t;
- if ($key < $count - 1) {
- $str .= ',';
- }
- }
- $val['attr_str'] = $str;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount'],
- ],
- ];
- }
- public function actionAttr()
- {
- $good_id = get_params('goods_id');
- $goods = Goods::findOne(['id' => $good_id, 'store_id' => get_store_id()]);
- if (!$goods->attr) {
- return [];
- }
- $attr_data = json_decode($goods->attr, true);
- foreach ($attr_data as $i => $attr_data_item) {
- $str = '';
- $count = count($attr_data[$i]['attr_list']);
- foreach ($attr_data[$i]['attr_list'] as $j => $attr_list) {
- $attr_group = GoodsForm::getAttrGroupByAttId($attr_data[$i]['attr_list'][$j]['attr_id']);
- $t = $attr_data[$i]['attr_list'][$j]['attr_name'];
- unset($attr_data[$i]['attr_list'][$j]['attr_name']);
- $attr_data[$i]['attr_list'][$j]['attr_group_name'] = $attr_group ? $attr_group->attr_group_name : null;
- $attr_data[$i]['attr_list'][$j]['attr_name'] = $t;
- $str .= $attr_data[$i]['attr_list'][$j]['attr_group_name'] . ':' . $t;
- if ($j < $count - 1) {
- $str .= ',';
- }
- }
- $attr_data[$i]['name'] = $str;
- }
- return [
- 'code' => 0,
- 'data' => [
- 'attr' => $attr_data
- ]
- ];
- }
- public function actionSave()
- {
- if (post_params('type') && post_params('type') == 'edit') {
- return $this->edit(post_params());
- }
- $form = new GoodsForm();
- $form->store_id = get_store_id();
- $form->attributes = post_params();
- return $form->save();
- }
- public function actionDetail()
- {
- $id = get_params('id');
- $store_id = get_store_id();
- $goods = SaasIntegralGoods::find()->where(['store_id' => $store_id, 'id' => $id])->asArray()->one();
- if (!$goods) {
- return [
- 'code' => 0,
- 'msg' => '未找到该记录'
- ];
- }
- $goods['origin_name'] = Goods::findOne($goods['goods_id'])->name;
- $goods['name'] = $goods['name'] ? $goods['name'] : $goods['origin_name'];
- $attr = json_decode($goods['attr'], true);
- $count = count($attr);
- $str = '';
- foreach ($attr as $key => &$value) {
- $attr_group = GoodsForm::getAttrGroupByAttId($value['attr_id']);
- $t = $value['attr_name'];
- unset($value['attr_name']);
- $value['attr_group_name'] = $attr_group ? $attr_group->attr_group_name : null;
- $value['attr_name'] = $t;
- $str .= $value['attr_group_name'] . ':' . $t;
- if ($key < $count - 1) {
- $str .= ',';
- }
- }
- $ids = array_column($attr, 'attr_id');
- $goods_num = 0;
- $goods_attr = json_decode(Goods::findOne($goods['goods_id'])->attr, true);
- foreach ($goods_attr as $value) {
- $goods_attr = array_column($value['attr_list'], 'attr_id');
- if ($ids == $goods_attr) {
- $goods_num = $value['num'];
- break;
- }
- }
- $goods['cat_id'] = intval($goods['cat_id']);
- $goods['goods_num'] = $goods_num;
- $goods['attr_str'] = $str;
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $goods
- ];
- }
- private function edit($args)
- {
- $t = \Yii::$app->db->beginTransaction();
- $integral_goods = SaasIntegralGoods::findOne($args['id']);
- $before = $integral_goods->goods_count;
- if (!$integral_goods) {
- return [
- 'code' => 1,
- 'msg' => '商品不存在'
- ];
- }
- if (empty($args['detail'])) {
- return [
- 'code' => 1,
- 'msg' => '请填写商品详情'
- ];
- }
- if ($args['goods_num'] >= $args['goods_count']) {
- $integral_goods->goods_count = $args['user_num'];
- } else {
- return [
- 'code' => 1,
- 'msg' => '积分商品规格库存不能大于本商品规格库存'
- ];
- }
- if ($args['name']) {
- $integral_goods->name = $args['name'];
- }
- if ($args['integral'] >= 0) {
- $integral_goods->integral = $args['integral'];
- }
- if ($args['user_num'] >= 0) {
- $integral_goods->user_num = $args['user_num'];
- }
- if ($args['sales'] >= 0) {
- $integral_goods->sales = $args['sales'];
- }
- if ($args['goods_count'] >= 0) {
- $integral_goods->goods_count = $args['goods_count'];
- }
- if ($args['cat_id'] >= 0) {
- $integral_goods->cat_id = $args['cat_id'];
- }
- if ($args['sort'] >= 0) {
- $integral_goods->sort = $args['sort'];
- }
- $integral_goods->detail = $args['detail'];
- if (!$integral_goods->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '编辑失败'
- ];
- } else {
- $ids = array_column(json_decode($integral_goods->attr, true), 'attr_id');
- // 修改库存
- $goods_attr_res = Goods::findOne($integral_goods['goods_id']);
- $goods_attr = json_decode($goods_attr_res->attr, true);
- foreach ($goods_attr as $key => $value) {
- $goods_attr_id = array_column($value['attr_list'], 'attr_id');
- if ($ids == $goods_attr_id) {
- $v = $integral_goods['goods_count'] - $before;
- $goods_attr[$key]['num'] -= $v;
- if ($goods_attr[$key]['num'] < 0) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '编辑失败'
- ];
- }
- break;
- }
- }
- $goods_attr_res->attr = json_encode($goods_attr);
- if (!$goods_attr_res->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '编辑失败'
- ];
- } else {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '编辑成功'
- ];
- }
- }
- }
- /**
- * 修改商品状态
- */
- public function actionStatus()
- {
- if (!in_array(intval(post_params('status')), [0, 1])) {
- return [
- 'code' => 1,
- 'msg' => 'status状态不正确'
- ];
- }
- $integral_goods = SaasIntegralGoods::findOne(post_params('id'));
- if (empty($integral_goods)) {
- return [
- 'code' => 1,
- 'msg' => '查无此商品'
- ];
- }
- if ($integral_goods->goods_count < 1 && post_params('status') == 1) {
- return [
- 'code' => 1,
- 'msg' => '积分商品库存不足'
- ];
- }
- $integral_goods->status = intval(post_params('status'));
- if (!$integral_goods->save()) {
- return [
- 'code' => 1,
- 'msg' => $integral_goods->errors[0]
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '更新状态成功'
- ];
- }
- /**
- * 删除商品
- */
- public function actionDelete()
- {
- $integral_goods = SaasIntegralGoods::find()->where(['id' => post_params('id'), 'is_delete' => 0])->one();
- if (empty($integral_goods)) {
- return [
- 'code' => 1,
- 'msg' => '查无此商品'
- ];
- }
- $integral_goods->is_delete = 1;
- if (!$integral_goods->save()) {
- return [
- 'code' => 1,
- 'msg' => $integral_goods->errors[0]
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '删除成功'
- ];
- }
- public function actionCat() {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => SaasIntegralCat::findAll(['is_delete' => SaasIntegralCat::DELETE_STATUS_FALSE, 'is_enable' => SaasIntegralCat::IS_ENABLE_TRUE])
- ];
- }
- }
|