ActivityWechatRoomForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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\ActivityWechatRoom;
  13. use app\models\ActivityWechatRoomGoods;
  14. use app\models\ActivityWechatRoomOrder;
  15. use yii\base\Model;
  16. use app\models\Option;
  17. use app\constants\OptionSetting;
  18. use app\models\SaasUser;
  19. use app\models\Order;
  20. use app\modules\client\models\v1\order\OrderSubmitPreviewForm;
  21. use app\models\Attr;
  22. use app\models\AttrGroup;
  23. class ActivityWechatRoomForm extends Model
  24. {
  25. public $store_id;
  26. public $saas_id;
  27. public $user_id;
  28. public $id;
  29. public $activity_id;
  30. public $ids;
  31. public $name;
  32. public $start_time;
  33. public $end_time;
  34. public $status;
  35. public $goods_ext;
  36. public $goods_ids;
  37. public $goods_name;
  38. public $wechat_room_id;
  39. public $qr_code;
  40. public function rules()
  41. {
  42. return [
  43. [['status', 'id'], 'integer'],
  44. [['start_time', 'end_time', 'ids', 'name'], 'string'],
  45. [['goods_name', 'store_id', 'goods_ids', 'goods_ext', 'activity_id',
  46. 'saas_id', 'user_id', 'qr_code', 'wechat_room_id'], 'safe'],
  47. ];
  48. }
  49. public function init() {
  50. parent::init();
  51. if(empty($this->store_id)){
  52. $this->store_id = get_store_id();
  53. }
  54. }
  55. public function search ()
  56. {
  57. try {
  58. $query = ActivityWechatRoom::find()->where(['is_delete' => 0, 'store_id' => get_store_id()]);
  59. if ((int)$this->status === 1) {//未开始
  60. $query->andWhere(['>' , 'start_time', time()]);
  61. }
  62. if ((int)$this->status === 2) {//进行中
  63. $query->andWhere(['AND', ['<' , 'start_time', time()], ['>' , 'end_time', time()]]);
  64. }
  65. if ((int)$this->status === 3) { //已结束
  66. $query->andWhere(['<' , 'end_time', time()]);
  67. }
  68. if (!empty($this->name)) { //名称
  69. $query->andWhere(['LIKE' , 'name', $this->name]);
  70. }
  71. if (!empty($this->start_time)) {
  72. $query->andWhere(['>' , 'end_time', strtotime($this->start_time)]);
  73. }
  74. if (!empty($this->end_time)) {
  75. $query->andWhere(['<' , 'start_time', strtotime($this->end_time)]);
  76. }
  77. $query->orderBy('id DESC');
  78. $pagination = pagination_make($query);
  79. foreach ($pagination['list'] as &$item) {
  80. $item['publish'] = $item['status'];
  81. //获取活动状态
  82. if ($item['start_time'] > time()) {
  83. $item['status'] = 1;
  84. }
  85. if ($item['start_time'] < time() && $item['end_time'] > time()) {
  86. $item['status'] = 2;
  87. }
  88. if ($item['end_time'] < time()) {
  89. $item['status'] = 3;
  90. }
  91. //格式化时间
  92. $item['start_time'] = date("Y-m-d H:i:s", $item['start_time']);
  93. $item['end_time'] = date("Y-m-d H:i:s", $item['end_time']);
  94. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  95. $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
  96. $userCount = Order::find()->where(['activity_wechat_room_id' => $item['id'], 'is_pay' => 1])->groupBy(['saas_id'])->count();
  97. $item['userCount'] = $userCount ?? 0;
  98. $orderCount = Order::find()->where(['activity_wechat_room_id' => $item['id'], 'is_pay' => 1])->count();
  99. $item['orderCount'] = $orderCount ?? 0;
  100. $orderSum = Order::find()->where(['activity_wechat_room_id' => $item['id'], 'is_pay' => 1])->sum('pay_price');
  101. $item['orderSum'] = $orderSum ?? 0;
  102. }
  103. return [
  104. 'code' => 0,
  105. 'msg' => 'success',
  106. 'data' => [
  107. 'data' => $pagination['list'],
  108. 'pageNo' => $pagination['pageNo'],
  109. 'totalCount' => $pagination['totalCount'],
  110. ]
  111. ];
  112. } catch (\Exception $e) {
  113. return [
  114. 'code' => 1,
  115. 'msg' => $e->getMessage()
  116. ];
  117. }
  118. }
  119. public function listSelect() {
  120. $query = ActivityWechatRoom::find()->where(['is_delete' => 0]);
  121. $this->store_id && $query->andWhere(['store_id' => $this->store_id]);
  122. if($this->name){
  123. $query->andWhere(['like', 'name', $this->name]);
  124. }
  125. if (!empty($this->start_time)) {
  126. $query->andWhere(['>' , 'end_time', strtotime($this->start_time)]);
  127. }
  128. if (!empty($this->end_time)) {
  129. $query->andWhere(['<' , 'start_time', strtotime($this->end_time)]);
  130. }
  131. $query->select('id, name');
  132. $query->orderBy('id desc');
  133. $res = $query->asArray()->all();
  134. return [
  135. 'code' => 0,
  136. 'msg' => 'success',
  137. 'data' => $res,
  138. ];
  139. }
  140. public static function sortGoods($goods_ext, $goods){
  141. $res = [];
  142. foreach($goods_ext as $eitem){
  143. foreach($goods as $gitem){
  144. if($gitem['id'] == $eitem['goods_id']){
  145. $res[] = $gitem;
  146. }
  147. }
  148. }
  149. return $res;
  150. }
  151. public function getInfo()
  152. {
  153. try {
  154. $activity = ActivityWechatRoom::find()->where(['id' => $this->id])->one();
  155. if ($activity) {
  156. $activity_goods = Goods::find()->where(['in', 'id', explode(',', $activity->goods_ids)])->andWhere(['is_delete' => 0])->asArray()->all();
  157. $activity['start_time'] = date("Y-m-d H:i:s", $activity['start_time']);
  158. $activity['end_time'] = date("Y-m-d H:i:s", $activity['end_time']);
  159. $activity['wechat_room_id'] = explode(',',$activity['wechat_room_id']);
  160. $goods_ext = ActivityWechatRoomGoods::findAll(['activity_id' => $activity->id, 'is_delete' => 0]);
  161. $activity_goods = self::sortGoods($goods_ext, $activity_goods);
  162. }
  163. return [
  164. 'code' => 0,
  165. 'msg' => '获取成功',
  166. 'data' => [
  167. 'activity_goods' => $activity_goods ?? [],
  168. 'goods_ext' => $goods_ext ?? [],
  169. 'activity' => $activity ?: [],
  170. ]
  171. ];
  172. } catch (\Exception $e) {
  173. return [
  174. 'code' => 1,
  175. 'msg' => $e->getMessage() . $e->getFile() . $e->getLine()
  176. ];
  177. }
  178. }
  179. public function save ()
  180. {
  181. $t = \Yii::$app->db->beginTransaction();
  182. try {
  183. if (!$this->name || !$this->start_time || !$this->end_time) {
  184. throw new \Exception("请将参数填充完整");
  185. }
  186. $activity = ActivityWechatRoom::findOne($this->id);
  187. if (empty($activity)) {
  188. $activity = new ActivityWechatRoom();
  189. $activity->store_id = $this->store_id;
  190. }
  191. $activity->name = $this->name;
  192. $activity->start_time = strtotime($this->start_time);
  193. $activity->end_time = strtotime($this->end_time);
  194. $activity->goods_ids = $this->goods_ids;
  195. $activity->wechat_room_id = implode(',', json_decode($this->wechat_room_id,true));
  196. $activity->qr_code = $this->qr_code;
  197. if (!$activity->save()) {
  198. \Yii::error([__METHOD__, $activity->attributes]);
  199. throw new \Exception(array_shift($activity->getFirstErrors()));
  200. }
  201. if(!empty($this->goods_ext)){
  202. foreach($this->goods_ext as &$item){
  203. $item['activity_id'] = $activity->id;
  204. $item['store_id'] = $this->store_id;
  205. }
  206. ActivityWechatRoomGoods::saveList($this->goods_ext, $activity->id);
  207. }
  208. $t->commit();
  209. return [
  210. 'code' => 0,
  211. 'msg' => '操作成功!'
  212. ];
  213. } catch (\Exception $e) {
  214. $t->rollBack();
  215. return [
  216. 'code' => 1,
  217. 'msg' => $e->getMessage()
  218. ];
  219. }
  220. }
  221. public function del ()
  222. {
  223. try {
  224. if ($this->ids) {
  225. $ids = explode(',', $this->ids);
  226. ActivityWechatRoom::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]);
  227. }
  228. return [
  229. 'code' => 0,
  230. 'msg' => '操作成功!'
  231. ];
  232. } catch (\Exception $e) {
  233. return [
  234. 'code' => 1,
  235. 'msg' => $e->getMessage()
  236. ];
  237. }
  238. }
  239. public function close ()
  240. {
  241. try {
  242. if ($this->ids) {
  243. $ids = explode(',', $this->ids);
  244. ActivityWechatRoom::updateAll(['end_time' => time() - 1], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]);
  245. }
  246. return [
  247. 'code' => 0,
  248. 'msg' => '操作成功!'
  249. ];
  250. } catch (\Exception $e) {
  251. return [
  252. 'code' => 1,
  253. 'msg' => $e->getMessage()
  254. ];
  255. }
  256. }
  257. public function getGoodsAttrItem($goods_ext, $mch_list)
  258. {
  259. // var_dump($mch_list);
  260. $attr_id_list = array_column($mch_list[0]['goods_list'][0]['attr'], 'attr_id');
  261. sort($attr_id_list);
  262. $goods = Goods::findOne($goods_ext['goods_id']);
  263. if (empty($goods)) {
  264. return null;
  265. }
  266. $attr = $goods['attr'];
  267. $attr_rows = json_decode($attr, true);
  268. if (empty($attr_rows)) {
  269. return null;
  270. }
  271. foreach ($attr_rows as $i => $attr_row) {
  272. $key = [];
  273. foreach ($attr_row['attr_list'] as $j => $attr) {
  274. $key[] = $attr['attr_id'];
  275. }
  276. sort($key);
  277. if (!array_diff($attr_id_list, $key)) {
  278. if (!$attr_rows[$i]['price']) {
  279. return null;
  280. }
  281. return $attr_rows[$i];
  282. }
  283. }
  284. return null;
  285. }
  286. public function getGoodsExtAttrItem($goods_ext, $mch_list)
  287. {
  288. // var_dump($mch_list);
  289. $attr_id_list = array_column($mch_list[0]['goods_list'][0]['attr'], 'attr_id');
  290. sort($attr_id_list);
  291. $attr = $goods_ext['attr'];
  292. $attr_rows = json_decode($attr, true);
  293. if (empty($attr_rows)) {
  294. return null;
  295. }
  296. foreach ($attr_rows as $i => $attr_row) {
  297. $key = [];
  298. foreach ($attr_row['attr_list'] as $j => $attr) {
  299. $key[] = $attr['attr_id'];
  300. }
  301. sort($key);
  302. if (!array_diff($attr_id_list, $key)) {
  303. if (!$attr_rows[$i]['price']) {
  304. return null;
  305. }
  306. return $attr_rows[$i];
  307. }
  308. }
  309. return null;
  310. }
  311. }