ActivityController.php 14 KB

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