SettingService.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace addons\QiDingDong\common\service;
  3. use addons\QiDingDong\common\enums\QiDingDongEnums;
  4. use common\enums\AddonsEnum;
  5. use common\models\mall\Mall;
  6. use common\models\mall\MallAddons;
  7. use yii\helpers\Json;
  8. class SettingService
  9. {
  10. /**
  11. * 获取配置信息
  12. * @param int $mall_id
  13. * @return array
  14. */
  15. public function get(int $mall_id) :array
  16. {
  17. return \Yii::$app->services->setting->addons->get($mall_id, QiDingDongEnums::ADDONS_NAME, 'basic') ?? [];
  18. }
  19. /**
  20. * 设置配置信息
  21. * @param int $mall_id
  22. * @param array $data
  23. * @return bool
  24. */
  25. public function save(int $mall_id, array $data) :bool
  26. {
  27. return \Yii::$app->services->setting->addons->set($mall_id, QiDingDongEnums::ADDONS_NAME, ['basic' => $data]);
  28. }
  29. public static function getPrices(int $mall_id)
  30. {
  31. $price = \Yii::$app->services->setting->addons->get($mall_id, QiDingDongEnums::ADDONS_NAME, 'basic.price') ?? '[]';
  32. return Json::decode($price);
  33. }
  34. const PRICE = 'price';
  35. const COST_PRICE = 'cost_price';
  36. const ORIGINAL_PRICE = 'original_price';
  37. /**
  38. * 获取价格
  39. * @param int $mall_id
  40. * @param string $field
  41. * @param array $third_prices
  42. * @param array|null $prices
  43. * @param float $multi
  44. * @return string
  45. */
  46. public static function getPrice(int $mall_id, string $field, array $third_prices, array $prices = null, float $multi = 1)
  47. {
  48. $prices === null && self::getPrices($mall_id);
  49. $tmp = $prices[$field]; // type, rate
  50. return bcmul(bcmul(bcmul(bcmul($third_prices[$tmp['type']], $tmp['rate'][$tmp['type']], 2), 0.01, 2), $multi, 2),
  51. MallService::rate($mall_id), 2);
  52. }
  53. public static function basicPrice(int $mall_id, string $price)
  54. {
  55. return bcmul(MallService::rate($mall_id), $price, 2);
  56. }
  57. /**
  58. * @return mixed|null
  59. */
  60. public static function getMasterMall()
  61. {
  62. foreach (Mall::getAllMallIds() as $mallId) {
  63. if (MallAddons::isInstall($mallId, AddonsEnum::ADDONS_NAME_QI_DING_DONG)) {
  64. $config = (new SettingService())->get($mallId);
  65. if (!empty($config['is_default'])) {
  66. return $mallId;
  67. }
  68. }
  69. }
  70. return null;
  71. }
  72. /**
  73. * 判断是否为主商城
  74. * @param int $mall_id
  75. * @return bool
  76. */
  77. public static function isMasterMall(int $mall_id)
  78. {
  79. return $mall_id == self::getMasterMall();
  80. }
  81. }