Coupon.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. use yii\db\ActiveRecord;
  10. use yii\behaviors\TimestampBehavior;
  11. use app\models\WechatConfig;
  12. use app\models\Option;
  13. use app\modules\admin\models\mochat\MochatForm;
  14. /**
  15. * This is the model class for table "{{%coupon}}".
  16. *
  17. * @property int $id
  18. * @property int $store_id
  19. * @property string $name 优惠券名称
  20. * @property string $desc 优惠券描述
  21. * @property string|null $pic_url 缩略图
  22. * @property int $discount_type 优惠券类型:1=折扣,2=满减
  23. * @property float $min_price 最低消费金额
  24. * @property float $sub_price 优惠金额
  25. * @property float $discount 折扣率
  26. * @property int $expire_type 到期类型:1=领取后N天过期,2=指定有效期
  27. * @property int $expire_day 有效天数,expire_type=1时
  28. * @property int $begin_time 有效期开始时间
  29. * @property int $end_time 有效期结束时间
  30. * @property int $created_at 添加时间
  31. * @property int $is_delete 是否删除
  32. * @property int $total_count 发放总数量
  33. * @property int $is_join 是否加入领券中心 1--不加入领券中心 2--加入领券中心
  34. * @property int $is_alipay_voucher
  35. * @property int|null $sort 排序按升序排列
  36. * @property string|null $cat_id_list
  37. * @property int|null $appoint_type
  38. * @property int $is_integral 是否加入积分商城 1--不加入 2--加入
  39. * @property int $integral 兑换需要积分数量
  40. * @property float $price 售价
  41. * @property int $total_num 积分商城发放总数
  42. * @property int $user_num 每人限制兑换数量
  43. * @property string|null $rule 使用说明
  44. * @property string|null $goods_id_list
  45. * @property int|null $mch_id 入驻商id
  46. * @property int|null $updated_at
  47. * @property int $is_join_welfare 是否加入到福利中心
  48. * @property int $is_give
  49. */
  50. class Coupon extends \yii\db\ActiveRecord
  51. {
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public static function tableName()
  56. {
  57. return '{{%coupon}}';
  58. }
  59. const IS_DELETE_YES = 1;//已删除
  60. const IS_DELETE_NO = 0;//未删除
  61. const IS_JOIN_YES = 2; //加入领券中心
  62. const IS_JOIN_NO = 1;//不加入领券中心
  63. const DISCOUNT_TYPE_ONE = 1;//折扣类型
  64. const DISCOUNT_TYPE_TWO = 2; //满减类型
  65. const EXPIRE_TYPE_DAY = 1;//领取后n天过期
  66. const EXPIRE_TYPE_DATE = 2;//指定有效期
  67. const IS_INTEGRAL_YES = 2;//加入积分商城
  68. const IS_INTEGRAL_NO = 1;//不加入积分商城
  69. const APPOINT_TYPE_CAT = 1;//商品分类
  70. const APPOINT_TYPE_GOODS = 2;//商品
  71. const APPOINT_TYPE_FACE = 3;//当面付
  72. /**
  73. * saas 加入福利中心
  74. */
  75. const IS_JOIN_WELFARE_YES = 1;
  76. /**
  77. * saas 不加入福利中心
  78. */
  79. const IS_JOIN_WELFARE_NO = 0;
  80. public function behaviors()
  81. {
  82. return [
  83. [
  84. 'class' => TimestampBehavior::class,
  85. 'attributes' => [
  86. ActiveRecord::EVENT_BEFORE_INSERT => ['updated_at', 'created_at'],
  87. ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at'
  88. ]
  89. ]
  90. ];
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function rules()
  96. {
  97. return [
  98. [['store_id', 'name'], 'required'],
  99. [['store_id', 'discount_type', 'updated_at', 'expire_type', 'expire_day', 'begin_time', 'end_time', 'created_at', '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'],
  100. [['min_price', 'sub_price', 'discount', 'price'], 'number'],
  101. [['name', 'cat_id_list', 'goods_id_list'], 'string', 'max' => 255],
  102. [['desc'], 'string', 'max' => 2000],
  103. [['pic_url'], 'string', 'max' => 2048],
  104. [['rule'], 'string', 'max' => 1000],
  105. ['discount', 'default', 'value' => 10],
  106. ['is_integral', 'default', 'value' => 1],
  107. ['integral', 'default', 'value' => 0],
  108. ['price', 'default', 'value' => 0],
  109. ['total_num', 'default', 'value' => 0],
  110. ['expire_day', 'default', 'value' => 0],
  111. ['user_num', 'default', 'value' => 0],
  112. ['sub_price', 'default', 'value' => 0],
  113. ['total_count', 'default', 'value' => -1],
  114. [['begin_time', 'end_time'], 'default', 'value' => 0],
  115. [['is_alipay_voucher'], 'safe']
  116. ];
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function attributeLabels()
  122. {
  123. return [
  124. 'id' => 'ID',
  125. 'store_id' => 'Store ID',
  126. 'name' => '优惠券名称',
  127. 'desc' => '优惠券描述',
  128. 'pic_url' => '缩略图',
  129. 'discount_type' => '优惠券类型:1=折扣,2=满减',
  130. 'min_price' => '最低消费金额',
  131. 'sub_price' => '优惠金额',
  132. 'discount' => '折扣率',
  133. 'expire_type' => '到期类型:1=领取后N天过期,2=指定有效期',
  134. 'expire_day' => '有效天数,expire_type=1时',
  135. 'begin_time' => '有效期开始时间',
  136. 'end_time' => '有效期结束时间',
  137. 'created_at' => '添加时间',
  138. 'is_delete' => '是否删除',
  139. 'total_count' => '发放总数量',
  140. 'is_join' => '是否加入领券中心 1--不加入领券中心 2--加入领券中心',
  141. 'sort' => '排序按降序排列',
  142. 'cat_id_list' => 'Cat Id List',
  143. 'appoint_type' => 'Appoint Type',
  144. 'is_integral' => '是否加入积分商城 1--不加入 2--加入',
  145. 'integral' => '兑换需要积分数量',
  146. 'price' => '售价',
  147. 'total_num' => '积分商城发放总数',
  148. 'user_num' => '每人限制兑换数量',
  149. 'rule' => '使用说明',
  150. 'goods_id_list' => 'Goods Id List',
  151. 'mch_id' => '入驻商id',
  152. 'updated_at' => 'Update Time',
  153. 'is_join_welfare' => '是否加入福利中心 1--不加入福利中心 2--加入福利中心',
  154. 'is_give' => '是否可转增',
  155. 'is_business' => '是否是商盟',
  156. 'business_type' => '展示位置',
  157. ];
  158. }
  159. /**
  160. * @param int $mch_id
  161. * @param int $limit
  162. * @param int $all
  163. * @return array
  164. */
  165. public static function getList($mch_id = 0, $limit = 0, $all = 0, $goods_id_list = [], $coupon_id = 0)
  166. {
  167. $user_id = get_user_id();
  168. $store_id = get_store_id();
  169. //商盟优惠券不展示在领取列表
  170. $coupon_form = Coupon::find()->alias('c')->where([
  171. 'c.is_delete' => 0, 'c.is_join' => 2, 'c.is_business' => 0,'c.store_id' => $store_id
  172. ])->andWhere(['!=', 'c.total_count', 0])
  173. ->leftJoin(UserCoupon::tableName() . ' uc', "uc.coupon_id=c.id and uc.user_id ={$user_id} and uc.type != 3 and uc.is_delete=0")
  174. ->select(['c.*', '(case when isnull(uc.id) then 0 else 1 end) as is_receive']);
  175. if (empty($all)) {
  176. $coupon_form->andWhere([
  177. 'c.mch_id' => $mch_id
  178. ]);
  179. }
  180. if (!empty($coupon_id)) {
  181. $coupon_form->andWhere([
  182. 'c.id' => $coupon_id
  183. ]);
  184. MochatForm::sendMsg(1, $store_id, MochatForm::MSG_TYPE_VIEW_COUPON, get_saas_user_id(), ['coupon_id' => $coupon_id]);
  185. }
  186. if ($limit > 0) {
  187. $coupon_form->limit($limit);
  188. }
  189. $coupon_list = $coupon_form->orderBy('is_receive ASC, sort DESC, id DESC')->asArray()->all();
  190. // \Yii::error([__METHOD__, $coupon_id, empty($coupon_id), $coupon_form->createCommand()->getRawSql()]);
  191. $new_list = [];
  192. foreach ($coupon_list as $index => $value) {
  193. $coupon_list[$index]['mch_id'] = $value['mch_id'] ? $value['mch_id'] : 0;
  194. $coupon_list[$index]['desc'] = !empty($value['desc']) ? $value['desc'] : '';
  195. if ($value['min_price'] >= 100) {
  196. $coupon_list[$index]['min_price'] = (int)$value['min_price'];
  197. }
  198. if ($value['sub_price'] >= 100) {
  199. $coupon_list[$index]['sub_price'] = (int)$value['sub_price'];
  200. }
  201. $coupon_list[$index]['begintime'] = date('Y.m.d', $value['begin_time']);
  202. $coupon_list[$index]['endtime'] = date('Y.m.d', $value['end_time']);
  203. if ($value['mch_id'] > 0) {
  204. $coupon_list[$index]['content'] = "适用范围:". Mch::findOne($value['mch_id'])->name;
  205. } else {
  206. $coupon_list[$index]['content'] = "适用范围:全场通用";
  207. }
  208. if ($value['appoint_type'] == 1 && $value['cat_id_list'] !== 'null' && $value['cat_id_list'] !== '[]') {
  209. if ($value['mch_id'] > 0) {
  210. $coupon_list[$index]['cat'] = MchCat::find()->select('id, name')->where(['is_delete'=>0, 'id' => json_decode($value['cat_id_list'])])->asArray()->all();
  211. }else {
  212. $coupon_list[$index]['cat'] = Cat::find()->select('id, name')->where(['store_id' => $store_id,'is_delete' => 0, 'id' => json_decode($value['cat_id_list'])])->asArray()->all();
  213. }
  214. $cat_list = [];
  215. foreach ($coupon_list[$index]['cat'] as $item) {
  216. $cat_list[] = $item['name'];
  217. }
  218. $coupon_list[$index]['content'] = "适用范围:仅限分类 ".implode('、', $cat_list)."使用";
  219. $coupon_list[$index]['goods'] = [];
  220. } elseif ($value['appoint_type'] == 2 && $value['goods_id_list'] !== 'null' && $value['goods_id_list'] !== null) {
  221. $coupon_list[$index]['goods'] = Goods::find()->select('id,name,price,cover_pic')->where(['store_id' => $store_id, 'is_delete' => 0, 'id' => json_decode($value['goods_id_list'])])->asArray()->all();
  222. $coupon_list[$index]['cat'] = [];
  223. $coupon_list[$index]['content'] = "适用范围:指定商品使用";
  224. } else {
  225. $coupon_list[$index]['goods'] = [];
  226. $coupon_list[$index]['cat'] = [];
  227. }
  228. if ($value['is_receive'] == 0) {
  229. $coupon_list[$index]['receive_content'] = '立即领取';
  230. } else {
  231. $coupon_list[$index]['receive_content'] = '已领取';
  232. }
  233. $coupon_count = UserCoupon::find()->where([
  234. 'store_id' => $store_id, 'is_delete' => 0, 'coupon_id' => $value['id'], 'type' => 2
  235. ])->count();
  236. if ($value['total_count'] > $coupon_count || $value['total_count'] == -1) {
  237. if ($value['expire_type'] == 2) {
  238. if (strtotime(date('Y-m-d 23:59:59', $value['end_time'])) >= time()) {
  239. $new_list[] = $coupon_list[$index];
  240. }
  241. } else {
  242. $new_list[] = $coupon_list[$index];
  243. }
  244. }
  245. }
  246. if (empty($goods_id_list)) {
  247. return $new_list;
  248. }
  249. $list = [];
  250. foreach ($new_list as $value) {
  251. if ($value['is_receive'] == 1) {
  252. continue; // 已领取的跳过
  253. }
  254. if ($value['appoint_type'] == 1 && $value['cat_id_list'] !== 'null' && $value['cat_id_list'] !== '[]') {
  255. continue; // 适用于分类的跳过
  256. }
  257. if ($value['appoint_type'] == 2 && $value['goods_id_list'] !== 'null' && $value['goods_id_list'] !== null) {
  258. // 判断该优惠券是否指定商品,如果指定但是给定的goods_id_list中的商品id不存在,则跳过
  259. $idList = json_decode($value['goods_id_list']);
  260. $is = false;
  261. foreach ($goods_id_list as $v) {
  262. if (in_array($v, $idList)) {
  263. $is = true;
  264. break;
  265. }
  266. }
  267. if (!$is) {
  268. continue;
  269. }
  270. }
  271. $list[] = $value;
  272. }
  273. return $list;
  274. }
  275. /**
  276. * @param int $limit
  277. * @param int $all
  278. * @return array
  279. */
  280. public static function getSaasList($limit = 0)
  281. {
  282. $user_id = get_user_id();
  283. $store_id = get_store_id();
  284. $coupon_form = Coupon::find()->alias('c')->where([
  285. 'c.is_delete' => 0,
  286. ])->andWhere(['!=', 'c.total_count', 0])
  287. ->leftJoin(UserCoupon::tableName() . ' uc', "uc.coupon_id=c.id and uc.user_id ={$user_id} and (uc.type = 3 or uc.type = 4) and uc.is_delete=0")
  288. ->select(['c.*', '(case when isnull(uc.id) then 0 else 1 end) as is_receive']);
  289. if ($limit > 0) {
  290. $coupon_form->limit($limit);
  291. }
  292. $coupon_list = $coupon_form->orderBy('is_receive ASC, sort ASC, id DESC')->asArray()->all();
  293. $new_list = [];
  294. foreach ($coupon_list as $index => $value) {
  295. // TODO: store 信息
  296. $coupon_list[$index]['store_id'] = $value['store_id'] ? $value['store_id'] : 0;
  297. $coupon_list[$index]['desc'] = !empty($value['desc']) ? $value['desc'] : '';
  298. if ($value['min_price'] >= 100) {
  299. $coupon_list[$index]['min_price'] = (int)$value['min_price'];
  300. }
  301. if ($value['sub_price'] >= 100) {
  302. $coupon_list[$index]['sub_price'] = (int)$value['sub_price'];
  303. }
  304. $coupon_list[$index]['begintime'] = date('Y.m.d', $value['begin_time']);
  305. $coupon_list[$index]['endtime'] = date('Y.m.d', $value['end_time']);
  306. $coupon_list[$index]['content'] = "适用范围:全场通用";
  307. if ($value['appoint_type'] == 1 && $value['cat_id_list'] !== 'null') {
  308. $coupon_list[$index]['cat'] = Cat::find()->select('id, name')->where(['is_delete' => 0, 'id' => json_decode($value['cat_id_list'])])->asArray()->all();
  309. $cat_list = [];
  310. foreach ($coupon_list[$index]['cat'] as $item) {
  311. $cat_list[] = $item['name'];
  312. }
  313. $coupon_list[$index]['content'] = "适用范围:仅限分类:".implode('、', $cat_list)."使用";
  314. $coupon_list[$index]['goods'] = [];
  315. } elseif ($value['appoint_type'] == 2 && $value['goods_id_list'] !== 'null') {
  316. $coupon_list[$index]['goods'] = Goods::find()->select('id')->where(['store_id' => $store_id, 'is_delete' => 0, 'id' => json_decode($value['goods_id_list'])])->asArray()->all();
  317. $coupon_list[$index]['cat'] = [];
  318. $coupon_list[$index]['content'] = "指定商品使用 点击查看指定商品";
  319. } else {
  320. $coupon_list[$index]['goods'] = [];
  321. $coupon_list[$index]['cat'] = [];
  322. }
  323. if ($value['is_receive'] == 0) {
  324. $coupon_list[$index]['receive_content'] = '立即领取';
  325. } else {
  326. $coupon_list[$index]['receive_content'] = '已领取';
  327. }
  328. $coupon_count = UserCoupon::find()->where([
  329. 'is_delete' => 0, 'coupon_id' => $value['id'], 'type' => 3
  330. ])->count();
  331. if ($value['total_count'] > $coupon_count || $value['total_count'] == -1) {
  332. if ($value['expire_type'] == 2) {
  333. if ($value['end_time'] >= time()) {
  334. $new_list[] = $coupon_list[$index];
  335. }
  336. } else {
  337. $new_list[] = $coupon_list[$index];
  338. }
  339. }
  340. }
  341. return $new_list;
  342. }
  343. /**
  344. * @param int $limit
  345. * @param int $all
  346. * @return array
  347. */
  348. public static function getAllianceList($page = 1, $pageSize = 20, $goods_id_list=[], $store_id = 0)
  349. {
  350. $saas_id = get_saas_user_id();
  351. $user_id = User::find()->where(['binding' => get_saas_user()->mobile, 'is_delete' => 0])->select('id')->column();
  352. // $user_id = get_user_id();
  353. $user_id = array_merge($user_id, [0]);
  354. $user_id = implode(',', $user_id);
  355. $offset = ($page - 1) * $pageSize;
  356. $time = time();
  357. $sql = "SELECT
  358. c.*,
  359. ( CASE WHEN isnull( uc_saas.id )
  360. AND isnull( uc_user.id )
  361. THEN 0 ELSE 1 END ) AS is_receive
  362. FROM
  363. cyy_coupon c
  364. LEFT JOIN cyy_saas_coupon uc_saas ON c.id = uc_saas.coupon_id
  365. AND c.is_business in (0, 1) AND uc_saas.saas_id = {$saas_id} AND uc_saas.is_delete = 0
  366. LEFT JOIN cyy_user_coupon uc_user ON c.id = uc_user.coupon_id
  367. AND c.is_business = 0 AND uc_user.user_id in ({$user_id}) AND uc_user.is_delete = 0
  368. LEFT JOIN cyy_store uc_s ON c.store_id = uc_s.id
  369. WHERE c.is_delete = 0 AND ((c.expire_type = 2 AND c.end_time >= {$time} AND c.begin_time <= {$time}) OR c.expire_type <> 2) ";
  370. if ($store_id > 0) {
  371. $sql .= " AND c.store_id = {$store_id}";
  372. } else {
  373. $sql .= " AND uc_s.business_model != 1";
  374. }
  375. $sql .= " GROUP BY c.id ORDER BY is_receive ASC, c.sort ASC, c.id DESC";
  376. $count = \Yii::$app->db->createCommand($sql)->query()->count();
  377. $sql .= " LIMIT {$pageSize} OFFSET {$offset}";
  378. $coupon_list = \Yii::$app->db->createCommand($sql)->queryAll();
  379. // $coupon_form = Coupon::find()->alias('c')->where([
  380. // 'c.is_delete' => 0,'c.is_business'=>1,'c.business_type'=>1,'c.business_status'=>1,
  381. // ])->andWhere(['!=', 'c.total_count', 0])
  382. // ->leftJoin(SaasCoupon::tableName() . ' sc', "sc.coupon_id=c.id and sc.saas_id ={$saas_id} and sc.is_delete=0")
  383. // ->select(['c.*', '(case when isnull(sc.id) then 0 else 1 end) as is_receive']);
  384. // $countQuery = clone $coupon_form;
  385. // $count = $countQuery->count();
  386. // $coupon_form->offset(($page - 1) * $pageSize)->limit($pageSize);
  387. // $coupon_list = $coupon_form->orderBy('is_receive ASC, c.sort ASC, c.id DESC')->asArray()->all();
  388. $new_list = $list = [];
  389. foreach ($coupon_list as $index => $value) {
  390. // TODO: store 信息
  391. $coupon_list[$index]['store_id'] = $value['store_id'] ? $value['store_id'] : 0;
  392. $coupon_list[$index]['desc'] = !empty($value['desc']) ? $value['desc'] : '';
  393. if ($value['min_price'] >= 100) {
  394. $coupon_list[$index]['min_price'] = (int)$value['min_price'];
  395. }
  396. if ($value['sub_price'] >= 100) {
  397. $coupon_list[$index]['sub_price'] = (int)$value['sub_price'];
  398. }
  399. $coupon_list[$index]['begintime'] = date('Y.m.d', $value['begin_time']);
  400. $coupon_list[$index]['endtime'] = date('Y.m.d', $value['end_time']);
  401. $coupon_list[$index]['content'] = "适用范围:全场通用";
  402. if ($value['appoint_type'] == 1 && $value['cat_id_list'] !== 'null') {
  403. $coupon_list[$index]['cat'] = Cat::find()->select('id, name')->where(['is_delete' => 0, 'id' => json_decode($value['cat_id_list'])])->asArray()->all();
  404. $cat_list = [];
  405. foreach ($coupon_list[$index]['cat'] as $item) {
  406. $cat_list[] = $item['name'];
  407. }
  408. $coupon_list[$index]['content'] = "适用范围:仅限分类:".implode('、', $cat_list)."使用";
  409. $coupon_list[$index]['goods'] = [];
  410. } elseif ($value['appoint_type'] == 2 && $value['goods_id_list'] !== 'null') {
  411. $coupon_list[$index]['goods'] = Goods::find()->select('id')->where(['store_id' => $value['store_id'], 'is_delete' => 0, 'id' => json_decode($value['goods_id_list'])])->asArray()->all();
  412. $coupon_list[$index]['cat'] = [];
  413. $coupon_list[$index]['content'] = "指定商品使用 点击查看指定商品";
  414. } else {
  415. $coupon_list[$index]['goods'] = [];
  416. $coupon_list[$index]['cat'] = [];
  417. }
  418. if ($value['is_receive'] == 0) {
  419. $coupon_list[$index]['receive_content'] = '立即领取';
  420. } else {
  421. $coupon_list[$index]['receive_content'] = '已领取';
  422. }
  423. if($coupon_list[$index]['store_id'] >0 ){
  424. $coupon_list[$index]['storeInfo'] = Store::find()->where(['id'=>$coupon_list[$index]['store_id'],'is_delete'=>0])->select(['id', 'name', 'logo', 'coordinate', 'created_at', 'category_id', 'tags', 'sales', 'rank', 'per_spend','business_model','address'])->asArray()->one();
  425. $wechatInfo = WechatConfig::find()->where(['store_id'=>$coupon_list[$index]['store_id'],'is_delete'=>0,'type'=>1])->asArray()->one();
  426. if(isset($coupon_list[$index]['storeInfo']['id']) && $coupon_list[$index]['storeInfo']['id']>0){
  427. if($wechatInfo){
  428. $coupon_list[$index]['storeInfo']['wechat_app_id'] = $wechatInfo['app_id'];
  429. }
  430. $alipayInfo = Option::get('alipay_config',$coupon_list[$index]['store_id'],'alipay');
  431. if($alipayInfo && $alipayInfo['value']){
  432. $alipayInfo = json_decode($alipayInfo['value'],true);
  433. $coupon_list[$index]['storeInfo']['alipay_app_id'] = $alipayInfo['app_id'];
  434. }
  435. }
  436. }
  437. $coupon_count = SaasCoupon::find()->where([
  438. 'is_delete' => 0, 'coupon_id' => $value['id'], 'type' => 2
  439. ])->count();
  440. if ($value['total_count'] > $coupon_count || $value['total_count'] == -1) {
  441. if ($value['expire_type'] == 2) {
  442. if ($value['end_time'] >= time()) {
  443. $new_list[] = $coupon_list[$index];
  444. }
  445. } else {
  446. $new_list[] = $coupon_list[$index];
  447. }
  448. }else{
  449. $new_list[] = $coupon_list[$index];
  450. }
  451. }
  452. if (!empty($goods_id_list)) {
  453. foreach ($new_list as $value) {
  454. if ($value['is_receive'] == 1) {
  455. continue; // 已领取的跳过
  456. }
  457. if ($value['appoint_type'] == 1 && $value['cat_id_list'] !== 'null' && $value['cat_id_list'] !== '[]') {
  458. continue; // 适用于分类的跳过
  459. }
  460. if ($value['appoint_type'] == 2 && $value['goods_id_list'] !== 'null' && $value['goods_id_list'] !== null) {
  461. // 判断该优惠券是否指定商品,如果指定但是给定的goods_id_list中的商品id不存在,则跳过
  462. $idList = json_decode($value['goods_id_list']);
  463. $is = false;
  464. foreach ($goods_id_list as $v) {
  465. if (in_array($v, $idList)) {
  466. $is = true;
  467. break;
  468. }
  469. }
  470. if (!$is) {
  471. continue;
  472. }
  473. }
  474. $list[] = $value;
  475. }
  476. }else{
  477. $list = $new_list;
  478. }
  479. $data = [
  480. 'list' => $list,
  481. 'page' => $page,
  482. 'total_count'=> $count
  483. ];
  484. return $data;
  485. }
  486. /**
  487. * 给用户发放优惠券
  488. * @param integer $user_id 用户id
  489. * @param integer $coupon_id 优惠券id
  490. * @param integer $coupon_auto_send_id 自动发放id
  491. * @param integer $type 领券类型
  492. * @return boolean | integer
  493. */
  494. public static function userAddCoupon($user_id, $coupon_id, $coupon_auto_send_id = 0, $type = 0)
  495. {
  496. $user = User::findOne($user_id);
  497. if (!$user) {
  498. return false;
  499. }
  500. $coupon = Coupon::findOne([
  501. 'id' => $coupon_id,
  502. 'is_delete' => 0,
  503. ]);
  504. if (!$coupon) {
  505. return false;
  506. }
  507. if ($coupon->total_count == 0) {
  508. return false;
  509. }
  510. $user_coupon = new UserCoupon();
  511. if ($type == 2) {
  512. $res = UserCoupon::find()->where(['is_delete'=>0,'type'=>2,'user_id'=>$user_id,'coupon_id'=>$coupon_id])->exists();
  513. if ($res) {
  514. return false;
  515. }
  516. }
  517. if ($coupon_auto_send_id) {
  518. $coupon_auto_send = CouponAutoSend::findOne([
  519. 'id' => $coupon_auto_send_id,
  520. 'is_delete' => 0,
  521. ]);
  522. if (!$coupon_auto_send) {
  523. return false;
  524. }
  525. if ($coupon_auto_send->send_times != 0) {
  526. $send_count = UserCoupon::find()->where([
  527. 'coupon_auto_send_id' => $coupon_auto_send->id,
  528. 'user_id' => $user->id,
  529. ])->count();
  530. if ($send_count && $send_count >= $coupon_auto_send->send_times) {
  531. return false;
  532. }
  533. }
  534. $user_coupon->coupon_auto_send_id = $coupon_auto_send->id;
  535. $type = 1;
  536. }
  537. $user_coupon->type = $type;
  538. $user_coupon->store_id = $user->store_id;
  539. $user_coupon->user_id = $user->id;
  540. $user_coupon->coupon_id = $coupon->id;
  541. if ($coupon->expire_type == 1) {
  542. $user_coupon->begin_time = time();
  543. $user_coupon->end_time = time() + max(0, 86400 * $coupon->expire_day);
  544. } elseif ($coupon->expire_type == 2) {
  545. $user_coupon->begin_time = $coupon->begin_time;
  546. $user_coupon->end_time = $coupon->end_time;
  547. }
  548. $user_coupon->is_expire = 0;
  549. $user_coupon->is_use = 0;
  550. $user_coupon->is_delete = 0;
  551. $user_coupon->created_at = time();
  552. $user_coupon->save();
  553. return $user_coupon->id;
  554. }
  555. public function getCount()
  556. {
  557. return UserCoupon::find()->where(['coupon_id'=>$this->id,'is_delete'=>0,'type'=>2])->count();
  558. }
  559. }