PauseSave.Class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * 暂存Controller
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2020/03/30
  7. * Time: 16:30
  8. */
  9. namespace JinDouYun\Controller\Common;
  10. use Exception;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use JinDouYun\Controller\BaseController;
  14. use JinDouYun\Cache\PauseSave as CachePauseSave;
  15. class PauseSave extends BaseController
  16. {
  17. private $objCachePauseSave;
  18. public function __construct($isCheckAcl = true, $isMustLogin = true)
  19. {
  20. parent::__construct($isCheckAcl, $isMustLogin);
  21. $this->objCachePauseSave = new CachePauseSave($this->onlineEnterpriseId,$this->onlineUserId);
  22. }
  23. /**
  24. * 暂存添加
  25. */
  26. public function addPauseSave()
  27. {
  28. //获取数据
  29. $params = $this->request->getRawJson();
  30. if(empty($params)){
  31. parent::sendOutput('数据为空', ErrorCode::$paramError);
  32. }
  33. $key = isset($params['key']) ? $params['key'] : '';
  34. if(empty($key)){
  35. parent::sendOutput('参数错误', ErrorCode::$paramError);
  36. }
  37. $data = isset($params['data']) ? $params['data'] : [];
  38. if(empty($data)){
  39. parent::sendOutput('暂存数据为空', ErrorCode::$paramError);
  40. }
  41. //查询是否已有暂存
  42. //不用覆盖的原因是覆盖返回值是0
  43. $cacheResult = $this->objCachePauseSave->getCacheHash($key);
  44. if($cacheResult){
  45. //如果已有暂存 删除之前的暂存数据
  46. $delResult = $this->objCachePauseSave->delCacheHash($key);
  47. if(!$delResult){
  48. parent::sendOutput('删除缓存失败', ErrorCode::$paramError);
  49. }
  50. }
  51. //保存新的暂存
  52. $saveResult = $this->objCachePauseSave->addCacheHash($key, $data);
  53. if(!$saveResult){
  54. parent::sendOutput('缓存失败', ErrorCode::$paramError);
  55. }
  56. parent::sendOutput($saveResult);
  57. }
  58. /**
  59. * 暂存获取
  60. */
  61. public function getPauseSave()
  62. {
  63. $params = $this->request->getRawJson();
  64. $key = isset($params['key']) ? $params['key'] : '';
  65. if(empty($key)){
  66. parent::sendOutput('参数错误', ErrorCode::$paramError);
  67. }
  68. $cacheResult = $this->objCachePauseSave->getCacheHash($key);
  69. parent::sendOutput($cacheResult);
  70. }
  71. /**
  72. * 暂存删除
  73. */
  74. public function delPauseSave()
  75. {
  76. $params = $this->request->getRawJson();
  77. $key = isset($params['key']) ? $params['key'] : '';
  78. if(empty($key)){
  79. parent::sendOutput('参数错误', ErrorCode::$paramError);
  80. }
  81. $cacheResult = $this->objCachePauseSave->delCacheHash($key);
  82. parent::sendOutput($cacheResult);
  83. }
  84. /**************************************************** 价格暂存 ******************************************************/
  85. /**
  86. * 暂存添加
  87. * @throws Exception
  88. */
  89. public function addMoneyPauseSave()
  90. {
  91. //获取数据
  92. $params = $this->request->getRawJson();
  93. if(empty($params)){
  94. parent::sendOutput('数据为空', ErrorCode::$paramError);
  95. }
  96. $data = isset($params['data']) ? $params['data'] : [];
  97. if(empty($data)){
  98. parent::sendOutput('暂存数据为空', ErrorCode::$paramError);
  99. }
  100. $cacheData = [];
  101. foreach($data as $value){
  102. $cacheData[$value['skuId']] = sprintf("%.2f",substr(sprintf("%.3f", $value['money']), 0, -2));
  103. }
  104. //保存新的暂存
  105. $saveResult = $this->objCachePauseSave->addMoneyCache($cacheData);
  106. if(!$saveResult){
  107. parent::sendOutput('缓存失败', ErrorCode::$paramError);
  108. }
  109. parent::sendOutput($saveResult);
  110. }
  111. /**
  112. * 暂存获取
  113. */
  114. public function getMoneyPauseSave()
  115. {
  116. $params = $this->request->getRawJson();
  117. if(empty($params)){
  118. parent::sendOutput('参数为空', ErrorCode::$paramError);
  119. }
  120. $array = isset($params['skuIds']) ? $params['skuIds'] : [];
  121. if(empty($array)){
  122. parent::sendOutput('参数错误', ErrorCode::$paramError);
  123. }
  124. $returnData = [];
  125. foreach($array as $skuId){
  126. $cacheResult = $this->objCachePauseSave->getMoneyCache($skuId);
  127. if($cacheResult){
  128. $returnData[$skuId] = sprintf("%.2f",substr(sprintf("%.3f", $cacheResult), 0, -2));
  129. }else{
  130. $returnData[$skuId] = 0.00;
  131. }
  132. }
  133. parent::sendOutput($returnData);
  134. }
  135. }