CustomController.php 16 KB

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