FreightRules.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace common\models\goods;
  3. use common\enums\StatusEnum;
  4. use common\enums\WhetherEnum;
  5. use Yii;
  6. use yii\helpers\Json;
  7. /**
  8. * This is the model class for table "{{%freight_rules}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property int $mch_id 商户ID
  13. * @property string $name 规则名称
  14. * @property int $select_com 选择地区组件[0=多选地区,1=单选地区]
  15. * @property string $rule 规则详情
  16. * @property int $is_default 是否默认
  17. * @property int $status 状态【1启用 0禁用 -1删除】
  18. * @property int $type 计费方式【1=>按重计费、2=>按件计费】
  19. * @property int $compute_type 计算方式[1=按不同商品计算,2=按同件商品计算]
  20. * @property int $created_at 创建时间
  21. * @property int $updated_at 更新时间
  22. */
  23. class FreightRules extends \common\models\base\BaseActiveRecord
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%freight_rules}}';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['mall_id', 'mch_id', 'select_com', 'is_default', 'status', 'type', 'compute_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. 'mch_id' => '商户ID',
  53. 'name' => '规则名称',
  54. 'select_com' => '选择地区组件[0=多选地区,1=单选地区]',
  55. 'rule' => '规则详情',
  56. 'is_default' => '是否默认',
  57. 'status' => '状态【1启用 0禁用 -1删除】',
  58. 'type' => '计费方式【1=>按重计费、2=>按件计费】',
  59. 'compute_type' => '计算方式[1=按不同商品计算,2=按同件商品计算]',
  60. 'created_at' => '创建时间',
  61. 'updated_at' => '更新时间',
  62. ];
  63. }
  64. /**
  65. * 保存数据
  66. * @Author bing
  67. * @DateTime 2021-08-26 14:16:24
  68. * @copyright: Copyright (c) 2020 广东七件事集团
  69. * @param array $params
  70. * @return object|boolean
  71. */
  72. public static function setData(array $params){
  73. try{
  74. if(!empty($params['id'])){
  75. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  76. if(empty($model)) throw new \Exception('规则存在或已删除');
  77. }else{
  78. $model = new self();
  79. $model->loadDefaultValues();
  80. $model->mall_id = $params['mall_id'];
  81. $model->mch_id = $params['mch_id'];
  82. }
  83. isset($params['name']) && $model->name = $params['name'];
  84. isset($params['select_com']) && $model->select_com = $params['select_com'];
  85. isset($params['rule']) && $model->rule = json_encode($params['rule'],JSON_UNESCAPED_UNICODE);
  86. if(isset($params['is_default']) && $params['is_default'] == WhetherEnum::ENABLED){
  87. self::updateAll(['is_default' => WhetherEnum::DISABLED], [
  88. 'mall_id' => $params['mall_id'],
  89. 'mch_id' => $params['mch_id']
  90. ]);
  91. $model->is_default = $params['is_default'];
  92. }
  93. isset($params['status']) && $model->status = $params['status'];
  94. isset($params['type']) && $model->type = $params['type'];
  95. isset($params['compute_type']) && $model->compute_type = $params['compute_type'];
  96. $res = $model->save();
  97. if($res === false) throw new \Exception($model->getErrorMessage());
  98. return $model;
  99. }catch(\Exception $e){
  100. self::$static_error = $e->getMessage();
  101. return false;
  102. }
  103. }
  104. /**
  105. * 获取运费模板列表
  106. * @Author bing
  107. * @DateTime 2021-08-26 13:00:11
  108. * @copyright: Copyright (c) 2020 广东七件事集团
  109. * @param array $cond
  110. * @param array $with
  111. * @param integer $is_array
  112. * @return array|null
  113. */
  114. public static function getFreightRules(array $cond=[],array $with=[],bool $is_array=true,string $select='*'){
  115. $default_cond = [['<>','status',StatusEnum::DELETE]];
  116. $cond = array_merge($default_cond,$cond);
  117. $query = self::find()->select($select);
  118. self::paramPack(['where'=>$cond,'with'=>$with],$query);
  119. $cates = $query->asArray($is_array)->all();
  120. return $cates;
  121. }
  122. /**
  123. * 获取包邮规则的包邮地区id
  124. * @Author: lun
  125. * @DateTime: 2021/9/29 0029 9:26
  126. * @Copyright: copyright (c) 2021 广东七件事集团
  127. * @param $mall_id
  128. * @return array
  129. */
  130. public static function getFreightRulesArea($mall_id, $mch_id)
  131. {
  132. $list = self::getFreightRules([['mall_id' => $mall_id, 'mch_id' => $mch_id ?? 0]]);
  133. if (empty($list)) return [];
  134. $districtArr = [];
  135. foreach ($list as $item) {
  136. $districtArr[$item['id']] = [
  137. 'id' => $item['id'],
  138. 'type' => $item['type'],// 计费方式【1=>按重计费、2=>按件计费】
  139. 'compute_type' => $item['compute_type'],// 计算方式【1=>按不同商品计算、2=>按同件商品计算】
  140. 'rule' => Json::decode($item['rule'], true),
  141. ];
  142. }
  143. return $districtArr;
  144. }
  145. }