MMerchantSettlement.Class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. var_dump("xxxx");
  101. var_dump($merchant);
  102. $afterMoney = $merchant['balance'];
  103. foreach($orderData as $orderId => $value){
  104. $afterMoney = bcadd($afterMoney, $value['goodsMoney'], 2);
  105. //组装流水数据
  106. $insertDetails[] = [
  107. 'merchantId' => $value['merchantId'],
  108. 'merchantName' => $value['merchantName'],
  109. 'originId' => $value['orderId'],
  110. 'originNo' => $value['orderNo'],
  111. 'money' => $value['goodsMoney'],
  112. 'afterMoney' => $afterMoney,
  113. 'type' => StatusCode::$standard,
  114. 'source' => 2,
  115. 'remark' => '订单结算',
  116. 'createTime' => time(),
  117. 'updateTime' => time()
  118. ];
  119. }
  120. $detailsDbResult = $this->objDMerchantDetail->insert($insertDetails,true);
  121. if($detailsDbResult === false){
  122. $this->objDMerchantSettlement->rollBack();
  123. return ResultWrapper::fail($this->objDMerchantDetail->error(), ErrorCode::$dberror);
  124. }
  125. $dbResult = $this->objDMerchantApply->update(['balance' => $afterMoney, 'updateTime' => time()], ['id' => $merchantId]);
  126. if($dbResult === false){
  127. $this->objDMerchantSettlement->rollBack();
  128. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  129. }
  130. }
  131. //更新结算状态
  132. $updateStatus = $this->objDMerchantSettlement->update(['settlementStatus'=>StatusCode::$standard, 'updateTime' => time(), 'finishTime'=>time()],['orderId'=>$params['orderId'],'settlementStatus'=>StatusCode::$delete]);
  133. if ($updateStatus === false) {
  134. $this->objDMerchantSettlement->rollBack();
  135. return ResultWrapper::fail($this->objDMerchantSettlement->error(), ErrorCode::$dberror);
  136. }
  137. if($beginTransactionStatus){
  138. $this->objDMerchantSettlement->commit();
  139. }
  140. return ResultWrapper::success($updateStatus);
  141. }
  142. /*
  143. * 获取所有结算记录
  144. * */
  145. public function getAllMerchantSettlement($selectParams)
  146. {
  147. $limit = $selectParams['limit'];
  148. unset($selectParams['limit']);
  149. $offset = $selectParams['offset'];
  150. unset($selectParams['offset']);
  151. $returnData = [
  152. 'data' => [],
  153. 'total' => 0,
  154. ];
  155. $whereSql = '';
  156. // if (isset($selectParams['operatorName']) && !empty($selectParams['operatorName'])) {
  157. // $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  158. // $whereSql .= $where . ' operatorName like "%' . $selectParams['operatorName'] . '%"';
  159. // }
  160. if (isset($selectParams['merchantId']) && !empty($selectParams['merchantId'])) {
  161. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  162. $whereSql .= $where . ' merchantId = ' . $selectParams['merchantId'];
  163. }
  164. if (isset($selectParams['goodsName']) && !empty($selectParams['goodsName'])) {
  165. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  166. $whereSql .= $where . ' goodsName LIKE "%' . $selectParams['goodsName'] .'%"';
  167. }
  168. if (isset($selectParams['settlementStatus']) && !empty($selectParams['settlementStatus'])) {
  169. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  170. $whereSql .= $where . ' settlementStatus = ' . $selectParams['settlementStatus'];
  171. }
  172. if ( (isset($selectParams['start']) && !empty($selectParams['start']))&&(isset($selectParams['end']) && !empty($selectParams['end'])) ) {
  173. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  174. $whereSql .= $where . ' createTime BETWEEN ' . $selectParams['start'] . ' AND '. $selectParams['end'];
  175. }
  176. $sql = 'SELECT * FROM ' .$this->objDMerchantSettlement->get_Table().$whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
  177. $dbResult = $this->objDMerchantSettlement->query($sql);
  178. if ($dbResult === false) {
  179. return ResultWrapper::fail($this->objDMerchantSettlement->error(), ErrorCode::$dberror);
  180. }
  181. if(empty($dbResult)){
  182. return ResultWrapper::success($returnData);
  183. }
  184. $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDMerchantSettlement->get_Table().$whereSql;
  185. $dbTotalResult = $this->objDMerchantSettlement->query($totalSql);
  186. if ($dbTotalResult === false) {
  187. return ResultWrapper::fail($this->objDMerchantSettlement->error(), ErrorCode::$dberror);
  188. }
  189. if(empty($dbTotalResult)){
  190. return ResultWrapper::success([]);
  191. }
  192. $return = [
  193. 'data' => $dbResult,
  194. 'total' => $dbTotalResult[0]['count']
  195. ];
  196. return ResultWrapper::success($return);
  197. }
  198. }