FreeShippingRules.php 4.2 KB

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