CouponForm.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  8. use app\models\WechatConfig;
  9. use app\models\Option;
  10. use app\models\Coupon;
  11. use app\models\CouponAutoSend;
  12. use app\models\Goods;
  13. use app\models\SaasUser;
  14. use app\models\SaasCoupon;
  15. use app\models\User;
  16. use app\models\Cat;
  17. use app\models\Store;
  18. use app\models\UserCoupon;
  19. use app\modules\admin\models\mochat\MochatForm;
  20. use yii\base\BaseObject;
  21. use yii\base\Model;
  22. class CouponForm extends Model
  23. {
  24. public $send_type;
  25. public $saas_id;
  26. public $status;
  27. public $id;
  28. public $coupon_id;
  29. public $store_id;
  30. public function rules()
  31. {
  32. return [
  33. [['status', 'id', 'coupon_id', 'saas_id','send_type', 'store_id'], 'number'],
  34. ];
  35. }
  36. /**
  37. * 优惠券列表
  38. * @return array
  39. */
  40. public function search()
  41. {
  42. if (!$this->validate()) {
  43. return [
  44. 'code' => 1,
  45. 'msg' => $this->getErrorSummary(false)[0],
  46. ];
  47. }
  48. $query = SaasCoupon::find()->alias('sc')->leftJoin(['c' => Coupon::tableName()], 'sc.coupon_id=c.id')
  49. ->where(['sc.saas_id' => $this->saas_id]);
  50. if ($this->status == 0) {
  51. $query->andWhere([
  52. 'sc.is_delete' => 0,
  53. ]);
  54. }
  55. if ($this->status == 1) {
  56. $query->andWhere([
  57. 'sc.is_delete' => 0,
  58. 'sc.is_use' => 0,
  59. 'sc.is_expire' => 0,
  60. ]);
  61. }
  62. if ($this->status == 2) {
  63. $query->andWhere([
  64. 'sc.is_delete' => 0,
  65. 'sc.is_use' => 1,
  66. 'sc.is_expire' => 0,
  67. ]);
  68. }
  69. if ($this->status == 3) {
  70. $query->andWhere([
  71. 'sc.is_delete' => 0,
  72. 'sc.is_expire' => 1,
  73. ]);
  74. }
  75. if ($this->status == 0) {
  76. $query->orderBy('sc.is_expire ASC, sc.is_use ASC, sc.created_at DESC');
  77. } else {
  78. $query->orderBy('sc.created_at DESC');
  79. }
  80. $list = $query
  81. ->limit(50)
  82. ->select('c.id coupon_id, sc.id user_coupon_id,c.sub_price,c.mch_id,c.min_price,sc.begin_time,sc.end_time,sc.is_use,
  83. sc.is_expire,sc.type,c.appoint_type,c.cat_id_list,c.goods_id_list,c.name,c.rule,c.desc,c.discount,c.discount_max_price,c.discount_type,c.expire_type,c.expire_day,c.is_give,sc.give_user_id,c.store_id')->asArray()->all();
  84. // $events = [
  85. // 0 => '领券中心',
  86. // 1 => '会员卡赠送',
  87. // 2 => '购物返券',
  88. // 3 => '领券中心',
  89. // 4 => '积分兑换',
  90. // ];
  91. foreach ($list as $i => $item) {
  92. $list[$i]['can_send'] = $item['is_give'];
  93. if ($item['is_use'] || $item['is_expire']) {
  94. $list[$i]['can_send'] = 0;
  95. }
  96. $list[$i]['status'] = 0;
  97. if ($item['is_use']) {
  98. $list[$i]['status'] = 1;
  99. }
  100. if ($item['is_expire']) {
  101. $list[$i]['status'] = 2;
  102. }
  103. $list[$i]['begintime'] = date('Y.m.d', $item['begin_time']);
  104. $list[$i]['endtime'] = date('Y.m.d', $item['end_time']);
  105. $list[$i]['min_price_desc'] = $item['min_price'] == 0 ? '无门槛' : '满' . $item['min_price'] . '元可用';
  106. // if (!$item['event']) {
  107. // if ($item['type'] == 2) {
  108. // $list[$i]['event'] = $item['event'] = 3;
  109. // } elseif ($item['type'] == 0) {
  110. // $list[$i]['event'] = $item['event'] = 0;
  111. // } else {
  112. // $list[$i]['event'] = $item['event'] = 4;
  113. // }
  114. // }
  115. // $list[$i]['event_desc'] = $events[$item['event']];
  116. if ($item['appoint_type'] == 1 && $item['cat_id_list'] !== 'null' && $item['cat_id_list'] !== '[]') {
  117. $list[$i]['cat'] = Cat::find()->select('id, name')->where(['is_delete' => 0,
  118. 'id' => json_decode($item['cat_id_list'])])->asArray()->all();
  119. $cat_list = [];
  120. foreach ($list[$i]['cat'] as $value) {
  121. $cat_list[] = $value['name'];
  122. }
  123. $list[$i]['content'] = "适用范围:仅限分类:".implode('、', $cat_list)."使用";
  124. $list[$i]['goods'] = [];
  125. } elseif ($item['appoint_type'] == 2 && $item['goods_id_list'] !== 'null' && $item['goods_id_list'] !== null) {
  126. $list[$i]['goods'] = Goods::find()->select('id, name, price, cover_pic')->where(['is_delete' => 0, 'id' => json_decode($item['goods_id_list'])])->asArray()->all();
  127. $list[$i]['content'] = "适用范围:指定商品使用";
  128. $list[$i]['cat'] = [];
  129. } else {
  130. $list[$i]['goods'] = [];
  131. $list[$i]['cat'] = [];
  132. }
  133. $list[$i]['give_user_info'] = [];
  134. if ($item['give_user_id']) {
  135. $list[$i]['give_user_info'] = SaasUser::find()->where(['id'=>$item['give_user_id']])->select('avatar,name')->asArray()->one();
  136. //$list[$i]['give_user_info'] = User::find()->where(['u.id' => $item['give_user_id']])->alias('u')->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')->select('su.avatar,su.name')->asArray()->one();
  137. }
  138. if($item['store_id'] > 0){
  139. $list[$i]['storeInfo'] = Store::find()->where(['id'=>$item['store_id'],'is_delete'=>0])->select(['id', 'name', 'logo', 'coordinate', 'created_at', 'category_id', 'tags', 'sales', 'rank', 'per_spend','business_model','address'])->asArray()->one();
  140. $wechatInfo = WechatConfig::find()->where(['store_id'=>$item['store_id'],'is_delete'=>0,'type'=>1])->asArray()->one();
  141. if(isset($list[$i]['storeInfo']['id']) && $list[$i]['storeInfo']['id']>0){
  142. if($wechatInfo){
  143. $list[$i]['storeInfo']['wechat_app_id'] = $wechatInfo['app_id'];
  144. }
  145. $alipayInfo = Option::get('alipay_config',$item['store_id'],'alipay');
  146. if($alipayInfo && $alipayInfo['value']){
  147. $alipayInfo = json_decode($alipayInfo['value'],true);
  148. $list[$i]['storeInfo']['alipay_app_id'] = $alipayInfo['app_id'];
  149. }
  150. }
  151. }
  152. }
  153. return [
  154. 'code' => 0,
  155. 'msg' => 'success',
  156. 'data' => [
  157. 'list' => $list,
  158. ],
  159. ];
  160. }
  161. /**
  162. * 领取优惠券
  163. * @return array
  164. */
  165. public function sendSaasCoupon()
  166. {
  167. $coupon = Coupon::find()->where(['is_delete' => 0, 'id' => $this->id])->asArray()->one(); // ,'is_business'=>1, 'business_type'=>$this->send_type 领取正常商城优惠券修改
  168. if (!$coupon) {
  169. return [
  170. 'code' => 1,
  171. 'msg' => '优惠券不存在'
  172. ];
  173. }
  174. $saas_coupon = SaasCoupon::find() ->where(['coupon_id' => $this->id, 'saas_id' => $this->saas_id, 'type' => $this->send_type, 'is_delete' => 0])->exists();
  175. if ($saas_coupon) {
  176. return [
  177. 'code' => 1,
  178. 'msg' => '已领取',
  179. ];
  180. }
  181. $coupon_count = SaasCoupon::find()->where(['is_delete' => 0, 'coupon_id' => $this->id ])->count();
  182. if ($coupon['total_count'] != -1 && $coupon['total_count'] <= $coupon_count) {
  183. return [
  184. 'code' => 1,
  185. 'msg' => '优惠券已领完'
  186. ];
  187. }
  188. $res = new SaasCoupon();
  189. $res->saas_id = $this->saas_id;
  190. $res->store_id = $coupon['store_id'];
  191. $res->coupon_id = $this->id;
  192. $res->coupon_auto_send_id = 0;
  193. $res->type = $this->send_type;
  194. $res->is_use = 0;
  195. $res->is_expire = 0;
  196. $res->is_delete = 0;
  197. $res->created_at = time();
  198. if ($coupon['expire_type'] == 1) {
  199. $res->begin_time = time();
  200. if ($coupon['expire_day'] != 0) {
  201. $res->end_time = time() + max(0, 86400 * $coupon['expire_day']);
  202. } else {
  203. $res->end_time = 0; //永久有效
  204. }
  205. } elseif ($coupon['expire_type'] == 2) {
  206. $res->begin_time = $coupon['begin_time'];
  207. $res->end_time = $coupon['end_time'];
  208. }
  209. if ($res->save()) {
  210. return [
  211. 'code' => 0,
  212. 'msg' => 'success'
  213. ];
  214. } else {
  215. return [
  216. 'code' => 1,
  217. 'msg' => '网络异常'
  218. ];
  219. }
  220. }
  221. /**
  222. * 优惠券详情
  223. * @return array
  224. */
  225. public function detail()
  226. {
  227. $id = 0;
  228. if ($this->coupon_id) {
  229. $user_ids = User::find()->where(['binding' => get_saas_user()->mobile])->select('id')->column();
  230. $UserCoupon = UserCoupon::findOne(['coupon_id' => $this->coupon_id, 'user_id' => $user_ids]);
  231. $list = Coupon::find()->alias('c')
  232. ->where([
  233. 'c.is_delete' => 0,
  234. 'c.is_join' => 2,
  235. 'c.id' => $this->coupon_id,
  236. ])
  237. ->leftJoin(UserCoupon::tableName() . ' uc', "uc.coupon_id=c.id and uc.user_id ={$UserCoupon->user_id}
  238. and uc.type = 2 and uc.is_delete=0")
  239. ->select(['c.*', '(case when isnull(uc.id) then 0 else 1 end) as is_receive'])
  240. ->asArray()
  241. ->one();
  242. $coupon = $list;
  243. }
  244. if ($this->id) {
  245. $list = SaasCoupon::find()
  246. ->where([
  247. 'is_delete' => 0,
  248. 'id' => $this->id,
  249. ])->with('coupon')->asArray()->one();
  250. if ($coupon = $list['coupon']) {
  251. $list['min_price'] = $coupon['min_price'];
  252. $list['sub_price'] = $coupon['sub_price'];
  253. $list['appoint_type'] = $coupon['appoint_type'];
  254. $list['rule'] = $coupon['rule'];
  255. $list['expire_type'] = 2;
  256. $list['status'] = 0;
  257. if ($list['is_use']) {
  258. $list['status'] = 1;
  259. }
  260. if ($list['is_expire']) {
  261. $list['status'] = 2;
  262. }
  263. if ($list['give_user_id']) {
  264. $list['status'] = 3;
  265. }
  266. }
  267. }
  268. if ($list) {
  269. $list['can_send'] = $coupon['is_give'];
  270. if ($list['is_use'] || $list['is_expire']) {
  271. $list['can_send'] = 0;
  272. }
  273. $list['sub_price'] = $coupon['sub_price'];
  274. $list['min_price'] = $coupon['min_price'];
  275. $list['min_price_desc'] = $coupon['min_price'] == 0 ? '无门槛' : '满' . $coupon['min_price'] . '元可用';
  276. $list['begin_time'] = date('Y.m.d', $list['begin_time']);
  277. $list['end_time'] = date('Y.m.d', $list['end_time']);
  278. if ($list['appoint_type'] == 1 && $coupon['cat_id_list'] !== 'null' && $coupon['cat_id_list'] !== '[]') {
  279. $list['cat'] = Cat::find()->select('id, name')->where(['store_id' => $this->store_id, 'is_delete' => 0,
  280. 'id' => json_decode($coupon['cat_id_list'])])->asArray()->all();
  281. $cat_list = [];
  282. foreach ($list['cat'] as $value) {
  283. $cat_list[] = $value['name'];
  284. }
  285. $list['content'] = "适用范围:仅限分类:".implode('、', $cat_list)."使用";
  286. $list['goods'] = [];
  287. } elseif ($list['appoint_type'] == 2 && $coupon['goods_id_list'] !== 'null' && $coupon['goods_id_list'] !== null) {
  288. $list['goods'] = Goods::find()->select('id, name, price, cover_pic')->where(['store_id' => $this->store_id, 'is_delete' => 0, 'id' => json_decode($coupon['goods_id_list'])])->asArray()->all();
  289. $list['content'] = "适用范围:指定商品使用";
  290. $list['cat'] = [];
  291. } else {
  292. $list['goods'] = [];
  293. $list['cat'] = [];
  294. }
  295. return [
  296. 'code' => 0,
  297. 'msg' => 'success',
  298. 'data' => [
  299. 'list' => $list,
  300. ],
  301. ];
  302. } else {
  303. return [
  304. 'code' => 0,
  305. 'msg' => '不存在',
  306. 'data' => [
  307. 'list' => [],
  308. ],
  309. ];
  310. }
  311. }
  312. /**
  313. * 领取优惠券
  314. * @return array
  315. */
  316. public function sendCoupon()
  317. {
  318. $coupon = Coupon::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'id' => $this->id])->asArray()->one();
  319. if (!$coupon) {
  320. return [
  321. 'code' => 1,
  322. 'msg' => '网络异常'
  323. ];
  324. }
  325. $user_id = 0;
  326. if ($this->saas_id) {
  327. $saasUser = SaasUser::findOne($this->saas_id);
  328. $user = User::findOne(['binding' => $saasUser->mobile, 'store_id' => $this->store_id]);
  329. $coupon['type'] = 2;
  330. $user_coupon = UserCoupon::find()
  331. ->where(['store_id' => $this->store_id , 'coupon_id' => $this->id, 'user_id' => $user->id, 'type' => 2,
  332. 'is_delete' => 0])->exists();
  333. if ($user_coupon) {
  334. $coupon_list[] = $coupon;
  335. $coupon_list_new = Coupon::getList();
  336. return [
  337. 'code' => 1,
  338. 'msg' => '已领取',
  339. 'data'=> [
  340. 'coupon_list'=>$coupon_list_new
  341. ]
  342. ];
  343. }
  344. $coupon_count = UserCoupon::find()->where(['store_id' => $this->store_id, 'is_delete' => 0,
  345. 'coupon_id' => $coupon['id']])->andWhere(['!=', 'type', 3])->count();
  346. if ($coupon['total_count'] != -1 && $coupon['total_count'] <= $coupon_count) {
  347. return [
  348. 'code' => 1,
  349. 'msg' => '优惠券已领完'
  350. ];
  351. }
  352. $res = new UserCoupon();
  353. $res->user_id = $user->id;
  354. $res->store_id = $this->store_id;
  355. $res->coupon_id = $this->id;
  356. $res->coupon_auto_send_id = 0;
  357. $res->type = 2;
  358. $res->is_use = 0;
  359. $res->is_expire = 0;
  360. $res->is_delete = 0;
  361. $res->created_at = time();
  362. if ($coupon['expire_type'] == 1) {
  363. $res->begin_time = time();
  364. $res->end_time = time() + max(0, 86400 * $coupon['expire_day']);
  365. } elseif ($coupon['expire_type'] == 2) {
  366. $res->begin_time = $coupon['begin_time'];
  367. $res->end_time = $coupon['end_time'];
  368. }
  369. if ($res->save()) {
  370. MochatForm::sendMsg(1, $this->store_id, MochatForm::MSG_TYPE_GET_COUPON, $this->saas_id, ['coupon_id' => $this->id]);
  371. $coupon_list[] = $coupon;
  372. $coupon_list_new = Coupon::getList();
  373. return [
  374. 'code' => 0,
  375. 'msg' => 'success',
  376. 'data' => [
  377. 'list' => $coupon_list,
  378. 'coupon_list' => $coupon_list_new
  379. ],
  380. ];
  381. } else {
  382. return [
  383. 'code' => 1,
  384. 'msg' => '网络异常'
  385. ];
  386. }
  387. }
  388. return [
  389. 'code' => 1,
  390. 'msg' => '网络异常'
  391. ];
  392. }
  393. }