| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582 |
- <?php
- /**
- * 零售端花材管理控制器
- * 主要用途:供花店后台管理花材,例如:获取花材列表、隐藏或显示花材、修改或清除花材展示备注说明、限购设置等。
- * 使用角色:花店管理员/商户管理员。
- * 解决问题:在总店修改或清除花材备注说明时,能跨越直营和加盟店的限制,一键同步更新到所有分店,保证全店备注信息统一。
- */
- namespace hd\controllers;
- use bizHd\shop\classes\ShopClass;
- use biz\item\classes\PtItemClass;
- use bizHd\item\classes\ItemClass;
- use bizGhs\pictext\classes\PicTextGhsItemClass;
- use bizHd\custom\classes\CustomClass;
- use bizHd\product\classes\ProductClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\miniUtil;
- use common\components\PosterUtil;
- use common\components\stringUtil;
- use common\components\util;
- use Yii;
- class ItemController extends BaseController
- {
- //列出所有花材 ssh 20240222
- public function actionGetItemList()
- {
- $get = Yii::$app->request->get();
- $search = $get['search'] ?? '';
- $requestType = $get['requestType'] ?? 'kd';
- $classId = $get['classId'] ?? 0;
- $lookStock = $get['lookStock'] ?? 0;
- $where['mainId'] = $this->mainId;
- if ($requestType == 'kd') {
- $where['delStatus'] = 0;
- $where['status'] = 1;
- } elseif ($requestType == 'itemList') {
- $where['delStatus'] = 0;
- if ($lookStock == 1) {
- $where['stock>'] = 0;
- }
- if ($lookStock == 2) {
- $where['stock'] = 0;
- }
- unset($where['status']);
- } elseif ($requestType == 'changePrice') {
- $where['delStatus'] = 0;
- $where['stock>'] = 0;
- } elseif ($requestType == 'hasStockList') {
- $where['delStatus'] = 0;
- $where['stock>'] = 0;
- } elseif ($requestType == 'book') {
- $where['delStatus'] = 0;
- unset($where['status']);
- } elseif ($requestType == 'outList') {
- $where['delStatus'] = 0;
- $where['status'] = 2;
- $where['removeStatus'] = 0;
- } elseif ($requestType == 'stopList') {
- $where['delStatus'] = 1;
- unset($where['status']);
- $where['removeStatus'] = 0;
- } elseif ($requestType == 'removeList') {
- $where['delStatus'] = 1;
- unset($where['status']);
- $where['removeStatus'] = 1;
- } elseif ($requestType == 'cg') {
- $where['delStatus'] = 0;
- unset($where['status']);
- } elseif ($requestType == 'changeStock') {
- $where['delStatus'] = 0;
- unset($where['status']);
- } elseif ($requestType == 'check') {
- $where['delStatus'] = 0;
- unset($where['status']);
- } elseif ($requestType == 'stockOut') {
- $where['delStatus'] = 0;
- $where['status'] = 1;
- } elseif ($requestType == 'break') {
- $where['delStatus'] = 0;
- $where['status'] = 1;
- } elseif ($requestType == 'part') {
- $where['delStatus'] = 0;
- $where['status'] = 1;
- } else {
- util::fail('没有定义的请求方式');
- }
- if (!empty($search)) {
- if (stringUtil::isLetter($search)) {
- $search = strtolower($search);
- $where['py'] = ['like', $search];
- } else {
- $where['name'] = ['like', $search];
- }
- } else {
- if (!empty($classId)) {
- if ($classId == -1) {
- $where['discountPrice>'] = 0;
- } elseif ($classId == -2) {
- $where['reachNum>'] = 0;
- } elseif ($classId == -3) {
- $where['presell'] = 1;
- } else {
- $where['classId'] = $classId;
- }
- }
- }
- $customId = $get['customId'] ?? 0;
- $custom = CustomClass::getById($customId, true);
- if (!empty($custom)) {
- if ($custom->shopId != $this->shopId) {
- util::fail('不是你的客户哦,编号55236');
- }
- }
- $level = 0;
- if($requestType == 'itemList'){
- //如果是花材列表页,则显示三个价格,其他地方,比如拆散和报损那些,只显示零售价
- $level = 1;
- }
- $field = '*';
- $result = ProductClass::getList($field, $where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC]);
- $list = $result['list'] ?? [];
- if ($requestType == 'kd') {
- $list = ProductClass::kdItemGroup($list, $level);
- } elseif ($requestType == 'itemList') {
- $list = ProductClass::itemListGroup($list, $level);
- } elseif ($requestType == 'changePrice') {
- $list = ProductClass::changePriceGroup($list, $level);
- } elseif ($requestType == 'break') {
- $list = ProductClass::breakItemGroup($list, $level);
- } elseif ($requestType == 'part') {
- $list = ProductClass::partItemGroup($list, $level);
- } elseif ($requestType == 'check') {
- $list = ProductClass::checkItemGroup($list, $level);
- } elseif ($requestType == 'cg') {
- $list = ProductClass::cgItemGroup($list, $level);
- } elseif ($requestType == 'changeStock') {
- $list = ProductClass::changeStockGroup($list, $level);
- } elseif ($requestType == 'stockOut') {
- $list = ProductClass::stockOutGroup($list, $level);
- } else {
- util::fail('没有数据');
- }
- $result['list'] = $list;
- util::success($result);
- }
- //增加和减少半扎 ssh 20250417
- public function actionChangeHalf()
- {
- util::fail('此功能已停用');
- $post = Yii::$app->request->post();
- $productId = $post['productId'] ?? '';
- $add = $post['add'] ?? 0;
- $remark = $post['remark'] ?? '';
- $product = ProductClass::getLockById($productId);
- if (empty($product)) {
- util::fail('没有找到花材');
- }
- if ($product->mainId != $this->mainId) {
- util::fail('不是你的花材');
- }
- if ($product->ratioType == 1) {
- util::fail('若干扎的花材不能减半份');
- }
- //避免重复提交
- util::checkRepeatCommit($this->adminId, 4);
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $shop = $this->shop;
- $staff = $this->shopAdmin;
- $staffName = $staff->name ?? '';
- $staffId = $staff->id;
- $adminId = $this->adminId;
- $params = [
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'adminId' => $adminId,
- 'remark' => $remark,
- ];
- ItemClass::modifyHalf($shop, $product, $params, $add);
- $product = ProductClass::getLockById($productId);
- $stock = $product->stock;
- $stock = floatval($stock);
- $transaction->commit();
- util::success(['stock' => $stock]);
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::info("加减半份失败原因:" . $e->getMessage());
- util::fail('加减半份失败');
- }
- }
- //检测是否要刷新
- public function actionCheckRefresh()
- {
- $mainId = $this->mainId;
- $staffId = $this->shopAdminId;
- $respond = Yii::$app->redis->executeCommand('GET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
- if ($respond == 'yes') {
- util::success(['remind' => 1]);
- }
- util::complete();
- }
- //列出所有花材,包括特价花材列表 ssh 20220315
- public function actionShowList()
- {
- $headers = Yii::$app->getRequest()->getHeaders();
- $requestVersion = $headers->get('appVersion');
- $currentVersion = dict::getDict('appVersion');
- if ($requestVersion < $currentVersion) {
- util::fail('请先升级系统');
- }
- $get = Yii::$app->request->get();
- $isCashier = $get['isCashier'] ?? 0;
- $mainId = $this->mainId;
- if ($isCashier == 1) {
- //去掉收银台提示价格更新
- $staffId = $this->shopAdminId;
- Yii::$app->redis->executeCommand('DEL', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh']);
- }
- $where['mainId'] = $mainId;
- $where['delStatus'] = 0;
- $where['status'] = 1;
- $list = \bizHd\item\classes\ItemClass::getShowList($where);
- $productList = [];
- if (!empty($list)) {
- foreach ($list as $key => $item) {
- if (!empty($item)) {
- $productList = array_merge($productList, $item);
- }
- }
- }
- util::success(['list' => $list, 'productList' => $productList]);
- }
- public function actionAdd()
- {
- util::fail('开发中');
- }
- public function actionBatchAdd()
- {
- util::fail('开发中');
- }
- public function actionDelete()
- {
- util::fail('不能删除哦!');
- }
- //获取平台里的花材信息
- public function actionPlatformItem()
- {
- $py = Yii::$app->request->get('py', '');
- $where = [];
- if ($py) {
- $where = ['py' => $py];
- }
- $list = PtItemClass::getItemList($where);
- util::success($list);
- }
- //获取给散客下单的花材海报 ssh 20220111
- public function actionGetPoster()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $item = ItemClass::getById($id, true);
- if (empty($item) || $item->mainId != $this->mainId) {
- util::fail('获取失败');
- }
- $cover = $item->cover ?? '';
- if (empty($cover)) {
- util::fail('获取失败');
- }
- $cover = imgUtil::normalizeOssObjectPath($cover);
- $merchant = WxOpenClass::getMallWxInfo();
- $page = 'pages/item/detail';
- $ptStyle = dict::getDict('ptStyle', 'mall');
- $shop = $this->shop;
- $shopId = $shop->id ?? 0;
- //scene不能超过32个字条
- $scene = "id=" . $id . '&a=' . $shopId;
- $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
- $miniCode = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle, $envVersion);
- Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
- $prefix = imgUtil::getPrefix();
- $level = 0;
- $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
- $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
- $price = \bizGhs\product\classes\ProductClass::getFinalPrice($item, $level, $priceMap, $addPriceMap);
- $staff = $this->shopAdmin;
- $staffName = $staff->name ?? '';
- $url = PosterUtil::buildMallItemPosterUrl($prefix, PosterUtil::withShopAndRemark([
- 'cover' => $cover,
- 'miniCode' => $miniCode,
- 'priceText' => '¥' . floatval($price),
- 'name' => $item->name ?? '',
- 'staffText' => $staffName . ' 为您推荐',
- 'mainId' => $this->mainId,
- ], $this->shop, $this->sj ?? null, $item));
- util::success(['imgUrl' => $url]);
- }
- //获取花材描述
- public function actionDesc()
- {
- $itemId = Yii::$app->request->get('itemId', 0);
- $picText = PicTextGhsItemClass::getByCondition(['mainId'=> $this->mainId, 'ghsItemId' => $itemId, 'delStatus' => 0], true);
- if (empty($picText)) {
- // 适配前端的返回处理 -- 本来要使用 util::fail()
- util::success('not_exist');
- }
- if ($picText->mainId != $this->mainId) {
- util::fail('没有找到描述');
- }
- util::success($picText);
- }
- //创建花材描述(花材介绍)
- public function actionCreateDesc()
- {
- $form = new \ghs\models\item\CreateDescForm(); // 暂时使用同一份 ghs,项目独立时,再复制一份
- $form->load(Yii::$app->request->post(), '');
- if (!$form->validateForm()) {
- return;
- }
- $data = $form->attributes;
- $exists = PicTextGhsItemClass::exists(['mainId'=>$this->mainId, 'ghsItemId' => $data['itemId'], 'delStatus' => 0]);
- if ($exists) {
- util::fail('该商品已存在图文内容,请勿重复添加');
- }
- $data['mainId'] = $this->mainId;
- $data['ghsItemId'] = $data['itemId'];
- $result = PicTextGhsItemClass::add($data);
- if ($result) {
- util::success($result);
- }
- util::fail('创建失败');
- }
- //更新花材描述(花材介绍)
- public function actionUpdateDesc()
- {
- $form = new \ghs\models\item\CreateDescForm();
- $form->load(Yii::$app->request->post(), '');
- if (!$form->validateForm()) {
- return;
- }
- $data = $form->attributes;
- $id = $data['id'];
- $info = PicTextGhsItemClass::getById($id, false, 'id');
- if (empty($info)) {
- util::fail('描述不存在');
- }
- $data['mainId'] = $this->mainId;
- $result = PicTextGhsItemClass::updateById($id, $data);
- if ($result) {
- util::success($result);
- }
- util::fail('更新失败');
- }
- public function actionChangeFrontHide()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $status = $get['status'] ?? '';
- if (is_numeric($status) == false) {
- util::fail('请选择方式');
- }
- $info = ItemClass::getById($id, true);
- if (empty($info)) {
- util::fail('没有找到花材');
- }
- if ($info->mainId != $this->mainId) {
- util::fail('不是你的花材呢');
- }
- $info->frontHide = $status;
- $info->save();
- util::complete();
- }
- public function actionChangeVirtualStock()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $virtualStock = $get['virtualStock'] ?? 0;
- $item = ItemClass::getById($id, true);
- if (empty($item)) {
- util::fail('没有找到花材');
- }
- if ($item->mainId != $this->mainId) {
- util::fail('不是你的花材');
- }
- $item->virtualStock = $virtualStock;
- $item->save();
- util::complete('修改成功');
- }
- //取消花材全部客户的已限数,重新开始计算
- public function actionClearLimitBuy()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? '';
- $item = ItemClass::getById($id, true);
- if (empty($item)) {
- util::fail('没有找到花材');
- }
- if ($item->mainId != $this->mainId) {
- util::fail('不是你的花材');
- }
- ProductClass::clearLimitBuyCache($id);
- util::complete('清除成功');
- }
- //清空单个客户的已购
- public function actionClearOneLimitBuy()
- {
- $get = Yii::$app->request->get();
- $productId = $get['productId'] ?? '';
- $customId = $get['customId'] ?? 0;
- $item = ItemClass::getById($productId, true);
- if (empty($item)) {
- util::fail('没有找到花材');
- }
- if ($item->mainId != $this->mainId) {
- util::fail('不是你的花材');
- }
- $custom = CustomClass::getById($customId, true);
- if (empty($custom)) {
- util::fail('没有客户');
- }
- if ($custom->shopId != $this->shopId) {
- util::fail('不是你的客户');
- }
- ProductClass::baseClearLimitBuy($productId, $customId);
- util::complete('清除成功');
- }
- //零售端--花材已购客户列表
- public function actionHasLimitBuyCustomList()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? '';
- $item = ItemClass::getById($id, true);
- if (empty($item)) {
- util::fail('没有找到花材');
- }
- if ($item->mainId != $this->mainId) {
- util::fail('不是你的花材');
- }
- $customList = ProductClass::getHasLimitBuyList($item);
- util::success(['customList' => $customList]);
- }
- /**
- * 修改花材展示备注说明
- * 职责:更新指定花材的备注内容。如果当前店铺是总店,则需将修改同步到商户下的所有分店(不论直营还是加盟店)。
- * 入参:id (花材ID,来自 GET 传参), itemRemark (备注内容,来自 GET 传参)
- * 返回:保存成功提示(JSON 格式)
- * 副作用:会更新该商家下所有分店对应花材的备注说明。
- */
- public function actionUpdateItemRemark()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $itemRemark = trim($get['itemRemark'] ?? '');
- if (empty($itemRemark)) {
- util::fail('请填写备注');
- }
- if (mb_strlen($itemRemark) > 200) {
- util::fail('备注不能超过200字');
- }
- $info = ItemClass::getById($id, true);
- if (empty($info)) {
- util::fail('没有找到花材');
- }
- if ($info->mainId != $this->mainId) {
- util::fail('无法操作');
- }
- $info->itemRemark = $itemRemark;
- $info->save();
- // 复杂分支:若当前店铺是总店(default = 1),不管分店是直营还是加盟,都需要将修改后的花材备注同步至旗下所有分店,保证全渠道花材备注的一致性
- $shop = $this->shop;
- if (isset($shop->default) && $shop->default == 1) {
- $sjId = $shop->sjId ?? 0;
- $ptItemId = $info->itemId ?? 0;
- $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
- foreach ($chainShopList as $chainShop) {
- $chainShopId = $chainShop->id ?? 0;
- if ($chainShopId == $shop->id) {
- continue;
- }
- $chainMainId = $chainShop->mainId ?? 0;
- $chainProduct = ItemClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
- if (empty($chainProduct)) {
- continue;
- }
- $chainProduct->itemRemark = $itemRemark;
- $chainProduct->save();
- }
- }
- util::complete('保存成功');
- }
- /**
- * 清除花材展示备注说明
- * 职责:清空指定花材的备注内容。如果当前店铺是总店,则需同步清除商户下的所有分店(不论直营还是加盟店)对应花材的备注内容。
- * 入参:id (花材ID,来自 GET 传参)
- * 返回:清除成功提示(JSON 格式)
- * 副作用:会清空该商家下所有分店对应花材的备注说明。
- */
- public function actionClearItemRemark()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $info = ItemClass::getById($id, true);
- if (empty($info)) {
- util::fail('没有找到花材');
- }
- if ($info->mainId != $this->mainId) {
- util::fail('无法操作');
- }
- $info->itemRemark = '';
- $info->save();
- // 复杂分支:若当前店铺是总店(default = 1),不管分店是直营还是加盟,都需要同步清空其花材备注说明,确保全渠道数据同步与统一
- $shop = $this->shop;
- if (isset($shop->default) && $shop->default == 1) {
- $sjId = $shop->sjId ?? 0;
- $ptItemId = $info->itemId ?? 0;
- $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
- foreach ($chainShopList as $chainShop) {
- $chainShopId = $chainShop->id ?? 0;
- if ($chainShopId == $shop->id) {
- continue;
- }
- $chainMainId = $chainShop->mainId ?? 0;
- $chainProduct = ItemClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
- if (empty($chainProduct)) {
- continue;
- }
- $chainProduct->itemRemark = '';
- $chainProduct->save();
- }
- }
- util::complete('清除成功');
- }
- }
|