MCommissionWithdrawals.Class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * 分销体现记录Model
  4. * Created by PhpStorm.
  5. * User: phperstar
  6. * Date: 2020/07/22
  7. * Time: 15:00
  8. */
  9. namespace JinDouYun\Model\Commission;
  10. use Mall\Framework\Core\ResultWrapper;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use JinDouYun\Model\Customer\MCustomer;
  14. use JinDouYun\Model\Commission\MCommissionGrade;
  15. use JinDouYun\Dao\Commission\DCommissionBusinessman;
  16. use JinDouYun\Dao\Commission\DCommissionWithdrawals;
  17. class MCommissionWithdrawals
  18. {
  19. /**
  20. * @var int
  21. */
  22. private $userCenterId;
  23. /**
  24. * @var int
  25. */
  26. private $enterpriseId;
  27. /**
  28. * @var DCommissionWithdrawals
  29. */
  30. private $objDCommissionWithdrawals;
  31. /**
  32. * @var MCommissionCalculate
  33. */
  34. private $objMCommissionCalculate;
  35. /**
  36. * @var DCommissionBusinessman
  37. */
  38. private $objDCommissionBusinessman;
  39. /**
  40. * MCommissionWithdrawals constructor.
  41. * @param $enterpriseId
  42. * @param $userCenterId
  43. * @throws \Exception
  44. */
  45. public function __construct($userCenterId, $enterpriseId)
  46. {
  47. $this->enterpriseId = $enterpriseId;
  48. $this->userCenterId = $userCenterId;
  49. $this->objDCommissionWithdrawals = new DCommissionWithdrawals();
  50. $this->objDCommissionWithdrawals->setTable('qianniao_commission_withdrawals_'.$this->enterpriseId);
  51. $this->objMCommissionCalculate = new MCommissionCalculate($this->enterpriseId,$this->userCenterId);
  52. $this->objDCommissionBusinessman = new DCommissionBusinessman();
  53. $this->objDCommissionBusinessman->setTable('qianniao_commission_businessman_' . $this->enterpriseId);
  54. }
  55. /**
  56. * 添加提现记录
  57. * @throws \Exception
  58. */
  59. public function add($params)
  60. {
  61. $params['accountContent'] = json_encode($params['accountContent']);
  62. $this->objDCommissionWithdrawals->setTable('qianniao_commission_withdrawals_'.$this->enterpriseId);
  63. $this->objDCommissionWithdrawals->beginTransaction();
  64. $dbResult = $this->objDCommissionWithdrawals->insert($params);
  65. if ($dbResult === false) {
  66. $this->objDCommissionWithdrawals->rollBack();
  67. return ResultWrapper::fail($this->objDCommissionWithdrawals->error(), ErrorCode::$dberror);
  68. }
  69. $resAudit = self::updateAuditStatus($dbResult,StatusCode::$auditStatus['auditing']);
  70. if (!$resAudit->isSuccess()){
  71. $this->objDCommissionWithdrawals->rollBack();
  72. return ResultWrapper::fail($resAudit->getData(),ErrorCode::$dberror);
  73. }
  74. //查询配置项,是否开启自动审核
  75. $objMCommissionSetting = new MCommissionSetting($this->enterpriseId,$this->userCenterId);
  76. $settingResult = $objMCommissionSetting->getSetting('withdrawAudit');
  77. if (!$settingResult->isSuccess()){
  78. $this->objDCommissionWithdrawals->rollBack();
  79. return ResultWrapper::fail($settingResult->getData(),$settingResult->getErrorCode());
  80. }
  81. $withdrawAudit = $settingResult->getData();// 4:自动审核 5:手动审核
  82. if ($withdrawAudit == StatusCode::$delete){
  83. $resAudit = self::updateAuditStatus($dbResult,StatusCode::$auditStatus['auditIng']);
  84. if (!$resAudit->isSuccess()){
  85. $this->objDCommissionWithdrawals->rollBack();
  86. return ResultWrapper::fail($resAudit->getData(),ErrorCode::$dberror);
  87. }
  88. }
  89. $this->objDCommissionWithdrawals->commit();
  90. return ResultWrapper::success($dbResult);
  91. }
  92. /**
  93. * 获取所有的提现记录
  94. */
  95. public function getAll($selectParams)
  96. {
  97. $condition = '';
  98. if(isset($selectParams['keyword']) && !empty($selectParams['keyword'])){
  99. $keyword = 'JSON_CONTAINS(b.info->'."'$[*].value'".','."'".'"'.$selectParams['keyword'].'"'."')";
  100. $condition .= !empty($condition) ? ' AND '.$keyword:' where '.$keyword;
  101. }
  102. if(isset($selectParams['type']) && !empty($selectParams['type'])){
  103. $type = 'w.type = '.$selectParams['type'];
  104. $condition .= !empty($condition) ? ' AND '.$type:' where '.$type;
  105. }
  106. if (isset($selectParams['startTime']) && !empty($selectParams['startTime'])){
  107. $startime = 'w.createTime BETWEEN '.$selectParams['startTime'].' AND '.$selectParams['endTime'];
  108. $condition .= !empty($condition) ? ' AND '.$startime:' where '.$startime;
  109. }
  110. if(isset($selectParams['auditStatus']) && !empty($selectParams['auditStatus'])){
  111. $auditStatus = 'w.auditStatus = '.$selectParams['auditStatus'];
  112. $condition .= !empty($condition) ? ' AND '.$auditStatus:' where '.$auditStatus;
  113. }
  114. $returnData = [
  115. 'data' => [],
  116. 'total' => 0,
  117. ];
  118. $sql = 'select count(w.id) as total from qianniao_commission_withdrawals_'.$this->enterpriseId.' as w left join qianniao_commission_businessman_'.$this->enterpriseId.' as b on w.businessmanId = b.id'. $condition;
  119. $dbResult = $this->objDCommissionWithdrawals->query($sql);
  120. if ($dbResult === false) {
  121. return ResultWrapper::fail($this->objDCommissionWithdrawals->error(), ErrorCode::$dberror);
  122. }
  123. $total = $dbResult[0]['total'];
  124. $data = [];
  125. if(!empty($total)){
  126. $sql = 'select w.*,b.grade from qianniao_commission_withdrawals_'.$this->enterpriseId.' as w left join qianniao_commission_businessman_'.$this->enterpriseId.' as b on w.businessmanId = b.id'.$condition;
  127. $dbResult = $this->objDCommissionWithdrawals->query($sql);
  128. if ($dbResult === false) {
  129. return ResultWrapper::fail($this->objDCommissionWithdrawals->error(), ErrorCode::$dberror);
  130. }
  131. $data = $dbResult;
  132. }
  133. $returnData = [
  134. 'data' => empty($data)?[]:self::format($data),
  135. 'total' => $total,
  136. ];
  137. return ResultWrapper::success($returnData);
  138. }
  139. /**
  140. * 详情
  141. */
  142. public function detail($id)
  143. {
  144. $this->objDCommissionWithdrawals->setTable('qianniao_commission_withdrawals_'.$this->enterpriseId);
  145. $dbResult = $this->objDCommissionWithdrawals->get($id);
  146. if ($dbResult === false) {
  147. return ResultWrapper::fail($this->objDCommissionWithdrawals->error(), ErrorCode::$dberror);
  148. }
  149. if(!empty($dbResult)){
  150. $data = self::format([$dbResult]);
  151. $dbResult = array_pop($data);
  152. }
  153. return ResultWrapper::success($dbResult);
  154. }
  155. /**
  156. * 公共格式化
  157. */
  158. public function format($data)
  159. {
  160. // 提取所有userCenterIds
  161. $userCenterIds = [];
  162. // 提取所有分销商等级
  163. $gradeIds = [];
  164. foreach ($data as $key => $value){
  165. $userCenterIds[] = $value['userCenterId'];
  166. if(isset($value['grade'])){
  167. $gradeIds[] = $value['grade'];
  168. }
  169. }
  170. $allCustomerData = [];
  171. $allGrageData = [];
  172. if(!empty($userCenterIds)){
  173. // 查询客户信息
  174. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  175. $result = $objMCustomer->getCustomerBasicInfoByUserCenterIds($userCenterIds);
  176. if($result->isSuccess()){
  177. $customerData = $result->getData();
  178. if(!empty($customerData)){
  179. foreach ($customerData as $key => $value){
  180. $allCustomerData[$value['userCenterId']] = $value;
  181. }
  182. }
  183. }
  184. // 查询等级信息
  185. unset($result);
  186. $objMCommissionGrade = new MCommissionGrade($this->enterpriseId, $this->userCenterId);
  187. $result = $objMCommissionGrade->getAllGrade(['grade'=>$gradeIds]);
  188. if($result->isSuccess()){
  189. $gradeData = $result->getData();
  190. if(!empty($gradeData)){
  191. foreach ($gradeData as $key => $value){
  192. $allGrageData[$value['grade']] = $value;
  193. }
  194. }
  195. }
  196. }
  197. foreach ($data as $key => $value){
  198. if(isset($value['grade'])){
  199. $data[$key]['gradeName'] = isset($allGrageData[$value['grade']]) ? $allGrageData[$value['grade']]['name'] : '';
  200. }
  201. $data[$key]['customer'] = isset($allCustomerData[$value['userCenterId']]) ? $allCustomerData[$value['userCenterId']]['name'] : '';
  202. $data[$key]['mobile'] = isset($allCustomerData[$value['userCenterId']]) ? $allCustomerData[$value['userCenterId']]['mobile'] : '';
  203. $data[$key]['accountContent'] = json_decode($value['accountContent'], true);
  204. }
  205. return $data;
  206. }
  207. /**
  208. * 修改提现记录状态
  209. * @param int $id 提现记录表id
  210. * @param int 状态
  211. * @return ResultWrapper
  212. * @throws \Exception
  213. */
  214. public function updateAuditStatus($id, $status)
  215. {
  216. if(!in_array($status, array_keys(StatusCode::$auditStatus))){
  217. return ResultWrapper::fail('状态不存在', ErrorCode::$notAllowAccess);
  218. }
  219. //查询当前记录
  220. $commissionWithdrawals = $this->objDCommissionWithdrawals->get(['id'=>$id],'userCenterId,businessmanId,money,auditStatus');
  221. if ($commissionWithdrawals === false){
  222. return ResultWrapper::fail($this->objDCommissionWithdrawals->error(),ErrorCode::$dberror);
  223. }
  224. if (empty($commissionWithdrawals)){
  225. return ResultWrapper::fail('获取提现记录失败',ErrorCode::$paramError);
  226. }
  227. $buildWithdrawUpdateResult = $this->objMCommissionCalculate->buildWithdraw($commissionWithdrawals,$status);
  228. if (!$buildWithdrawUpdateResult->isSuccess()){
  229. return ResultWrapper::fail($buildWithdrawUpdateResult->getData(),$buildWithdrawUpdateResult->getErrorCode());
  230. }
  231. $withdrawUpdate = $buildWithdrawUpdateResult->getData();
  232. if (empty($withdrawUpdate)){
  233. return ResultWrapper::fail('更新数据为空',ErrorCode::$paramError);
  234. }
  235. $businessmanId = $withdrawUpdate['id'];
  236. unset($withdrawUpdate['id']);
  237. $this->objDCommissionWithdrawals->beginTransaction();
  238. $updateBusinessman = $this->objDCommissionBusinessman->update($withdrawUpdate,['id' => $businessmanId]);
  239. if ($updateBusinessman === false){
  240. $this->objDCommissionWithdrawals->rollBack();
  241. return ResultWrapper::fail($this->objDCommissionBusinessman->error(),ErrorCode::$dberror);
  242. }
  243. $updateData = [
  244. 'auditStatus' => $status,
  245. 'updatetime' => time(),
  246. ];
  247. // 审核通过时间
  248. if($status == StatusCode::$auditStatus['auditIng']){
  249. $updateData['auditTime'] = time();
  250. }
  251. // 打款完成时间
  252. if($status == StatusCode::$auditStatus['auditIng']){
  253. $updateData['payTime'] = time();
  254. }
  255. $dbResult = $this->objDCommissionWithdrawals->update($updateData, $id);
  256. if ($dbResult === false) {
  257. $this->objDCommissionWithdrawals->rollBack();
  258. return ResultWrapper::fail($this->objDCommissionWithdrawals->error(), ErrorCode::$dberror);
  259. }
  260. $this->objDCommissionWithdrawals->commit();
  261. return ResultWrapper::success($dbResult);
  262. }
  263. }