FinanceCache.Class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * 首付款暂存
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/11/5
  7. * Time: 10:14
  8. */
  9. namespace JinDouYun\Cache;
  10. use http\Exception;
  11. use Mall\Framework\Factory;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use Mall\Framework\Core\ErrorCode;
  14. class FinanceCache
  15. {
  16. private $cache;
  17. protected $receivedReceiptTemporarySaveKey = 'receivedReceiptTemporarySave';//收款单暂存
  18. protected $paidReceiptTemporarySaveKey = 'paidReceiptTemporarySave';//付款单暂存
  19. protected $accountTransferTemporarySaveKey = 'accountTransferTemporarySave';//资金转账单暂存
  20. protected $salesOutReceiveKey = 'salesOutReceive';//销售出库生成应收,缓存应收单的id
  21. private $existPayEnterpriseKey = 'existPayEnterprise'; // 存在应付单的企业标识
  22. public function __construct()
  23. {
  24. $this->cache = Factory::cache('finance');
  25. }
  26. /**
  27. * 收款单暂存
  28. * @param $enterpriseId
  29. * @param $userCenterId
  30. * @param $data
  31. * @return ResultWrapper
  32. */
  33. public function saveReceivedReceipt($enterpriseId, $userCenterId, $data)
  34. {
  35. if( empty($data) ){
  36. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  37. }
  38. return $this->cache->hset($this->receivedReceiptTemporarySaveKey.'::'.$enterpriseId, $userCenterId, json_encode($data));
  39. }
  40. /**
  41. * 获取客户的收款单暂存数据
  42. * @param $enterpriseId
  43. * @param $userCenterId
  44. * @return boolean
  45. */
  46. public function getReceivedReceipt($enterpriseId, $userCenterId)
  47. {
  48. $receivedData = $this->cache->hget($this->receivedReceiptTemporarySaveKey.'::'.$enterpriseId, $userCenterId);
  49. return $receivedData ? json_decode($receivedData,true) : [];
  50. }
  51. /**
  52. * 删除客户的收款单暂存数据
  53. * @param $enterpriseId
  54. * @param $userCenterId
  55. */
  56. public function delReceivedReceipt($enterpriseId,$userCenterId)
  57. {
  58. $result = $this->cache->hdel($this->receivedReceiptTemporarySaveKey.'::'.$enterpriseId, $userCenterId);
  59. }
  60. /**
  61. * 付款单暂存
  62. * @param $enterpriseId
  63. * @param $userCenterId
  64. * @param $data
  65. * @return ResultWrapper
  66. */
  67. public function savePaidReceipt($enterpriseId, $userCenterId, $data)
  68. {
  69. if( empty($data) ){
  70. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  71. }
  72. return $this->cache->hset($this->paidReceiptTemporarySaveKey.'::'.$enterpriseId, $userCenterId, json_encode($data));
  73. }
  74. /**
  75. * 获取客户的付款单暂存数据
  76. * @param $enterpriseId
  77. * @param $userCenterId
  78. * @return boolean
  79. */
  80. public function getPaidReceipt($enterpriseId, $userCenterId)
  81. {
  82. $paidData = $this->cache->hget($this->paidReceiptTemporarySaveKey.'::'.$enterpriseId, $userCenterId);
  83. return $paidData ? json_decode($paidData,true) : [];
  84. }
  85. /**
  86. * 删除客户的付款单暂存数据
  87. * @param $enterpriseId
  88. * @param $userCenterId
  89. */
  90. public function delPaidReceipt($enterpriseId,$userCenterId)
  91. {
  92. $result = $this->cache->hdel($this->paidReceiptTemporarySaveKey.'::'.$enterpriseId, $userCenterId);
  93. }
  94. /**
  95. * 资金转账单暂存
  96. * @param $enterpriseId
  97. * @param $userCenterId
  98. * @param $data
  99. * @return ResultWrapper
  100. */
  101. public function saveAccountTransfer($enterpriseId, $userCenterId, $data)
  102. {
  103. if( empty($data) ){
  104. return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
  105. }
  106. return $this->cache->hset($this->accountTransferTemporarySaveKey.'::'.$enterpriseId, $userCenterId, json_encode($data));
  107. }
  108. /**
  109. * 获取资金转账单暂存数据
  110. * @param $enterpriseId
  111. * @param $userCenterId
  112. * @return boolean
  113. */
  114. public function getAccountTransfer($enterpriseId, $userCenterId)
  115. {
  116. $data = $this->cache->hget($this->accountTransferTemporarySaveKey.'::'.$enterpriseId, $userCenterId);
  117. return $data ? json_decode($data,true) : [];
  118. }
  119. /**
  120. * 删除资金转账单 暂存数据
  121. * @param $enterpriseId
  122. * @param $userCenterId
  123. */
  124. public function delAccountTransfer($enterpriseId,$userCenterId)
  125. {
  126. $result = $this->cache->hdel($this->accountTransferTemporarySaveKey.'::'.$enterpriseId, $userCenterId);
  127. }
  128. /**
  129. * 销售出库生成应收单,应收单自动审核 缓存应收单id和时间
  130. * @param $enterpriseId
  131. * @param $id
  132. * @param $createTime
  133. * @return array|mixed
  134. */
  135. public function saveSalesOutReceive($enterpriseId, $id, $createTime)
  136. {
  137. $this->cache->zadd($this->salesOutReceiveKey.'::'.$enterpriseId, $createTime, $id);
  138. }
  139. public function delSalesOutReceive($enterpriseId, $id)
  140. {
  141. $this->cache->zrem($this->salesOutReceiveKey.'::'.$enterpriseId, $id);
  142. }
  143. public function getAllSalesOutReceive() {
  144. $data = [];
  145. $keysList = $this->cache->keys($this->salesOutReceiveKey.'*');
  146. foreach ($keysList as $value) {
  147. $keyArr = explode('::', $value);
  148. $result = $this->cache->zrangebyscore($this->salesOutReceiveKey.'::'.$keyArr[2], 0, time(),array('withscores' => TRUE));
  149. if($result) {
  150. foreach ($result as $k => $v) {
  151. $data[$keyArr[2]][] = [
  152. 'id'=> $k,
  153. 'createTime'=>(int)$v
  154. ];
  155. }
  156. }
  157. }
  158. return $data;
  159. }
  160. /**
  161. * 缓存一下那个企业有应付单生成,便于计划任务进行自动切换企业,做自动审核
  162. */
  163. public function cacheExistPayEnterprise($enterpriseId)
  164. {
  165. return $this->cache->sadd($this->existPayEnterpriseKey, $enterpriseId);
  166. }
  167. /**
  168. * 获取一下有应付单的企业数据
  169. * @return array|boolean
  170. */
  171. public function getExistPayEnterprise()
  172. {
  173. return $this->cache->smembers($this->existPayEnterpriseKey);
  174. }
  175. /**
  176. * 把企业从缓存中删除
  177. */
  178. public function delExistPayEnterprise($enterpriseId)
  179. {
  180. return $this->cache->srem($this->existPayEnterpriseKey, $enterpriseId);
  181. }
  182. }