MMerchantSettlement.Class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Gss
  5. * Date: 2021/4/6 0006
  6. * Time: 14:19
  7. */
  8. namespace JinDouYun\Model\Merchant;
  9. use JinDouYun\Dao\Merchant\DMerchantSettlement;
  10. use JinDouYun\Dao\Merchant\DMerchantApply;
  11. use JinDouYun\Dao\Merchant\DMerchantDetail;
  12. use Mall\Framework\Core\ErrorCode;
  13. use Mall\Framework\Core\StatusCode;
  14. use Mall\Framework\Core\ResultWrapper;
  15. class MMerchantSettlement
  16. {
  17. private $objDMerchantSettlement;
  18. private $objDMerchantApply;
  19. private $objDMerchantDetail;
  20. private $enterpriseId;
  21. private $userCenterId;
  22. public function __construct($enterpriseId, $userCenterId)
  23. {
  24. $this->userCenterId = $userCenterId;
  25. $this->enterpriseId = $enterpriseId;
  26. $this->objDMerchantSettlement = new DMerchantSettlement('finance');
  27. $this->objDMerchantApply = new DMerchantApply();
  28. $this->objDMerchantDetail = new DMerchantDetail();
  29. $this->objDMerchantSettlement->setTable('qianniao_merchant_settlement_' . $enterpriseId);
  30. $this->objDMerchantDetail->setTable('qianniao_merchant_detail_' . $enterpriseId);
  31. }
  32. /*
  33. * 新增结算记录
  34. * */
  35. public function addMerchantSettlement($params)
  36. {
  37. $insert = [];
  38. foreach ($params as $key =>$value){
  39. $insert[] = [
  40. "orderId"=>$value['orderId'],
  41. "orderNo"=>$value['orderNo'],
  42. "goodsId"=>$value['goodsId'],
  43. "goodsName"=>$value['goodsName'],
  44. "goodsNum"=>$value['goodsNum'],
  45. "goodsPrice"=>$value['goodsPrice'],
  46. "goodsMoney"=>$value['goodsMoney'],
  47. "merchantId"=>$value['merchantId'],
  48. "merchantName"=>isset($value['merchantName']) ? $value['merchantName'] : '',
  49. "outStockTime"=>$value['outStockTime']
  50. ];
  51. }
  52. $beginTransactionStatus = $this->objDMerchantSettlement->beginTransaction();
  53. $merchantSettlementId = $this->objDMerchantSettlement->insert($insert,true);
  54. if ($merchantSettlementId === false) {
  55. return ResultWrapper::fail($this->objDMerchantSettlement->error(), ErrorCode::$dberror);
  56. }
  57. if($beginTransactionStatus){
  58. $this->objDMerchantSettlement->commit();
  59. }
  60. return ResultWrapper::success($merchantSettlementId);
  61. }
  62. /*
  63. * 结算记录状态
  64. * */
  65. public function updateMerchantSettlement($params)
  66. {
  67. //根据订单id查询当前结算记录
  68. $merchantSettlementData = $this->objDMerchantSettlement->select( ['orderId'=>$params['orderId'],'settlementStatus'=>StatusCode::$delete] );
  69. if ($merchantSettlementData === false) {
  70. return ResultWrapper::fail($this->objDMerchantSettlement->error(), ErrorCode::$dberror);
  71. }
  72. if( empty($merchantSettlementData) ){
  73. return ResultWrapper::success(false);
  74. }
  75. $beginTransactionStatus = $this->objDMerchantSettlement->beginTransaction();
  76. $merchantData = [];
  77. $insertDetails = [];
  78. //循环统计商户的金额
  79. foreach ($merchantSettlementData as $key => $value){
  80. if( isset($merchantData[$value['merchantId']][$value['orderId']]) ){
  81. $merchantData[$value['merchantId']][$value['orderId']]['goodsMoney'] =
  82. bcadd($value['goodsMoney'],$merchantData[$value['merchantId']][$value['orderId']]['goodsMoney'],2);
  83. }else{
  84. $merchantData[$value['merchantId']][$value['orderId']] = [
  85. 'orderId' => $value['orderId'],
  86. 'orderNo' => $value['orderNo'],
  87. 'merchantId' => $value['merchantId'],
  88. 'merchantName' => $value['merchantName'],
  89. 'goodsMoney' => $value['goodsMoney']
  90. ];
  91. }
  92. }
  93. foreach ($merchantData as $merchantId => $orderData){
  94. //查询商户余额
  95. $merchant = $this->objDMerchantApply->get(['id' => $merchantId]);
  96. if($merchant === false){
  97. $this->objDMerchantSettlement->rollBack();
  98. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  99. }
  100. $afterMoney = empty($merchant['balance'])?0:$merchant['balance'];
  101. foreach($orderData as $orderId => $value){
  102. $afterMoney = bcadd($afterMoney, $value['goodsMoney'], 2);
  103. //组装流水数据
  104. $insertDetails[] = [
  105. 'merchantId' => $value['merchantId'],
  106. 'merchantName' => $value['merchantName'],
  107. 'originId' => $value['orderId'],
  108. 'originNo' => $value['orderNo'],
  109. 'money' => $value['goodsMoney'],
  110. 'afterMoney' => $afterMoney,
  111. 'type' => StatusCode::$standard,
  112. 'source' => 2,
  113. 'remark' => '订单结算',
  114. 'createTime' => time(),
  115. 'updateTime' => time()
  116. ];
  117. }
  118. $detailsDbResult = $this->objDMerchantDetail->insert($insertDetails,true);
  119. if($detailsDbResult === false){
  120. $this->objDMerchantSettlement->rollBack();
  121. return ResultWrapper::fail($this->objDMerchantDetail->error(), ErrorCode::$dberror);
  122. }
  123. $dbResult = $this->objDMerchantApply->update(['balance' => $afterMoney, 'updateTime' => time()], ['id' => $merchantId]);
  124. if($dbResult === false){
  125. $this->objDMerchantSettlement->rollBack();
  126. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  127. }
  128. }
  129. //更新结算状态
  130. $updateStatus = $this->objDMerchantSettlement->update(['settlementStatus'=>StatusCode::$standard, 'updateTime' => time(), 'finishTime'=>time()],['orderId'=>$params['orderId'],'settlementStatus'=>StatusCode::$delete]);
  131. if ($updateStatus === false) {
  132. $this->objDMerchantSettlement->rollBack();
  133. return ResultWrapper::fail($this->objDMerchantSettlement->error(), ErrorCode::$dberror);
  134. }
  135. if($beginTransactionStatus){
  136. $this->objDMerchantSettlement->commit();
  137. }
  138. return ResultWrapper::success($updateStatus);
  139. }
  140. /*
  141. * 获取所有结算记录
  142. * */
  143. public function getAllMerchantSettlement($selectParams)
  144. {
  145. $limit = $selectParams['limit'];
  146. unset($selectParams['limit']);
  147. $offset = $selectParams['offset'];
  148. unset($selectParams['offset']);
  149. $returnData = [
  150. 'data' => [],
  151. 'total' => 0,
  152. ];
  153. $whereSql = '';
  154. // if (isset($selectParams['operatorName']) && !empty($selectParams['operatorName'])) {
  155. // $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  156. // $whereSql .= $where . ' operatorName like "%' . $selectParams['operatorName'] . '%"';
  157. // }
  158. if (isset($selectParams['merchantId']) && !empty($selectParams['merchantId'])) {
  159. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  160. $whereSql .= $where . ' merchantId = ' . $selectParams['merchantId'];
  161. }
  162. if (isset($selectParams['goodsName']) && !empty($selectParams['goodsName'])) {
  163. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  164. $whereSql .= $where . ' goodsName LIKE "%' . $selectParams['goodsName'] .'%"';
  165. }
  166. if (isset($selectParams['settlementStatus']) && !empty($selectParams['settlementStatus'])) {
  167. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  168. $whereSql .= $where . ' settlementStatus = ' . $selectParams['settlementStatus'];
  169. }
  170. if ( (isset($selectParams['start']) && !empty($selectParams['start']))&&(isset($selectParams['end']) && !empty($selectParams['end'])) ) {
  171. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  172. $whereSql .= $where . ' createTime BETWEEN ' . $selectParams['start'] . ' AND '. $selectParams['end'];
  173. }
  174. $sql = 'SELECT * FROM ' .$this->objDMerchantSettlement->get_Table().$whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
  175. $dbResult = $this->objDMerchantSettlement->query($sql);
  176. if ($dbResult === false) {
  177. return ResultWrapper::fail($this->objDMerchantSettlement->error(), ErrorCode::$dberror);
  178. }
  179. if(empty($dbResult)){
  180. return ResultWrapper::success($returnData);
  181. }
  182. $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDMerchantSettlement->get_Table().$whereSql;
  183. $dbTotalResult = $this->objDMerchantSettlement->query($totalSql);
  184. if ($dbTotalResult === false) {
  185. return ResultWrapper::fail($this->objDMerchantSettlement->error(), ErrorCode::$dberror);
  186. }
  187. if(empty($dbTotalResult)){
  188. return ResultWrapper::success([]);
  189. }
  190. $return = [
  191. 'data' => $dbResult,
  192. 'total' => $dbTotalResult[0]['count']
  193. ];
  194. return ResultWrapper::success($return);
  195. }
  196. }