CustomController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. $shopList = ShopClass::getAllByCondition(['ptStyle' => 2], null, '*', null, true);
  26. if (empty($shopList)) {
  27. return false;
  28. }
  29. foreach ($shopList as $key => $shop) {
  30. $shopId = $shop->id ?? 0;
  31. // $shopName = $shop->shopName ?? '';
  32. // $merchantName = $shop->merchantName ?? '';
  33. // echo "{$merchantName}-{$shopName} \n\n\n\n";
  34. $query = new \yii\db\Query();
  35. $query->from(Custom::tableName());
  36. $query->where(['ownShopId' => $shopId]);
  37. $query->orderBy('addTime ASC');
  38. foreach ($query->batch(50) as $customList) {
  39. if (!empty($customList)) {
  40. foreach ($customList as $custom) {
  41. //$customName = $custom['name'] ?? '';
  42. $customId = $custom['id'] ?? 0;
  43. $where = ['customId' => $customId, 'debt' => 1];
  44. $orderList = OrderClass::getAllByCondition($where, 'addTime DESC', '*');
  45. $ghsOrderDebtAmount = 0;
  46. if (!empty($orderList)) {
  47. foreach ($orderList as $order) {
  48. $actPrice = $order['remainDebtPrice'] ?? 0;
  49. $ghsOrderDebtAmount = bcadd($ghsOrderDebtAmount, $actPrice, 2);
  50. }
  51. }
  52. $ghsId = $custom['ghsId'] ?? 0;
  53. $ghs = \bizHd\ghs\classes\GhsClass::getById($ghsId);
  54. $where = ['ghsId' => $ghsId, 'debt' => PurchaseClass::DEBT_YES];
  55. $cgList = PurchaseClass::getAllByCondition($where, 'addTime DESC', '*');
  56. $hdCgOrderDebtAmount = 0;
  57. if (!empty($cgList)) {
  58. foreach ($cgList as $cg) {
  59. $actPrice = $cg['remainDebtPrice'] ?? 0;
  60. $hdCgOrderDebtAmount = bcadd($hdCgOrderDebtAmount, $actPrice, 2);
  61. }
  62. }
  63. if ($ghsOrderDebtAmount < 0 || $hdCgOrderDebtAmount < 0) {
  64. noticeUtil::push("!!!!!!!供货商 {$ghsId} 客户id {$customId} 出现负数 $ghsOrderDebtAmount $hdCgOrderDebtAmount", '15280215347');
  65. echo "@@@@@@@@@@@@@供货商 {$ghsId} 客户id {$customId} 出现负数 $ghsOrderDebtAmount $hdCgOrderDebtAmount \n";
  66. }
  67. $ghsOrderDebtAmount = floatval($ghsOrderDebtAmount);
  68. $hdCgOrderDebtAmount = floatval($hdCgOrderDebtAmount);
  69. if ($ghsOrderDebtAmount != $hdCgOrderDebtAmount) {
  70. noticeUtil::push("!!!!!!!供货商 {$ghsId} 客户id {$customId} 二边欠款单总金额不一致", '15280215347');
  71. echo "---------供货商 {$ghsId} 客户id {$customId} 二边 欠款单 总金额 不一致 {$ghsOrderDebtAmount} {$hdCgOrderDebtAmount} \n";
  72. continue;
  73. }
  74. $customSaveDebtAmount = $custom['debtAmount'] ? floatval($custom['debtAmount']) : 0;
  75. $ghsSaveDebtAmount = $ghs['debtAmount'] ? floatval($ghs['debtAmount']) : 0;
  76. if ($customSaveDebtAmount != $ghsOrderDebtAmount) {
  77. echo "*********客户 {$customId} 欠款 和 订单总额 不一致 $customSaveDebtAmount $ghsOrderDebtAmount \n";
  78. noticeUtil::push("*********客户 {$customId} 欠款总金额 和 订单总合 不一致 $customSaveDebtAmount $ghsOrderDebtAmount ", '15280215347');
  79. // echo "已更:".$hdCgOrderDebtAmount.' '.$ghsOrderDebtAmount."\n";
  80. // \bizHd\ghs\classes\GhsClass::updateById($ghsId, ['debtAmount' => $hdCgOrderDebtAmount]);
  81. // CustomClass::updateById($customId, ['debtAmount' => $ghsOrderDebtAmount]);
  82. continue;
  83. }
  84. if ($ghsSaveDebtAmount != $hdCgOrderDebtAmount) {
  85. echo "#########欠供货商 {$ghsId} 和 订单总额 不一致 $ghsSaveDebtAmount $hdCgOrderDebtAmount \n";
  86. noticeUtil::push("#########欠供货商 {$ghsId} 金额 和 订单总合 不一致 $ghsSaveDebtAmount $hdCgOrderDebtAmount ", '15280215347');
  87. // echo "已更:".$hdCgOrderDebtAmount.' '.$ghsOrderDebtAmount."\n";
  88. // \bizHd\ghs\classes\GhsClass::updateById($ghsId, ['debtAmount' => $hdCgOrderDebtAmount]);
  89. // CustomClass::updateById($customId, ['debtAmount' => $ghsOrderDebtAmount]);
  90. continue;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. public function actionBatchCommand()
  98. {
  99. //批量创建客户
  100. $this->createCustomer();
  101. //从首店同步客户
  102. $this->copyCustomers();
  103. }
  104. //批量创建客户
  105. private function createCustomer()
  106. {
  107. $customerCachedKey = 'batch_create_customer_list';
  108. $customList = [];
  109. while ($count = Yii::$app->redis->executeCommand('LLEN', [$customerCachedKey])) {
  110. $customer = Yii::$app->redis->executeCommand('RPOP', [$customerCachedKey]);
  111. $customList[] = $customer;
  112. }
  113. if (empty($customList)) {
  114. return false;
  115. }
  116. foreach ($customList as $customer) {
  117. $connection = Yii::$app->db;
  118. $transaction = $connection->beginTransaction();
  119. $customerInfo = explode('_', $customer);
  120. $shopName = $customerInfo[0];
  121. $mobile = $customerInfo[1];
  122. try {
  123. // 组织表单数据
  124. $post = [];
  125. $post['introSjId'] = $customerInfo[2];
  126. $post['introShopId'] = $customerInfo[3];
  127. $post['introSjName'] = $customerInfo[4];
  128. $post['introStyle'] = SjClass::STYLE_SUPPLIER;
  129. $post['name'] = $shopName;
  130. if (empty($shopName)) {
  131. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称没有填写", '15280215347');
  132. $transaction->rollBack();
  133. continue;
  134. }
  135. $post['mobile'] = $mobile;
  136. if (stringUtil::isMobile($mobile) == false) {
  137. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号填写错误", '15280215347');
  138. $transaction->rollBack();
  139. continue;
  140. }
  141. if (stringUtil::getWordNum($shopName) > 8) {
  142. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称超过了8个汉字", '15280215347');
  143. $transaction->rollBack();
  144. continue;
  145. }
  146. $has = ApplyClass::getByCondition(['mobile' => $mobile]);
  147. if (!empty($has)) {
  148. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号已经存在了哦", '15280215347');
  149. $transaction->rollBack();
  150. continue;
  151. }
  152. $hasShop = ShopClass::getByCondition(['mobile' => $mobile]);
  153. if (!empty($hasShop)) {
  154. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号已经存在了", '15280215347');
  155. $transaction->rollBack();
  156. continue;
  157. }
  158. $adminName = $post['adminName'] ?? '';
  159. $miniOpenId = $post['miniOpenId'] ?? '';
  160. $adminInfo = ['name' => $adminName, 'mobile' => $mobile, 'miniOpenId' => $miniOpenId];
  161. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  162. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  163. $adminId = $admin['id'] ?? 0;
  164. $post['adminId'] = $adminId;
  165. $post['adminName'] = $admin['name'] ?? '';
  166. $post['long'] = isset($post['longitude']) ? $post['longitude'] : '';
  167. $post['lat'] = isset($post['latitude']) ? $post['latitude'] : '';
  168. $post['from'] = ApplyClass::FROM_RETAIL;
  169. $post['style'] = SjClass::STYLE_RETAIL;
  170. //增加或更新申请
  171. $respond = ApplyService::replaceRetail($post);
  172. $id = $respond['id'] ?? 0;
  173. Yii::$app->params['ptStyle'] = 2; // 后继代码,要用到此参数
  174. ApplyService::pass($id, '批发商手动添加');
  175. $transaction->commit();
  176. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 由批发商添加成功 ", '15280215347');
  177. } catch (\Exception $exception) {
  178. $transaction->rollBack();
  179. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,原因:" . $exception->getMessage(), '15280215347');
  180. }
  181. }
  182. }
  183. //批发端分店从首店同步客户
  184. private function copyCustomers()
  185. {
  186. $cachedKey = 'copy_other_shop_customers';
  187. $shopIdList = [];
  188. while ($count = Yii::$app->redis->executeCommand('LLEN', [$cachedKey])) {
  189. $shopIdData = Yii::$app->redis->executeCommand('RPOP', [$cachedKey]);
  190. $shopIdList[] = $shopIdData;
  191. }
  192. if (empty($shopIdList)) {
  193. return;
  194. }
  195. foreach ($shopIdList as $shopIdData) {
  196. $shopIdArr = explode('_', $shopIdData);
  197. if (count($shopIdArr) != 2) {
  198. Yii::info('copy customer 数据出错:' . $shopIdData);
  199. noticeUtil::push("从分店同步客户出错了:" . $shopIdData, '15280215347');
  200. continue;
  201. }
  202. //指定当前环境为批发商
  203. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  204. $toShopId = $shopIdArr[0];
  205. $fromShopId = $shopIdArr[1];
  206. $fromCustomList = CustomClass::getAllByCondition(['ownShopId' => $fromShopId], null, '*');
  207. foreach ($fromCustomList as $fromCustomer) {
  208. $exist = CustomClass::exists(['ownShopId' => $toShopId, 'shopId' => $fromCustomer['shopId']]);
  209. if (!$exist) {
  210. CustomClass::build($toShopId, $fromCustomer['shopId'], $fromCustomer['name']);
  211. }
  212. }
  213. }
  214. }
  215. }