xhGoodsSettingService.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace common\services;
  3. use common\components\util;
  4. use Yii;
  5. use common\components\stringUtil;
  6. use common\components\dict;
  7. use common\models\xhGoodsSetting;
  8. class xhGoodsSettingService {
  9. public static function getBySjId($sjId)
  10. {
  11. $user = xhGoodsSetting::find()->where(['sjId'=>$sjId])->asArray()->one();
  12. return $user;
  13. }
  14. public static function add($data,$sjId)
  15. {
  16. $return = xhGoodsSetting::add($data);
  17. return $return;
  18. }
  19. public static function updateBySjId($sjId,$data)
  20. {
  21. xhGoodsSetting::updateByCondition(['sjId'=>$sjId], $data);
  22. }
  23. /**
  24. * 将商品涨价
  25. */
  26. public static function unitySet($goods)
  27. {
  28. $sjId = $goods['sjId'];
  29. $set = self::getBySjId($sjId);
  30. $riseAmount = !empty($goods['riseAmount']) ? $goods['riseAmount'] : $set['riseAmount'];//单个商品有设置涨价金额则从商品里取
  31. $noRise = dict::getDict('riseType','noRise');
  32. $byPercent = dict::getDict('riseType','percent');
  33. $byAmount = dict::getDict('riseType','amount');
  34. if(!empty($set)){
  35. $goods['goodsIntroduce'] = $set['goodsIntroduce'];
  36. $goods['riseType'] = $noRise;
  37. $goods['riseAmount'] = $riseAmount;
  38. // if($set['riseType'] != $noRise){
  39. // $now = time();
  40. // if($now > $set['startRiseTime'] && $now < $set['endRiseTime']){
  41. // if($set['riseType'] == $byPercent){//百分比
  42. // $percent = $riseAmount / 100;
  43. // $goods['price'] = floor($goods['price']*(1+$percent));
  44. // }else{//金额
  45. // $goods['price'] = stringUtil::calcAdd($goods['price'], $riseAmount);
  46. // }
  47. // }
  48. // }
  49. }
  50. return $goods;
  51. }
  52. /**
  53. * 改写unitySet(将商品涨价)方法,传入价格参数,返回涨价
  54. */
  55. public static function changePrice($goods, $price)
  56. {
  57. $sjId = $goods['sjId'];
  58. $set = self::getBySjId($sjId);
  59. $riseAmount = !empty($goods['riseAmount']) ? $goods['riseAmount'] : $set['riseAmount'];//单个商品有设置涨价金额则从商品里取
  60. $noRise = dict::getDict('riseType','noRise');
  61. $byPercent = dict::getDict('riseType','percent');
  62. $byAmount = dict::getDict('riseType','amount');
  63. if(!empty($set)){
  64. $goods['goodsIntroduce'] = $set['goodsIntroduce'];
  65. $goods['riseType'] = $noRise;
  66. $goods['riseAmount'] = $riseAmount;
  67. if($set['riseType'] != $noRise){
  68. $now = time();
  69. if($now > $set['startRiseTime'] && $now < $set['endRiseTime']){
  70. if($set['riseType'] == $byPercent){//百分比
  71. $percent = $riseAmount / 100;
  72. $price = floor($price*(1+$percent));
  73. }else{//金额
  74. $price = stringUtil::calcAdd($price, $riseAmount);
  75. }
  76. }
  77. }
  78. }
  79. return $price;
  80. }
  81. }