MReflectDetail.Class.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Gss
  5. * Date: 2021/3/27 0027
  6. * Time: 16:25
  7. */
  8. namespace JinDouYun\Model\Customer;
  9. use Mall\Framework\Core\ErrorCode;
  10. use Mall\Framework\Core\StatusCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use JinDouYun\Dao\Customer\DReflectDetail;
  13. use JinDouYun\Dao\Customer\DCustomer;
  14. use JinDouYun\Model\Customer\MMemberBalanceDetail;
  15. use JinDouYun\Model\Customer\MCustomerDemand;
  16. class MReflectDetail
  17. {
  18. private $enterpriseId;
  19. private $userCenterId;
  20. private $objDReflectDetail;
  21. private $objDCustomer;
  22. private $cutTable = 1;//客户按照企业id分表
  23. public function __construct($enterpriseId, $userCenterId)
  24. {
  25. $this->userCenterId = $userCenterId;
  26. $this->enterpriseId = $enterpriseId;
  27. $this->objDReflectDetail = new DReflectDetail('default');
  28. $this->objDReflectDetail->setTable('qianniao_reflect_detail_' . $enterpriseId);
  29. $this->objDCustomer = new DCustomer('default');
  30. $tableName = $this->objDCustomer->getTableName($this->objDCustomer->get_Table(), $this->enterpriseId, $this->cutTable);
  31. $this->objDCustomer->setTable($tableName);
  32. }
  33. /**
  34. * 申请提现
  35. */
  36. public function addReflectDetail($params)
  37. {
  38. //查询当前会员的余额
  39. $dbResult = $this->objDCustomer->get($params['customerId']);
  40. if ($dbResult === false) {
  41. return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror);
  42. }
  43. $memberBalance = $dbResult['memberBalance'];
  44. if ( bcsub($memberBalance, $params['money'],2) < 0 ){
  45. return ResultWrapper::fail('客户余额不足!提现余额不能大于总余额', ErrorCode::$notAllowAccess);
  46. }
  47. unset($dbResult);
  48. $beginTransactionStatus = $this->objDReflectDetail->beginTransaction();
  49. //记录流水
  50. $memberBalanceDetailData = [
  51. 'customerId' => $params['customerId'],
  52. 'type' => StatusCode::$delete,
  53. 'userCenterId' => $this->userCenterId,
  54. 'money' =>$params['money'],
  55. 'purpose' => '提现',
  56. 'orderIds' =>'',
  57. 'remark' => '',
  58. 'financeType' => '会员余额提现',
  59. ];
  60. $objMMemberBalanceDetail = new MMemberBalanceDetail($this->enterpriseId,$this->userCenterId);
  61. $memberBalanceDbResult = $objMMemberBalanceDetail->addMemberBalanceDetail($memberBalanceDetailData);
  62. if (!$memberBalanceDbResult->isSuccess()) {
  63. $this->objDReflectDetail->rollBack();
  64. return ResultWrapper::fail($memberBalanceDbResult->getData(), $memberBalanceDbResult->getErrorCode());
  65. }
  66. $DReflectDetailDate = $this->objDReflectDetail->insert($params);
  67. if ($DReflectDetailDate === false) {
  68. $this->objDReflectDetail->rollBack();
  69. return ResultWrapper::fail($this->objDReflectDetail->error(), ErrorCode::$dberror);
  70. }
  71. if($beginTransactionStatus){
  72. $this->objDReflectDetail->commit();
  73. }
  74. return ResultWrapper::success($DReflectDetailDate);
  75. }
  76. /**
  77. * 所有提现记录
  78. */
  79. public function getAllReflectDetail($selectParams)
  80. {
  81. $limit = $selectParams['limit'];
  82. unset($selectParams['limit']);
  83. $offset = $selectParams['offset'];
  84. unset($selectParams['offset']);
  85. $whereSql = '';
  86. if (isset($selectParams['userCenterId']) && !empty($selectParams['userCenterId'])) {
  87. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  88. $whereSql .= $where . ' userCenterId = ' . $selectParams['userCenterId'];
  89. }
  90. if (isset($selectParams['customerId']) && !empty($selectParams['customerId'])) {
  91. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  92. $whereSql .= $where . ' customerId = ' . $selectParams['customerId'];
  93. }
  94. if (isset($selectParams['reflectStatus']) && !empty($selectParams['reflectStatus'])) {
  95. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  96. $whereSql .= $where . ' reflectStatus = ' . $selectParams['reflectStatus'];
  97. }
  98. if (isset($selectParams['auditStatus']) && !empty($selectParams['auditStatus'])) {
  99. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  100. $whereSql .= $where . ' auditStatus = ' . $selectParams['auditStatus'];
  101. }
  102. if (isset($selectParams['reflectType']) && !empty($selectParams['reflectType'])) {
  103. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  104. $whereSql .= $where . ' reflectType = "' . $selectParams['reflectType'].'"';
  105. }
  106. if ( (isset($selectParams['start']) && !empty($selectParams['start']))&&(isset($selectParams['end']) && !empty($selectParams['end'])) ) {
  107. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  108. $whereSql .= $where . ' createTime BETWEEN ' . $selectParams['start'] . ' AND '. $selectParams['end'];
  109. }
  110. $returnData = [
  111. 'data' => [],
  112. 'total' => 0,
  113. ];
  114. $sql = 'SELECT * FROM ' .$this->objDReflectDetail->get_Table().$whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
  115. $dbResult = $this->objDReflectDetail->query($sql);
  116. if ($dbResult === false) {
  117. return ResultWrapper::fail($this->objDReflectDetail->error(), ErrorCode::$dberror);
  118. }
  119. $reflectInfo = [];
  120. foreach ($dbResult as $key =>$value){
  121. $reflectInfoDbResult = json_decode($dbResult[$key]['reflectInfo']);
  122. foreach ($reflectInfoDbResult as $k => $v){
  123. $reflectInfo[$k] = $v;
  124. }
  125. $dbResult[$key]['reflectInfo'] = $reflectInfo;
  126. }
  127. //映射客户名称
  128. $MCustomerDemand= new MCustomerDemand($this->enterpriseId,$this->userCenterId);
  129. $customerName = $MCustomerDemand->format($dbResult);
  130. $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDReflectDetail->get_Table().$whereSql;
  131. $dbTotalResult = $this->objDReflectDetail->query($totalSql);
  132. $return = [
  133. 'data' => $customerName,
  134. 'total' => $dbTotalResult[0]['count'],
  135. ];
  136. return ResultWrapper::success($return);
  137. }
  138. //审核,打款公共方法
  139. public function updateReflectDetail($params)
  140. {
  141. $dbResult = $this->objDReflectDetail->get($params['id']);
  142. if ($dbResult === false) {
  143. return ResultWrapper::fail($this->objDReflectDetail->error(), ErrorCode::$dberror);
  144. }
  145. if(empty($dbResult)){
  146. return ResultWrapper::fail('要审核的单据不存在', ErrorCode::$contentNotExists);
  147. }
  148. if(empty($params['auditStatus']) && empty($params['reflectStatus'])){
  149. return ResultWrapper::fail('没有auditStatus|reflectStatus 不允许审核', ErrorCode::$notAllowAccess);
  150. }
  151. $beginTransactionStatus = $this->objDReflectDetail->beginTransaction();
  152. //判断是需要审核还是打款
  153. if(!empty($params['auditStatus'])){//审核
  154. if ($dbResult['auditStatus'] === StatusCode::$auditStatus['auditPass']) {
  155. return ResultWrapper::fail('单据已审核', ErrorCode::$actionIsDo);
  156. }
  157. $updateDReflectDetail = $this->objDReflectDetail->update(['auditStatus'=>StatusCode::$auditStatus['auditPass']],['id'=>$params['id']]);
  158. if ($updateDReflectDetail === false) {
  159. $this->objDReflectDetail->rollBack();
  160. return ResultWrapper::fail($this->objDReflectDetail->error(), ErrorCode::$dberror);
  161. }
  162. }
  163. if(!empty($params['reflectStatus'])){//打款
  164. if ($dbResult['reflectStatus'] === StatusCode::$standard) {
  165. return ResultWrapper::fail('单据已打款', ErrorCode::$actionIsDo);
  166. }
  167. $updateDReflectDetail = $this->objDReflectDetail->update(['reflectStatus'=>$params['reflectStatus']],['id'=>$params['id']]);
  168. if ($updateDReflectDetail === false) {
  169. $this->objDReflectDetail->rollBack();
  170. return ResultWrapper::fail($this->objDReflectDetail->error(), ErrorCode::$dberror);
  171. }
  172. }
  173. if($beginTransactionStatus){
  174. $this->objDReflectDetail->commit();
  175. }
  176. return ResultWrapper::success($updateDReflectDetail);
  177. }
  178. }