ActivityRebateOrderNForm.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Coupon;
  9. use app\models\Cat;
  10. use app\models\Goods;
  11. use app\models\GoodsCat;
  12. use app\models\ActivityRebateOrderN;
  13. use app\models\ActivityRebateOrderNLog;
  14. use yii\base\Model;
  15. use app\models\Option;
  16. use app\constants\OptionSetting;
  17. use app\models\SaasUser;
  18. use app\models\Order;
  19. use app\models\Attr;
  20. use app\models\AttrGroup;
  21. class ActivityRebateOrderNForm extends Model
  22. {
  23. public $store_id;
  24. public $saas_id;
  25. public $user_id;
  26. public $user_level_min;
  27. public $child_level_min;
  28. public $user_name;
  29. public $mobile;
  30. public $id;
  31. public $activity_id;
  32. public $ids;
  33. public $name;
  34. public $status;
  35. public $base_rebate;
  36. public $rebate_rules;
  37. public $order_no;
  38. public $goods_id;
  39. public $goods_name;
  40. public $is_delete;
  41. public $key;
  42. public $val;
  43. public function rules()
  44. {
  45. return [
  46. [['status', 'id'], 'integer'],
  47. [['ids', 'name'], 'string'],
  48. [['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'],
  49. [['key', 'val', 'user_level_min', 'child_level_min'], 'safe'],
  50. ];
  51. }
  52. public function init() {
  53. parent::init();
  54. if(empty($this->store_id)){
  55. $this->store_id = get_store_id();
  56. }
  57. }
  58. public function search ()
  59. {
  60. try {
  61. $is_delete = 0;
  62. if ($this->is_delete == 1) {
  63. $is_delete = 1;
  64. }
  65. $query = ActivityRebateOrderN::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]);
  66. if (!is_null($this->status) && $this->status > -1) {
  67. $query->andWhere(['status' => $this->status]);
  68. }
  69. if (!empty($this->goods_name)) {
  70. $query->andWhere(['goods_id' => Goods::find()->select('id')->where(['like', 'name', $this->goods_name])]);
  71. }
  72. $query->orderBy('id DESC');
  73. $pagination = pagination_make($query);
  74. foreach ($pagination['list'] as &$item) {
  75. $item['rebate_rules'] = ActivityRebateOrderN::decodeRules($item['rebate_rules']);
  76. $item['activity_goods'] = Goods::findOne($item['goods_id']);
  77. //格式化时间
  78. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  79. $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
  80. }
  81. return [
  82. 'code' => 0,
  83. 'msg' => 'success',
  84. 'data' => $pagination,
  85. // 'q' => $query->createCommand()->getRawSql(),
  86. ];
  87. } catch (\Exception $e) {
  88. return [
  89. 'code' => 1,
  90. 'msg' => $e->getMessage()
  91. ];
  92. }
  93. }
  94. public function getInfo()
  95. {
  96. try {
  97. $activity = ActivityRebateOrderN::find()->where(['id' => $this->id, 'store_id' => $this->store_id])->one();
  98. if ($activity) {
  99. $activity['rebate_rules'] = ActivityRebateOrderN::decodeRules($activity['rebate_rules']);
  100. $activity_goods = Goods::find()->where(['id' => $activity['goods_id']])->select('id,name,cover_pic')->one();
  101. }
  102. return [
  103. 'code' => 0,
  104. 'msg' => '获取成功',
  105. 'data' => [
  106. 'activity_goods' => $activity_goods ?? [],
  107. 'activity' => $activity ?: [],
  108. ]
  109. ];
  110. } catch (\Exception $e) {
  111. return [
  112. 'code' => 1,
  113. 'msg' => $e->getMessage() . $e->getFile() . $e->getLine()
  114. ];
  115. }
  116. }
  117. public function save ()
  118. {
  119. $t = \Yii::$app->db->beginTransaction();
  120. try {
  121. $activity = ActivityRebateOrderN::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
  122. if (empty($activity)) {
  123. $activity = new ActivityRebateOrderN();
  124. $activity->store_id = $this->store_id;
  125. }
  126. // $activity->name = $this->name;
  127. $activity->goods_id = $this->goods_id;
  128. $activity->status = (int)$this->status;
  129. $activity->base_rebate = $this->base_rebate;
  130. $rebate_rules = ActivityRebateOrderN::decodeRules($this->rebate_rules);
  131. $activity->rebate_rules = ActivityRebateOrderN::encodeRules($rebate_rules);
  132. $activity->total_price = $rebate_rules['total_price'];
  133. if(isset($this->user_level_min)){
  134. $activity->user_level_min = $this->user_level_min;
  135. }
  136. if(isset($this->child_level_min)){
  137. $activity->child_level_min = $this->child_level_min;
  138. }
  139. if (!$activity->save()) {
  140. \Yii::error([__METHOD__, $activity->attributes]);
  141. throw new \Exception(array_shift($activity->getFirstErrors()));
  142. }
  143. $t->commit();
  144. return [
  145. 'code' => 0,
  146. 'msg' => '操作成功!'
  147. ];
  148. } catch (\Exception $e) {
  149. $t->rollBack();
  150. return [
  151. 'code' => 1,
  152. 'msg' => $e->getMessage()
  153. ];
  154. }
  155. }
  156. public function saveKey ()
  157. {
  158. $t = \Yii::$app->db->beginTransaction();
  159. try {
  160. $activity = ActivityRebateOrderN::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
  161. if (empty($activity)) {
  162. throw new \Exception('参数错误');
  163. }
  164. $key = $this->key;
  165. $activity->$key = $this->val;
  166. if (!$activity->save()) {
  167. \Yii::error([__METHOD__, $activity->attributes]);
  168. throw new \Exception(array_shift($activity->getFirstErrors()));
  169. }
  170. $t->commit();
  171. return [
  172. 'code' => 0,
  173. 'msg' => '操作成功!'
  174. ];
  175. } catch (\Exception $e) {
  176. $t->rollBack();
  177. return [
  178. 'code' => 1,
  179. 'msg' => $e->getMessage()
  180. ];
  181. }
  182. }
  183. public function del ()
  184. {
  185. try {
  186. if ($this->ids) {
  187. $ids = explode(',', $this->ids);
  188. ActivityRebateOrderN::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]);
  189. }
  190. return [
  191. 'code' => 0,
  192. 'msg' => '操作成功!'
  193. ];
  194. } catch (\Exception $e) {
  195. return [
  196. 'code' => 1,
  197. 'msg' => $e->getMessage()
  198. ];
  199. }
  200. }
  201. public function logList() {
  202. $query = ActivityRebateOrderNLog::find()->where(['status' => 1, 'is_delete' => 0]);
  203. $this->store_id && $query->andWhere(['store_id' => $this->store_id]);
  204. $this->saas_id && $query->andWhere(['saas_id' => $this->saas_id]);
  205. $this->user_id && $query->andWhere(['user_id' => $this->user_id]);
  206. if($this->activity_id){
  207. $query->andWhere(['activity_id' => $this->activity_id]);
  208. }
  209. if($this->goods_name){
  210. $goods_query = Goods::find()->select('id')->where(['like', 'name', $this->goods_name]);
  211. $query->andWhere(['goods_id' => $goods_query]);
  212. }
  213. if($this->user_name){
  214. $user_query = SaasUser::find()->select('id')->where(['like', 'name', $this->user_name]);
  215. $query->andWhere(['saas_id' => $user_query]);
  216. }
  217. if($this->mobile){
  218. $user_query = SaasUser::find()->select('id')->where(['like', 'mobile', $this->mobile]);
  219. $query->andWhere(['saas_id' => $user_query]);
  220. }
  221. if($this->order_no){
  222. $order_id = (int)Order::findOne(['order_no' => $this->order_no])['id'];
  223. $query->andWhere('JSON_CONTAINS(pay_order_id, "'.$order_id.'")');
  224. }
  225. $query->orderBy('id desc');
  226. $res = pagination_make($query);
  227. foreach($res['list'] as &$item){
  228. $item['pay_order_no'] = Order::find()->where(['id' => (array)json_decode($item['pay_order_id'], true)])->select('order_no')->asArray()->column();
  229. $goods_info = Goods::find()->where(['id' => $item['goods_id']])->select('id,name,cover_pic')->one();
  230. // $goods_cat = GoodsCat::find()->alias('gc')
  231. // ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id')
  232. // ->where([ 'gc.goods_id' => $item['goods_id'] ])
  233. // ->select(['c.name'])
  234. // ->asArray()
  235. // ->all();
  236. $saasUser = SaasUser::findOne($item['saas_id']);
  237. $activity = ActivityRebateOrderN::findOne($item['activity_id']);
  238. $item['price_info'] = (array)ActivityRebateOrderN::decodeRules($item['price_info']);
  239. foreach($item['pay_order_no'] as &$i){
  240. if(!$item['price_info'] || !isset($item['price_info']['list'])){
  241. continue;
  242. }
  243. foreach ($item['price_info']['list'] as $k) {
  244. if(isset($k['user_price_order_no']) && (in_array($i, $k['user_price_order_no']))){
  245. $i .= '✔';
  246. }
  247. }
  248. }
  249. $item['goods_info'] = $goods_info;
  250. // $item['goods_cat'] = $goods_cat;
  251. $item['saasUser'] = $saasUser;
  252. $item['activity'] = $activity;
  253. }
  254. return [
  255. 'code' => 0,
  256. 'msg' => 'success',
  257. 'data' => $res,
  258. 'q' => $query->createCommand()->getRawSql(),
  259. ];
  260. }
  261. }