CustomController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. $customList = CustomClass::getAllByCondition(['mobile' => $mobile], null, '*', null, true);
  172. if (!empty($customList)) {
  173. foreach ($customList as $custom) {
  174. $custom->isHd = 1;
  175. $custom->passStatus = 1;
  176. $custom->save();
  177. $ghsId = $custom->ghsId;
  178. GhsClass::updateById($ghsId, ['passStatus' => 1]);
  179. }
  180. }
  181. //noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 由批发商添加成功");
  182. } catch (\Exception $exception) {
  183. $transaction->rollBack();
  184. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,原因:" . $exception->getMessage(), '15280215347');
  185. }
  186. }
  187. }
  188. //批发端分店从首店同步客户
  189. private function copyCustomers()
  190. {
  191. $cachedKey = 'copy_other_shop_customers';
  192. $shopIdList = [];
  193. while ($count = Yii::$app->redis->executeCommand('LLEN', [$cachedKey])) {
  194. $shopIdData = Yii::$app->redis->executeCommand('RPOP', [$cachedKey]);
  195. $shopIdList[] = $shopIdData;
  196. }
  197. if (empty($shopIdList)) {
  198. return;
  199. }
  200. foreach ($shopIdList as $shopIdData) {
  201. $shopIdArr = explode('_', $shopIdData);
  202. if (count($shopIdArr) != 2) {
  203. Yii::info('copy customer 数据出错:' . $shopIdData);
  204. noticeUtil::push("从分店同步客户出错了:" . $shopIdData, '15280215347');
  205. continue;
  206. }
  207. //指定当前环境为批发商
  208. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  209. $toShopId = $shopIdArr[0];
  210. $fromShopId = $shopIdArr[1];
  211. $fromCustomList = CustomClass::getAllByCondition(['ownShopId' => $fromShopId], null, '*');
  212. foreach ($fromCustomList as $fromCustomer) {
  213. $exist = CustomClass::exists(['ownShopId' => $toShopId, 'shopId' => $fromCustomer['shopId']]);
  214. if (!$exist) {
  215. CustomClass::build($toShopId, $fromCustomer['shopId'], $fromCustomer['name']);
  216. }
  217. }
  218. }
  219. }
  220. //从文件批量创建客户 ssh 20240426
  221. public function actionFileCreateCustom()
  222. {
  223. // $customerCachedKey = 'file_batch_create_customer_list';
  224. // $fileCustomList = [];
  225. // while ($count = Yii::$app->redis->executeCommand('LLEN', [$customerCachedKey])) {
  226. // $customer = Yii::$app->redis->executeCommand('RPOP', [$customerCachedKey]);
  227. // $fileCustomList[] = $customer;
  228. // }
  229. //
  230. // if (empty($fileCustomList)) {
  231. // noticeUtil::push("没有找到手机号", '15280215347');
  232. // return false;
  233. // }
  234. $fileCustomList = ['23580,/usr/local/nginx/html/huahuibao/vendor/../../resource/images/uploads/35869/23580/202404/27/f4ed6c0aa209b4d0d56dd6543b7c4e25.txt'];
  235. foreach ($fileCustomList as $fileCustom) {
  236. $customerInfo = explode(',', $fileCustom);
  237. $masterShopId = $customerInfo[0];
  238. $masterShop = ShopClass::getById($masterShopId, true);
  239. $masterSjId = $masterShop->sjId ?? 0;
  240. $masterSjName = $masterShop->merchantName ?? '';
  241. $filePath = $customerInfo[1];
  242. $content = file_get_contents($filePath);
  243. $content = trim($content);
  244. $arr = explode('
  245. ', $content);
  246. foreach ($arr as $key => $one) {
  247. $single = explode(',', $one);
  248. $shopName = trim($single[0]);
  249. $mobile = trim($single[1]);
  250. echo bcadd($key, 1) . ' ' . $shopName . ' ' . $mobile . "\n";
  251. $connection = Yii::$app->db;
  252. $transaction = $connection->beginTransaction();
  253. try {
  254. // 组织表单数据
  255. $post = [];
  256. $post['introSjId'] = $masterSjId;
  257. $post['introShopId'] = $masterShopId;
  258. $post['introSjName'] = $masterSjName;
  259. $post['introStyle'] = SjClass::STYLE_SUPPLIER;
  260. $post['name'] = $shopName;
  261. if (empty($shopName)) {
  262. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称没有填写", '15280215347');
  263. $transaction->rollBack();
  264. continue;
  265. }
  266. $post['mobile'] = $mobile;
  267. if (stringUtil::isMobile($mobile) == false) {
  268. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号填写错误", '15280215347');
  269. $transaction->rollBack();
  270. continue;
  271. }
  272. if (stringUtil::getWordNum($shopName) > 15) {
  273. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称超过了15个汉字", '15280215347');
  274. $transaction->rollBack();
  275. continue;
  276. }
  277. $hasShop = ShopClass::getByCondition(['mobile' => $mobile]);
  278. if (!empty($hasShop)) {
  279. //noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号已经存在了", '15280215347');
  280. $transaction->rollBack();
  281. continue;
  282. }
  283. $has = ApplyClass::getByCondition(['mobile' => $mobile]);
  284. if (!empty($has)) {
  285. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,在申请列表,手机号已经存在了哦", '15280215347');
  286. $transaction->rollBack();
  287. continue;
  288. }
  289. $adminName = $post['adminName'] ?? '';
  290. $miniOpenId = $post['miniOpenId'] ?? '';
  291. $adminInfo = ['name' => $adminName, 'mobile' => $mobile, 'miniOpenId' => $miniOpenId];
  292. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  293. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  294. $adminId = $admin['id'] ?? 0;
  295. $post['adminId'] = $adminId;
  296. $post['adminName'] = $admin['name'] ?? '';
  297. $post['long'] = isset($post['longitude']) ? $post['longitude'] : '';
  298. $post['lat'] = isset($post['latitude']) ? $post['latitude'] : '';
  299. $post['from'] = ApplyClass::FROM_RETAIL;
  300. $post['style'] = SjClass::STYLE_RETAIL;
  301. //增加或更新申请
  302. $respond = ApplyService::replaceRetail($post);
  303. $id = $respond['id'] ?? 0;
  304. //后继代码,要用到此参数
  305. Yii::$app->params['ptStyle'] = 2;
  306. ApplyService::pass($id, '批发商手动添加');
  307. $transaction->commit();
  308. //只要手动添加的都标记为花店,任何情况都不会标记为散客
  309. CustomClass::updateByCondition(['mobile' => $mobile], ['isHd' => 1]);
  310. //noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 由批发商添加成功");
  311. } catch (\Exception $exception) {
  312. $transaction->rollBack();
  313. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,原因:" . $exception->getMessage(), '15280215347');
  314. }
  315. }
  316. }
  317. }
  318. }