ActivityController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\controllers;
  8. use app\models\ActivityNewUser;
  9. use app\models\ActivityNewUserGoods;
  10. use app\models\ActivityCutPrice;
  11. use app\models\ActivityCutPriceGoods;
  12. use app\models\ActivityCutPriceBanner;
  13. use app\models\ActivityCutPriceCat;
  14. use app\models\ActivityCutPriceOrder;
  15. use app\models\ActivityCutPriceLog;
  16. use app\models\User;
  17. use app\modules\admin\models\ActivityCutPriceForm;
  18. use app\models\Attr;
  19. use app\models\AttrGroup;
  20. use app\models\Coupon;
  21. use app\models\CouponAutoSend;
  22. use app\models\UserCoupon;
  23. use app\models\Option;
  24. use app\constants\OptionSetting;
  25. use yii\db\Query;
  26. use app\models\GoodsCat;
  27. use app\models\Goods;
  28. use app\models\SaasUser;
  29. class ActivityController extends BaseController
  30. {
  31. public function actionNewUser() {
  32. $query = ActivityNewUser::find();
  33. $query->andWhere([
  34. 'and',
  35. ['is_delete' => 0, 'status' => 1, 'store_id' => get_store_id(), 'is_platform' => 0],
  36. ['<', 'start_time', time()],
  37. ['>', 'end_time', time()],
  38. ]);
  39. $user = get_user();
  40. $activityNewUserList = $query->select('coupon_ids, id, goods_ids')->asArray()->all();
  41. $coupons = [];
  42. $goods_ids = [];
  43. foreach ($activityNewUserList as $newUserItem) {
  44. $coupons_ = explode(',', $newUserItem['coupon_ids']);
  45. $coupons = array_merge($coupons, $coupons_);
  46. $goods_ids_ = explode(',', $newUserItem['goods_ids']);
  47. $goods_ids = array_merge($goods_ids, $goods_ids_);
  48. }
  49. if(!empty($coupons)){
  50. $coupon_list = Coupon::find()->where(['in', 'id', $coupons])->andWhere(['is_delete' => 0])->asArray()->all();
  51. $auto_sends = CouponAutoSend::find()->where(['store_id'=>get_store_id(), 'is_delete' => 0, 'status' => 1])->asArray()->all();
  52. if($auto_sends){
  53. foreach($coupon_list as &$item){
  54. $item['auto_send'] = [];
  55. foreach ($auto_sends as $k){
  56. $cids = json_decode($k['coupon_id'], true);
  57. if(in_array($item['id'], $cids)){
  58. $item['auto_send'] = $k;
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. if($user){
  65. $user_coupons = UserCoupon::find()->where(['store_id'=>get_store_id(), 'user_id' => $user->id])->asArray()->all();
  66. foreach($coupon_list as &$item){
  67. foreach ($user_coupons as $k) {
  68. if($item['id'] == $k['coupon_id']){
  69. $item['user_coupon'] = $k;
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. }
  76. $activityNewUserIds = array_column($activityNewUserList, 'id');
  77. $goods_ext = ActivityNewUserGoods::find()->where(['activity_id' => $activityNewUserIds, 'is_delete' => 0])->all();
  78. // var_dump($goods_ext, $activityNewUserIds);die;
  79. return $this->asJson([
  80. 'code' => 0,
  81. 'data' => [
  82. 'goods_ids' => implode(',', $goods_ids ?: [])
  83. ],
  84. 'coupon_list' => $coupon_list ?? [],
  85. 'goods_ext' => $goods_ext,
  86. ]);
  87. }
  88. public function actionNewUserGoodsList() {
  89. $activity_id = input_params('activity_id', 0);
  90. $goods_ext = ActivityNewUserGoods::findAll(['activity_id' => $activity_id, 'is_delete' => 0]);
  91. return [
  92. 'code' => 0,
  93. 'msg' => '获取成功',
  94. 'data' => [
  95. 'goods_ext' => $goods_ext ?? [],
  96. ]
  97. ];
  98. }
  99. public function actionNewUserGoodsInfo() {
  100. $activity_id = input_params('activity_id', 0);
  101. $goods_id = input_params('goods_id', 0);
  102. $goods_ext = ActivityNewUserGoods::findOne(['activity_id' => $activity_id, 'goods_id' => $goods_id, 'is_delete' => 0]);
  103. return [
  104. 'code' => 0,
  105. 'msg' => '获取成功',
  106. 'data' => [
  107. 'goods_ext' => $goods_ext ?? [],
  108. ]
  109. ];
  110. }
  111. public function actionNewUserConf(){
  112. $conf = Option::get(OptionSetting::ACTIVITY_NEW_USER_CONF, get_store_id(), 'store')['value'];
  113. if($conf){
  114. $conf = json_decode($conf, true);
  115. }else{
  116. $conf = ['conf' => [
  117. 'head' => ['bg_color' => '#f00', 'bg_img' => ''],
  118. 'body' => ['bg_color' => '#f00', 'bg_img' => ''],
  119. ]];
  120. }
  121. return $this->asJson([
  122. 'code'=>0,
  123. 'msg'=>'ok',
  124. 'data' => $conf,
  125. ]);
  126. }
  127. public function actionNewUserPushGoods(){
  128. //{'conf':[{'tab':'推荐',goods_ids:[1,2,3],cat_ids:[5,6,7]}]}
  129. $conf = Option::get(OptionSetting::ACTIVITY_NEW_USER_GOODS_CONF, get_store_id(), 'store')['value'];
  130. if($conf){
  131. $conf = json_decode($conf, true);
  132. }else{
  133. $conf = ['conf' => []];
  134. }
  135. $gname = input_params('goods_name', '');
  136. if($gname){
  137. $gids = [];
  138. $cids = [];
  139. foreach($conf['conf'] as $item){
  140. $item['goods_ids'] && $gids = array_merge($gids, (array)$item['goods_ids']);
  141. $item['cat_ids'] && $cids = array_merge($cids, (array)$item['cat_ids']);
  142. }
  143. if($cids){
  144. $cids = \app\models\Cat::getCatListByPids(get_store_id(), $cids);
  145. }
  146. $query = Goods::find();
  147. $query->andWhere([
  148. 'and',
  149. ['is_delete' => 0, 'status' => 1, 'store_id' => get_store_id()],
  150. ['like', 'name', $gname],
  151. ]);
  152. $idwhere = ['or', 0];
  153. if($gids){
  154. $idwhere[] = ['id' => $gids];
  155. }
  156. if($cids){
  157. $cquery = (new Query())->select('goods_id')->from(GoodsCat::tableName())->where(['cat_id' => $cids]);
  158. $idwhere[] = ['id' => $cquery];
  159. }
  160. $query->andWhere($idwhere);
  161. $gres = $query->select(['id'])->asArray()->column();
  162. }
  163. return $this->asJson([
  164. 'code'=>0,
  165. 'msg'=>'ok',
  166. 'data' => $conf,
  167. 'search_goods_ids' => $gres,
  168. 'q' => $query ? $query->createCommand()->getRawSql() : '',
  169. ]);
  170. }
  171. public function actionCutPriceConf(){
  172. $form = new ActivityCutPriceForm();
  173. $form->store_id = get_store_id();
  174. $res = $form->conf();
  175. return $this->asJson($res);
  176. }
  177. public function actionCutPriceInfo() {
  178. $store_id = get_store_id();
  179. $activity_id = intval(input_params('activity_id', 0));
  180. if($activity_id){
  181. $info = ActivityCutPrice::activityAt($activity_id);
  182. if(!$info){
  183. return $this->asJson([
  184. 'code' => 1,
  185. 'msg' => '活动不存在',
  186. ]);
  187. }
  188. }else{
  189. $alist = ActivityCutPrice::activityAtList($store_id, true);
  190. $activity_id = array_column($alist, 'id');
  191. }
  192. $gcatQuery = ActivityCutPriceGoods::find()->select('cat_id')->where(['activity_id' => $activity_id, 'is_delete' => 0]);
  193. $cat = ActivityCutPriceCat::find()->where(['id' => $gcatQuery, 'is_delete' => 0, 'is_show' => 1])
  194. ->select('id,name')->orderBy('sort DESC')->all();
  195. $banner = ActivityCutPriceBanner::find()->where(['is_delete' => 0])
  196. ->select('id,name,url,pic_url')->orderBy('sort DESC')->all();
  197. return $this->asJson([
  198. 'code' => 0,
  199. 'data' => $info,
  200. 'cat' => $cat,
  201. 'banner' => $banner,
  202. ]);
  203. }
  204. public function actionCutPriceGoodsList() {
  205. $store_id = get_store_id();
  206. $activity_id = intval(input_params('activity_id', 0));
  207. if(!$activity_id){
  208. $alist = ActivityCutPrice::activityAtList($store_id, true);
  209. $activity_id = array_column($alist, 'id');
  210. }
  211. $cat_id = input_params('cat_id', 0);
  212. $goods_name = input_params('goods_name', '');
  213. $goods_ext_query = ActivityCutPriceGoods::find()->where(['activity_id' => $activity_id, 'cat_id' => $cat_id, 'is_delete' => 0]);
  214. if($goods_name){
  215. $goods_query = Goods::find()->select('id')->where(['like', 'name', $goods_name]);
  216. $goods_ext_query->andWhere(['goods_id' => $goods_query]);
  217. }
  218. $goods_ext = pagination_make($goods_ext_query);
  219. $goods_ids = array_column($goods_ext['list'], 'goods_id');
  220. foreach($goods_ext['list'] as &$item){
  221. $userCount = ActivityCutPriceOrder::find()->where(['activity_id' => $item['activity_id'], 'goods_id' => $item['goods_id']])->groupBy(['saas_id'])->count();
  222. $item['userCount'] = $userCount;
  223. }
  224. $goods_list = Goods::find()->where(['id' => $goods_ids, 'status' => 1, 'is_delete' => 0])
  225. ->select('id,name,cover_pic,price')->all();
  226. $goods_list = ActivityCutPriceForm::sortGoods($goods_ext['list'], $goods_list);
  227. return $this->asJson([
  228. 'code' => 0,
  229. 'goods_ids' => $goods_ids,
  230. 'goods_ext' => $goods_ext,
  231. 'goods_list' => $goods_list,
  232. // 'q' => $goods_ext_query->createCommand()->getRawSql(),
  233. ]);
  234. }
  235. public function actionCutPriceOrderByGoodsId() {
  236. $activity_id = input_params('activity_id', 0);
  237. $goods_id = input_params('goods_id', 0);
  238. $user_id = get_user_id();
  239. $cutOrder = ActivityCutPriceForm::getHasOrder($user_id, get_saas_user_id(), $activity_id, $goods_id);
  240. return $this->asJson([
  241. 'code' => 0,
  242. 'cutOrder' => $cutOrder,
  243. ]);
  244. }
  245. public function actionCutPriceOrderCreate() {
  246. $activity_id = input_params('activity_id', 0);
  247. $goods_id = input_params('goods_id', 0);
  248. $mch_list = input_params('mch_list', '');
  249. $form = new ActivityCutPriceForm();
  250. $form->store_id = get_store_id();
  251. $form->is_platform = 1;
  252. $user_id = get_user_id();
  253. $saas_id = get_saas_user_id();
  254. $res = $form->orderCreate($user_id, $saas_id, $activity_id, $goods_id, $mch_list);
  255. return $this->asJson($res);
  256. }
  257. public function actionCutPriceOrderList() {
  258. $form = new ActivityCutPriceForm();
  259. $form->store_id = get_store_id();
  260. $form->saas_id = get_saas_user_id();
  261. $form->user_id = get_user_id();
  262. $form->status = input_params('status', -1);
  263. $form->is_platform = 1;
  264. $res = $form->orderList();
  265. return $this->asJson($res);
  266. }
  267. public function actionCutPriceOrderInfo() {
  268. $order_id = input_params('order_id', 0);
  269. $order = ActivityCutPriceOrder::findOne(['id' => $order_id, 'is_delete' => 0]);
  270. if(!$order){
  271. return $this->asJson([
  272. 'code' => 1,
  273. 'msg' => '砍价订单不存在',
  274. ]);
  275. }
  276. $store_id = $order->store_id;
  277. //帮砍
  278. $form = new ActivityCutPriceForm();
  279. $form->store_id = $store_id;
  280. $user_id = get_user_id() ?: 0;
  281. $saas_id = get_saas_user_id();
  282. $user = User::findOne(['is_delete' => 0, 'binding' => get_saas_user()->mobile, 'store_id' => $store_id]);
  283. if ($user) {
  284. $user_id = $user->id;
  285. }
  286. $logSave = $form->logSave($user_id, $saas_id, $order_id);
  287. $log = ActivityCutPriceLog::findOne([
  288. 'order_id' => $order_id,
  289. 'saas_id' => $saas_id,
  290. ]);
  291. $order = ActivityCutPriceOrder::find()->where(['id' => $order_id, 'is_delete' => 0])->one();
  292. $status = ActivityCutPriceOrder::getStatus($order);
  293. $activity = ActivityCutPrice::findOne($order['activity_id']);
  294. $goods_ext = ActivityCutPriceGoods::findOne(['activity_id' => $order['activity_id'], 'goods_id' => $order['goods_id'], 'is_delete' => 0]);
  295. $goods_info = Goods::find()->where(['id' => $order['goods_id']])->one();
  296. if ($goods_info) {
  297. $pic = $goods_info['cover_pic'];
  298. $index = strstr($goods_info['cover_pic'], 'http');
  299. if (!$index) {
  300. $pic = 'https:' . $goods_info['cover_pic'];
  301. }
  302. $goods_info['cover_pic'] = $pic;
  303. }
  304. $saasUser = SaasUser::find()->where(['id' => $order['saas_id']])->select('name,avatar')->asArray()->one();
  305. $mch_list = json_decode($order['order_mch_list'], true);
  306. $attr_id_list = array_column($mch_list[0]['goods_list'][0]['attr'], 'attr_id');
  307. sort($attr_id_list);
  308. $attr_list = Attr::find()->alias('a')
  309. ->select('ag.id AS attr_group_id,ag.attr_group_name,a.id AS attr_id,a.attr_name')
  310. ->leftJoin(['ag' => AttrGroup::tableName()], 'a.attr_group_id=ag.id')
  311. ->where(['a.id' => $attr_id_list, 'ag.store_id' => $store_id])
  312. ->asArray()->all();
  313. return $this->asJson([
  314. 'code' => 0,
  315. 'msg' => 'success',
  316. 'data' => $order,
  317. 'status' => $status,
  318. 'attr_list' => $attr_list,
  319. 'activity' => $activity,
  320. 'activity_finished' => $activity->end_time < time() ? 1 : 0,
  321. 'goods_ext' => $goods_ext,
  322. 'goods_info' => $goods_info,
  323. 'saasUser' => $saasUser,
  324. 'logSave' => $logSave,
  325. 'log' => $log,
  326. 'is_self_order' => $user_id == $order->user_id ? 1 : 0,
  327. ]);
  328. }
  329. public function actionCutPriceLogList() {
  330. $order_id = input_params('order_id', 0);
  331. $query = ActivityCutPriceLog::find()->alias('acp')
  332. ->leftJoin(['su' => SaasUser::tableName()], 'acp.saas_id = su.id')
  333. ->where(['order_id' => $order_id, 'acp.is_delete' => 0])
  334. ->select('acp.user_id,acp.saas_id,acp.created_at,acp.cut_price,su.name,su.avatar');
  335. $list = $query->asArray()->all();
  336. $sum = array_sum(array_column($list, 'cut_price'));
  337. return $this->asJson([
  338. 'code' => 0,
  339. 'data' => $list,
  340. 'count' => count($list),
  341. 'sum' => $sum,
  342. // 'q' => $query->createCommand()->getRawSql(),
  343. ]);
  344. }
  345. }