ProductController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace console\controllers;
  3. use biz\item\classes\PtItemClass;
  4. use biz\product\classes\ProductClass;
  5. use biz\product\models\Product;
  6. use biz\shop\classes\ShopClass;
  7. use bizGhs\item\classes\ItemClass;
  8. use bizGhs\item\models\Item;
  9. use common\components\stringUtil;
  10. use common\components\util;
  11. use yii\console\Controller;
  12. use Yii;
  13. class ProductController extends Controller
  14. {
  15. //复制花材 ssh 20240422
  16. public function actionClone()
  17. {
  18. ini_set('memory_limit', '5045M');
  19. set_time_limit(0);
  20. if (getenv('YII_ENV') == 'production') {
  21. $shopId = 23580;
  22. } else {
  23. $shopId = 36523;
  24. }
  25. $shop = ShopClass::getById($shopId, true);
  26. if (empty($shop)) {
  27. util::stop();
  28. }
  29. $mainId = $shop->mainId ?? 0;
  30. $productList = ProductClass::getAllByCondition(['mainId' => $mainId, 'classId' => ['in', [64848, 64849, 64850, 64851, 64852, 64853, 64854, 64855, 64856, 64857, 64858, 64859, 64860, 64861, 64862, 64863, 64865, 64866]]], null, '*');
  31. if (!empty($productList)) {
  32. foreach ($productList as $post) {
  33. $post['copy'] = 1;
  34. $post['auth'] = 0;
  35. $post['skPrice'] = $post['price'];
  36. $post['hjPrice'] = $post['price'];
  37. $post['sjId'] = $shop->sjId ?? 0;
  38. unset($post['viewNum']);
  39. unset($post['actualSold']);
  40. unset($post['itemId']);
  41. unset($post['id']);
  42. unset($post['delStatus']);
  43. $a = $post;
  44. $a['name'] = $a['name'] . ' A级';
  45. $a['py'] = stringUtil::py($a['name']);
  46. \bizGhs\product\classes\ProductClass::addProduct($a, $shop);
  47. $b = $post;
  48. $b['name'] = $b['name'] . ' B级';
  49. $b['py'] = stringUtil::py($b['name']);
  50. \bizGhs\product\classes\ProductClass::addProduct($b, $shop);
  51. $ab = $post;
  52. $ab['name'] = $ab['name'] . ' AB级';
  53. $ab['py'] = stringUtil::py($ab['name']);
  54. \bizGhs\product\classes\ProductClass::addProduct($ab, $shop);
  55. // $c = $post;
  56. // $c['name'] = $c['name'] . ' C级';
  57. // $c['py'] = stringUtil::py($c['name']);
  58. // \bizGhs\product\classes\ProductClass::addProduct($c, $shop);
  59. //
  60. // $d = $post;
  61. // $d['name'] = $d['name'] . ' D级';
  62. // $d['py'] = stringUtil::py($d['name']);
  63. // \bizGhs\product\classes\ProductClass::addProduct($d, $shop);
  64. // $e = $post;
  65. // $e['name'] = $e['name'] . ' E级';
  66. // $e['py'] = stringUtil::py($e['name']);
  67. // \bizGhs\product\classes\ProductClass::addProduct($e, $shop);
  68. }
  69. }
  70. }
  71. public function actionChangeSk()
  72. {
  73. ini_set('memory_limit', '2045M');
  74. set_time_limit(0);
  75. //$id = 36523;
  76. $id = 520;
  77. $shop = ShopClass::getById($id, true);
  78. $mainId = $shop->mainId ?? 0;
  79. $sjId = $shop->sjId ?? 0;
  80. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  81. $default = $shop->default ?? 0;
  82. $productList = ProductClass::getAllByCondition(['mainId' => $mainId], null, '*', 'id', true);
  83. if (!empty($productList)) {
  84. foreach ($productList as $product) {
  85. $price = $product->price ?? 0;
  86. $skPrice = bcadd(floatval($price), 3, 0);
  87. $product->skPrice = $skPrice;
  88. $product->save();
  89. $ptItemId = $product->itemId ?? 0;
  90. if ($default == 1) {
  91. foreach ($chainShopList as $chain) {
  92. if ($chain->id == $shop->id) {
  93. continue;
  94. }
  95. $chainMainId = $chain->mainId ?? 0;
  96. $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  97. if (!empty($chainProduct)) {
  98. $chainProduct->skPrice = $skPrice;
  99. $chainProduct->save();
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. //修改散客比批发价格贵 ssh 20220609
  107. public function actionSkMore()
  108. {
  109. $query = new \yii\db\Query();
  110. $query->from(Item::tableName());
  111. $query->where(['>', 'id', 0]);
  112. foreach ($query->batch() as $batchItem) {
  113. foreach ($batchItem as $item) {
  114. $addPrice = $item['addPrice'] ?? 0;
  115. $id = $item['id'] ?? 0;
  116. $skAddPrice = $item['skAddPrice'] ?? 0;
  117. if ($skAddPrice > $addPrice) {
  118. $add = bcsub($skAddPrice, $addPrice, 2);
  119. ItemClass::updateById($id, ['skMore' => $add]);
  120. } else {
  121. echo "零售加价小于批发加价 {$id}\n";
  122. }
  123. }
  124. }
  125. }
  126. //更新纯彩花艺的价格 ./yii product/update-price ssh 20211020
  127. public function actionUpdatePrice()
  128. {
  129. $list = ProductClass::getAllByCondition(['shopId' => 10], null, '*', null, true);
  130. if (!empty($list)) {
  131. foreach ($list as $key => $item) {
  132. $price = $item->price ?? 0;
  133. $addPrice = $item->addPrice ?? 0;
  134. $skAddPrice = $item->skAddPrice ?? 0;
  135. $hjAddPrice = $item->hjAddPrice ?? 0;
  136. $zsAddPrice = $item->zsAddPrice ?? 0;
  137. $skDiff = bcsub($addPrice, $skAddPrice, 2);
  138. $skPrice = bcsub($price, $skDiff, 2);
  139. $hjDiff = bcsub($addPrice, $hjAddPrice, 2);
  140. $hkPrice = bcsub($price, $hjDiff, 2);
  141. $zsDiff = bcsub($addPrice, $zsAddPrice, 2);
  142. $zsPrice = bcsub($price, $zsDiff, 2);
  143. $item->skPrice = $skPrice;
  144. $item->hjPrice = $hkPrice;
  145. $item->zsPrice = $zsPrice;
  146. $item->save();
  147. }
  148. }
  149. }
  150. // ./yii product/reset-cover
  151. public function actionResetCover()
  152. {
  153. $sql = "SELECT * FROM `" . Product::tableName() . "` WHERE cover not like '%.%'";
  154. $command = Yii::$app->db->createCommand($sql);
  155. $data = $command->queryAll();
  156. if (empty($data)) {
  157. return false;
  158. }
  159. foreach ($data as $item) {
  160. $id = $item['id'] ?? 0;
  161. $ptItemId = $item['itemId'] ?? 0;
  162. $preCover = $item['cover'] ?? '';
  163. $ptItem = PtItemClass::getById($ptItemId, true);
  164. $cover = $ptItem->cover ?? '';
  165. if (!empty($cover)) {
  166. ProductClass::updateById($id, ['cover' => $cover]);
  167. echo "productId:" . $id . "原图:{$preCover}, 恢复图片:" . $cover . "\n";
  168. } else {
  169. echo "productId:" . $id . " 恢复图片没有成功-----------------\n";
  170. }
  171. }
  172. }
  173. // ./yii product/sk-price
  174. public function actionSkPrice()
  175. {
  176. ini_set('memory_limit', '2045M');
  177. set_time_limit(0);
  178. $list = ProductClass::getAllByCondition([], null, '*', null, true);
  179. if (empty($list)) {
  180. return false;
  181. }
  182. foreach ($list as $key => $item) {
  183. $price = $item->price ?? 0;
  184. $skAddPrice = $item->skAddPrice ?? 0;
  185. $addPrice = $item->addPrice ?? 0;
  186. $diff = bcsub($skAddPrice, $addPrice, 2);
  187. $currentPrice = $price;
  188. if ($diff > 0) {
  189. $currentPrice = bcadd($price, $diff, 2);
  190. }
  191. $item->skPrice = $currentPrice;
  192. $item->save();
  193. }
  194. }
  195. //二个空间问题
  196. public function actionSame()
  197. {
  198. ini_set('memory_limit', '5045M');
  199. set_time_limit(0);
  200. if (getenv('YII_ENV') == 'production') {
  201. $mainId = 8164;
  202. } else {
  203. $mainId = 644;
  204. }
  205. $list = ProductClass::getAllByCondition(['mainId' => $mainId], null, '*', null);
  206. if (!empty($list)) {
  207. $ids = array_column($list, 'itemId');
  208. $ptInfo = PtItemClass::getByIds($ids, null, 'id');
  209. foreach ($list as $item) {
  210. $id = $item['id'] ?? 0;
  211. $name = $item['name'] ?? '';
  212. $ptItemId = $item['itemId'] ?? 0;
  213. $pt = $ptInfo[$ptItemId];
  214. $ptName = $pt['name'] ?? '';
  215. if ($name != $ptName) {
  216. echo $id . ' ' . $name . ' ' . $ptName . "\n";
  217. }
  218. }
  219. }
  220. }
  221. //二个空间问题
  222. public function actionEmpty()
  223. {
  224. ini_set('memory_limit', '5045M');
  225. set_time_limit(0);
  226. $list = PtItemClass::getAllByCondition(['delStatus' => 0], null, '*', null, true);
  227. if (!empty($list)) {
  228. foreach ($list as $item) {
  229. $name = $item->name ?? '';
  230. $name = trim($name);
  231. $name = str_replace(" ", " ", $name);
  232. $name = str_replace(" ", " ", $name);
  233. $item->name = $name;
  234. $item->save();
  235. }
  236. }
  237. }
  238. //恢复花材初始名称 ssh 20250213
  239. public function actionRecover()
  240. {
  241. ini_set('memory_limit', '5045M');
  242. set_time_limit(0);
  243. if (getenv('YII_ENV') == 'production') {
  244. $mainId = 26374;
  245. } else {
  246. $mainId = 644;
  247. }
  248. $list = ProductClass::getAllByCondition(['mainId' => $mainId], null, '*', null);
  249. if (!empty($list)) {
  250. $ids = array_column($list, 'itemId');
  251. $ptInfo = PtItemClass::getByIds($ids, null, 'id');
  252. foreach ($list as $item) {
  253. $id = $item['id'] ?? 0;
  254. $name = $item['name'] ?? '';
  255. $ptItemId = $item['itemId'] ?? 0;
  256. $pt = $ptInfo[$ptItemId];
  257. $ptName = $pt['name'] ?? '';
  258. if (!empty($ptName)) {
  259. $ptPy = stringUtil::py($ptName);
  260. ProductClass::updateById($id, ['name' => $ptName, 'py' => $ptPy]);
  261. }
  262. }
  263. }
  264. }
  265. //预售到期自动取消
  266. public function actionCancelPresell()
  267. {
  268. try{
  269. $now = time(); //注意脚本执行时间要在凌晨0点之后,否则会出问题
  270. $redis = Yii::$app->redis;
  271. $cursor = '0';
  272. do {
  273. $scanResult = $redis->executeCommand('SCAN', [$cursor, 'MATCH', 'product_presell:*', 'COUNT', 500]);
  274. $cursor = $scanResult[0] ?? '0';
  275. $keys = $scanResult[1] ?? [];
  276. if (empty($keys)) {
  277. continue;
  278. }
  279. foreach ($keys as $key) {
  280. $firstDate = (int)$redis->get($key);
  281. if ($firstDate >= $now) {
  282. continue;
  283. }
  284. $id = (int)str_replace('product_presell:', '', $key);
  285. if ($id <= 0) {
  286. $redis->del($key);
  287. continue;
  288. }
  289. $item = ProductClass::getById($id, true);
  290. if (empty($item)) {
  291. $redis->del($key);
  292. continue;
  293. }
  294. if ((int)$item->presell !== 1) {
  295. $redis->del($key);
  296. continue;
  297. }
  298. $item->presell = 0;
  299. $item->presellDate = '';
  300. $item->save();
  301. $redis->del($key);
  302. }
  303. } while ($cursor !== '0');
  304. Yii::info('预售到期自动取消脚本成功执行');
  305. }catch(\Exception $e){
  306. Yii::error('预售到期自动取消脚本执行失败:' . $e->getMessage());
  307. }
  308. }
  309. }