CpClass.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace bizGhs\cp\classes;
  3. use biz\cp\classes\PtCpItemClass;
  4. use biz\item\classes\PtItemClass;
  5. use bizGhs\base\classes\BaseClass;
  6. use bizGhs\item\classes\ItemClassClass;
  7. use bizGhs\product\classes\ProductClass;
  8. use bizGhs\shop\classes\ShopClass;
  9. use common\components\imgUtil;
  10. use common\components\noticeUtil;
  11. use common\components\stringUtil;
  12. use common\components\util;
  13. use Yii;
  14. class CpClass extends BaseClass
  15. {
  16. public static $baseFile = '\bizGhs\cp\models\Cp';
  17. //从平台导入产品 ssh 20240226
  18. public static function importPtCp($ptCp, $currentShop)
  19. {
  20. $ptCpId = $ptCp->id ?? 0;
  21. $mainId = $currentShop->mainId ?? 0;
  22. $shopCp = CpClass::getByCondition(['mainId' => $mainId, 'ptCpId' => $ptCpId], true);
  23. if (empty($shopCp)) {
  24. $ptCpClassId = $ptCp->classId ?? 0;
  25. $ptCpClass = PtCpClassClass::getById($ptCpClassId, true);
  26. if (empty($ptCpClass)) {
  27. util::fail('平台产品的分类没有找到');
  28. }
  29. $ptCpClassName = $ptCpClass->name ?? '';
  30. $cpClassList = CpClassClass::getAllByCondition(['mainId' => $mainId, 'delStatus' => 0], null, "*", null, true);
  31. $defaultClassId = 0;
  32. $currentClassId = 0;
  33. if (!empty($cpClassList)) {
  34. foreach ($cpClassList as $cpClass) {
  35. if ($cpClass->isDefault == 1) {
  36. $defaultClassId = $cpClass->id ?? 0;
  37. }
  38. if ($ptCpClassName == $cpClass->name) {
  39. $currentClassId = $cpClass->id ?? 0;
  40. }
  41. }
  42. }
  43. if (empty($defaultClassId) && empty($currentClassId)) {
  44. util::fail('没有找到默认分类');
  45. }
  46. $currentClassId = !empty($currentClassId) ? $currentClassId : $defaultClassId;
  47. $arr = $ptCp->attributes;
  48. unset($arr['id']);
  49. unset($arr['addTime']);
  50. unset($arr['updateTime']);
  51. unset($arr['classId']);
  52. $arr['ptCpId'] = $ptCpId;
  53. $arr['classId'] = $currentClassId;
  54. $arr['mainId'] = $mainId;
  55. $shopCp = self::addCp($arr, $currentShop);
  56. }
  57. if ($ptCp->auth == 0) {
  58. return true;
  59. }
  60. $cpId = $shopCp->id ?? 0;
  61. $ptCpItemList = PtCpItemClass::getAllByCondition(['cpId' => $ptCpId], null, '*', null, true);
  62. foreach ($ptCpItemList as $ptCpItem) {
  63. if ($ptCpItem->auth == 1) {
  64. $ptItemId = $ptCpItem->itemId ?? 0;
  65. $productInfo = ProductClass::getByCondition(['mainId' => $mainId, 'itemId' => $ptItemId], true);
  66. if (empty($productInfo)) {
  67. $ptItemInfo = PtItemClass::getById($ptItemId);
  68. if (empty($ptItemInfo)) {
  69. util::fail('平台花材没有找到哦');
  70. }
  71. $customDefaultClass = ItemClassClass::getByCondition(['mainId' => $mainId, 'isDefault' => 1], true);
  72. if (empty($customDefaultClass)) {
  73. noticeUtil::push("mainId:{$mainId} 导入平台产品和花材,没有找到花材默认分类", '15280215347');
  74. util::fail('没有找到默认分类哈!');
  75. }
  76. $customDefaultClassId = $customDefaultClass->id ?? 0;
  77. unset($ptItemInfo['id']);
  78. unset($ptItemInfo['addTime']);
  79. unset($ptItemInfo['updateTime']);
  80. unset($ptItemInfo['classId']);
  81. $ptItemInfo['itemId'] = $ptItemId;
  82. $ptItemInfo['hasCpId'] = 1;
  83. $ptItemInfo['skPrice'] = 36;
  84. $ptItemInfo['price'] = 36;
  85. $ptItemInfo['hjPrice'] = 36;
  86. $ptItemInfo['mainId'] = $mainId;
  87. $ptItemInfo['py'] = stringUtil::py($ptItemInfo['name']);
  88. $ptItemInfo['classId'] = $customDefaultClassId;
  89. //有二个添加花材的地方,请搜索关键词add_item_place
  90. $productInfo = ProductClass::add($ptItemInfo, true);
  91. }
  92. $shopItemId = $productInfo->id ?? 0;
  93. $currentData = [
  94. 'cpId' => $cpId,
  95. 'ptCpId' => $ptCpId,
  96. 'productId' => $shopItemId,
  97. 'ptItemId' => $ptItemId,
  98. 'name' => $productInfo->name ?? '',
  99. 'py' => $productInfo->py ?? '',
  100. 'cpName' => $shopCp->name ?? '',
  101. 'cpPy' => $shopCp->py ?? '',
  102. 'mainId' => $mainId,
  103. 'inTurn' => $ptCpItem->inTurn ?? 100,
  104. 'auth' => $ptCpItem->auth ?? 0,
  105. ];
  106. CpItemClass::add($currentData, true);
  107. }
  108. }
  109. return $shopCp;
  110. }
  111. //恢复产品 ssh 20240222
  112. public static function recoverCp($cp, $masterShop)
  113. {
  114. $ptCpId = $cp->ptCpId ?? 0;
  115. $cp->delStatus = 0;
  116. $cp->save();
  117. if (isset($masterShop->default) && $masterShop->default == 1) {
  118. $sjId = $masterShop->sjId ?? 0;
  119. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  120. if (!empty($shopList)) {
  121. foreach ($shopList as $shop) {
  122. if ($shop->id == $masterShop->id) {
  123. continue;
  124. }
  125. $chainMainId = $shop->mainId ?? 0;
  126. $has = self::getByCondition(['mainId' => $chainMainId, 'ptCpId' => $ptCpId, 'delStatus' => 0], true);
  127. if (!empty($has)) {
  128. $has->delStatus = 0;
  129. $has->save();
  130. }
  131. }
  132. }
  133. }
  134. }
  135. //删除产品 ssh 20240221
  136. public static function delCp($cp, $masterShop)
  137. {
  138. $ptCpId = $cp->ptCpId ?? 0;
  139. $cp->delStatus = 1;
  140. $cp->save();
  141. if (isset($masterShop->default) && $masterShop->default == 1) {
  142. $sjId = $masterShop->sjId ?? 0;
  143. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  144. if (!empty($shopList)) {
  145. foreach ($shopList as $shop) {
  146. if ($shop->id == $masterShop->id) {
  147. continue;
  148. }
  149. $shopName = $shop->shopName ?? '';
  150. $chainMainId = $shop->mainId ?? 0;
  151. $has = self::getByCondition(['mainId' => $chainMainId, 'ptCpId' => $ptCpId, 'delStatus' => 0], true);
  152. if (empty($has)) {
  153. util::fail($shopName . '的产品没有找到');
  154. }
  155. $has->delStatus = 1;
  156. $has->save();
  157. }
  158. }
  159. }
  160. }
  161. public static function copyCp($oldMainId, $newMainId)
  162. {
  163. $oldCpClassList = CpClassClass::getAllByCondition(['mainId' => $oldMainId], null, '*');
  164. if (empty($oldCpClassList)) {
  165. return false;
  166. }
  167. foreach ($oldCpClassList as $oldCpClass) {
  168. $oldCpClassId = $oldCpClass['id'] ?? 0;
  169. unset($oldCpClass['addTime']);
  170. unset($oldCpClass['updateTime']);
  171. unset($oldCpClass['id']);
  172. $oldCpClass['mainId'] = $newMainId;
  173. $newCpClass = CpClassClass::add($oldCpClass, true);
  174. $newCpClassId = $newCpClass->id ?? 0;
  175. $oldCpList = self::getAllByCondition(['classId' => $oldCpClassId], null, '*');
  176. if (!empty($oldCpList)) {
  177. foreach ($oldCpList as $oldCp) {
  178. $oldCpId = $oldCp['id'] ?? 0;
  179. unset($oldCp['addTime']);
  180. unset($oldCp['updateTime']);
  181. unset($oldCp['id']);
  182. $oldCp['mainId'] = $newMainId;
  183. $oldCp['status'] = 2;
  184. $oldCp['classId'] = $newCpClassId;
  185. $newCp = self::add($oldCp, true);
  186. $newCpId = $newCp->id ?? 0;
  187. $oldCpItemList = CpItemClass::getAllByCondition(['cpId' => $oldCpId], null, '*', 'ptItemId');
  188. if (!empty($oldCpItemList)) {
  189. $ptItemIds = array_keys($oldCpItemList);
  190. $newProductList = ProductClass::getAllByCondition(['mainId' => $newMainId, 'itemId' => ['in', $ptItemIds]], null, '*', 'itemId');
  191. foreach ($oldCpItemList as $ptItemId => $oldCpItem) {
  192. if (isset($newProductList[$ptItemId])) {
  193. $currentProduct = $newProductList[$ptItemId];
  194. $currentProductId = $currentProduct['id'] ?? 0;
  195. unset($oldCpItem['id']);
  196. unset($oldCpItem['addTime']);
  197. unset($oldCpItem['updateTime']);
  198. $oldCpItem['productId'] = $currentProductId;
  199. $oldCpItem['cpId'] = $newCpId;
  200. $oldCpItem['mainId'] = $newMainId;
  201. CpItemClass::add($oldCpItem, true);
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }
  209. public static function getCpList($where)
  210. {
  211. $data = self::getList('*', $where, 'inTurn DESC,addTime DESC');
  212. if (!empty($data['list'])) {
  213. foreach ($data['list'] as $key => $val) {
  214. $shortCover = $val['cover'] ?? '';
  215. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  216. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  217. $data['list'][$key]['shortCover'] = $shortCover;
  218. $data['list'][$key]['cover'] = $cover;
  219. $data['list'][$key]['bigCover'] = $bigCover;
  220. }
  221. }
  222. return $data;
  223. }
  224. //更新产品 ssh 20240221
  225. public static function updateCp($data, $cp, $masterShop)
  226. {
  227. $id = $cp->id ?? 0;
  228. $ptCpId = $cp->ptCpId ?? 0;
  229. $classId = $data['classId'] ?? 0;
  230. $cpClass = CpClassClass::getById($classId, true);
  231. if (empty($cpClass)) {
  232. util::fail('没有找到分类');
  233. }
  234. if ($cpClass->mainId != $cp->mainId) {
  235. util::fail('不是你的产品哦');
  236. }
  237. $cpClassName = $cpClass->name ?? '';
  238. self::updateById($id, $data);
  239. if (isset($masterShop->default) && $masterShop->default == 1) {
  240. $sjId = $masterShop->sjId ?? 0;
  241. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  242. if (!empty($shopList)) {
  243. foreach ($shopList as $shop) {
  244. if ($shop->id == $masterShop->id) {
  245. continue;
  246. }
  247. $shopName = $shop->shopName ?? '';
  248. $mainId = $shop->mainId ?? 0;
  249. $chainCp = self::getByCondition(['mainId' => $mainId, 'ptCpId' => $ptCpId], true);
  250. if (empty($chainCp)) {
  251. util::fail($shopName . ' 的产品没有找到');
  252. }
  253. $chainCpId = $chainCp->id ?? 0;
  254. $cpClassList = CpClassClass::getAllByCondition(['mainId' => $mainId, 'delStatus' => 0], null, '*', null, true);
  255. $cpClassId = 0;
  256. if (!empty($cpClassList)) {
  257. foreach ($cpClassList as $cpClass) {
  258. $currentCpClassId = $cpClass->id ?? 0;
  259. if ($cpClass->name == $cpClassName) {
  260. $cpClassId = $currentCpClassId;
  261. }
  262. }
  263. }
  264. if (empty($cpClassId)) {
  265. util::fail($shopName . '的分类 ' . $cpClassName . ' 没有找到');
  266. }
  267. $chainData = $data;
  268. $chainData['classId'] = $cpClassId;
  269. self::updateById($chainCpId, $chainData);
  270. }
  271. }
  272. }
  273. }
  274. //添加产品 ssh 20240221
  275. public static function addCp($data, $currentShop)
  276. {
  277. $mainId = $currentShop->mainId ?? 0;
  278. $name = $data['name'] ?? '';
  279. if (empty($name)) {
  280. util::fail('请填写名称');
  281. }
  282. $classId = $data['classId'] ?? 0;
  283. if (empty($classId)) {
  284. util::fail('请选择分类');
  285. }
  286. $shopCpClass = CpClassClass::getById($classId, true);
  287. if (empty($shopCpClass)) {
  288. util::fail('没有找到分类');
  289. }
  290. if ($shopCpClass->mainId != $mainId) {
  291. util::fail('不是你的分类');
  292. }
  293. $shopCpClassName = $shopCpClass->name ?? '';
  294. $connection = Yii::$app->db;
  295. $transaction = $connection->beginTransaction();
  296. try {
  297. if (isset($data['ptCpId']) == false || empty($data['ptCpId'])) {
  298. $ptData = $data;
  299. $ptCpClassId = 0;
  300. $ptCpClassDefaultId = 0;
  301. $ptCpClassList = PtCpClassClass::getAllByCondition(['delStatus' => 0], null, '*');
  302. if (!empty($ptCpClassList)) {
  303. foreach ($ptCpClassList as $ptCpClass) {
  304. $currentId = $ptCpClass['id'] ?? 0;
  305. if (isset($ptCpClass['name']) && $ptCpClass['name'] == $shopCpClassName) {
  306. $ptCpClassId = $currentId;
  307. }
  308. if (isset($ptCpClass['isDefault']) && $ptCpClass['isDefault'] == 1) {
  309. $ptCpClassDefaultId = $currentId;
  310. }
  311. }
  312. }
  313. if (!empty($ptCpClassDefaultId)) {
  314. $ptData['classId'] = $ptCpClassDefaultId;
  315. }
  316. if (!empty($ptCpClassId)) {
  317. $ptData['classId'] = $ptCpClassId;
  318. }
  319. if (empty($ptCpClassDefaultId) && empty($ptCpClassId)) {
  320. util::fail('没有找到平台的分类');
  321. }
  322. $respond = PtCpClass::addCp($ptData);
  323. $ptCpId = $respond->id ?? 0;
  324. $data['ptCpId'] = $ptCpId;
  325. }
  326. $result = self::add($data, true);
  327. //在首店添加,则找到父级sj下的所有直营和加盟店,都进行同步
  328. if (isset($currentShop->default) && $currentShop->default == 1) {
  329. $shopList = ShopClass::getAllByCondition(['sjId' => $currentShop->sjId], null, '*', null, true);
  330. foreach ($shopList as $shop) {
  331. $shopId = $shop->id ?? 0;
  332. if ($shopId == $currentShop->id) {
  333. //前面已经添加过了
  334. continue;
  335. }
  336. $currentMainId = $shop->mainId ?? 0;
  337. $shopName = $shop->shopName ?? '';
  338. $chainClassList = CpClassClass::getAllByCondition(['mainId' => $currentMainId, 'delStatus' => 0], null, '*', null, true);
  339. $currentCpClassId = 0;
  340. if (!empty($chainClassList)) {
  341. foreach ($chainClassList as $chainClass) {
  342. $chainClassId = $chainClass->id ?? 0;
  343. if ($chainClass->name == $shopCpClassName) {
  344. $currentCpClassId = $chainClassId;
  345. }
  346. }
  347. }
  348. if (empty($currentCpClassId)) {
  349. util::fail($shopName . '没有找到相应的分类');
  350. }
  351. $currentData = $data;
  352. $currentData['shopId'] = $shopId;
  353. $currentData['mainId'] = $currentMainId;
  354. $currentData['classId'] = $currentCpClassId;
  355. self::add($currentData);
  356. }
  357. }
  358. $transaction->commit();
  359. return $result;
  360. } catch (\Exception $exception) {
  361. $transaction->rollBack();
  362. Yii::info('添加失败原因:' . $exception->getMessage());
  363. util::fail('添加失败');
  364. }
  365. }
  366. }