CustomController.php 12 KB

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