CouponForm.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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\CouponAutoSend;
  10. use app\models\SaasCoupon;
  11. use app\models\User;
  12. use app\models\Store;
  13. use app\models\SaasUser;
  14. use app\models\UserCoupon;
  15. use yii\base\Model;
  16. class CouponForm extends Model
  17. {
  18. public $id;
  19. public $store_id;
  20. public $name;
  21. public $desc;
  22. public $pic_url;
  23. public $discount_type;
  24. public $min_price;
  25. public $sub_price;
  26. public $discount;
  27. public $discount_max_price;
  28. public $expire_type;
  29. public $expire_day;
  30. public $begin_time;
  31. public $end_time;
  32. public $is_delete;
  33. public $total_count;
  34. public $is_join;
  35. public $is_alipay_voucher = 0;
  36. public $sort;
  37. public $cat_id_list;
  38. public $appoint_type;
  39. public $is_integral;
  40. public $integral;
  41. public $price;
  42. public $total_num;
  43. public $user_num;
  44. public $rule;
  45. public $goods_id_list;
  46. public $mch_id;
  47. public $is_join_welfare;
  48. public $is_give;
  49. public $is_business;
  50. public $business_type;
  51. //搜索
  52. public $search_key;
  53. public $search_is_show;
  54. public $search_shop_name;
  55. public $status;
  56. const SCENARIO_ADD = 'add';
  57. const SCENARIO_EDIT = 'edit';
  58. const SCENARIO_DEL = 'del';
  59. const SCENARIO_LIST = 'list';
  60. const DISCOUNT_TYPE_TEXT = [
  61. Coupon::DISCOUNT_TYPE_ONE => '折扣',
  62. Coupon::DISCOUNT_TYPE_TWO => '满减'
  63. ];
  64. const EXPIRE_TYPE_TEXT = [
  65. Coupon::EXPIRE_TYPE_DAY => '天数',
  66. Coupon::IS_INTEGRAL_YES => '指定时间'
  67. ];
  68. const IS_JOIN_TEXT = [
  69. Coupon::IS_JOIN_YES => '加入',
  70. Coupon::IS_JOIN_NO => '不加入'
  71. ];
  72. const APPOINT_TYPE_TEXT = [
  73. Coupon::APPOINT_TYPE_CAT => '商品分类',
  74. Coupon::APPOINT_TYPE_GOODS => '商品',
  75. Coupon::APPOINT_TYPE_FACE => '当面付'
  76. ];
  77. const IS_INTEGRAL_TEXT = [
  78. Coupon::IS_INTEGRAL_YES => '加入',
  79. Coupon::IS_INTEGRAL_NO => '不加入'
  80. ];
  81. const USER_PLATFORM_TYPE = [
  82. User::USER_FROM_WECHAT => '微信小程序',
  83. User::USER_FROM_ALIPAY => '支付宝',
  84. User::USER_FROM_OFFICIAL_ACCOUNTS => '公众号',
  85. User::USER_FROM_APP => 'app微信',
  86. User::USER_FROM_PHONE => '手机号注册',
  87. User::USER_FROM_BYTEDANCE => '抖音',
  88. ];
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function rules()
  93. {
  94. return [
  95. [['status', 'store_id', 'discount_type', 'expire_type', 'expire_day', 'begin_time', 'end_time', 'is_delete', 'total_count', 'is_join', 'sort', 'appoint_type', 'is_integral', 'integral', 'total_num', 'user_num', 'mch_id', 'is_join_welfare', 'is_give','is_business','business_type','is_alipay_voucher'], 'integer'],
  96. [['min_price', 'sub_price', 'discount', 'price'], 'number'],
  97. [['name', 'cat_id_list', 'goods_id_list' ,'search_shop_name'], 'string', 'max' => 255],
  98. [['desc'], 'string', 'max' => 2000],
  99. [['pic_url'], 'string', 'max' => 2048],
  100. [['rule'], 'string', 'max' => 1000],
  101. ['is_delete', 'default', 'value' => Coupon::IS_DELETE_NO],
  102. [['store_id', 'name', 'discount_type', 'min_price', 'expire_type', 'is_join', 'sort', 'appoint_type', 'rule'], 'required', 'on' => [self::SCENARIO_ADD, self::SCENARIO_EDIT]],
  103. [['id'], 'required', 'on' => [self::SCENARIO_DEL, self::SCENARIO_EDIT]],
  104. [['discount_max_price'], 'safe'],
  105. ['search_key', 'string', 'on' => self::SCENARIO_LIST]
  106. ];
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function attributeLabels()
  112. {
  113. return [
  114. 'id' => 'ID',
  115. 'store_id' => 'Store ID',
  116. 'name' => '优惠券名称',
  117. 'desc' => '优惠券描述',
  118. 'pic_url' => '缩略图',
  119. 'discount_type' => '优惠券类型:1=折扣,2=满减',
  120. 'min_price' => '最低消费金额',
  121. 'sub_price' => '优惠金额',
  122. 'discount' => '折扣率',
  123. 'expire_type' => '到期类型:1=领取后N天过期,2=指定有效期',
  124. 'expire_day' => '有效天数,expire_type=1时',
  125. 'begin_time' => '有效期开始时间',
  126. 'end_time' => '有效期结束时间',
  127. 'is_delete' => '是否删除',
  128. 'total_count' => '发放总数量',
  129. 'is_join' => '是否加入领券中心 1--不加入领券中心 2--加入领券中心',
  130. 'sort' => '排序按降序排列',
  131. 'cat_id_list' => 'Cat Id List',
  132. 'appoint_type' => 'Appoint Type',
  133. 'is_integral' => '是否加入积分商城 1--不加入 2--加入',
  134. 'integral' => '兑换需要积分数量',
  135. 'price' => '售价',
  136. 'total_num' => '积分商城发放总数',
  137. 'user_num' => '每人限制兑换数量',
  138. 'rule' => '使用说明',
  139. 'goods_id_list' => 'Goods Id List',
  140. 'mch_id' => '入驻商id',
  141. 'is_give' => '是否可转增',
  142. 'is_business' => '是否是商盟',
  143. 'business_type' => '展示位置',
  144. ];
  145. }
  146. public function scenarios()
  147. {
  148. $scenarios = parent::scenarios();
  149. return $scenarios;
  150. }
  151. public function saveCoupon()
  152. {
  153. if ($this->validate()) {
  154. //$t = \Yii::$app->db->beginTransaction();
  155. $model = Coupon::findOne(['id' => $this->id]) ?: new Coupon();
  156. $model->attributes = $this->attributes;
  157. $model->is_business = $this->is_business ?: 0;
  158. $model->mch_id = get_mch_id();
  159. if (!$model->save()) {
  160. //$t->rollBack();
  161. return [
  162. 'code' => 1,
  163. 'msg' => $model->getErrorSummary(false)[0]
  164. ];
  165. }
  166. //$t->commit();
  167. return [
  168. 'code' => 0,
  169. 'msg' => '保存成功'
  170. ];
  171. } else {
  172. // 验证失败:$errors 是一个包含错误信息的数组
  173. return [
  174. 'code' => 1,
  175. "msg" => $this->getErrorSummary(false)[0]
  176. ];
  177. }
  178. }
  179. //获取店铺信息
  180. private function getStoreInfoById($store_id){
  181. $storeInfo = Store::find()->where(['id'=>$store_id,'is_delete'=>0])->asArray()->one();
  182. if($storeInfo && $storeInfo['name']){
  183. return $storeInfo['name'];
  184. }else{
  185. return "店铺暂无名称";
  186. }
  187. }
  188. public function searchCoupon()
  189. {
  190. $query = Coupon::find()->alias('c')->leftJoin(['uc' => UserCoupon::find()->where(['store_id' => $this->store_id, 'is_delete' => 0])->select('coupon_id, count(`coupon_id`) send_num')->groupBy('coupon_id')], 'c.id = uc.coupon_id');
  191. //is_business 商盟优惠券不展示在店铺优惠券列表
  192. $query->where(['c.is_delete' => Coupon::IS_DELETE_NO])->orderBy("c.sort desc,c.id desc");
  193. if($this->search_shop_name){
  194. $query->leftJoin(['s' => Store::tableName()], 's.id=c.store_id')->andWhere(['like', 's.name', $this->search_shop_name]);
  195. }
  196. if($this->store_id){
  197. $query->andWhere(['c.store_id'=>$this->store_id]);
  198. }
  199. if($this->is_join){
  200. $query->andWhere(['c.is_join'=>$this->is_join]);
  201. }
  202. if($this->is_alipay_voucher){
  203. $query->andWhere(['c.is_alipay_voucher'=>$this->is_alipay_voucher]);
  204. }
  205. if($this->is_business){
  206. $query->andWhere(['c.is_business'=>$this->is_business]);
  207. }else{
  208. $query->andWhere(['c.is_business'=>0]);
  209. }
  210. if($this->business_type){
  211. $query->andWhere(['c.business_type'=>$this->business_type]);
  212. }
  213. // 搜索
  214. if ($this->search_key) {
  215. $query->andWhere(['like', 'c.name', $this->search_key]);
  216. }
  217. // 已结束
  218. $arr = $storeName = [];
  219. if ($this->status == 2) {
  220. $query->andWhere(['or',
  221. ['and',
  222. ['<','c.end_time',time()],
  223. ['<>','c.total_count', -1]
  224. ],[
  225. '>', 'uc.send_num', 'c.total_count'
  226. ]
  227. ]);
  228. $list = $query->asArray()->all();
  229. foreach ($list as $val) {
  230. $val['coupon_status'] = 1;
  231. if($this->is_business){
  232. //获取店铺名称
  233. if(isset($storeName[$val['store_id']]) && $storeName[$val['store_id']] ){
  234. $val['store_name'] = $storeName[$val['store_id']];
  235. }else{
  236. $store_name = $this->getStoreInfoById($val['store_id']);
  237. $storeName[$val['store_id']] = $store_name;
  238. $val['store_name'] = $store_name;
  239. }
  240. }
  241. if ($val['total_count'] - self::getUsedCount($val['id'], $this->is_business) == 0) {
  242. $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
  243. $val['updated_at'] = date('Y-m-d H:i:s', $val['updated_at']);
  244. $val['begin_time'] = date('Y-m-d', $val['begin_time']);
  245. $val['end_time'] = date('Y-m-d', $val['end_time']);
  246. $val['discount_type_text'] = self::DISCOUNT_TYPE_TEXT[$val['discount_type']];
  247. $val['is_join_text'] = self::IS_JOIN_TEXT[$val['is_join']];
  248. // 优惠券剩余
  249. if ($val['total_count'] == -1) {
  250. $val['surplus_count'] = -1;
  251. } else {
  252. $val['surplus_count'] = (int)$val['total_count'] - (int)self::getUsedCount($val['id'], $this->is_business);
  253. }
  254. // 已领
  255. $val['receive_count'] = (int)self::getUsedCount($val['id'], $this->is_business);
  256. $val['coupon_status'] = 2;
  257. $val['cat_list'] = CatForm::getCatListById(json_decode($val['cat_id_list']));
  258. $val['goods_list'] = GoodsForm::getGoodsListById(json_decode($val['goods_id_list']));
  259. $arr[] = $val;
  260. }
  261. }
  262. $count = count($arr);
  263. $data = array_slice($arr, (get_params('pageNo') - 1) * 10, 10);
  264. return [
  265. 'code' => 0,
  266. 'msg' => 'success',
  267. 'data' => [
  268. 'is_join_saas' => intval(\Yii::$app->isSaas()),
  269. 'data' => $data,
  270. 'pageNo' => intval(get_params('pageNo')),
  271. 'totalCount' => $count,
  272. ]
  273. ];
  274. } else if ($this->status == 1) {
  275. $list = $query->asArray()->all();
  276. foreach ($list as $val) {
  277. if($this->is_business){
  278. //获取店铺名称
  279. if(isset($storeName[$val['store_id']]) && $storeName[$val['store_id']] ){
  280. $val['store_name'] = $storeName[$val['store_id']];
  281. }else{
  282. $store_name = $this->getStoreInfoById($val['store_id']);
  283. $storeName[$val['store_id']] = $store_name;
  284. $val['store_name'] = $store_name;
  285. }
  286. }
  287. if ($val['total_count'] == -1 || $val['total_count'] - self::getUsedCount($val['id'], $this->is_business) > 0) {
  288. $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
  289. $val['updated_at'] = date('Y-m-d H:i:s', $val['updated_at']);
  290. $val['begin_time'] = date('Y-m-d', $val['begin_time']);
  291. $val['end_time'] = date('Y-m-d', $val['end_time']);
  292. $val['discount_type_text'] = self::DISCOUNT_TYPE_TEXT[$val['discount_type']];
  293. $val['is_join_text'] = self::IS_JOIN_TEXT[$val['is_join']];
  294. // 优惠券剩余
  295. $val['coupon_status'] = 1;
  296. // 优惠券剩余
  297. if ($val['total_count'] == -1) {
  298. $val['surplus_count'] = -1;
  299. } else {
  300. $val['surplus_count'] = (int)$val['total_count'] - (int)self::getUsedCount($val['id'], $this->is_business);
  301. }
  302. // 已领
  303. $val['receive_count'] = (int)self::getUsedCount($val['id'], $this->is_business);
  304. $val['cat_list'] = CatForm::getCatListById(json_decode($val['cat_id_list']));
  305. $val['goods_list'] = GoodsForm::getGoodsListById(json_decode($val['goods_id_list']));
  306. $arr[] = $val;
  307. }
  308. }
  309. foreach ($arr as $k => $v) {
  310. if (strtotime($v['begin_time']) > time()) {
  311. // 删除不符合条件的数组下标
  312. unset($arr[$k]);
  313. }
  314. }
  315. $count = count($arr);
  316. $data = array_slice($arr, (input_params('pageNo') - 1) * input_params('pageSize', 10), input_params('pageSize', 10));
  317. return [
  318. 'code' => 0,
  319. 'msg' => 'success',
  320. 'data' => [
  321. 'is_join_saas' => intval(\Yii::$app->isSaas()),
  322. 'data' => $data,
  323. 'pageNo' => intval(get_params('pageNo')),
  324. 'totalCount' => $count,
  325. ]
  326. ];
  327. } else if ($this->status == 0) {
  328. $query->andWhere(['>', 'c.begin_time', time()]);
  329. $list = pagination_make($query);
  330. foreach ($list['list'] as &$val) {
  331. if($this->is_business){
  332. //获取店铺名称
  333. if(isset($storeName[$val['store_id']]) && $storeName[$val['store_id']] ){
  334. $val['store_name'] = $storeName[$val['store_id']];
  335. }else{
  336. $store_name = $this->getStoreInfoById($val['store_id']);
  337. $storeName[$val['store_id']] = $store_name;
  338. $val['store_name'] = $store_name;
  339. }
  340. }
  341. $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
  342. $val['updated_at'] = date('Y-m-d H:i:s', $val['updated_at']);
  343. $val['begin_time'] = date('Y-m-d', $val['begin_time']);
  344. $val['end_time'] = date('Y-m-d', $val['end_time']);
  345. $val['discount_type_text'] = self::DISCOUNT_TYPE_TEXT[$val['discount_type']];
  346. $val['is_join_text'] = self::IS_JOIN_TEXT[$val['is_join']];
  347. $val['coupon_status'] = 0;
  348. // 优惠券剩余
  349. if ($val['total_count'] == -1) {
  350. $val['surplus_count'] = -1;
  351. } else {
  352. $val['surplus_count'] = (int)$val['total_count'] - (int)self::getUsedCount($val['id'], $this->is_business);
  353. }
  354. // 已领
  355. $val['receive_count'] = (int)self::getUsedCount($val['id'], $this->is_business);
  356. $val['cat_list'] = CatForm::getCatListById(json_decode($val['cat_id_list']));
  357. $val['goods_list'] = GoodsForm::getGoodsListById(json_decode($val['goods_id_list']));
  358. }
  359. } else {
  360. $list = pagination_make($query);
  361. foreach ($list['list'] as &$val) {
  362. if($this->is_business){
  363. //获取店铺名称
  364. if(isset($storeName[$val['store_id']]) && $storeName[$val['store_id']] ){
  365. $val['store_name'] = $storeName[$val['store_id']];
  366. }else{
  367. $store_name = $this->getStoreInfoById($val['store_id']);
  368. $storeName[$val['store_id']] = $store_name;
  369. $val['store_name'] = $store_name;
  370. }
  371. }
  372. // if ($val['total_count'] == -1 || $val['total_count'] - self::getUsedCount($val['id'], $this->is_business) > 0) {
  373. // $val['coupon_status'] = 1;//进行中
  374. // } elseif (($val['total_count'] !== -1 && $val['total_count'] - self::getUsedCount($val['id'], $this->is_business) == 0) || time() > $val['end_time'])
  375. // {
  376. // $val['coupon_status'] = 2;//已结束
  377. // } elseif ($val['begin_time'] > time()) {
  378. // $val['coupon_status'] = 0;//未开始
  379. // }
  380. $val['coupon_status'] = 1;
  381. if (intval($val['expire_type']) === 1) {//领取后N天过期
  382. if (intval($val['total_count']) !== -1 && $val['total_count'] - self::getUsedCount($val['id'], $this->is_business) <= 0) {//进行中的未设置未不限制 并且已经领取完毕
  383. $val['coupon_status'] = 2;//已结束
  384. }
  385. } else {//指定有效期
  386. if (intval($val['begin_time']) > time()) {
  387. $val['coupon_status'] = 0;//未开始
  388. }
  389. if (intval($val['end_time']) < time()) {
  390. $val['coupon_status'] = 2;//已结束
  391. }
  392. if (intval($val['coupon_status']) === 1) {//进行中
  393. if (intval($val['total_count']) !== -1 && $val['total_count'] - self::getUsedCount($val['id'], $this->is_business) <= 0) {//进行中的未设置未不限制 并且已经领取完毕
  394. $val['coupon_status'] = 2;//已结束
  395. }
  396. }
  397. }
  398. $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
  399. $val['updated_at'] = date('Y-m-d H:i:s', $val['updated_at']);
  400. $val['begin_time'] = date('Y-m-d', $val['begin_time']);
  401. $val['end_time'] = date('Y-m-d', $val['end_time']);
  402. $val['discount_type_text'] = self::DISCOUNT_TYPE_TEXT[$val['discount_type']];
  403. $val['is_join_text'] = self::IS_JOIN_TEXT[$val['is_join']];
  404. // 优惠券剩余
  405. if ($val['total_count'] == -1) {
  406. $val['surplus_count'] = -1;
  407. } else {
  408. $val['surplus_count'] = (int)$val['total_count'] - (int)self::getUsedCount($val['id'], $this->is_business);
  409. }
  410. // 已领
  411. $val['receive_count'] = (int)self::getUsedCount($val['id'], $this->is_business);
  412. $val['cat_list'] = CatForm::getCatListById(json_decode($val['cat_id_list']));
  413. $val['goods_list'] = GoodsForm::getGoodsListById(json_decode($val['goods_id_list']));
  414. }
  415. foreach ($list['list'] as $k => $v) {
  416. if (strtotime($v['begin_time']) > time()) {
  417. // 删除不符合条件的数组下标
  418. $list['list'][$k]['coupon_status'] = 0;
  419. }
  420. }
  421. }
  422. return [
  423. 'code' => 0,
  424. 'msg' => 'success',
  425. 'data' => [
  426. 'is_join_saas' => intval(\Yii::$app->isSaas()),
  427. 'data' => $list['list'],
  428. 'pageNo' => $list['pageNo'],
  429. 'totalCount' => $list['totalCount']
  430. ]
  431. ];
  432. }
  433. public function delCoupon()
  434. {
  435. if ($this->validate()) {
  436. $t = \Yii::$app->db->beginTransaction();
  437. if ($this->scenario !== self::SCENARIO_DEL) {
  438. return [
  439. 'code' => 1,
  440. 'msg' => '删除失败'
  441. ];
  442. }
  443. $model = Coupon::findOne(['id' => $this->id]);
  444. $model->is_delete = Coupon::IS_DELETE_YES;
  445. if (!$model || !$model->save()) {
  446. $t->rollBack();
  447. return [
  448. 'code' => 1,
  449. 'msg' => $model->getErrorSummary(false)[0]
  450. ];
  451. }
  452. $t->commit();
  453. return [
  454. 'code' => 0,
  455. 'msg' => '删除成功'
  456. ];
  457. // 所有输入数据都有效 all inputs are valid
  458. } else {
  459. // 验证失败:$errors 是一个包含错误信息的数组
  460. return [
  461. 'code' => 1,
  462. "msg" => $this->getErrorSummary(false)[0]
  463. ];
  464. }
  465. }
  466. public static function getCouponList()
  467. {
  468. return Coupon::find()->where(['is_delete' => Coupon::IS_DELETE_NO, 'store_id' => get_store_id()])->orderBy('sort desc, id desc')->select('*')->asArray()->all();
  469. }
  470. /**
  471. * Undocumented function
  472. *
  473. * @Author LGL 24963@qq.com
  474. * @DateTime 2021-01-13
  475. * @desc: 优惠券发放
  476. * @param [type] $user_id
  477. * @param [type] $coupon_id
  478. * @param integer $coupon_auto_send_id
  479. * @param integer $type
  480. */
  481. public static function userAddCoupon($user_id, $coupon_id, $coupon_auto_send_id = 0, $type = 0)
  482. {
  483. $user = User::findOne($user_id);
  484. if (!$user) {
  485. return [
  486. 'code' => 1,
  487. 'msg' => '用户不存在'
  488. ];
  489. }
  490. $coupon = Coupon::findOne([
  491. 'id' => $coupon_id,
  492. 'is_delete' => Coupon::IS_DELETE_NO,
  493. ]);
  494. if (!$coupon) {
  495. return [
  496. 'code' => 1,
  497. 'msg' => '优惠券不存在'
  498. ];
  499. }
  500. $receive_count = UserCoupon::find()->where(['coupon_id' => $coupon_id, 'is_delete' => UserCoupon::IS_DELETE_NO, 'store_id' => $user->store_id])->andWhere(['!=', 'type', 3])->count();
  501. if ($coupon->total_count != -1 && $receive_count >= $coupon->total_count) {
  502. return [
  503. 'code' => 1,
  504. 'msg' => '优惠券已发放完'
  505. ];
  506. }
  507. $user_coupon = new UserCoupon();
  508. if ($type == UserCoupon::TYPE_GET) {
  509. $res = UserCoupon::find()->where(['store_id' => $user->store_id, 'is_delete' => UserCoupon::IS_DELETE_NO, 'type' => UserCoupon::TYPE_GET, 'user_id' => $user_id, 'coupon_id' => $coupon_id])->exists();
  510. if ($res) {
  511. return [
  512. 'code' => 0,
  513. 'msg' => '领取成功'
  514. ];
  515. }
  516. }
  517. if ($coupon_auto_send_id) {
  518. $coupon_auto_send = CouponAutoSend::findOne([
  519. 'store_id' => $user->store_id,
  520. 'status' => 1,
  521. 'id' => $coupon_auto_send_id,
  522. 'is_delete' => CouponAutoSend::IS_DELETE_NO
  523. ]);
  524. if (!$coupon_auto_send) {
  525. return [
  526. 'code' => 1,
  527. 'msg' => '自动发放不存在'
  528. ];
  529. }
  530. $user_coupon->coupon_auto_send_id = $coupon_auto_send->id;
  531. $type = $user_coupon::TYPE_AUTO;
  532. }
  533. $user_coupon->type = $type;
  534. $user_coupon->store_id = $user->store_id;
  535. $user_coupon->user_id = $user->id;
  536. $user_coupon->coupon_id = $coupon->id;
  537. if ($coupon->expire_type == $coupon::EXPIRE_TYPE_DAY) {
  538. $user_coupon->begin_time = time();
  539. $user_coupon->end_time = time() + max(0, 86400 * $coupon->expire_day);
  540. } elseif ($coupon->expire_type == $coupon::EXPIRE_TYPE_DATE) {
  541. $user_coupon->begin_time = $coupon->begin_time;
  542. $user_coupon->end_time = $coupon->end_time;
  543. if (time() > $user_coupon->end_time) {
  544. return [
  545. 'code' => 1,
  546. 'msg' => '优惠券已过期'
  547. ];
  548. }
  549. }
  550. $user_coupon->is_expire = UserCoupon::IS_DELETE_NO;
  551. $user_coupon->is_use = UserCoupon::IS_USE_NO;
  552. $user_coupon->is_delete = UserCoupon::IS_DELETE_NO;
  553. if (!$user_coupon->save()) {
  554. return [
  555. 'code' => 1,
  556. 'msg' => '发放失败'
  557. ];
  558. }
  559. return [
  560. 'code' => 0,
  561. 'msg' => '发放成功',
  562. 'data' => $coupon,
  563. ];
  564. }
  565. /**
  566. * 优惠券发放记录
  567. * @return array
  568. */
  569. public function CouponSendRecord()
  570. {
  571. if ($this->id) {
  572. // 优惠券详情
  573. $couponDetail = Coupon::find()->where(['id' => $this->id, 'store_id' => $this->store_id])->asArray()->one();
  574. // 已领取数量
  575. $couponDetail['receive_count'] = (int)self::getUsedCount($this->id);
  576. // 剩余数量
  577. $couponDetail['surplus_count'] = $couponDetail['total_count'] == -1 ? '无限制' : (int)$couponDetail['total_count'] - (int)(self::getUsedCount($this->id));
  578. // 已结束
  579. if ((int)$couponDetail['total_count'] - (int)self::getUsedCount($this->id) <= 0) {
  580. $couponDetail['coupon_status'] = 2;
  581. }
  582. // 进行中
  583. if ((int)$couponDetail['total_count'] - (int)self::getUsedCount($this->id) > 0 || $couponDetail['total_count'] == -1) {
  584. $couponDetail['coupon_status'] = 1;
  585. }
  586. // 未开始
  587. if ($couponDetail['begin_time'] > time()) {
  588. $couponDetail['coupon_status'] = 0;
  589. }
  590. $couponDetail['event_text'] = CouponAutoSendForm::EVENT_TEXT;
  591. $couponDetail['begin_time'] = date('Y-m-d H:i:s', $couponDetail['begin_time']);
  592. $couponDetail['end_time'] = date('Y-m-d H:i:s', $couponDetail['end_time']);
  593. $arr = [];
  594. // 优惠券发放规则
  595. $couponRule = CouponAutoSend::find()
  596. ->where(['store_id' => $this->store_id, 'is_delete' => CouponAutoSend::IS_DELETE_NO])
  597. ->andWhere(['<>', 'coupon_id', ''])
  598. ->asArray()->all();
  599. foreach ($couponRule as &$val) {
  600. // 获取符合条件的id集合
  601. if (in_array($this->id, json_decode($val['coupon_id']))) {
  602. $arr[] = $val['id'];
  603. }
  604. }
  605. $couponList = CouponAutoSend::find()->where(['in', 'id', $arr, 'store_id' => $this->store_id, 'is_delete' => CouponAutoSend::IS_DELETE_NO])->asArray()->all();
  606. $query = UserCoupon::find()->alias('uc')->leftJoin(['u' => User::tableName()], 'u.id=uc.user_id')
  607. ->where(['uc.store_id' => $this->store_id, 'uc.is_delete' => UserCoupon::IS_DELETE_NO, 'uc.coupon_id' => $this->id])->andWhere(['!=', 'uc.type', 3])->groupBy('uc.user_id');
  608. $query->select(['uc.*', 'u.binding', 'u.avatar_url', 'u.nickname', 'u.platform']);
  609. $list = pagination_make($query);
  610. if(count($list['list']) > 0 ){
  611. foreach ($list['list'] as &$val) {
  612. $sassuser = SaasUser::find()->where(['mobile' => $val['binding']])->select('name, avatar')->asArray()->one();
  613. if($sassuser){
  614. $val['avatar_url'] = !empty($sassuser['avatar']) ? $sassuser['avatar'] : $val['avatar_url'];
  615. $val['nickname'] = !empty($sassuser['name']) ? $sassuser['name'] : $val['nickname'];
  616. }
  617. if($val['platform']) $val['platform'] = self::USER_PLATFORM_TYPE[$val['platform']];
  618. // 已领
  619. $val['receive_count'] = UserCoupon::find()->where(['user_id' => $val['user_id'], 'is_delete' => UserCoupon::IS_DELETE_NO, 'store_id' => $this->store_id, 'coupon_id' => $val['coupon_id']])->count();
  620. // 已使用
  621. $val['use_count'] = UserCoupon::find()->where(['user_id' => $val['user_id'], 'is_delete' => UserCoupon::IS_DELETE_NO, 'store_id' => $this->store_id, 'is_use' => UserCoupon::IS_USE_YES, 'coupon_id' => $val['coupon_id']])->count();
  622. }
  623. }
  624. $data = [
  625. 'data' => $list['list'],
  626. 'pageNo' => $list['pageNo'],
  627. 'totalCount' => $list['totalCount'],
  628. 'couponDetail' => $couponDetail,
  629. 'couponList' => $couponList,
  630. ];
  631. return [
  632. 'code' => 0,
  633. 'msg' => 'success',
  634. 'data' => $data,
  635. ];
  636. } else {
  637. return [
  638. 'code' => 1,
  639. 'msg' => '参数错误',
  640. ];
  641. }
  642. }
  643. /**
  644. * Undocumented function
  645. *
  646. * @Author LGL 24963@qq.com
  647. * @DateTime 2021-01-13
  648. * @desc: 获取已领取的优惠券数
  649. * @param [int] $coupon_id
  650. */
  651. public static function getUsedCount($coupon_id, $is_business = 0)
  652. {
  653. if ($is_business) {
  654. $model = SaasCoupon::find();
  655. } else {
  656. $model = UserCoupon::find();
  657. }
  658. return $model->where(['coupon_id' => $coupon_id, 'is_delete' => UserCoupon::IS_DELETE_NO, 'store_id' => get_store_id()])->andWhere(['!=', 'type', 3])->count();
  659. }
  660. }