ProfitShareReceiverEntity.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Lakala\Entity;
  4. use SixShop\Core\Entity\BaseEntity;
  5. use SixShop\Core\Exception\LogicException;
  6. use SixShop\Lakala\Enum\ReceiverActionEnum;
  7. use SixShop\Lakala\Enum\UploadFileTypeEnum;
  8. use SixShop\Lakala\Facade\Config;
  9. use SixShop\Lakala\Enum\ProfitShareOrderStatusEnum;
  10. use SixShop\Lakala\Enum\ReceiverStatusEnum;
  11. use SixShop\Lakala\Facade\LedgerService;
  12. use SixShop\Lakala\Facade\MMSService;
  13. use SixShop\Lakala\Model\ProfitShareReceiverModel;
  14. use think\facade\Db;
  15. use think\Model;
  16. use think\Paginator;
  17. use function SixShop\Core\throw_logic_exception;
  18. /**
  19. * @mixin ProfitShareReceiverModel
  20. */
  21. class ProfitShareReceiverEntity extends BaseEntity
  22. {
  23. public function saveReceiver(array $data): self
  24. {
  25. /* @var self $entity */
  26. $entity = $this->where(['user_id' => $data['user_id']])->findOrEmpty();
  27. if (!$entity->isEmpty()) {
  28. throw_logic_exception('已存在分账接收方申请记录,请勿重复提交!');
  29. }
  30. $entity->data($data);
  31. Db::transaction(function () use ($entity) {
  32. $entity->action = ReceiverActionEnum::ADD;
  33. $entity->status = ReceiverStatusEnum::PENDING;
  34. $entity->save();
  35. $result = MMSService::cardBin($entity->order_no, $entity->acct_no, $entity->org_code);
  36. $entity->acct_open_bank_code = $result->bankCode;
  37. $entity->acct_clear_bank_code = $result->clearingBankCode;
  38. $entity->save();
  39. });
  40. return $entity;
  41. }
  42. public function getReceiverList(array $params, array $pageAndLimit): Paginator
  43. {
  44. return $this->withSearch(['order_no', 'status', 'user_id'], $params)
  45. ->append(['action_text','status_text'])
  46. ->order('id', 'desc')
  47. ->paginate($pageAndLimit);
  48. }
  49. public function getReceiver(array $params): self
  50. {
  51. $entity = $this->where($params)
  52. ->append(['action_text','status_text'])
  53. ->findOrEmpty();
  54. if ($entity->isEmpty()) {
  55. throw_logic_exception('分账接收方申请记录不存在!');
  56. }
  57. if ($entity->receiver_no && $entity->status == ReceiverStatusEnum::SUBMITTING) {
  58. $response = LedgerService::queryReceiverDetail($entity->order_no, $entity->receiver_no, $entity->org_code);
  59. if ($response->rowStatus == 'VALID') {
  60. $entity->status = ReceiverStatusEnum::VERIFIED;
  61. $entity->wallet_id = $response->walletId;
  62. }
  63. $entity->save();
  64. }
  65. if ($entity->status == ReceiverStatusEnum::BINDING) {
  66. $response = LedgerService::queryBindApplyList([
  67. 'orderNo' => $entity->order_no,
  68. 'orgCode' => $entity->org_code,
  69. 'receiverNo' => $entity->receiver_no,
  70. ]);
  71. if (isset($response->list[0])
  72. && $response->list[0]->rowSno == $entity->order_no) {
  73. $entity->status = match ($response->list[0]->auditStatus) {
  74. '0' => ReceiverStatusEnum::BINDING,
  75. '1' => ReceiverStatusEnum::BOUND,
  76. '2' => ReceiverStatusEnum::BIND_FAILED
  77. };
  78. if ($entity->status == ReceiverStatusEnum::BOUND) {
  79. $entity->effective_time = date('Y-m-d H:i:s');
  80. }
  81. $entity->fail_reason = $response->list[0]->remark;
  82. $entity->save();
  83. }
  84. }
  85. return $entity;
  86. }
  87. public function apply(int $id): self
  88. {
  89. $entity = $this->getReceiver(['id' => $id]);
  90. if ($entity->status != ReceiverStatusEnum::PENDING || $entity->action != ReceiverActionEnum::ADD) {
  91. throw_logic_exception('待审核状态新增记录才可以申请!');
  92. }
  93. $reqData = [
  94. 'orderNo' => $entity->order_no,
  95. 'orgCode' => $entity->org_code,
  96. 'receiverName' => $entity->receiver_name,
  97. 'contactMobile' => $entity->contact_mobile,
  98. 'acctNo' => $entity->acct_no,
  99. 'acctName' => $entity->acct_name,
  100. 'acctTypeCode' => $entity->acct_type_code,
  101. 'acctCertificateType' => $entity->acct_certificate_type,
  102. 'acctCertificateNo' => $entity->acct_certificate_no,
  103. 'acctOpenBankCode' => $entity->acct_open_bank_code,
  104. 'acctOpenBankName' => $entity->acct_open_bank_name,
  105. 'acctClearBankCode' => $entity->acct_clear_bank_code,
  106. ];
  107. try {
  108. $response = LedgerService::applyLedgerReceiver($reqData);
  109. } catch (LogicException $e) {
  110. if ($e->getResponse()->getData()['code'] == 103001) {
  111. // 系统异常,请稍后重试
  112. // 创建新的申请记录
  113. $newEntity = $entity->clone()->toArray();
  114. unset($newEntity['id'], $newEntity['order_no']);
  115. $this->save($newEntity);
  116. $entity->delete();
  117. }
  118. throw $e;
  119. }
  120. $entity->org_id = $response->orgId;
  121. $entity->org_name = $response->orgName;
  122. $entity->receiver_no = $response->receiverNo;
  123. $entity->status = ReceiverStatusEnum::SUBMITTING;
  124. $entity->save();
  125. return $entity;
  126. }
  127. public function bind(int $id): self
  128. {
  129. $entity = $this->getReceiver(['id' => $id]);
  130. if ($entity->status != ReceiverStatusEnum::VERIFIED || $entity->action != ReceiverActionEnum::ADD) {
  131. throw_logic_exception('验证通过状态新增记录才可以绑定!');
  132. }
  133. if (!$entity->entrust_file_path) {
  134. if (!$entity->entrust_local_path) {
  135. // 添加默认合作协议
  136. $defaultReceiverAgreementFile = Config::getConfig('receiver_agreement_file');
  137. if (!$defaultReceiverAgreementFile) {
  138. throw_logic_exception('请先添加默认合作协议!');
  139. }
  140. $entity->entrust_local_path = Config::getConfig('receiver_agreement_file')[0];
  141. $entity->save();
  142. }
  143. // 上传协议文件
  144. $entrustLocalPath = public_path() . $entity->entrust_local_path;
  145. $pathInfo = pathinfo($entrustLocalPath);
  146. $response = MMSService::uploadFile(
  147. orderNo: $entity->order_no,
  148. attType: UploadFileTypeEnum::SPLIT_COOPERATION_FILE,
  149. attExtName: $pathInfo['extension'],
  150. fileContent: file_get_contents($entrustLocalPath),
  151. );
  152. $entity->entrust_file_name = '合作协议.' . $pathInfo['extension'];
  153. $entity->entrust_file_path = $response->attFileId;
  154. $entity->save();
  155. }
  156. LedgerService::applyBind([
  157. 'orderNo' => $entity->order_no,
  158. 'orgCode' => $entity->org_code,
  159. 'receiverNo' => $entity->receiver_no,
  160. 'entrustFileName' => $entity->entrust_file_name,
  161. 'entrustFilePath' => $entity->entrust_file_path,
  162. ]);
  163. $entity->status = ReceiverStatusEnum::BINDING;
  164. $entity->save();
  165. return $entity;
  166. }
  167. public function updateReceiver(array $params, array $data):self
  168. {
  169. /* @var self $entity */
  170. $entity = $this->where($params)->findOrEmpty();
  171. if ($entity->isEmpty()) {
  172. throw_logic_exception('分账接收方申请记录不存在!');
  173. }
  174. $saveData = $entity->toArray();
  175. unset($saveData['id'], $saveData['order_no'], $saveData['create_time'], $saveData['effective_time']);
  176. $saveData = array_merge($saveData, $data);
  177. $this->data($saveData);
  178. Db::transaction(function () use ($entity) {
  179. $this->action = ReceiverActionEnum::UPDATE;
  180. $this->status = ReceiverStatusEnum::PENDING;
  181. $this->save();
  182. $result = MMSService::cardBin($this->order_no, $this->acct_no, $this->org_code);
  183. $this->acct_open_bank_code = $result->bankCode;
  184. $this->acct_clear_bank_code = $result->clearingBankCode;
  185. $this->fail_reason = '';
  186. $this->save();
  187. });
  188. return $this;
  189. }
  190. public function modify(int $id): self
  191. {
  192. $entity = $this->where(['id' => $id])
  193. ->append(['action_text','status_text'])
  194. ->findOrEmpty();
  195. if ($entity->isEmpty()) {
  196. throw_logic_exception('分账接收方申请记录不存在!');
  197. }
  198. $reqData = [
  199. 'orderNo' => $entity->order_no,
  200. 'orgCode' => $entity->org_code,
  201. 'receiverNo' => $entity->receiver_no,
  202. 'receiverName' => $entity->receiver_name,
  203. 'contactMobile' => $entity->contact_mobile,
  204. 'acctNo' => $entity->acct_no,
  205. 'acctTypeCode' => $entity->acct_type_code,
  206. 'acctOpenBankCode' => $entity->acct_open_bank_code,
  207. 'acctOpenBankName' => $entity->acct_open_bank_name,
  208. 'acctClearBankCode' => $entity->acct_clear_bank_code,
  209. ];
  210. $response = LedgerService::modifyLedgerReceiver($reqData);
  211. if ($response->receiverNo == $entity->receiver_no) {
  212. $entity->status = ReceiverStatusEnum::BOUND;
  213. $entity->effective_time = date('Y-m-d H:i:s');
  214. $entity->save();
  215. }
  216. return $entity;
  217. }
  218. }