StoreBossAgentSetting.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace addons\Store\common\models;
  3. use Yii;
  4. /**
  5. * This is the model class for table "{{%addons_store_boss_agent_setting}}".
  6. *
  7. * @property int $id
  8. * @property int $mall_id
  9. * @property int $store_id
  10. * @property string $key
  11. * @property string $value
  12. * @property int $created_at 创建时间
  13. * @property int $updated_at 修改时间
  14. * @property int $is_delete 是否删除 0--未删除 1--已删除
  15. * @property int $deleted_at 删除时间
  16. */
  17. class StoreBossAgentSetting extends \common\models\base\BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return '{{%addons_store_boss_agent_setting}}';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['mall_id', 'key', 'value'], 'required'],
  33. [['mall_id', 'store_id', 'created_at', 'updated_at', 'is_delete', 'deleted_at'], 'integer'],
  34. [['value'], 'string'],
  35. [['key'], 'string', 'max' => 255],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => 'ID',
  45. 'mall_id' => 'Mall ID',
  46. 'store_id' => 'Store ID',
  47. 'key' => 'Key',
  48. 'value' => 'Value',
  49. 'created_at' => 'Created At',
  50. 'updated_at' => 'Updated At',
  51. 'is_delete' => 'Is Delete',
  52. 'deleted_at' => 'Deleted At',
  53. ];
  54. }
  55. public static function strToNumber($key, $str)
  56. {
  57. $default = ['is_enable', 'compute_period', 'compute_type', 'settlement_method', 'settlement_period'];
  58. if (in_array($key, $default)) {
  59. return round($str, 2);
  60. }
  61. return $str;
  62. }
  63. public static function setData($params)
  64. {
  65. $t = \Yii::$app->db->beginTransaction();
  66. try {
  67. $store = \Yii::$app->session->get('o2o');
  68. if(!$store){
  69. $admin = \Yii::$app->store_user->identity;
  70. $store = Store::getOne([['id' => $admin['store_id'],'mall_id' =>$admin['mall_id']]],true);
  71. }
  72. $setting_list = [];
  73. foreach ($params as $index => $item) {
  74. $setting_list[] = [
  75. 'key' => $index,
  76. 'value' => $item
  77. ];
  78. }
  79. foreach ($setting_list as $item) {
  80. $setting = self::findOne(['mall_id' =>$store['mall_id'],'store_id' => $store['id'], 'key' => $item['key']]);
  81. if (!$setting) {
  82. $setting = new self();
  83. }
  84. $setting->key = $item['key'];
  85. $setting->value = $item['value'];
  86. $setting->mall_id = $store['mall_id'];
  87. $setting->store_id = $store['id'];
  88. $setting->is_delete = 0;
  89. if (!$setting->save()) throw new \Exception($setting->getErrorMessage());
  90. }
  91. $t->commit();
  92. return true;
  93. } catch (\Exception $e) {
  94. $t->rollBack();
  95. self::$static_error = $e->getMessage();
  96. return false;
  97. }
  98. }
  99. public static function search()
  100. {
  101. $store = \Yii::$app->session->get('o2o');
  102. if(!$store){
  103. $admin = \Yii::$app->store_user->identity;
  104. $store = Store::getOne([['id' => $admin['store_id'],'mall_id' =>$admin['mall_id']]],true);
  105. }
  106. $list = self::find()->where(['mall_id' =>$store['mall_id'],'store_id' => $store['id']
  107. , 'is_delete' => 0])->asArray()->all();
  108. $newItem = [];
  109. foreach ($list as $item) {
  110. $newItem[$item['key']] = self::strToNumber($item['key'], $item['value']);
  111. }
  112. return $newItem;
  113. }
  114. }