Coupon.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\controller;
  13. // use app\common\model\coupon\Usercoupon as UserCouponModel;
  14. // use app\common\model\coupon\Usercoupondetail as UsercoupondetailModel;
  15. // use app\common\model\coupon\Couponcard as CouponcardModel;
  16. use app\store\model\Coupon as CouponModel;
  17. use app\common\service\Export as ExportService;
  18. use app\store\model\coupon\CouponGenRedeemCodes;
  19. use app\store\model\coupon\CouponRedeemCodes;
  20. use app\store\model\UserCoupon;
  21. use think\facade\Validate;
  22. /**
  23. * 优惠券管理
  24. * Class Files
  25. * @package app\store\controller
  26. */
  27. class Coupon extends Controller
  28. {
  29. /**
  30. * 添加卡
  31. * @return array|string
  32. */
  33. public function add()
  34. {
  35. // 新增记录
  36. $model = new CouponModel;
  37. $post = $this->postForm();
  38. // $post['coupon_type'] = CouponModel::ManJian;
  39. $post['create_user'] =$this->store['user']['real_name'];
  40. $post['audit_admin_id'] = $this->store['user']['store_user_id']?:'';
  41. if(isset($post['limit_total_type']) && $post['limit_total_type']==10){
  42. if(empty($post['total_num'])){
  43. return $this->renderError('限制数量total_num参数值要大于0');
  44. }
  45. }
  46. if(isset($post['limit_total_type']) && $post['limit_total_type']==20){
  47. $post['total_num'] = $post['total_num']??0;
  48. if($post['total_num']>0){
  49. return $this->renderError('不限制数量total_num应该为0');
  50. }
  51. }
  52. if ($model->addOne($post)) {
  53. return $this->renderSuccess('添加成功');
  54. }
  55. return $this->renderError($model->getError() ?: '添加失败');
  56. }
  57. /**
  58. * 添加卡
  59. * @return array|string
  60. */
  61. public function update()
  62. {
  63. $post = $this->postForm();
  64. $model = CouponModel::detail((int)$post['coupon_id']);
  65. $post['audit_user'] =$this->store['user']['real_name'];
  66. $post['audit_admin_id'] = $this->store['user']['store_user_id']?:'';
  67. if(isset($post['limit_total_type']) && $post['limit_total_type']==10){
  68. if(empty($post['total_num'])){
  69. return $this->renderError('限制数量total_num参数值要大于0');
  70. }
  71. }
  72. if(isset($post['limit_total_type']) && $post['limit_total_type']==20){
  73. $post['total_num'] = $post['total_num']??0;
  74. if($post['total_num']>0){
  75. return $this->renderError('不限制数量total_num应该为0');
  76. }
  77. }
  78. if ($model->updateOne($post)) {
  79. return $this->renderSuccess('修改成功');
  80. }
  81. return $this->renderError($model->getError() ?: '修改失败');
  82. }
  83. //详情
  84. public function detail(int $coupon_id){
  85. $detail = CouponModel::detail($coupon_id,['goodsExcept']);
  86. return $this->renderSuccess(compact('detail'));
  87. }
  88. // /**
  89. // * 启用 禁用
  90. // * @param int $coupon_id
  91. // * @return array|bool
  92. // */
  93. public function status(int $coupon_id){
  94. $model = CouponModel::detail($coupon_id);
  95. $postData = $this->postForm();
  96. unset($postData['coupon_id']);
  97. $res ='操作成功';
  98. $status =$postData['status'];
  99. if($status==1){
  100. $res = '已启用';
  101. }
  102. if($status==0){
  103. $res = '已禁用';
  104. }
  105. if ($model->status($postData)) {
  106. return $this->renderSuccess($postData,$res);
  107. }
  108. return $this->renderError($model->getError() ?: '操作失败');
  109. }
  110. // /**
  111. // * 审核
  112. // * @param int $coupon_id
  113. // * @return array|bool
  114. // */
  115. public function audit(int $coupon_id)
  116. {
  117. $model = CouponModel::detail($coupon_id);
  118. if(empty($model)){
  119. return $this->renderError('找不到优惠券');
  120. }
  121. // 确认审核
  122. $postData = $this->postForm();
  123. unset($postData['coupon_id']);
  124. $res ='操作成功';
  125. $audit_status =$postData['audit_status'];
  126. if($audit_status==10){
  127. $res = '已审核通过';
  128. $postData['status'] =1;//启用
  129. }
  130. if($audit_status==20){
  131. $res = '已审核不通过';
  132. $postData['status'] = 0;//禁用
  133. }
  134. $postData['audit_time'] = Date("Y-m-d H:i:s",time());
  135. $postData['audit_user'] = $this->store['user']['real_name'];
  136. //$postData['create_user'] = $this->store['user']['real_name'];s
  137. $postData['audit_admin_id'] = $this->store['user']['store_user_id']?:'';
  138. if ($model->audit($postData)) {
  139. return $this->renderSuccess($postData,$res);
  140. }
  141. return $this->renderError($model->getError() ?: '操作失败');
  142. }
  143. //裂变活动优惠券
  144. public function couponGroupBuyLblist(){
  145. $model = new CouponModel;
  146. $list = $model->getCouponGroupBuyLbPagelist($this->request->param());
  147. return $this->renderSuccess(compact('list'));
  148. }
  149. //后台优惠券列表
  150. public function couponlist(){
  151. $model = new CouponModel;
  152. $list = $model->getList($this->request->param());
  153. return $this->renderSuccess(compact('list'));
  154. }
  155. /**
  156. * 米卡发放列表
  157. * @return array
  158. * @throws \think\db\exception\DbException
  159. */
  160. public function list()
  161. {
  162. $model = new UserCouponModel;
  163. $list = $model->getAllList($this->request->param());
  164. return $this->renderSuccess(compact('list'));
  165. }
  166. //冻结
  167. public function frozen($id,$frozen_state=1){
  168. $model = new UserCouponModel;
  169. $model->where('id',$id)->update(['frozen_state'=>$frozen_state]);
  170. return $this->renderSuccess('更新成功');
  171. }
  172. //兑换明细
  173. public function detaillist($user_coupon_id=0){
  174. $model = new UsercoupondetailModel;
  175. $param['user_coupon_id'] = $user_coupon_id;
  176. $list = $model->getAllList($param);
  177. return $this->renderSuccess(compact('list'));
  178. }
  179. //米卡配置列表
  180. public function cardlist(){
  181. $model = new CouponcardModel;
  182. $list = $model->getAllList([]);
  183. return $this->renderSuccess(compact('list'));
  184. }
  185. //添加米卡券
  186. public function createcoupon($id,$num=1){
  187. $model = new CouponcardModel;
  188. $list = $model->addCard($id,$num);
  189. return $this->renderSuccess('添加成功');
  190. }
  191. //添加数据
  192. public function addcard(){
  193. $param = $this->request->param();
  194. $model = new CouponcardModel;
  195. $param['created_at'] =Date("Y-m-d H:i:s",time());
  196. $param['good_sku_id'] = empty($param['good_sku_id'])?0:$param['good_sku_id'];
  197. $model->save($param);
  198. return $this->renderSuccess('添加成功');
  199. }
  200. // /**
  201. // * 编辑文件
  202. // * @param int $fileId
  203. // * @return array
  204. // */
  205. // public function edit(int $fileId)
  206. // {
  207. // // 文件详情
  208. // $model = UploadFileModel::detail($fileId);
  209. // // 更新记录
  210. // if ($model->edit($this->postForm())) {
  211. // return $this->renderSuccess('更新成功');
  212. // }
  213. // return $this->renderError($model->getError() ?: '更新失败');
  214. // }
  215. // /**
  216. // * 删除文件(批量)
  217. // * @param array $fileIds 文件id集
  218. // * @return array
  219. // * @throws \think\Exception
  220. // * @throws \think\db\exception\DataNotFoundException
  221. // * @throws \think\db\exception\DbException
  222. // * @throws \think\db\exception\ModelNotFoundException
  223. // */
  224. // public function delete(array $fileIds)
  225. // {
  226. // $model = new UploadFileModel;
  227. // if (!$model->setDelete($fileIds)) {
  228. // return $this->renderError($model->getError() ?: '操作失败');
  229. // }
  230. // return $this->renderSuccess('操作成功');
  231. // }
  232. // /**
  233. // * 移动文件到指定分组(批量)
  234. // * @param int $groupId
  235. // * @param array $fileIds
  236. // * @return array
  237. // */
  238. // public function moveGroup(int $groupId, array $fileIds)
  239. // {
  240. // $model = new UploadFileModel;
  241. // if (!$model->moveGroup($groupId, $fileIds)) {
  242. // return $this->renderError($model->getError() ?: '操作失败');
  243. // }
  244. // return $this->renderSuccess('操作成功');
  245. // }
  246. /**
  247. * zq---------------------------------------------------------------------------------------------------------------
  248. * @Time: 2021/12/29 13:43
  249. */
  250. /**
  251. * 生成优惠券的兑换码
  252. * @return array
  253. */
  254. public function genCouponCodes(){
  255. $post = $this->postForm();
  256. $validate = Validate::rule('couponId', 'require|number|gt:0')
  257. ->rule([
  258. 'amount' => 'require|number|gt:0',
  259. 'giveType' => 'require|number|in:1,2'
  260. ]);
  261. if (!$validate->check($post)) {
  262. return $this->renderError($validate->getError());
  263. }
  264. //检验优惠券数量是否超发
  265. $m = new CouponGenRedeemCodes();
  266. /* $flag = $m->sunAmount($post['couponId'],$post['amount']);
  267. if($flag){
  268. return $this->renderError('优惠券数量已达上限');
  269. }*/
  270. if($post['giveType'] == 1){
  271. if (!isset($post['code']) || !$post['code']){
  272. return $this->renderError('兑换码不能为空');
  273. }
  274. //兑换码匹配汉字数字字母
  275. $pattern = "/^[a-zA-Z0-9\x{4e00}-\x{9fa5}]+$/u";
  276. if(!preg_match($pattern,$post['code'])){
  277. return $this->renderError('兑换码格式不正确');
  278. }
  279. $codeM = new CouponRedeemCodes();
  280. if ($codeM->checkExistedCode($post['code'])){
  281. return $this->renderError('兑换码已被使用');
  282. }
  283. }
  284. if( $m->addCodes($post)){
  285. return $this->renderSuccess([],'成功');
  286. }
  287. return $this->renderError('系统繁忙');
  288. }
  289. /**
  290. *优惠券兑换码列表
  291. * @return array
  292. */
  293. public function couponCodesList(){
  294. $param = $this->request->param();
  295. $m = new CouponGenRedeemCodes();
  296. $list = $m->getAllList($param);
  297. return $this->renderSuccess(compact('list'),'成功');
  298. }
  299. /**
  300. *优惠券兑换码明细表
  301. * @return array
  302. */
  303. public function codesDetail(){
  304. $m = new CouponRedeemCodes();
  305. $list = $m->getAllList($this->request->param('id'));
  306. return $this->renderSuccess(compact('list'),'成功');
  307. }
  308. /**
  309. * 兑换码导出功能
  310. * @return array
  311. * @Time: 2021/10/15 13:43
  312. */
  313. public function redeemCodesExport()
  314. {
  315. $param = $this->request->param();
  316. /* if(!isset($param['ids']) || empty($param['ids'])){
  317. return $this->renderError('请勾选兑换码后再导出');
  318. }*/
  319. $model = new CouponRedeemCodes;
  320. if(isset($param['id']) && $param['id'] > 0){
  321. $data = $model->redeemCodesExport($param,true);
  322. //return $this->renderError('请勾选兑换码后再导出');
  323. }else{
  324. $data = $model->redeemCodesExport($param);
  325. }
  326. //$data = $model->redeemCodesExport($param);
  327. if (!$data['data'] || !count($data['data'])){
  328. return $this->renderError('没有数据哟');
  329. }
  330. $res = ExportService::export($data['data'],$data['header'],$data['filename'],'兑换码列表');
  331. return $this->renderSuccess($res,'导出成功');
  332. }
  333. /**
  334. * 优惠券统计表
  335. * @return array
  336. */
  337. public function couponStatistics()
  338. {
  339. $params = $this->request->param();
  340. $m = new CouponModel();
  341. $list = $m->getStatisticList($params);
  342. return $this->renderSuccess(compact('list'),'成功');
  343. }
  344. /**
  345. * 优惠券统计表-统计明细
  346. * @return array
  347. */
  348. public function couponStatisticsDetail()
  349. {
  350. $params = $this->request->param();
  351. if (empty($params['couponId'])){
  352. return $this->renderError('要选择优惠券哟');
  353. }
  354. $m = new UserCoupon();
  355. $list = $m->getStatisticDetail($params);
  356. return $this->renderSuccess(compact('list'),'成功');
  357. }
  358. /**
  359. * 会员优惠券统计表
  360. * @return array
  361. */
  362. public function memberCouponStatistics()
  363. {
  364. $params = $this->request->param();
  365. $m = new CouponModel();
  366. $list = $m->getMemberStatisticList($params);
  367. return $this->renderSuccess(compact('list'),'成功');
  368. }
  369. }