CouponController.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. <?php
  2. namespace addons\Store\client\controllers;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use addons\Coupon\common\models\AddonsCoupon;
  5. use addons\Store\common\enums\CouponEnum;
  6. use addons\Store\common\models\Store;
  7. use addons\Store\common\models\StoreCoupon;
  8. use addons\Store\common\models\StoreCouponGoodStatistics;
  9. use addons\Store\common\models\StoreCouponMemberRelate;
  10. use addons\Store\common\models\StoreCouponReward;
  11. use addons\Store\common\models\StoreCouponStatistics;
  12. use addons\Store\common\models\StoreCouponUseLog;
  13. use addons\Store\common\models\StoreCouponUserRelate;
  14. use addons\Store\common\models\StoreGoods;
  15. use addons\Store\common\models\StoreGoodsCate;
  16. use addons\Store\common\services\StoreService;
  17. use common\enums\AddonsEnum;
  18. use common\enums\StatusEnum;
  19. use common\helpers\ApiCode;
  20. use common\helpers\ArrayHelper;
  21. use common\models\mall\MallAddons;
  22. use common\models\user\User;
  23. use common\models\user\UserLevel;
  24. use yii\db\Expression;
  25. class CouponController extends BaseController
  26. {
  27. public function actionIndex()
  28. {
  29. if (\Yii::$app->request->isAjax) {
  30. if (\Yii::$app->request->isGet) {
  31. $params = \Yii::$app->request->get();
  32. $where[] = ['mall_id' => $this->mall_id,'store_id'=>$this->store_id, 'status' => StatusEnum::ENABLED];
  33. if (!empty($params['name'])) $where[] = ['like', 'name', $params['name']];
  34. if (!empty($params['status'])) $where[] = ['coupon_status' => $params['status']];
  35. if (!empty($params['type'])) $where[] = ['type' => $params['type']];
  36. $params['select'] = 'id,mall_id,store_id,name,type,coupon_status,discount,discount_limit,min_price,sub_price,
  37. total_count,expire_type,expire_day,begin_at,end_at,appoint_type,rule,is_remind,remind_days,effect_days,
  38. is_receive_limit,receive_count,is_member,is_original_use,is_public_receive,is_giving,coupon_status,
  39. is_giving_reward,is_consume_reward,qrcode_url,poster_url,created_at';
  40. $params['where'] = $where;
  41. $params['order'] = 'id desc';
  42. $list = StoreCoupon::listPage($params);
  43. if (!empty($list['list'])) {
  44. $h5_domain = rtrim(\Yii::$app->services->setting->get('system.h5_url'),'/') .'/#/';
  45. $mall_logo = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.logo');
  46. if (empty($mall_logo)) \Yii::$app->request->hostInfo . '/resources/img/mall/mall_logo_default.jpg';
  47. $mall_logo = $this->imgBase64Encode($mall_logo);
  48. $mall_name = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.name');
  49. if (empty($mall_name)) $mall_name = '七件事商城';
  50. $coupon_ids = array_column($list['list'], 'id');
  51. $user_coupons = StoreCouponStatistics::lists(['where' => [['coupon_id' => $coupon_ids],['store_id'=>$this->store_id,]], 'select' => 'coupon_id,received_number,used_number', 'index' => 'coupon_id']);
  52. foreach ($list['list'] as &$lv) {
  53. $lv['received_number'] = $lv['used_number'] = 0;
  54. if (isset($user_coupons[$lv['id']])) {
  55. $lv['received_number'] = $user_coupons[$lv['id']]['received_number'];
  56. $lv['used_number'] = $user_coupons[$lv['id']]['used_number'];
  57. }
  58. $lv['page_link'] = $h5_domain . 'plugins2/Store/coupon/type?sign=' . $this->mall['mall_sign'] . '&coupon_id=' . $lv['id'] . '&coupon_share_log_id=0&from_type=2';
  59. $lv['poster_filename'] = substr(md5("{'poster'_{$lv['id']}_{$this->mall_id}"), 0, 16);
  60. $lv['qrcode_filename'] = substr(md5("{'qrcode'_{$lv['id']}_{$this->mall_id}"), 0, 16);
  61. $lv = array_merge($lv, StoreCoupon::parseCouponInfo($lv));
  62. }
  63. $list['mall_info']['mall_logo'] = $mall_logo;
  64. $list['mall_info']['mall_name'] = $mall_name;
  65. }
  66. return $this->success('操作成功', $list);
  67. }
  68. }
  69. return $this->render($this->action->id);
  70. }
  71. public function actionEdit()
  72. {
  73. if (\Yii::$app->request->isAjax) {
  74. if (\Yii::$app->request->isGet) {
  75. $coupon_id = \Yii::$app->request->get('coupon_id');
  76. $is_copy = \Yii::$app->request->get('is_copy') ?? false;
  77. $coupon_info = [];
  78. $member_level_limits = [];
  79. $good_lists = [];
  80. $goods_id_list = [];
  81. $giving_rewards = [];
  82. $consume_rewards = [];
  83. $checked_categorys = [];
  84. $store = [];
  85. if (!empty($coupon_id)) {
  86. $coupon_info = StoreCoupon::find()
  87. ->select('id,mall_id,store_id,name,type,discount,discount_limit,min_price,sub_price,total_count,sort,expire_type,expire_day,begin_at,end_at,appoint_type,rule,is_remind,remind_days,effect_days,is_receive_limit,receive_count,is_member,is_original_use,
  88. is_public_receive,is_giving,coupon_status,is_giving_reward,is_consume_reward,
  89. created_at,qrcode_url,poster_url,is_open_screen,first_frame,video_url,watch_times')
  90. ->where(['id' => $coupon_id, 'mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], 'coupon_status' => [1, 2, 3, 4]])
  91. ->one();
  92. if (empty($coupon_info)) return $this->error('优惠券不存在或已失效');
  93. if (($coupon_info->coupon_status != 1 || $coupon_info->coupon_status != 2) && $is_copy) return $this->error('该优惠券不能复制');
  94. if ($is_copy) $coupon_info->id = 0;
  95. $member_level_limits = StoreCouponMemberRelate::lists(['where' => [['mall_id' => $this->mall_id,], ['coupon_id' => $coupon_id]], 'select' => 'member_level']);
  96. if(!empty($member_level_limits)) $member_level_limits = array_values(array_flip(array_flip(array_column($member_level_limits, 'member_level'))));
  97. $good_lists =!empty( $coupon_info->goods)? $coupon_info->goods:[];
  98. $coupon_rewards = StoreCouponReward::lists(['where' => [['mall_id' => $this->mall_id], ['coupon_id' => $coupon_id]], 'select' => 'reward_type,balance,score,red_packet']);
  99. if (!empty($coupon_rewards)) {
  100. foreach ($coupon_rewards as $rk => $rv) {
  101. switch ($rv['reward_type']) {
  102. case 1:
  103. $giving_rewards = ['balance' => $rv['balance'], 'score' => $rv['score'], 'red_packet' => $rv['red_packet']];
  104. break;
  105. case 2:
  106. $consume_rewards = ['balance' => $rv['balance'], 'score' => $rv['score'], 'red_packet' => $rv['red_packet'],];
  107. break;
  108. }
  109. }
  110. }
  111. // 优惠券已经绑定的商品分类
  112. $checked_categorys = $coupon_info->cateRelate??[];
  113. if (!empty($checked_categorys)) {
  114. $cate_ids = array_column($checked_categorys, 'cate_id');
  115. foreach ($cate_ids as $cate_id) {
  116. $cateids[] = [$cate_id];
  117. }
  118. $checked_categorys = $cateids;
  119. } else {
  120. $checked_categorys = [];
  121. }
  122. }
  123. $member_levels = $member_levels = UserLevel::lists(['where' => [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], 'select' => 'id,level,name']);
  124. $conditions = [['is_show' => 1, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id, 'store_id' => $this->store_id]];
  125. $good_category = StoreGoodsCate::getCates($conditions, [], true, 'id,name,pic,parent_id');
  126. $good_category = StoreGoodsCate::getTreeCate($good_category);
  127. $coupon_info = ArrayHelper::toArray($coupon_info);
  128. if (!empty($good_lists)) {
  129. $good_lists = ArrayHelper::toArray($good_lists);
  130. foreach ($good_lists as &$gv) {
  131. $gv['goods_id'] = $gv['id'];
  132. }
  133. $goods_id_list = array_column($good_lists, 'id');
  134. }
  135. if(!isset($coupon_info['is_open_screen'])) $coupon_info['is_open_screen'] = 0;
  136. return $this->success('操作成功', compact('coupon_info', 'member_levels',
  137. 'good_category', 'member_level_limits',
  138. 'good_lists', 'goods_id_list',
  139. 'giving_rewards', 'consume_rewards', 'checked_categorys', 'store'));
  140. } else {
  141. try {
  142. $post = \Yii::$app->request->post();
  143. if (empty($post['coupon_info']['id'])) {
  144. $scenarios = 'create';
  145. $post['coupon_info']['mall_id'] = $this->mall_id;
  146. $post['coupon_info']['store_id'] = $this->store_id;
  147. } else {
  148. $scenarios = 'update';
  149. }
  150. $res = StoreCoupon::setData($post, $scenarios);
  151. if (!$res) {
  152. return $this->error(StoreCoupon::getStaticError());
  153. }
  154. return $this->success('添加优惠券成功');
  155. } catch (\Exception $e) {
  156. return $this->error($e->getMessage(), []);
  157. }
  158. }
  159. } else {
  160. return $this->render($this->action->id);
  161. }
  162. }
  163. public function actionCouponStatistics(){
  164. $params = \Yii::$app->request->get();
  165. $coupon_id = $params['coupon_id'];
  166. if (empty($params['coupon_id'])) return $this->error('参数错误');
  167. $user_coupons = StoreCouponStatistics::find()
  168. ->select('total_payed_amount,total_discount_amount,use_probability,receive_user_number,
  169. use_order_number,total_use_coupon_payed_amount,use_user_number')
  170. ->where(['mall_id' => $this->mall_id, 'store_id'=>$this->store_id,'coupon_id' => $coupon_id])
  171. ->asArray()->one();
  172. if (!empty($user_coupons)) {
  173. foreach ($user_coupons as $uk => $uv) {
  174. $user_coupons[$uk] = bcmul($uv,1,2);
  175. }
  176. $user_coupons['use_probability'] = $user_coupons['use_probability'] * 100 . '%';
  177. }
  178. $coupon_info = StoreCoupon::getOne([['mall_id'=>$this->mall_id],['store_id'=>$this->store_id],'id'=>$coupon_id],true);
  179. $coupon_info = array_merge($coupon_info, StoreCoupon::parseCouponInfo($coupon_info));
  180. $where[] = ['mall_id' => $this->mall_id, 'coupon_id' => $coupon_id];
  181. $params = [
  182. 'select' => 'coupon_id,good_id,payed_number,buyed_user_number',
  183. 'where' => $where,
  184. ];
  185. $good_statistics = StoreCouponGoodStatistics::lists($params);
  186. if (!empty($good_statistics)) {
  187. $good_ids = array_column($good_statistics, 'good_id');
  188. $goods = StoreGoods::lists(['where'=>[['id'=>$good_ids]],'select'=>'id,goods_name,cover_pic','index'=>'id']);
  189. foreach ($good_statistics as &$lv) {
  190. $lv['goods_name'] = $lv['cover_pic'] = '';
  191. if(isset($goods[$lv['good_id']])){
  192. $lv['goods_name'] = $goods[$lv['good_id']]['goods_name'];
  193. $lv['cover_pic'] = $goods[$lv['good_id']]['cover_pic'];
  194. }
  195. }
  196. }
  197. return $this->success('操作成功', compact('user_coupons', 'good_statistics', 'coupon_info'));
  198. }
  199. /**
  200. * 删除优惠券
  201. * @return mixed|void
  202. * @throws \yii\db\Exception
  203. */
  204. public function actionDel()
  205. {
  206. $params = \Yii::$app->request->post();
  207. $res = StoreCoupon::updateAll(['status' => StatusEnum::DELETE], ['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'id' => $params['id']]);
  208. if ($res === false) return $this->error(StoreCoupon::getStaticError());
  209. return $this->success('操作成功');
  210. }
  211. public function actionUserCoupon()
  212. {
  213. if (\Yii::$app->request->isAjax) {
  214. if (\Yii::$app->request->isGet) {
  215. $params = \Yii::$app->request->get();
  216. $where = [];
  217. $where[] = ['mall_id'=>$this->mall_id];
  218. $where[] =new Expression("FIND_IN_SET({$this->store_id},store_id)");
  219. if (!empty($params['user_id'])) $where[] = ['user_id'=>$params['user_id']];
  220. if (!empty($params['from_type'])) $where[] = ['from_type'=>$params['from_type']];
  221. if (!empty($params['keyword'])) {
  222. if (!empty($params['from_type'])) {
  223. switch ($params['from_type']){
  224. case 1:
  225. $store_coupon_list = AddonsCoupon::lists(['where'=>[['mall_id'=>$this->mall_id],['store_id'=>$this->store_id],['like','name',trim($params['keyword'])]],'select'=>'id']);
  226. $coupon_ids = array_column($store_coupon_list,'id');
  227. $where[] = ['coupon_id'=>$coupon_ids];
  228. break;
  229. case 2:
  230. $store_coupon_list = StoreCoupon::lists(['where'=>[['mall_id'=>$this->mall_id],['store_id'=>$this->store_id],['like','name',trim($params['keyword'])]],'select'=>'id']);
  231. $coupon_ids = array_column($store_coupon_list,'id');
  232. $where[] = ['coupon_id'=>$coupon_ids];
  233. break;
  234. }
  235. }
  236. }
  237. $params['where'] = $where;
  238. $params['order'] = 'id desc';
  239. $list = StoreCouponUserRelate::listPage($params);
  240. if(!empty($list['list'])){
  241. $coupon_ids = $store_coupon_ids = [];
  242. foreach ($list['list'] as $val){
  243. if($val['from_type']==1){
  244. $coupon_ids = array_column($list['list'],'coupon_id');
  245. }else{
  246. $store_coupon_ids = array_column($list['list'],'coupon_id');
  247. }
  248. }
  249. $user_id_arr = array_column($list['list'], 'user_id');
  250. $user_coupon_ids = array_column($list['list'], 'id');
  251. $user_list = User::lists(['where'=>[['id' => $user_id_arr],['mall_id'=>$this->mall_id]],'select'=>'id,avatar_url,nickname','index'=>'id']);
  252. $store_coupon_list = StoreCoupon::lists(['where'=>[['mall_id'=>$this->mall_id],['store_id'=>$this->store_id],['id'=>$store_coupon_ids]],'index'=>'id']);
  253. $coupon_list = AddonsCoupon::lists(['where'=>[
  254. ['mall_id'=>$this->mall_id],
  255. new Expression("FIND_IN_SET({$this->store_id},store_id)"),
  256. ['id'=>$coupon_ids]
  257. ],'index'=>'id']);
  258. $user_coupon_use_logs = StoreCouponUseLog::lists(['where'=>[['mall_id' => $this->mall_id],['status' => StatusEnum::ENABLED],['user_coupon_id' => $user_coupon_ids]],'select'=>'user_coupon_id,created_at','index'=>'user_coupon_id']);
  259. foreach ($list['list'] as &$item){
  260. $item['name']=$item['rule']='';
  261. $item['type'] =$item['discount'] = $item['min_price']= $item['begin_at']= $item['end_at']= $item['appoint_type']= $item['is_giving']= $item['coupon_status']
  262. =$item['sub_price']=$item['sort']=$item['expire_type']=$item['expire_day']=$item['is_member']=$item['discount_limit']=$item['is_original_use']=0;
  263. if($item['from_type']==1){
  264. if(isset($coupon_list[$item['coupon_id']])){
  265. $coupon = $coupon_list[$item['coupon_id']];
  266. $item = array_merge($item, AddonsCoupon::parseCouponInfo($coupon, true));
  267. }
  268. }else{
  269. if(isset($store_coupon_list[$item['coupon_id']])){
  270. $coupon = $store_coupon_list[$item['coupon_id']];
  271. $item = array_merge($item, StoreCoupon::parseCouponInfo($coupon, true));
  272. }
  273. }
  274. $item['nickname'] = $item['avatar_url'] = '';
  275. if(isset($user_list[$item['user_id']])){
  276. $item['nickname'] = $user_list[$item['user_id']]['nickname'];
  277. $item['avatar_url'] = $user_list[$item['user_id']]['avatar_url'];
  278. }
  279. $item['used_time'] = $user_coupon_use_logs[$item['id']]['created_at'] ?? 0;
  280. if($item['expire_type']==2){
  281. $item['is_failure'] = $item['end_at']<time()?"0":"1";
  282. }else{
  283. $item['is_failure'] = $item['end_time']<time()?"0":"1";
  284. }
  285. }
  286. }
  287. return $this->success('操作成功', $list);
  288. }
  289. }
  290. return $this->render($this->action->id);
  291. }
  292. public function actionSelectCoupon(){
  293. $params = \Yii::$app->request->post();
  294. $where = [];
  295. $where[] = ['mall_id' => $this->mall_id];
  296. $where[] = ['store_id' => $this->store_id];
  297. $where[] = ['status' => [StatusEnum::ENABLED]];
  298. $where[] = [
  299. 'or',
  300. ['=', 'total_count', -1],
  301. ['>', 'total_count', 'receive'],
  302. ];
  303. if (!empty($params['keyword'])) $where[] = ['like', 'name', $params['keyword']];
  304. $params['where'] = $where;
  305. $params['order'] = 'sort asc,id desc';
  306. $params['select'] = 'id,name,type,discount,min_price,sub_price,expire_type,expire_day,begin_at,end_at,status,total_count';
  307. $params['limit'] = 30;
  308. $store_coupon_list = StoreCoupon::lists($params);
  309. //判断是否过期
  310. foreach ($store_coupon_list as $k => $v) {
  311. $store_coupon_list[$k]['from_type'] = 2;
  312. $store_coupon_list[$k]['type_text'] = '门店优惠券';
  313. $v['end_at'] < time() && $v['expire_type'] == 2 ? $store_coupon_list[$k]['is_failure'] = 1 : $store_coupon_list[$k]['is_failure'] = 0;
  314. }
  315. $list = $store_coupon_list;
  316. $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_COUPON);
  317. if($is_install){
  318. $params = \Yii::$app->request->post();
  319. $where = [];
  320. $where[] = ['mall_id' => $this->mall_id];
  321. $where[] = ['store_id' => $this->store_id];
  322. $where[] = ['status' => [StatusEnum::ENABLED]];
  323. $where[] = [
  324. 'or',
  325. ['=', 'total_count', -1],
  326. ['>', 'total_count', 'receive'],
  327. ];
  328. if (!empty($params['keyword'])) $where[] = ['like', 'name', $params['keyword']];
  329. $params['where'] = $where;
  330. $params['order'] = 'sort asc,id desc';
  331. $params['select'] = 'id,name,type,discount,min_price,sub_price,expire_type,expire_day,begin_at,end_at,status,total_count';
  332. $params['limit'] = 30;
  333. $coupon_list = AddonsCoupon::lists($params);
  334. //判断是否过期
  335. foreach ($coupon_list as $k => $v) {
  336. $coupon_list[$k]['from_type'] = 1;
  337. $coupon_list[$k]['type_text'] = '平台优惠券';
  338. $v['end_at'] < time() && $v['expire_type'] == 2 ? $coupon_list[$k]['is_failure'] = 1 : $coupon_list[$k]['is_failure'] = 0;
  339. }
  340. $list = array_merge($list,$coupon_list);
  341. }
  342. return $this->success('操作成功', $list);
  343. }
  344. public function actionSelectCouponCompent(){
  345. $params = \Yii::$app->request->post();
  346. $where = [];
  347. $where[] = ['mall_id' => $this->mall_id];
  348. $where[] = ['store_id' => $this->store_id];
  349. $where[] = ['status' => [StatusEnum::ENABLED]];
  350. $where[] = [
  351. 'or',
  352. ['=', 'total_count', -1],
  353. ['>', 'total_count', 'receive'],
  354. ];
  355. if (!empty($params['keyword'])) $where[] = ['like', 'name', $params['keyword']];
  356. $params['where'] = $where;
  357. $params['order'] = 'sort asc,id desc';
  358. $params['select'] = 'id,name,type,discount,min_price,sub_price,expire_type,expire_day,begin_at,end_at,status,total_count';
  359. $store_coupon_list = StoreCoupon::lists($params);
  360. //判断是否过期
  361. foreach ($store_coupon_list as $k => $v) {
  362. $store_coupon_list[$k]['from_type'] = 2;
  363. $store_coupon_list[$k]['type_text'] = '门店优惠券';
  364. $v['end_at'] < time() && $v['expire_type'] == 2 ? $store_coupon_list[$k]['is_failure'] = 1 : $store_coupon_list[$k]['is_failure'] = 0;
  365. }
  366. $list = $store_coupon_list;
  367. $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_COUPON);
  368. if($is_install){
  369. $params = \Yii::$app->request->post();
  370. $where = [];
  371. $where[] = ['mall_id' => $this->mall_id];
  372. $where[] = ['store_id' => $this->store_id];
  373. $where[] = ['status' => [StatusEnum::ENABLED]];
  374. $where[] = [
  375. 'or',
  376. ['=', 'total_count', -1],
  377. ['>', 'total_count', 'receive'],
  378. ];
  379. if (!empty($params['keyword'])) $where[] = ['like', 'name', $params['keyword']];
  380. $params['where'] = $where;
  381. $params['order'] = 'sort asc,id desc';
  382. $params['select'] = 'id,name,type,discount,min_price,sub_price,expire_type,expire_day,begin_at,end_at,status,total_count';
  383. $coupon_list = AddonsCoupon::lists($params);
  384. //判断是否过期
  385. foreach ($coupon_list as $k => $v) {
  386. $coupon_list[$k]['from_type'] = 1;
  387. $coupon_list[$k]['type_text'] = '平台优惠券';
  388. $v['end_at'] < time() && $v['expire_type'] == 2 ? $coupon_list[$k]['is_failure'] = 1 : $coupon_list[$k]['is_failure'] = 0;
  389. }
  390. $list = array_merge($list,$coupon_list);
  391. }
  392. return $this->success('操作成功', $list);
  393. }
  394. //不用验证权限
  395. public function actionSearchGoods()
  396. {
  397. $params = \Yii::$app->request->get();
  398. $where[] = ['mall_id' => $this->mall_id];
  399. $where[] = ['store_id' => $this->store_id];
  400. $where[] = ['status' => StatusEnum::ENABLED];
  401. $where[] = ['check_status' => 1];
  402. $where[] = ['is_on_sale' => 1];
  403. if (!empty($params['keyword'])) $where[] = ['like', 'goods_name', $params['keyword']];
  404. $params['select'] = 'id as goods_id,goods_name,cover_pic,sort,goods_type';
  405. $params['where'] = $where;
  406. $params['order'] = 'sort asc,id desc';
  407. $list = StoreGoods::listPage($params);
  408. return $this->success('操作成功', $list);
  409. }
  410. /**
  411. * 图片base64编码
  412. * @param string $img
  413. * @param bool $imgHtmlCode
  414. * author 江南极客
  415. * @return string
  416. */
  417. public function imgBase64Encode($img = '', $imgHtmlCode = true)
  418. {
  419. //如果是本地文件
  420. if (strpos($img, 'http') === false && !file_exists($img)) return $img;
  421. //获取文件内容
  422. $file_content = file_get_contents($img);
  423. if ($file_content === false) return $img;
  424. $imageInfo = getimagesize($img);
  425. $prefiex = '';
  426. if ($imgHtmlCode) $prefiex = 'data:' . $imageInfo['mime'] . ';base64,';
  427. $base64 = $prefiex . chunk_split(base64_encode($file_content));
  428. return $base64;
  429. }
  430. public function actionUserCouponDetail()
  431. {
  432. if (!\Yii::$app->request->isAjax) {
  433. return $this->render('/coupon/detail-list');
  434. }
  435. if (!\Yii::$app->request->isGet) {
  436. return $this->error('请求方式错误');
  437. }
  438. $coupon_id = \Yii::$app->request->get('coupon_id');
  439. // 1:已领取,2:已使用
  440. $status = \Yii::$app->request->get('status');
  441. $page = \Yii::$app->request->get('page') ?? 1;
  442. $limit = \Yii::$app->request->get('page_size') ?? $this->pageSize;
  443. if (empty($status)) {
  444. return $this->error('参数错误');
  445. }
  446. $user_id = \Yii::$app->request->get('user_id');
  447. // 会员昵称/手机号
  448. $keyword = \Yii::$app->request->get('keyword');
  449. // 使用时间
  450. $start_time = \Yii::$app->request->get('start_time');
  451. $end_time = \Yii::$app->request->get('end_time');
  452. $where[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
  453. if (!empty($coupon_id)) {
  454. $where[] = ['coupon_id' => $coupon_id];
  455. }
  456. if (2 == $status) {
  457. $where[] = ['is_use' => 1];
  458. }
  459. if (!empty($user_id)) {
  460. $where[] = ['user_id' => $user_id];
  461. }
  462. if (!empty($start_time)) {
  463. $where[] = ['>=', 'start_time', $start_time];
  464. }
  465. if (!empty($end_time)) {
  466. $where[] = ['<=', 'end_time', $end_time];
  467. }
  468. if (!empty($keyword)) {
  469. $user_ids = User::find()
  470. ->select('id')
  471. ->where(['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]])
  472. ->asArray()
  473. ->column();
  474. $where[] = ['user_id' => $user_ids];
  475. }
  476. $params = [
  477. 'alias' => 'uc',
  478. 'select' => 'id,user_id,coupon_id,is_use,is_giving,giving_status,receive_type,is_force_failure,receive_platform,created_at,start_time as begin_at,end_time as end_at',
  479. 'where' => $where,
  480. 'page' => $page,
  481. 'limit' => $limit,
  482. 'order' => 'created_at desc',
  483. ];
  484. $list =StoreCouponUserRelate::listPage($params);
  485. if (!empty($list['list'])) {
  486. $user_coupon_ids = array_values(array_unique(array_column($list['list'], 'id')));
  487. $coupon_used_logs = StoreCouponUseLog::find()
  488. ->where(['user_coupon_id' => $user_coupon_ids, 'status' => 1, 'mall_id' => $this->mall_id])
  489. ->asArray()
  490. ->indexBy('user_coupon_id')
  491. ->all();
  492. $user_ids = array_values(array_unique(array_column($list['list'], 'user_id')));
  493. $user_query = User::find()
  494. ->select('id,nickname,mobile,avatar_url')
  495. ->where(['id' => $user_ids, 'status' => StatusEnum::ENABLED]);
  496. if (!empty($keyword)) {
  497. $user_query->andWhere(['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]]);
  498. }
  499. $user_arrs = $user_query->asArray()->indexBy('id')->all();
  500. $coupons = StoreCoupon::find()
  501. ->select('id,name')
  502. ->where(['id' => array_column($list['list'], 'coupon_id')])
  503. ->asArray()
  504. ->indexBy('id')
  505. ->all();
  506. foreach ($list['list'] as &$lv) {
  507. $lv['order_id'] = $coupon_used_logs[$lv['id']]['order_id'] ?? 0;
  508. $user = $user_arrs[$lv['user_id']] ?? [];
  509. $lv['nickname'] = $user['nickname'] ?? '';
  510. $lv['mobile'] = $user['mobile'] ?? '';
  511. $lv['avatar_url'] = $user['avatar_url'];
  512. if (empty($user['avatar_url'])) $lv['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  513. $coupon = $coupons[$lv['coupon_id']] ?? [];
  514. $lv['name'] = $coupon['name'] ?? '';
  515. }
  516. }
  517. // $list = UserCoupon::getError();dd($list);
  518. if (empty($coupon_id)) $coupon_id = $list['list'][0]['coupon_id'] ?? 0;
  519. $coupon_data = StoreCoupon::find()
  520. ->select('id,type,name,begin_at,end_at,discount,discount_limit,min_price,sub_price,coupon_status,is_original_use,rule,expire_type,expire_day,effect_days')
  521. ->where(['mall_id' => $this->mall_id, 'id' => $coupon_id, 'status' => 1])
  522. ->asArray()
  523. ->one();
  524. $coupon_info = !empty($coupon_data) ? StoreCoupon::parseCouponInfo($coupon_data) : [];
  525. $coupon_info = array_merge($coupon_data, $coupon_info);
  526. return $this->success('操作成功', ['list' => $list, 'coupon_info' => $coupon_info,'send_from_map'=>CouponEnum::getSendFromMap()]);
  527. }
  528. public function actionForceFailure()
  529. {
  530. if (!\Yii::$app->request->isGet) {
  531. return $this->error('请求方式错误');
  532. }
  533. $coupon_id = \Yii::$app->request->get('coupon_id');
  534. $user_id = \Yii::$app->request->get('user_id');
  535. if (empty($coupon_id) || empty($user_id)) {
  536. return $this->error('参数错误');
  537. }
  538. $user_coupon = StoreCouponUserRelate::find()
  539. ->where(['mall_id' => $this->mall_id,'store_id' => $this->store_id, 'coupon_id' => $coupon_id, 'user_id' => $user_id, 'is_use' => 0, 'is_giving' => 0, 'status' => 1, 'is_force_failure' => 0])
  540. ->limit(1)
  541. ->one();
  542. if (empty($user_coupon)) {
  543. return $this->error('无法停止该用户用券');
  544. }
  545. $user_coupon->is_force_failure = 1;
  546. $user_coupon->updated_at = time();
  547. // $user_coupon->status = 0;
  548. $user_coupon->coupon_status = 4;
  549. if ($user_coupon->save()) {
  550. return $this->success('操作成功');
  551. } else {
  552. return $this->error('操作失败');
  553. }
  554. }
  555. public function actionForceFailureAll()
  556. {
  557. if (!\Yii::$app->request->isGet) {
  558. return $this->error('请求方式错误');
  559. }
  560. $coupon_id = \Yii::$app->request->get('coupon_id');
  561. if (empty($coupon_id)) {
  562. return $this->error('参数错误');
  563. }
  564. $user_coupons = StoreCouponUserRelate::findAll(['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'coupon_id' => $coupon_id, 'is_use' => 0, 'is_giving' => 0, 'status' => 1, 'is_force_failure' => 0]);
  565. if (!empty($user_coupons)) {
  566. $res = StoreCouponUserRelate::updateAll(
  567. ['is_force_failure' => 1, 'updated_at' => time(), 'coupon_status' => 4],
  568. ['mall_id' => $this->mall_id, 'store_id' => $this->store_id,'coupon_id' => $coupon_id, 'is_use' => 0, 'is_giving' => 0, 'status' => 1, 'is_force_failure' => 0]
  569. );
  570. if ($res === false) {
  571. return $this->error('操作失败');
  572. }
  573. }
  574. return $this->success('操作成功');
  575. }
  576. public function actionSetting()
  577. {
  578. $retuest = \Yii::$app->request;
  579. if ($retuest->isAjax) {
  580. if ($retuest->isPost) {
  581. $params = \Yii::$app->request->post();
  582. $res = \Yii::$app->services->validate->validate($params,[
  583. [['is_show_coupon'], 'safe'],
  584. ]);
  585. if($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  586. $service = new StoreService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]);
  587. return $service->updateIsShowCoupon($params)
  588. ? $this->success('成功')
  589. : $this->error($service->getError());
  590. } else {
  591. // 获取配置
  592. $store = Store::findOne(['id'=>$this->store_id]);
  593. $config = [
  594. 'is_show_coupon'=>$store['is_show_coupon']??1,
  595. ];
  596. $this->success('保存成功', $config);
  597. }
  598. }
  599. return $this->render($this->action->id);
  600. }
  601. }