MSupplierWithdraw.Class.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace JinDouYun\Model\Purchase;
  3. use JinDouYun\Dao\Purchase\DSupplierWithdraw;
  4. use JinDouYun\Model\Finance\MSupplierBalance;
  5. use Mall\Framework\Core\ErrorCode;
  6. use Mall\Framework\Core\ResultWrapper;
  7. use Mall\Framework\Core\StatusCode;
  8. class MSupplierWithdraw
  9. {
  10. private $objDSupplierWithdraw;
  11. private $onlineUserId;
  12. private $onlineEnterpriseId;
  13. /**
  14. * MSupplierWithdraw constructor.
  15. * @param $onlineUserId
  16. * @param $onlineEnterpriseId
  17. * @throws \Exception
  18. */
  19. public function __construct($onlineUserId, $onlineEnterpriseId)
  20. {
  21. $this->onlineUserId = $onlineUserId;
  22. $this->onlineEnterpriseId = $onlineEnterpriseId;
  23. $this->objDSupplierWithdraw = new DSupplierWithdraw();
  24. $this->objDSupplierWithdraw->setTable('qianniao_supplier_withdraw_'.$this->onlineEnterpriseId);
  25. }
  26. public function add(array $params,int $supplierId): ResultWrapper
  27. {
  28. $objMSupplierBalance = new MSupplierBalance($this->onlineEnterpriseId,$this->onlineUserId);
  29. $supplierResult = $objMSupplierBalance->getSupplier($supplierId);
  30. if (!$supplierResult->isSuccess()){
  31. return ResultWrapper::fail($supplierResult->getData(),$supplierResult->getErrorCode());
  32. }
  33. $supplier = $supplierResult->getData();
  34. if ($supplier['money'] + $params['money'] > 0) {
  35. return ResultWrapper::fail('可提现余额不足', ErrorCode::$paramError);
  36. }
  37. $insert = [
  38. 'no' => createOrderSn(4,1,$this->onlineUserId),
  39. 'supplierId' => $supplierId,
  40. 'supplierName' => $supplier['title'],
  41. 'type' => $params['type'],
  42. 'accountContent' => json_encode($params['accountContent']),
  43. 'money' => $params['money'],
  44. 'auditStatus' => StatusCode::$auditStatus['auditing'],
  45. 'rate' => 0,//抽成比率
  46. 'fee' => 0,//抽成金额
  47. 'nowMoney' => $params['money'],//预计到账金额
  48. 'createTime' => time(),
  49. ];
  50. $update = [
  51. 'money' => bcadd($supplier['money'],$params['money'],2),
  52. 'waitAuditWithdraw' => bcadd($supplier['waitAuditWithdraw'],$params['money'],2),
  53. 'updateTime' => time()
  54. ];
  55. $this->objDSupplierWithdraw->beginTransaction();
  56. $result = $this->objDSupplierWithdraw->insert($insert);
  57. if ($result === false){
  58. $this->objDSupplierWithdraw->rollBack();
  59. return ResultWrapper::fail($this->objDSupplierWithdraw->error(),ErrorCode::$dberror);
  60. }
  61. $result = $objMSupplierBalance->updateSupplier($update,$supplierId);
  62. if (!$result->isSuccess()){
  63. $this->objDSupplierWithdraw->rollBack();
  64. return ResultWrapper::fail($result->getData(),$result->getErrorCode());
  65. }
  66. $this->objDSupplierWithdraw->commit();
  67. return ResultWrapper::success(true);
  68. }
  69. /**
  70. * Doc: (des="")
  71. * User: XMing
  72. * Date: 2020/12/18
  73. * Time: 6:08 下午
  74. * @param array $selectParams
  75. * @return ResultWrapper
  76. */
  77. public function getAll(array $selectParams): ResultWrapper
  78. {
  79. $fields = ' * ';
  80. $countField = ' COUNT(id) as total ';
  81. $whereSql = '';
  82. if (isset($selectParams['type']) && !empty($selectParams['type'])) {
  83. $whereSql .= ' AND `type` = ' . $selectParams['type'];
  84. }
  85. if (isset($selectParams['auditStatus']) && !empty($selectParams['auditStatus'])) {
  86. $whereSql .= ' AND auditStatus = ' . $selectParams['auditStatus'];
  87. }
  88. if (isset($selectParams['supplierId']) && !empty($selectParams['supplierId'])) {
  89. $whereSql .= ' AND supplierId = ' . $selectParams['supplierId'];
  90. }
  91. if (isset($selectParams['startTime']) && !empty($selectParams['startTime'])) {
  92. $whereSql .= ' AND createTime BETWEEN ' . $selectParams['startTime'] . ' AND ' . $selectParams['endTime'];
  93. }
  94. $countSql = 'SELECT ' . $countField . ' FROM ' . $this->objDSupplierWithdraw->get_Table() . ' WHERE id IS NOT NULL ' . $whereSql;
  95. $whereSql .= ' ORDER BY createTime DESC ';
  96. if (isset($selectParams['limit']) && !empty($selectParams['limit'])) {
  97. $whereSql .= ' LIMIT ' . $selectParams['offset'] . ',' . $selectParams['limit'];
  98. }
  99. $sql = 'SELECT ' . $fields . ' FROM ' . $this->objDSupplierWithdraw->get_Table() . ' WHERE id IS NOT NULL ' . $whereSql;;
  100. $dbResult = $this->objDSupplierWithdraw->query($sql);
  101. if ($dbResult === false) {
  102. return ResultWrapper::fail($this->objDSupplierWithdraw->error(), ErrorCode::$dberror);
  103. }
  104. $total = $this->objDSupplierWithdraw->query($countSql);
  105. foreach ($dbResult as &$value) {
  106. $value['accountContent'] = json_decode($value['accountContent'], true);
  107. }
  108. $return = [
  109. 'data' => $dbResult,
  110. 'total' => isset($total[0]['total']) ? $total[0]['total'] : 0,
  111. ];
  112. return ResultWrapper::success($return);
  113. }
  114. /**
  115. * Doc: (des="")
  116. * User: XMing
  117. * Date: 2020/12/18
  118. * Time: 6:16 下午
  119. * @param int $id
  120. * @param int $status
  121. * @param $reason
  122. * @return ResultWrapper
  123. */
  124. public function updateAuditStatus(int $id, int $status,$reason): ResultWrapper
  125. {
  126. if (!in_array($status, array_keys(StatusCode::$auditStatus))) {
  127. return ResultWrapper::fail('状态不存在', ErrorCode::$notAllowAccess);
  128. }
  129. //查询当前记录
  130. $withdraw = $this->objDSupplierWithdraw->get($id);
  131. if ($withdraw === false) {
  132. return ResultWrapper::fail($this->objDSupplierWithdraw->error, ErrorCode::$dberror);
  133. }
  134. if (empty($withdraw)) {
  135. return ResultWrapper::fail('未获取到指定的记录', ErrorCode::$paramError);
  136. }
  137. $objMSupplierBalance = new MSupplierBalance($this->onlineEnterpriseId,$this->onlineUserId);
  138. $supplierResult = $objMSupplierBalance->getSupplier($withdraw['supplierId']);
  139. if (!$supplierResult->isSuccess()){
  140. return ResultWrapper::fail($supplierResult->getData(),$supplierResult->getErrorCode());
  141. }
  142. $supplier = $supplierResult->getData();
  143. $oldBalance = $supplier['money'];//可提现
  144. $oldWaitAuditWithdraw = $supplier['waitAuditWithdraw'];//提现等待审核
  145. $oldAuditWithdraw = $supplier['auditWithdraw'];//提现待入账金额
  146. $oldWithdraw = $supplier['withdraw'];//已提现金额
  147. $changeMoney = $withdraw['money'];//本次变动金额
  148. switch ($status){
  149. case StatusCode::$auditStatus['auditing']:
  150. //申请提现
  151. break;
  152. case StatusCode::$auditStatus['auditPass']:
  153. ////打款成功
  154. if (bcsub($oldAuditWithdraw, $changeMoney, 2) < 0) {
  155. return ResultWrapper::fail('待入账金额不足', ErrorCode::$paramError);
  156. }
  157. $updateMap = [
  158. 'id' => $supplier['id'],
  159. 'updateTime' => time(),
  160. 'auditWithdraw' => bcsub($oldAuditWithdraw,$changeMoney,2),
  161. 'withdraw' => bcadd($oldWithdraw,$changeMoney,2),
  162. ];
  163. break;
  164. case StatusCode::$auditStatus['auditNotPass']:
  165. //提现审核拒绝,回到余额
  166. if (bcsub($oldWaitAuditWithdraw, $changeMoney, 2) < 0) {
  167. return ResultWrapper::fail('待审核金额不足', ErrorCode::$paramError);
  168. }
  169. $updateMap = [
  170. 'id' => $supplier['id'],
  171. 'updateTime' => time(),
  172. 'money' => bcsub($oldBalance,$changeMoney,2),
  173. 'waitAuditWithdraw' => bcsub($oldWaitAuditWithdraw,$changeMoney,2),
  174. ];
  175. break;
  176. case StatusCode::$auditStatus['auditIng']:
  177. //等待打款,审核成功
  178. if (bcsub($oldWaitAuditWithdraw, $changeMoney, 2) < 0) {
  179. return ResultWrapper::fail('待审核金额不足', ErrorCode::$paramError);
  180. }
  181. $updateMap = [
  182. 'id' => $supplier['id'],
  183. 'updateTime' => time(),
  184. 'waitAuditWithdraw' => bcsub($oldWaitAuditWithdraw,$changeMoney,2),
  185. 'auditWithdraw' => bcadd($oldAuditWithdraw,$changeMoney,2),
  186. ];
  187. break;
  188. default:
  189. return ResultWrapper::fail('状态值有误', ErrorCode::$paramError);
  190. break;
  191. }
  192. $updateData = [
  193. 'auditStatus' => $status,
  194. 'updatetime' => time(),
  195. 'reason' => $reason,
  196. ];
  197. // 审核通过时间
  198. if ($status == StatusCode::$auditStatus['auditIng']) {
  199. $updateData['auditTime'] = time();
  200. }
  201. // 打款完成时间
  202. if ($status == StatusCode::$auditStatus['auditIng']) {
  203. $updateData['payTime'] = time();
  204. }
  205. $this->objDSupplierWithdraw->beginTransaction();
  206. $result = $this->objDSupplierWithdraw->update($updateData,$id);
  207. if ($result === false){
  208. $this->objDSupplierWithdraw->rollBack();
  209. return ResultWrapper::fail($this->objDSupplierWithdraw->error(),ErrorCode::$dberror);
  210. }
  211. $updateResult = $objMSupplierBalance->updateSupplier($updateMap,$supplier['id']);
  212. if (!$updateResult->isSuccess()){
  213. $this->objDSupplierWithdraw->rollBack();
  214. return ResultWrapper::fail($updateResult->getData(),$updateResult->getErrorCode());
  215. }
  216. $this->objDSupplierWithdraw->commit();
  217. return ResultWrapper::success(true);
  218. }
  219. }