| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- <?php
- namespace bizGhs\cp\classes;
- use biz\cp\classes\PtCpItemClass;
- use biz\item\classes\PtItemClass;
- use bizGhs\base\classes\BaseClass;
- use bizGhs\item\classes\ItemClassClass;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\shop\classes\ShopClass;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\stringUtil;
- use common\components\util;
- use Yii;
- class CpClass extends BaseClass
- {
- public static $baseFile = '\bizGhs\cp\models\Cp';
- //从平台导入产品 ssh 20240226
- public static function importPtCp($ptCp, $currentShop)
- {
- $ptCpId = $ptCp->id ?? 0;
- $mainId = $currentShop->mainId ?? 0;
- $shopCp = CpClass::getByCondition(['mainId' => $mainId, 'ptCpId' => $ptCpId], true);
- if (empty($shopCp)) {
- $ptCpClassId = $ptCp->classId ?? 0;
- $ptCpClass = PtCpClassClass::getById($ptCpClassId, true);
- if (empty($ptCpClass)) {
- util::fail('平台产品的分类没有找到');
- }
- $ptCpClassName = $ptCpClass->name ?? '';
- $cpClassList = CpClassClass::getAllByCondition(['mainId' => $mainId, 'delStatus' => 0], null, "*", null, true);
- $defaultClassId = 0;
- $currentClassId = 0;
- if (!empty($cpClassList)) {
- foreach ($cpClassList as $cpClass) {
- if ($cpClass->isDefault == 1) {
- $defaultClassId = $cpClass->id ?? 0;
- }
- if ($ptCpClassName == $cpClass->name) {
- $currentClassId = $cpClass->id ?? 0;
- }
- }
- }
- if (empty($defaultClassId) && empty($currentClassId)) {
- util::fail('没有找到默认分类');
- }
- $currentClassId = !empty($currentClassId) ? $currentClassId : $defaultClassId;
- $arr = $ptCp->attributes;
- unset($arr['id']);
- unset($arr['addTime']);
- unset($arr['updateTime']);
- unset($arr['classId']);
- $arr['ptCpId'] = $ptCpId;
- $arr['classId'] = $currentClassId;
- $arr['mainId'] = $mainId;
- $shopCp = self::addCp($arr, $currentShop);
- }
- if ($ptCp->auth == 0) {
- return true;
- }
- $cpId = $shopCp->id ?? 0;
- $ptCpItemList = PtCpItemClass::getAllByCondition(['cpId' => $ptCpId], null, '*', null, true);
- foreach ($ptCpItemList as $ptCpItem) {
- if ($ptCpItem->auth == 1) {
- $ptItemId = $ptCpItem->itemId ?? 0;
- $productInfo = ProductClass::getByCondition(['mainId' => $mainId, 'itemId' => $ptItemId], true);
- if (empty($productInfo)) {
- $ptItemInfo = PtItemClass::getById($ptItemId);
- if (empty($ptItemInfo)) {
- util::fail('平台花材没有找到哦');
- }
- $customDefaultClass = ItemClassClass::getByCondition(['mainId' => $mainId, 'isDefault' => 1], true);
- if (empty($customDefaultClass)) {
- noticeUtil::push("mainId:{$mainId} 导入平台产品和花材,没有找到花材默认分类", '15280215347');
- util::fail('没有找到默认分类哈!');
- }
- $customDefaultClassId = $customDefaultClass->id ?? 0;
- unset($ptItemInfo['id']);
- unset($ptItemInfo['addTime']);
- unset($ptItemInfo['updateTime']);
- unset($ptItemInfo['classId']);
- $ptItemInfo['itemId'] = $ptItemId;
- $ptItemInfo['hasCpId'] = 1;
- $ptItemInfo['skPrice'] = 36;
- $ptItemInfo['price'] = 36;
- $ptItemInfo['hjPrice'] = 36;
- $ptItemInfo['mainId'] = $mainId;
- $ptItemInfo['py'] = stringUtil::py($ptItemInfo['name']);
- $ptItemInfo['classId'] = $customDefaultClassId;
- //有二个添加花材的地方,请搜索关键词add_item_place
- $productInfo = ProductClass::add($ptItemInfo, true);
- }
- $shopItemId = $productInfo->id ?? 0;
- $currentData = [
- 'cpId' => $cpId,
- 'ptCpId' => $ptCpId,
- 'productId' => $shopItemId,
- 'ptItemId' => $ptItemId,
- 'name' => $productInfo->name ?? '',
- 'py' => $productInfo->py ?? '',
- 'cpName' => $shopCp->name ?? '',
- 'cpPy' => $shopCp->py ?? '',
- 'mainId' => $mainId,
- 'inTurn' => $ptCpItem->inTurn ?? 100,
- 'auth' => $ptCpItem->auth ?? 0,
- ];
- CpItemClass::add($currentData, true);
- }
- }
- return $shopCp;
- }
- //恢复产品 ssh 20240222
- public static function recoverCp($cp, $masterShop)
- {
- $ptCpId = $cp->ptCpId ?? 0;
- $cp->delStatus = 0;
- $cp->save();
- if (isset($masterShop->default) && $masterShop->default == 1) {
- $sjId = $masterShop->sjId ?? 0;
- $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
- if (!empty($shopList)) {
- foreach ($shopList as $shop) {
- if ($shop->id == $masterShop->id) {
- continue;
- }
- $chainMainId = $shop->mainId ?? 0;
- $has = self::getByCondition(['mainId' => $chainMainId, 'ptCpId' => $ptCpId, 'delStatus' => 0], true);
- if (!empty($has)) {
- $has->delStatus = 0;
- $has->save();
- }
- }
- }
- }
- }
- //删除产品 ssh 20240221
- public static function delCp($cp, $masterShop)
- {
- $ptCpId = $cp->ptCpId ?? 0;
- $cp->delStatus = 1;
- $cp->save();
- if (isset($masterShop->default) && $masterShop->default == 1) {
- $sjId = $masterShop->sjId ?? 0;
- $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
- if (!empty($shopList)) {
- foreach ($shopList as $shop) {
- if ($shop->id == $masterShop->id) {
- continue;
- }
- $shopName = $shop->shopName ?? '';
- $chainMainId = $shop->mainId ?? 0;
- $has = self::getByCondition(['mainId' => $chainMainId, 'ptCpId' => $ptCpId, 'delStatus' => 0], true);
- if (empty($has)) {
- util::fail($shopName . '的产品没有找到');
- }
- $has->delStatus = 1;
- $has->save();
- }
- }
- }
- }
- public static function copyCp($oldMainId, $newMainId)
- {
- $oldCpClassList = CpClassClass::getAllByCondition(['mainId' => $oldMainId], null, '*');
- if (empty($oldCpClassList)) {
- return false;
- }
- foreach ($oldCpClassList as $oldCpClass) {
- $oldCpClassId = $oldCpClass['id'] ?? 0;
- unset($oldCpClass['addTime']);
- unset($oldCpClass['updateTime']);
- unset($oldCpClass['id']);
- $oldCpClass['mainId'] = $newMainId;
- $newCpClass = CpClassClass::add($oldCpClass, true);
- $newCpClassId = $newCpClass->id ?? 0;
- $oldCpList = self::getAllByCondition(['classId' => $oldCpClassId], null, '*');
- if (!empty($oldCpList)) {
- foreach ($oldCpList as $oldCp) {
- $oldCpId = $oldCp['id'] ?? 0;
- unset($oldCp['addTime']);
- unset($oldCp['updateTime']);
- unset($oldCp['id']);
- $oldCp['mainId'] = $newMainId;
- $oldCp['status'] = 2;
- $oldCp['classId'] = $newCpClassId;
- $newCp = self::add($oldCp, true);
- $newCpId = $newCp->id ?? 0;
- $oldCpItemList = CpItemClass::getAllByCondition(['cpId' => $oldCpId], null, '*', 'ptItemId');
- if (!empty($oldCpItemList)) {
- $ptItemIds = array_keys($oldCpItemList);
- $newProductList = ProductClass::getAllByCondition(['mainId' => $newMainId, 'itemId' => ['in', $ptItemIds]], null, '*', 'itemId');
- foreach ($oldCpItemList as $ptItemId => $oldCpItem) {
- if (isset($newProductList[$ptItemId])) {
- $currentProduct = $newProductList[$ptItemId];
- $currentProductId = $currentProduct['id'] ?? 0;
- unset($oldCpItem['id']);
- unset($oldCpItem['addTime']);
- unset($oldCpItem['updateTime']);
- $oldCpItem['productId'] = $currentProductId;
- $oldCpItem['cpId'] = $newCpId;
- $oldCpItem['mainId'] = $newMainId;
- CpItemClass::add($oldCpItem, true);
- }
- }
- }
- }
- }
- }
- }
- public static function getCpList($where)
- {
- $data = self::getList('*', $where, 'inTurn DESC,addTime DESC');
- if (!empty($data['list'])) {
- foreach ($data['list'] as $key => $val) {
- $shortCover = $val['cover'] ?? '';
- $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
- $data['list'][$key]['shortCover'] = $shortCover;
- $data['list'][$key]['cover'] = $cover;
- $data['list'][$key]['bigCover'] = $bigCover;
- }
- }
- return $data;
- }
- //更新产品 ssh 20240221
- public static function updateCp($data, $cp, $masterShop)
- {
- $id = $cp->id ?? 0;
- $ptCpId = $cp->ptCpId ?? 0;
- $classId = $data['classId'] ?? 0;
- $cpClass = CpClassClass::getById($classId, true);
- if (empty($cpClass)) {
- util::fail('没有找到分类');
- }
- if ($cpClass->mainId != $cp->mainId) {
- util::fail('不是你的产品哦');
- }
- $cpClassName = $cpClass->name ?? '';
- self::updateById($id, $data);
- if (isset($masterShop->default) && $masterShop->default == 1) {
- $sjId = $masterShop->sjId ?? 0;
- $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
- if (!empty($shopList)) {
- foreach ($shopList as $shop) {
- if ($shop->id == $masterShop->id) {
- continue;
- }
- $shopName = $shop->shopName ?? '';
- $mainId = $shop->mainId ?? 0;
- $chainCp = self::getByCondition(['mainId' => $mainId, 'ptCpId' => $ptCpId], true);
- if (empty($chainCp)) {
- util::fail($shopName . ' 的产品没有找到');
- }
- $chainCpId = $chainCp->id ?? 0;
- $cpClassList = CpClassClass::getAllByCondition(['mainId' => $mainId, 'delStatus' => 0], null, '*', null, true);
- $cpClassId = 0;
- if (!empty($cpClassList)) {
- foreach ($cpClassList as $cpClass) {
- $currentCpClassId = $cpClass->id ?? 0;
- if ($cpClass->name == $cpClassName) {
- $cpClassId = $currentCpClassId;
- }
- }
- }
- if (empty($cpClassId)) {
- util::fail($shopName . '的分类 ' . $cpClassName . ' 没有找到');
- }
- $chainData = $data;
- $chainData['classId'] = $cpClassId;
- self::updateById($chainCpId, $chainData);
- }
- }
- }
- }
- //添加产品 ssh 20240221
- public static function addCp($data, $currentShop)
- {
- $mainId = $currentShop->mainId ?? 0;
- $name = $data['name'] ?? '';
- if (empty($name)) {
- util::fail('请填写名称');
- }
- $classId = $data['classId'] ?? 0;
- if (empty($classId)) {
- util::fail('请选择分类');
- }
- $shopCpClass = CpClassClass::getById($classId, true);
- if (empty($shopCpClass)) {
- util::fail('没有找到分类');
- }
- if ($shopCpClass->mainId != $mainId) {
- util::fail('不是你的分类');
- }
- $shopCpClassName = $shopCpClass->name ?? '';
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- if (isset($data['ptCpId']) == false || empty($data['ptCpId'])) {
- $ptData = $data;
- $ptCpClassId = 0;
- $ptCpClassDefaultId = 0;
- $ptCpClassList = PtCpClassClass::getAllByCondition(['delStatus' => 0], null, '*');
- if (!empty($ptCpClassList)) {
- foreach ($ptCpClassList as $ptCpClass) {
- $currentId = $ptCpClass['id'] ?? 0;
- if (isset($ptCpClass['name']) && $ptCpClass['name'] == $shopCpClassName) {
- $ptCpClassId = $currentId;
- }
- if (isset($ptCpClass['isDefault']) && $ptCpClass['isDefault'] == 1) {
- $ptCpClassDefaultId = $currentId;
- }
- }
- }
- if (!empty($ptCpClassDefaultId)) {
- $ptData['classId'] = $ptCpClassDefaultId;
- }
- if (!empty($ptCpClassId)) {
- $ptData['classId'] = $ptCpClassId;
- }
- if (empty($ptCpClassDefaultId) && empty($ptCpClassId)) {
- util::fail('没有找到平台的分类');
- }
- $respond = PtCpClass::addCp($ptData);
- $ptCpId = $respond->id ?? 0;
- $data['ptCpId'] = $ptCpId;
- }
- $result = self::add($data, true);
- //在首店添加,则找到父级sj下的所有直营和加盟店,都进行同步
- if (isset($currentShop->default) && $currentShop->default == 1) {
- $shopList = ShopClass::getAllByCondition(['sjId' => $currentShop->sjId], null, '*', null, true);
- foreach ($shopList as $shop) {
- $shopId = $shop->id ?? 0;
- if ($shopId == $currentShop->id) {
- //前面已经添加过了
- continue;
- }
- $currentMainId = $shop->mainId ?? 0;
- $shopName = $shop->shopName ?? '';
- $chainClassList = CpClassClass::getAllByCondition(['mainId' => $currentMainId, 'delStatus' => 0], null, '*', null, true);
- $currentCpClassId = 0;
- if (!empty($chainClassList)) {
- foreach ($chainClassList as $chainClass) {
- $chainClassId = $chainClass->id ?? 0;
- if ($chainClass->name == $shopCpClassName) {
- $currentCpClassId = $chainClassId;
- }
- }
- }
- if (empty($currentCpClassId)) {
- util::fail($shopName . '没有找到相应的分类');
- }
- $currentData = $data;
- $currentData['shopId'] = $shopId;
- $currentData['mainId'] = $currentMainId;
- $currentData['classId'] = $currentCpClassId;
- self::add($currentData);
- }
- }
- $transaction->commit();
- return $result;
- } catch (\Exception $exception) {
- $transaction->rollBack();
- Yii::info('添加失败原因:' . $exception->getMessage());
- util::fail('添加失败');
- }
- }
- }
|