| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Coupon;
- use app\models\Cat;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\ActivityRebateOrderN;
- use app\models\ActivityRebateOrderNLog;
- use yii\base\Model;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\SaasUser;
- use app\models\Order;
- use app\models\Attr;
- use app\models\AttrGroup;
- class ActivityRebateOrderNForm extends Model
- {
- public $store_id;
- public $saas_id;
- public $user_id;
- public $user_level_min;
- public $child_level_min;
- public $user_name;
- public $mobile;
- public $id;
- public $activity_id;
- public $ids;
- public $name;
- public $status;
- public $base_rebate;
- public $rebate_rules;
- public $order_no;
- public $goods_id;
- public $goods_name;
- public $is_delete;
- public $key;
- public $val;
- public function rules()
- {
- return [
- [['status', 'id'], 'integer'],
- [['ids', 'name'], 'string'],
- [['store_id', 'goods_id', 'goods_name', 'activity_id', 'saas_id', 'user_id', 'is_delete', 'total_price', 'base_rebate', 'rebate_rules', 'user_name', 'order_no', 'mobile'], 'safe'],
- [['key', 'val', 'user_level_min', 'child_level_min'], 'safe'],
- ];
- }
- public function init() {
- parent::init();
- if(empty($this->store_id)){
- $this->store_id = get_store_id();
- }
- }
- public function search ()
- {
- try {
- $is_delete = 0;
- if ($this->is_delete == 1) {
- $is_delete = 1;
- }
- $query = ActivityRebateOrderN::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]);
- if (!is_null($this->status) && $this->status > -1) {
- $query->andWhere(['status' => $this->status]);
- }
- if (!empty($this->goods_name)) {
- $query->andWhere(['goods_id' => Goods::find()->select('id')->where(['like', 'name', $this->goods_name])]);
- }
- $query->orderBy('id DESC');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['rebate_rules'] = ActivityRebateOrderN::decodeRules($item['rebate_rules']);
- $item['activity_goods'] = Goods::findOne($item['goods_id']);
- //格式化时间
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination,
- // 'q' => $query->createCommand()->getRawSql(),
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function getInfo()
- {
- try {
- $activity = ActivityRebateOrderN::find()->where(['id' => $this->id, 'store_id' => $this->store_id])->one();
- if ($activity) {
- $activity['rebate_rules'] = ActivityRebateOrderN::decodeRules($activity['rebate_rules']);
- $activity_goods = Goods::find()->where(['id' => $activity['goods_id']])->select('id,name,cover_pic')->one();
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'activity_goods' => $activity_goods ?? [],
- 'activity' => $activity ?: [],
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getFile() . $e->getLine()
- ];
- }
- }
- public function save ()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $activity = ActivityRebateOrderN::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
- if (empty($activity)) {
- $activity = new ActivityRebateOrderN();
- $activity->store_id = $this->store_id;
- }
- // $activity->name = $this->name;
- $activity->goods_id = $this->goods_id;
- $activity->status = (int)$this->status;
- $activity->base_rebate = $this->base_rebate;
- $rebate_rules = ActivityRebateOrderN::decodeRules($this->rebate_rules);
- $activity->rebate_rules = ActivityRebateOrderN::encodeRules($rebate_rules);
- $activity->total_price = $rebate_rules['total_price'];
- if(isset($this->user_level_min)){
- $activity->user_level_min = $this->user_level_min;
- }
- if(isset($this->child_level_min)){
- $activity->child_level_min = $this->child_level_min;
- }
- if (!$activity->save()) {
- \Yii::error([__METHOD__, $activity->attributes]);
- throw new \Exception(array_shift($activity->getFirstErrors()));
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function saveKey ()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $activity = ActivityRebateOrderN::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
- if (empty($activity)) {
- throw new \Exception('参数错误');
- }
- $key = $this->key;
- $activity->$key = $this->val;
-
- if (!$activity->save()) {
- \Yii::error([__METHOD__, $activity->attributes]);
- throw new \Exception(array_shift($activity->getFirstErrors()));
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function del ()
- {
- try {
- if ($this->ids) {
- $ids = explode(',', $this->ids);
- ActivityRebateOrderN::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]);
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
-
-
-
-
-
-
-
-
- public function logList() {
- $query = ActivityRebateOrderNLog::find()->where(['status' => 1, 'is_delete' => 0]);
- $this->store_id && $query->andWhere(['store_id' => $this->store_id]);
- $this->saas_id && $query->andWhere(['saas_id' => $this->saas_id]);
- $this->user_id && $query->andWhere(['user_id' => $this->user_id]);
- if($this->activity_id){
- $query->andWhere(['activity_id' => $this->activity_id]);
- }
- if($this->goods_name){
- $goods_query = Goods::find()->select('id')->where(['like', 'name', $this->goods_name]);
- $query->andWhere(['goods_id' => $goods_query]);
- }
- if($this->user_name){
- $user_query = SaasUser::find()->select('id')->where(['like', 'name', $this->user_name]);
- $query->andWhere(['saas_id' => $user_query]);
- }
- if($this->mobile){
- $user_query = SaasUser::find()->select('id')->where(['like', 'mobile', $this->mobile]);
- $query->andWhere(['saas_id' => $user_query]);
- }
- if($this->order_no){
- $order_id = (int)Order::findOne(['order_no' => $this->order_no])['id'];
- $query->andWhere('JSON_CONTAINS(pay_order_id, "'.$order_id.'")');
- }
- $query->orderBy('id desc');
- $res = pagination_make($query);
- foreach($res['list'] as &$item){
- $item['pay_order_no'] = Order::find()->where(['id' => (array)json_decode($item['pay_order_id'], true)])->select('order_no')->asArray()->column();
- $goods_info = Goods::find()->where(['id' => $item['goods_id']])->select('id,name,cover_pic')->one();
- // $goods_cat = GoodsCat::find()->alias('gc')
- // ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id')
- // ->where([ 'gc.goods_id' => $item['goods_id'] ])
- // ->select(['c.name'])
- // ->asArray()
- // ->all();
- $saasUser = SaasUser::findOne($item['saas_id']);
- $activity = ActivityRebateOrderN::findOne($item['activity_id']);
- $item['price_info'] = (array)ActivityRebateOrderN::decodeRules($item['price_info']);
- foreach($item['pay_order_no'] as &$i){
- if(!$item['price_info'] || !isset($item['price_info']['list'])){
- continue;
- }
- foreach ($item['price_info']['list'] as $k) {
- if(isset($k['user_price_order_no']) && (in_array($i, $k['user_price_order_no']))){
- $i .= '✔';
- }
- }
- }
- $item['goods_info'] = $goods_info;
- // $item['goods_cat'] = $goods_cat;
- $item['saasUser'] = $saasUser;
- $item['activity'] = $activity;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res,
- 'q' => $query->createCommand()->getRawSql(),
- ];
- }
-
- }
|