CustomController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. namespace console\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\shop\classes\ShopClass;
  5. use biz\shop\services\ShopAdminService;
  6. use biz\sj\classes\SjClass;
  7. use bizGhs\admin\classes\AdminClass;
  8. use bizGhs\custom\classes\CustomClass;
  9. use bizGhs\custom\models\Custom;
  10. use bizGhs\order\classes\OrderClass;
  11. use bizHd\purchase\classes\PurchaseClass;
  12. use bizHd\saas\classes\ApplyClass;
  13. use bizHd\saas\services\ApplyService;
  14. use common\components\dict;
  15. use common\components\noticeUtil;
  16. use common\components\stringUtil;
  17. use common\components\util;
  18. use yii\console\Controller;
  19. use Yii;
  20. class CustomController extends Controller
  21. {
  22. //欠款信息不一致跟踪 ssh 20220307 ./yii custom/adjust-debt
  23. public function actionAdjustDebt()
  24. {
  25. //感觉好像没有用了,可以停了 ssh 20250724
  26. util::stop();
  27. ini_set('memory_limit', '2045M');
  28. set_time_limit(0);
  29. $shopList = ShopClass::getAllByCondition(['ptStyle' => 2], null, '*', null, true);
  30. if (empty($shopList)) {
  31. return false;
  32. }
  33. foreach ($shopList as $key => $shop) {
  34. $shopId = $shop->id ?? 0;
  35. $query = new \yii\db\Query();
  36. $query->from(Custom::tableName());
  37. $query->where(['ownShopId' => $shopId]);
  38. $query->orderBy('addTime ASC');
  39. foreach ($query->batch(50) as $customList) {
  40. if (!empty($customList)) {
  41. foreach ($customList as $custom) {
  42. //$customName = $custom['name'] ?? '';
  43. $customId = $custom['id'] ?? 0;
  44. $where = ['customId' => $customId, 'debt' => 1];
  45. $orderList = OrderClass::getAllByCondition($where, 'addTime DESC', '*');
  46. $ghsOrderDebtAmount = 0;
  47. if (!empty($orderList)) {
  48. foreach ($orderList as $order) {
  49. $actPrice = $order['remainDebtPrice'] ?? 0;
  50. $ghsOrderDebtAmount = bcadd($ghsOrderDebtAmount, $actPrice, 2);
  51. }
  52. }
  53. $ghsId = $custom['ghsId'] ?? 0;
  54. $ghs = \bizHd\ghs\classes\GhsClass::getById($ghsId);
  55. $where = ['ghsId' => $ghsId, 'debt' => PurchaseClass::DEBT_YES];
  56. $cgList = PurchaseClass::getAllByCondition($where, 'addTime DESC', '*');
  57. $hdCgOrderDebtAmount = 0;
  58. if (!empty($cgList)) {
  59. foreach ($cgList as $cg) {
  60. $actPrice = $cg['remainDebtPrice'] ?? 0;
  61. $hdCgOrderDebtAmount = bcadd($hdCgOrderDebtAmount, $actPrice, 2);
  62. }
  63. }
  64. if ($ghsOrderDebtAmount < 0 || $hdCgOrderDebtAmount < 0) {
  65. noticeUtil::push("!!!!!!!供货商 {$ghsId} 客户id {$customId} 出现负数 $ghsOrderDebtAmount $hdCgOrderDebtAmount", '15280215347');
  66. echo "@@@@@@@@@@@@@供货商 {$ghsId} 客户id {$customId} 出现负数 $ghsOrderDebtAmount $hdCgOrderDebtAmount \n";
  67. }
  68. $ghsOrderDebtAmount = floatval($ghsOrderDebtAmount);
  69. $hdCgOrderDebtAmount = floatval($hdCgOrderDebtAmount);
  70. if ($ghsOrderDebtAmount != $hdCgOrderDebtAmount) {
  71. noticeUtil::push("!!!!!!!供货商 {$ghsId} 客户id {$customId} 二边欠款单总金额不一致", '15280215347');
  72. echo "---------供货商 {$ghsId} 客户id {$customId} 二边 欠款单 总金额 不一致 {$ghsOrderDebtAmount} {$hdCgOrderDebtAmount} \n";
  73. continue;
  74. }
  75. $customSaveDebtAmount = $custom['debtAmount'] ? floatval($custom['debtAmount']) : 0;
  76. $ghsSaveDebtAmount = $ghs['debtAmount'] ? floatval($ghs['debtAmount']) : 0;
  77. if ($customSaveDebtAmount != $ghsOrderDebtAmount) {
  78. echo "*********客户 {$customId} 欠款 和 订单总额 不一致 $customSaveDebtAmount $ghsOrderDebtAmount \n";
  79. noticeUtil::push("*********客户 {$customId} 欠款总金额 和 订单总合 不一致 $customSaveDebtAmount $ghsOrderDebtAmount ", '15280215347');
  80. continue;
  81. }
  82. if ($ghsSaveDebtAmount != $hdCgOrderDebtAmount) {
  83. echo "#########欠供货商 {$ghsId} 和 订单总额 不一致 $ghsSaveDebtAmount $hdCgOrderDebtAmount \n";
  84. noticeUtil::push("#########欠供货商 {$ghsId} 金额 和 订单总合 不一致 $ghsSaveDebtAmount $hdCgOrderDebtAmount ", '15280215347');
  85. continue;
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. public function actionBatchCommand()
  93. {
  94. //批量创建客户
  95. $this->createCustomer();
  96. //从首店同步客户
  97. $this->copyCustomers();
  98. }
  99. //批量创建客户
  100. private function createCustomer()
  101. {
  102. $customerCachedKey = 'batch_create_customer_list';
  103. $customList = [];
  104. while ($count = Yii::$app->redis->executeCommand('LLEN', [$customerCachedKey])) {
  105. $customer = Yii::$app->redis->executeCommand('RPOP', [$customerCachedKey]);
  106. $customList[] = $customer;
  107. }
  108. if (empty($customList)) {
  109. return false;
  110. }
  111. foreach ($customList as $customer) {
  112. $connection = Yii::$app->db;
  113. $transaction = $connection->beginTransaction();
  114. $customerInfo = explode('_', $customer);
  115. $shopName = $customerInfo[0];
  116. $mobile = $customerInfo[1];
  117. try {
  118. // 组织表单数据
  119. $post = [];
  120. $post['introSjId'] = $customerInfo[2];
  121. $post['introShopId'] = $customerInfo[3];
  122. $post['introSjName'] = $customerInfo[4];
  123. $post['introStyle'] = SjClass::STYLE_SUPPLIER;
  124. $post['name'] = $shopName;
  125. if (empty($shopName)) {
  126. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称没有填写", '15280215347');
  127. $transaction->rollBack();
  128. continue;
  129. }
  130. $post['mobile'] = $mobile;
  131. if (stringUtil::isMobile($mobile) == false) {
  132. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号填写错误", '15280215347');
  133. $transaction->rollBack();
  134. continue;
  135. }
  136. if (stringUtil::getWordNum($shopName) > 15) {
  137. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称超过了15个汉字", '15280215347');
  138. $transaction->rollBack();
  139. continue;
  140. }
  141. $has = ApplyClass::getByCondition(['mobile' => $mobile]);
  142. if (!empty($has)) {
  143. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号已经存在了哦", '15280215347');
  144. $transaction->rollBack();
  145. continue;
  146. }
  147. $hasShop = ShopClass::getByCondition(['mobile' => $mobile]);
  148. if (!empty($hasShop)) {
  149. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号已经存在了", '15280215347');
  150. $transaction->rollBack();
  151. continue;
  152. }
  153. $adminName = $post['adminName'] ?? '';
  154. $miniOpenId = $post['miniOpenId'] ?? '';
  155. $adminInfo = ['name' => $adminName, 'mobile' => $mobile, 'miniOpenId' => $miniOpenId];
  156. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  157. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  158. $adminId = $admin['id'] ?? 0;
  159. $post['adminId'] = $adminId;
  160. $post['adminName'] = $admin['name'] ?? '';
  161. $post['long'] = isset($post['longitude']) ? $post['longitude'] : '';
  162. $post['lat'] = isset($post['latitude']) ? $post['latitude'] : '';
  163. $post['from'] = ApplyClass::FROM_RETAIL;
  164. $post['style'] = SjClass::STYLE_RETAIL;
  165. //增加或更新申请
  166. $respond = ApplyService::replaceRetail($post);
  167. $id = $respond['id'] ?? 0;
  168. //后继代码,要用到此参数
  169. Yii::$app->params['ptStyle'] = 2;
  170. ApplyService::pass($id, '批发商手动添加');
  171. $transaction->commit();
  172. //只要手动添加的都标记为花店,任何情况都不会标记为散客,任何情况都是审核通过的状态
  173. $customList = CustomClass::getAllByCondition(['mobile' => $mobile], null, '*', null, true);
  174. if (!empty($customList)) {
  175. foreach ($customList as $custom) {
  176. $custom->isHd = 1;
  177. $custom->passStatus = 1;
  178. $custom->save();
  179. $ghsId = $custom->ghsId;
  180. GhsClass::updateById($ghsId, ['passStatus' => 1]);
  181. }
  182. }
  183. //noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 由批发商添加成功");
  184. } catch (\Exception $exception) {
  185. $transaction->rollBack();
  186. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,原因:" . $exception->getMessage(), '15280215347');
  187. }
  188. }
  189. }
  190. //批发端分店从首店同步客户
  191. private function copyCustomers()
  192. {
  193. $cachedKey = 'copy_other_shop_customers';
  194. $shopIdList = [];
  195. while ($count = Yii::$app->redis->executeCommand('LLEN', [$cachedKey])) {
  196. $shopIdData = Yii::$app->redis->executeCommand('RPOP', [$cachedKey]);
  197. $shopIdList[] = $shopIdData;
  198. }
  199. if (empty($shopIdList)) {
  200. return;
  201. }
  202. foreach ($shopIdList as $shopIdData) {
  203. $shopIdArr = explode('_', $shopIdData);
  204. if (count($shopIdArr) != 2) {
  205. Yii::info('copy customer 数据出错:' . $shopIdData);
  206. noticeUtil::push("从分店同步客户出错了:" . $shopIdData, '15280215347');
  207. continue;
  208. }
  209. //指定当前环境为批发商
  210. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  211. $toShopId = $shopIdArr[0];
  212. $fromShopId = $shopIdArr[1];
  213. $fromCustomList = CustomClass::getAllByCondition(['ownShopId' => $fromShopId], null, '*');
  214. foreach ($fromCustomList as $fromCustomer) {
  215. $exist = CustomClass::exists(['ownShopId' => $toShopId, 'shopId' => $fromCustomer['shopId']]);
  216. if (!$exist) {
  217. CustomClass::build($toShopId, $fromCustomer['shopId'], $fromCustomer['name']);
  218. }
  219. }
  220. }
  221. }
  222. //从文件批量创建客户 ssh 20240426
  223. public function actionFileCreateCustom()
  224. {
  225. // $customerCachedKey = 'file_batch_create_customer_list';
  226. // $fileCustomList = [];
  227. // while ($count = Yii::$app->redis->executeCommand('LLEN', [$customerCachedKey])) {
  228. // $customer = Yii::$app->redis->executeCommand('RPOP', [$customerCachedKey]);
  229. // $fileCustomList[] = $customer;
  230. // }
  231. //
  232. // if (empty($fileCustomList)) {
  233. // noticeUtil::push("没有找到手机号", '15280215347');
  234. // return false;
  235. // }
  236. $fileCustomList = ['23580,/usr/local/nginx/html/huahuibao/vendor/../../resource/images/uploads/35869/23580/202404/27/f4ed6c0aa209b4d0d56dd6543b7c4e25.txt'];
  237. foreach ($fileCustomList as $fileCustom) {
  238. $customerInfo = explode(',', $fileCustom);
  239. $masterShopId = $customerInfo[0];
  240. $masterShop = ShopClass::getById($masterShopId, true);
  241. $masterSjId = $masterShop->sjId ?? 0;
  242. $masterSjName = $masterShop->merchantName ?? '';
  243. $filePath = $customerInfo[1];
  244. $content = file_get_contents($filePath);
  245. $content = trim($content);
  246. $arr = explode('
  247. ', $content);
  248. foreach ($arr as $key => $one) {
  249. $single = explode(',', $one);
  250. $shopName = trim($single[0]);
  251. $mobile = trim($single[1]);
  252. echo bcadd($key, 1) . ' ' . $shopName . ' ' . $mobile . "\n";
  253. $connection = Yii::$app->db;
  254. $transaction = $connection->beginTransaction();
  255. try {
  256. // 组织表单数据
  257. $post = [];
  258. $post['introSjId'] = $masterSjId;
  259. $post['introShopId'] = $masterShopId;
  260. $post['introSjName'] = $masterSjName;
  261. $post['introStyle'] = SjClass::STYLE_SUPPLIER;
  262. $post['name'] = $shopName;
  263. if (empty($shopName)) {
  264. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称没有填写", '15280215347');
  265. $transaction->rollBack();
  266. continue;
  267. }
  268. $post['mobile'] = $mobile;
  269. if (stringUtil::isMobile($mobile) == false) {
  270. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号填写错误", '15280215347');
  271. $transaction->rollBack();
  272. continue;
  273. }
  274. if (stringUtil::getWordNum($shopName) > 15) {
  275. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称超过了15个汉字", '15280215347');
  276. $transaction->rollBack();
  277. continue;
  278. }
  279. $hasShop = ShopClass::getByCondition(['mobile' => $mobile]);
  280. if (!empty($hasShop)) {
  281. //noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号已经存在了", '15280215347');
  282. $transaction->rollBack();
  283. continue;
  284. }
  285. $has = ApplyClass::getByCondition(['mobile' => $mobile]);
  286. if (!empty($has)) {
  287. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,在申请列表,手机号已经存在了哦", '15280215347');
  288. $transaction->rollBack();
  289. continue;
  290. }
  291. $adminName = $post['adminName'] ?? '';
  292. $miniOpenId = $post['miniOpenId'] ?? '';
  293. $adminInfo = ['name' => $adminName, 'mobile' => $mobile, 'miniOpenId' => $miniOpenId];
  294. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  295. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  296. $adminId = $admin['id'] ?? 0;
  297. $post['adminId'] = $adminId;
  298. $post['adminName'] = $admin['name'] ?? '';
  299. $post['long'] = isset($post['longitude']) ? $post['longitude'] : '';
  300. $post['lat'] = isset($post['latitude']) ? $post['latitude'] : '';
  301. $post['from'] = ApplyClass::FROM_RETAIL;
  302. $post['style'] = SjClass::STYLE_RETAIL;
  303. //增加或更新申请
  304. $respond = ApplyService::replaceRetail($post);
  305. $id = $respond['id'] ?? 0;
  306. //后继代码,要用到此参数
  307. Yii::$app->params['ptStyle'] = 2;
  308. ApplyService::pass($id, '批发商手动添加');
  309. $transaction->commit();
  310. //只要手动添加的都标记为花店,任何情况都不会标记为散客
  311. CustomClass::updateByCondition(['mobile' => $mobile], ['isHd' => 1]);
  312. //noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 由批发商添加成功");
  313. } catch (\Exception $exception) {
  314. $transaction->rollBack();
  315. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,原因:" . $exception->getMessage(), '15280215347');
  316. }
  317. }
  318. }
  319. }
  320. }