StoreFreightRules.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace addons\Store\common\models;
  3. use common\enums\StatusEnum;
  4. use common\enums\WhetherEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use Yii;
  7. use yii\helpers\Json;
  8. /**
  9. * This is the model class for table "qimall_addons_store_freight_rules".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城ID
  13. * @property int $store_id 门店id
  14. * @property string $name 规则名称
  15. * @property int $select_com 选择地区组件[0=多选地区,1=单选地区]
  16. * @property string $rule 规则详情
  17. * @property int $is_default 是否默认
  18. * @property int $status 状态【1启用 0禁用 -1删除】
  19. * @property int $type 计费方式【1=>按重计费、2=>按件计费】
  20. * @property int $created_at 创建时间
  21. * @property int $updated_at
  22. */
  23. class StoreFreightRules extends BaseActiveRecord
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%addons_store_freight_rules}}';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['mall_id', 'store_id', 'select_com', 'is_default', 'status', 'type', 'created_at', 'updated_at'], 'integer'],
  39. [['rule'], 'required'],
  40. [['rule'], 'string'],
  41. [['name'], 'string', 'max' => 65],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'mall_id' => '商城ID',
  52. 'store_id' => '门店id',
  53. 'name' => '规则名称',
  54. 'select_com' => '选择地区组件[0=多选地区,1=单选地区]',
  55. 'rule' => '规则详情',
  56. 'is_default' => '是否默认',
  57. 'status' => '状态【1启用 0禁用 -1删除】',
  58. 'type' => '计费方式【1=>按重计费、2=>按件计费】',
  59. 'created_at' => '创建时间',
  60. 'updated_at' => 'Updated At',
  61. ];
  62. }
  63. public static function setData(array $params){
  64. try{
  65. if(!empty($params['id'])){
  66. $model = self::findOne(['and',['id' => $params['id'],'store_id'=>$params['store_id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  67. if(empty($model)) throw new \Exception('规则存在或已删除');
  68. }else{
  69. $model = new self();
  70. $model->loadDefaultValues();
  71. $model->mall_id = $params['mall_id'];
  72. $model->store_id = $params['store_id'];
  73. }
  74. isset($params['name']) && $model->name = $params['name'];
  75. isset($params['select_com']) && $model->select_com = $params['select_com'];
  76. isset($params['rule']) && $model->rule = json_encode($params['rule'],JSON_UNESCAPED_UNICODE);
  77. if(isset($params['is_default']) && $params['is_default'] == WhetherEnum::ENABLED){
  78. self::updateAll(['is_default' => WhetherEnum::DISABLED], [
  79. 'mall_id' => $params['mall_id'],
  80. 'store_id' => $params['store_id']
  81. ]);
  82. $model->is_default = $params['is_default'];
  83. }
  84. isset($params['status']) && $model->status = $params['status'];
  85. isset($params['type']) && $model->type = $params['type'];
  86. $res = $model->save();
  87. if($res === false) throw new \Exception($model->getErrorMessage());
  88. return $model;
  89. }catch(\Exception $e){
  90. self::$static_error = $e->getMessage();
  91. return false;
  92. }
  93. }
  94. public static function getFreightRules(array $cond=[],array $with=[],bool $is_array=true,string $select='*'){
  95. $default_cond = [['<>','status',StatusEnum::DELETE]];
  96. $cond = array_merge($default_cond,$cond);
  97. $query = self::find()->select($select);
  98. self::paramPack(['where'=>$cond,'with'=>$with],$query);
  99. $cates = $query->asArray($is_array)->all();
  100. return $cates;
  101. }
  102. /**
  103. * 获取包邮规则的包邮地区id
  104. * @Author: lun
  105. * @DateTime: 2021/9/29 0029 9:26
  106. * @Copyright: copyright (c) 2021 广东七件事集团
  107. * @param $mall_id
  108. * @param $store_id
  109. * @return array
  110. */
  111. public static function getFreightRulesArea($mall_id, $store_id)
  112. {
  113. $list = self::getFreightRules([['mall_id' => $mall_id, 'store_id' => $store_id ?? 0]]);
  114. if (empty($list)) return [];
  115. $districtArr = [];
  116. foreach ($list as $item) {
  117. $districtArr[$item['id']] = [
  118. 'type' => $item['type'],// 计费方式【1=>按重计费、2=>按件计费
  119. 'rule' => json_decode($item['rule'], true),
  120. ];
  121. }
  122. return $districtArr;
  123. }
  124. }