xhGoodsPriceService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace common\services;
  3. use common\models\xhGoods;
  4. use Yii;
  5. use common\models\xhGoodsPrice;
  6. use common\components\dict;
  7. use common\services\xhGoodsSettingService;
  8. class xhGoodsPriceService {
  9. public static function add($data, $multi=false)
  10. {
  11. if(!$multi){
  12. $goodsPrice = xhGoodsPrice::add($data);
  13. self::refreshById($goodsPrice['id']);
  14. return $goodsPrice;
  15. }else{
  16. $goodsPriceIds = '';
  17. foreach ($data as $item){
  18. $goodsPrice = xhGoodsPrice::add($item);
  19. self::refreshById($goodsPrice['id']);
  20. $goodsPriceIds = $goodsPriceIds . ',' . $goodsPrice['id'];
  21. }
  22. $goodsPriceIds = ltrim($goodsPriceIds, ',');
  23. return $goodsPriceIds;
  24. }
  25. }
  26. public static function updateById($id, $data){
  27. xhGoodsPrice::updateById($id, $data);
  28. self::refreshById($id);
  29. }
  30. /**
  31. * 重用软删除的表数据
  32. * @param $data 商品价格数据(可多个)
  33. * @param int $num 当商品价格数据为多个时(即二维数组),$num = count($data);
  34. */
  35. public static function reuse($data, $num = 1){
  36. $count = xhGoodsPrice::getCount(['softDel' => 1]);
  37. if($count >= $num){
  38. if($num == 1){
  39. if(count($data) != count($data,1)){
  40. $data = $data[0];
  41. }
  42. $dbData = xhGoodsPrice::getByCondition(['softDel' => 1]);
  43. $data['softDel'] = 0;//此字段也要更新,勿忘
  44. $re = xhGoodsPrice::updateById($dbData['id'], $data);
  45. if(!$re){
  46. Yii::warning('xhGoodsPriceService reuseSoftDel fail!');
  47. return false;
  48. }
  49. self::refreshById($dbData['id']);
  50. return $dbData['id'];
  51. }else{
  52. $idStr = '';
  53. foreach($data as $item){
  54. $item['softDel'] = 0;//此字段也要更新,勿忘
  55. $dbData = xhGoodsPrice::getByCondition(['softDel' => 1]);
  56. $re = xhGoodsPrice::updateById($dbData['id'], $item);
  57. if(!$re){
  58. Yii::warning('xhGoodsPriceService reuseSoftDel fail:2!');
  59. return false;
  60. }
  61. self::refreshById($dbData['id']);
  62. $idStr .= $dbData['id'].',';
  63. }
  64. return rtrim($idStr, ',');
  65. }
  66. }else{
  67. if($num == 1){
  68. if(count($data) != count($data,1)){
  69. $data = $data[0];
  70. }
  71. $re = self::add($data);
  72. if(!$re){
  73. throw new \Exception('xhGoodsPrice 保存数据失败');
  74. }
  75. return $re['id'];
  76. }else{
  77. return self::add($data, true);
  78. }
  79. }
  80. }
  81. public static function goodsMultiPriceAdd($goodTitle, $multiPrice, $multiImg){
  82. $data = [];
  83. foreach($goodTitle as $key => $title){
  84. $data[$key] = ['title' => $title, 'picture' => $multiImg[$key], 'price' => $multiPrice[$key], 'order' => 0];
  85. }
  86. //$idStr = self::add($data, true);
  87. $idStr = self::reuse($data, count($data));
  88. return $idStr;
  89. }
  90. public static function goodsMultiPriceUpdate($ids, $goodTitle, $multiPrice, $multiImg){
  91. foreach($goodTitle as $key => $title){
  92. $data = ['title' => $title, 'picture' => $multiImg[$key], 'price' => $multiPrice[$key], 'order' => 0];
  93. $id = $ids[$key];
  94. self::updateById($id, $data);
  95. }
  96. }
  97. /**
  98. * 更新商品的多种价格字段,并返回新ids字符串
  99. * @param $beforeIdArr
  100. * @param $post
  101. * @return bool|string
  102. */
  103. public static function updateMultiPrice($beforeIdArr, $post){
  104. $nowIdArr = $post['priceId'];
  105. $goodTitle = $post['goodTitle'];
  106. $multiPrice = $post['multiPrice'];
  107. $multiImg = $post['multiImg'];
  108. if(empty($beforeIdArr)){//未设置多价格
  109. $idStr = self::goodsMultiPriceAdd($goodTitle, $multiPrice, $multiImg);
  110. return $idStr;
  111. }else{
  112. $stillInIdArr = [];
  113. $notInIdArr = [];
  114. $newIdStr = '';
  115. $oldIdStr = '';
  116. $idStr = '';
  117. foreach($beforeIdArr as $key => $id){
  118. if(in_array($id, $nowIdArr)){
  119. $stillInIdArr[] = $id;
  120. }else{
  121. $notInIdArr[] = $id;
  122. }
  123. }
  124. //清未加入的Ids
  125. if(!empty($notInIdArr)){
  126. $notInIdStr = implode(',', $notInIdArr);
  127. self::softDeleteIds($notInIdStr);
  128. }
  129. $count = count($nowIdArr);
  130. $newGoodTitle = array_slice($goodTitle, $count);
  131. $newMultiPrice = array_slice($multiPrice, $count);
  132. if(!empty($newGoodTitle) && !empty($newMultiPrice)){
  133. $newIdStr = self::goodsMultiPriceAdd($newGoodTitle, $newMultiPrice, $multiImg);
  134. }
  135. if(!empty($stillInIdArr)){
  136. //更新
  137. $oldGoodTitle = array_slice($goodTitle, 0, $count);
  138. $oldMultiPrice = array_slice($multiPrice, 0, $count);
  139. if(!empty($oldGoodTitle) && !empty($oldMultiPrice)){
  140. $oldIdStr = self::goodsMultiPriceUpdate($nowIdArr, $oldGoodTitle, $oldMultiPrice, $multiImg);
  141. }
  142. $idStr = implode(',', $stillInIdArr) . ($newIdStr == '' ? '' : ','.$newIdStr);
  143. }
  144. return (string)$idStr;
  145. }
  146. }
  147. /**
  148. * @param $id
  149. * @param int $goodsId 商品ID (当xhGoodsSettingService::changePrice($goods, $goodPrice['price'])不好取商品详情$goods时,传入商品ID)
  150. * @return array|null|\yii\db\ActiveRecord
  151. */
  152. public static function getById($id, $goodsId = 0)
  153. {
  154. $preKey = dict::getCacheKey('xhGoodsPrice');
  155. $cacheKey = $preKey.$id;
  156. $cacheData = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  157. if(!empty($cacheData)){
  158. $arr = [];
  159. $i = 0;
  160. $x = 0;
  161. foreach($cacheData as $key => $val){
  162. $i++;
  163. if($i%2 == 0){
  164. $arr[$x]['value'] = $val;
  165. $x++;
  166. }else{
  167. $arr[$x]['key'] = $val;
  168. }
  169. }
  170. $goodsPriceArr = [];
  171. foreach($arr as $key => $val){
  172. $goodsPriceArr[$val['key']] = $val['value'];
  173. }
  174. unset($goodsPriceArr['info_already_get_by_sql']);
  175. //设置涨价
  176. if($goodsId > 0){
  177. $goods = xhGoodsService::getById($goodsId);
  178. $goodsPriceArr['price'] = xhGoodsSettingService::changePrice($goods, $goodsPriceArr['price']);//统一对商品进行价格等方面设置
  179. }
  180. return $goodsPriceArr;
  181. }
  182. $goodsPrice = xhGoodsPrice::find()->where(['id' => $id])->asArray()->one();
  183. $params = [$cacheKey];
  184. $params[] = 'info_already_get_by_sql';
  185. $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
  186. if(!empty($goodsPrice)){
  187. foreach($goodsPrice as $key => $val){
  188. $params[] = $key;
  189. $params[] = $val;
  190. }
  191. }
  192. Yii::$app->redis->executeCommand('HMSET',$params);//将xhGoods信息放入缓存
  193. //设置涨价
  194. if($goodsId > 0) {
  195. $goods = xhGoodsService::getById($goodsId);
  196. $goodsPrice['price'] = xhGoodsSettingService::changePrice($goods, $goodsPrice['price']);//统一对商品进行价格等方面设置
  197. }
  198. return $goodsPrice;
  199. }
  200. /**
  201. * 传入id号(数组 或 字符串(例:1,2,3))
  202. * @param $ids
  203. * @return array
  204. */
  205. public static function getByIds($ids, $goodsId = 0){
  206. $data = [];
  207. if(is_string($ids)){
  208. $ids = explode(',', $ids);
  209. }
  210. if(!is_array($ids)){
  211. throw new \Exception('传入的参数不是数组');
  212. }
  213. foreach ($ids as $id){
  214. if(empty($id)) continue;
  215. $data[] = self::getById($id, $goodsId);
  216. }
  217. return $data;
  218. }
  219. public static function deleteById($id)
  220. {
  221. xhGoodsPrice::deleteById($id);
  222. $cacheKey = dict::getCacheKey('xhGoodsPrice').$id;
  223. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  224. }
  225. public static function refreshById($id)
  226. {
  227. $cacheKey = dict::getCacheKey('xhGoodsPrice').$id;
  228. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  229. self::getById($id);
  230. }
  231. /**
  232. * 软删除,并清缓存(表字段:softDel标记为1)
  233. * @param $id ID
  234. */
  235. public static function softDelete($id){
  236. self::updateById($id, ['softDel' => 1]);
  237. }
  238. /**
  239. * 软删除多个,并清缓存(表字段:softDel标记为1)
  240. * @param $id ID
  241. */
  242. public static function softDeleteIds($ids){
  243. if(is_string($ids)){
  244. $ids = explode(',', $ids);
  245. }
  246. if(!is_array($ids)){
  247. throw new \Exception('传入的参数不是数组');
  248. }
  249. foreach ($ids as $id){
  250. self::updateById($id, ['softDel' => 1]);
  251. }
  252. }
  253. /**
  254. * 通过id清价格缓存
  255. * @param $id
  256. */
  257. /*public static function cleanById($id){
  258. $cacheKey = dict::getCacheKey('xhGoodsPrice').$id;
  259. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  260. }*/
  261. /**
  262. * 通过多个id 清多个价格缓存
  263. * @param $ids
  264. * @throws \Exception
  265. */
  266. /*public static function cleanByIds($ids){
  267. if(is_string($ids)){
  268. $ids = explode(',', $ids);
  269. }
  270. if(!is_array($ids)){
  271. throw new \Exception('传入的参数不是数组');
  272. }
  273. foreach ($ids as $id){
  274. $cacheKey = dict::getCacheKey('xhGoodsPrice').$id;
  275. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  276. }
  277. }*/
  278. }