ActivityDiscount.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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\common\model\ActivityDiscount as ActivityDiscountModel;
  17. use app\common\model\ActivityDiscountGoods as ActivityDiscountGoodsModel;
  18. use app\common\service\Export as ExportService;
  19. use think\facade\Validate;
  20. use app\common\model\Goods;
  21. use app\common\model\GoodsSku;
  22. use app\common\library\helper;
  23. /**
  24. * 活动中心 ▸ N件X折 管理
  25. * Class Files
  26. * @package app\store\controller
  27. */
  28. class ActivityDiscount extends Controller
  29. {
  30. /**
  31. * 添加
  32. * @return array|string
  33. */
  34. public function add()
  35. {
  36. // 新增记录
  37. $model = new ActivityDiscountModel;
  38. $post = $this->postForm();
  39. $post['code'] = rand(10000000,99999999);
  40. if (isset($post['role']) && $post['role'] == 1){
  41. $post['audit_status_zg'] = 10;
  42. $post['is_up'] = 1;
  43. }else{
  44. $post['audit_status_zg'] = 0;
  45. }
  46. $arr = array_column($post['goods'],'limit_stock','goods_id');
  47. $goods_id = helper::getArrayColumn($post['goods'],'goods_id');
  48. $goods = \app\store\model\Goods::whereIn('goods_id',$goods_id)->select();
  49. foreach ($goods as $good){
  50. if(isset($arr[$good['goods_id']]) && $arr[$good['goods_id']]<=0){
  51. return $this->renderError('商品 '.$good['goods_name'].' 库存不能设置为0');
  52. }
  53. if(isset($arr[$good['goods_id']]) && $good['stock_total']<$arr[$good['goods_id']]){
  54. return $this->renderError('商品 '.$good['goods_name'].' 库存不足');
  55. }
  56. }
  57. if ($model->addOne($post)) {
  58. return $this->renderSuccess('添加成功');
  59. }
  60. return $this->renderError($model->getError() ?: '添加失败');
  61. }
  62. public function edit(){
  63. // 新增记录
  64. $model = new ActivityDiscountModel;
  65. $post = $this->postForm();
  66. if (isset($post['role']) && $post['role'] == 1){
  67. $post['audit_status_zg'] = 10;
  68. $post['is_up'] = 1;
  69. }else{
  70. $post['audit_status_zg'] = 0;
  71. }
  72. $arr = array_column($post['goods'],'limit_stock','goods_id');
  73. $goods_id = helper::getArrayColumn($post['goods'],'goods_id');
  74. $goods = Goods::whereIn('goods_id',$goods_id)->select();
  75. foreach ($goods as $good){
  76. if(isset($arr[$good['goods_id']]) && $arr[$good['goods_id']]<=0){
  77. return $this->renderError('商品 '.$good['goods_name'].' 库存不能设置为0');
  78. }
  79. $fullSendGoods = ActivityDiscountGoodsModel::where(['activity_id'=>$post['id'],'goods_id'=>$good['goods_id']])->find();
  80. if(isset($arr[$good['goods_id']]) && $good['stock_total']<($arr[$good['goods_id']]-$fullSendGoods['purchase_stock'])){
  81. return $this->renderError('商品 '.$good['goods_name'].' 库存不足');
  82. }
  83. }
  84. $model = ActivityDiscountModel::where('id',$post['id'])->find();
  85. if ($model->edit($post)) {
  86. return $this->renderSuccess('修改成功');
  87. }
  88. return $this->renderError($model->getError() ?: '添加失败');
  89. }
  90. /**
  91. * 审核
  92. * @param int $coupon_id
  93. * @return array|bool
  94. */
  95. public function audit()
  96. {
  97. // 确认审核
  98. $postData = $this->postForm();
  99. $model = ActivityDiscountModel::where('id',$postData['id'])->find();
  100. if(empty($model)){
  101. return $this->renderError('找不到数据');
  102. }
  103. unset($postData['id']);
  104. $res ='操作成功';
  105. $audit_status = $postData['audit_status_zg'];
  106. if($audit_status==10){
  107. $res = '已审核通过';
  108. }
  109. if($audit_status==20){
  110. $res = '已审核不通过';
  111. }
  112. $postData['audit_time'] = Date("Y-m-d H:i:s",time());
  113. $postData['audit_user'] = $this->store['user']['real_name'];
  114. if ($model->audit($postData)) {
  115. return $this->renderSuccess($postData,$res);
  116. }
  117. return $this->renderError($model->getError() ?: '操作失败');
  118. }
  119. //活动详情
  120. public function detail(int $id){
  121. $detail = ActivityDiscountModel::detail($id,['goods','his']);
  122. $goods_ids = helper::getArrayColumn($detail['goods'],'goods_id');
  123. $arr = \app\store\model\Goods::whereIn('goods_id',$goods_ids)->column('stock_total','goods_id');
  124. $newarr = [];
  125. foreach($detail['goods'] as &$item){
  126. // 商品信息
  127. $goods = Goods::detail($item['goods_id'], ['images' => ['file'], 'skuList' => ['image']]);
  128. if (empty($goods)) {
  129. $this->error = "商品信息错误";
  130. continue;
  131. // return false;
  132. }
  133. if(isset($arr[$item['goods_id']])){
  134. $item['stock_total'] = $arr[$item['goods_id']];
  135. }
  136. $goodsInfo['goods_id'] = $item['goods_id'];
  137. $goodsInfo['limit_stock'] = $item['limit_stock'];
  138. $goodsInfo['residue_stock'] = $item['residue_stock'];
  139. $goodsInfo['goods_sku_id'] = $item['goods_sku_id'];
  140. $goodsInfo['goods_no'] = $goods['goods_no'];
  141. $goodsInfo['goods_images'] = helper::getArrayColumn($goods['images'], 'file');
  142. // 商品主图
  143. $goodsInfo['goods_image'] = current($goodsInfo['goods_images'])['preview_url'];
  144. $goodsInfo['image_id'] = current($goodsInfo['goods_images'])['file_id'];
  145. $goodsSku = GoodsSku::detail($item['goods_id'], $item['goods_sku_id']);
  146. // if(empty($goodsSku)) {
  147. // $this->error = "商品信息错误";
  148. // continue;
  149. // return false;
  150. // }
  151. $goodsInfo['goods_price_min'] = $goods['goods_price_min'];
  152. // $goodsInfo['goods_price'] = $goodsSku['goods_price'];
  153. $goodsInfo['status'] = $goods['status'];
  154. $goodsInfo['goods_name'] = $goods['goods_name'];
  155. unset($goodsInfo['goods_images']);
  156. $newarr[] = ['goods_info'=>$goodsInfo];
  157. }
  158. $detail['goods_list'] = $newarr;
  159. return $this->renderSuccess(compact('detail'));
  160. }
  161. /**
  162. * 启用 禁用
  163. * @param int $coupon_id
  164. * @return array|bool
  165. */
  166. public function down(int $id){
  167. // 确认审核
  168. // $postData = $this->postForm();
  169. $model = ActivityDiscountModel::where('id',$id)->find();
  170. if(empty($model)){
  171. return $this->renderError('找不到数据');
  172. }
  173. if($model['is_up']==0){
  174. return $this->renderError('已经作废了,不要重复操作');
  175. }
  176. $postData['is_up'] = 0;
  177. $postData['end_time'] = Date("Y-m-d H:i:s",time()-1);
  178. if ($model->down($postData)) {
  179. return $this->renderSuccess($postData,'作废成功,活动已失效');
  180. }
  181. return $this->renderError($model->getError() ?: '操作失败');
  182. }
  183. // /**
  184. // * 添加卡
  185. // * @return array|string
  186. // */
  187. // public function update()
  188. // {
  189. // // 新增记录
  190. // $model = new CouponModel;
  191. // $post = $this->postForm();
  192. // // $post['coupon_type'] = CouponModel::ManJian;
  193. // $post['create_user'] =$this->store['user']['real_name'];
  194. // if($post['limit_total_type']==10){
  195. // if(empty($post['total_num'])){
  196. // return $this->renderError('限制数量total_num参数值要大于0');
  197. // }
  198. // }
  199. // if($post['limit_total_type']==20){
  200. // $post['total_num'] = $post['total_num']??0;
  201. // if($post['total_num']>0){
  202. // return $this->renderError('不限制数量total_num应该为0');
  203. // }
  204. // }
  205. // if ($model->updateOne($post)) {
  206. // return $this->renderSuccess('修改成功');
  207. // }
  208. // return $this->renderError($model->getError() ?: '修改失败');
  209. // }
  210. // //后台优惠券列表
  211. // public function couponlist(){
  212. // $model = new CouponModel;
  213. // $list = $model->getList($this->request->param());
  214. // return $this->renderSuccess(compact('list'));
  215. // }
  216. /**
  217. * 列表
  218. * @return array
  219. * @throws \think\db\exception\DbException
  220. */
  221. public function list()
  222. {
  223. $model = new ActivityDiscountModel;
  224. $list = $model->getList($this->request->param());
  225. return $this->renderSuccess(compact('list'));
  226. }
  227. // //冻结
  228. // public function frozen($id,$frozen_state=1){
  229. // $model = new UserCouponModel;
  230. // $model->where('id',$id)->update(['frozen_state'=>$frozen_state]);
  231. // return $this->renderSuccess('更新成功');
  232. // }
  233. // //兑换明细
  234. // public function detaillist($user_coupon_id=0){
  235. // $model = new UsercoupondetailModel;
  236. // $param['user_coupon_id'] = $user_coupon_id;
  237. // $list = $model->getAllList($param);
  238. // return $this->renderSuccess(compact('list'));
  239. // }
  240. // //米卡配置列表
  241. // public function cardlist(){
  242. // $model = new CouponcardModel;
  243. // $list = $model->getAllList([]);
  244. // return $this->renderSuccess(compact('list'));
  245. // }
  246. // //添加米卡券
  247. // public function createcoupon($id,$num=1){
  248. // $model = new CouponcardModel;
  249. // $list = $model->addCard($id,$num);
  250. // return $this->renderSuccess('添加成功');
  251. // }
  252. // //添加数据
  253. // public function addcard(){
  254. // $param = $this->request->param();
  255. // $model = new CouponcardModel;
  256. // $param['created_at'] =Date("Y-m-d H:i:s",time());
  257. // $param['good_sku_id'] = empty($param['good_sku_id'])?0:$param['good_sku_id'];
  258. // $model->save($param);
  259. // return $this->renderSuccess('添加成功');
  260. // }
  261. // /**
  262. // * 编辑文件
  263. // * @param int $fileId
  264. // * @return array
  265. // */
  266. // public function edit(int $fileId)
  267. // {
  268. // // 文件详情
  269. // $model = UploadFileModel::detail($fileId);
  270. // // 更新记录
  271. // if ($model->edit($this->postForm())) {
  272. // return $this->renderSuccess('更新成功');
  273. // }
  274. // return $this->renderError($model->getError() ?: '更新失败');
  275. // }
  276. // /**
  277. // * 删除文件(批量)
  278. // * @param array $fileIds 文件id集
  279. // * @return array
  280. // * @throws \think\Exception
  281. // * @throws \think\db\exception\DataNotFoundException
  282. // * @throws \think\db\exception\DbException
  283. // * @throws \think\db\exception\ModelNotFoundException
  284. // */
  285. // public function delete(array $fileIds)
  286. // {
  287. // $model = new UploadFileModel;
  288. // if (!$model->setDelete($fileIds)) {
  289. // return $this->renderError($model->getError() ?: '操作失败');
  290. // }
  291. // return $this->renderSuccess('操作成功');
  292. // }
  293. // /**
  294. // * 移动文件到指定分组(批量)
  295. // * @param int $groupId
  296. // * @param array $fileIds
  297. // * @return array
  298. // */
  299. // public function moveGroup(int $groupId, array $fileIds)
  300. // {
  301. // $model = new UploadFileModel;
  302. // if (!$model->moveGroup($groupId, $fileIds)) {
  303. // return $this->renderError($model->getError() ?: '操作失败');
  304. // }
  305. // return $this->renderSuccess('操作成功');
  306. // }
  307. /**
  308. * zq---------------------------------------------------------------------------------------------------------------
  309. * @Time: 2021/12/29 13:43
  310. */
  311. // /**
  312. // * 生成优惠券的兑换码
  313. // * @return array
  314. // */
  315. // public function genCouponCodes(){
  316. // $post = $this->postForm();
  317. // $validate = Validate::rule('couponId', 'require|number|gt:0')
  318. // ->rule([
  319. // 'amount' => 'require|number|gt:0',
  320. // 'giveType' => 'require|number|in:1,2'
  321. // ]);
  322. // if (!$validate->check($post)) {
  323. // return $this->renderError($validate->getError());
  324. // }
  325. // //检验优惠券数量是否超发
  326. // $m = new CouponGenRedeemCodes();
  327. // /* $flag = $m->sunAmount($post['couponId'],$post['amount']);
  328. // if($flag){
  329. // return $this->renderError('优惠券数量已达上限');
  330. // }*/
  331. // if($post['giveType'] == 1){
  332. // if (!isset($post['code']) || !$post['code']){
  333. // return $this->renderError('兑换码不能为空');
  334. // }
  335. // //兑换码匹配汉字数字字母
  336. // $pattern = "/^[a-zA-Z0-9\x{4e00}-\x{9fa5}]+$/u";
  337. // if(!preg_match($pattern,$post['code'])){
  338. // return $this->renderError('兑换码格式不正确');
  339. // }
  340. // $codeM = new CouponRedeemCodes();
  341. // if ($codeM->checkExistedCode($post['code'])){
  342. // return $this->renderError('兑换码已被使用');
  343. // }
  344. // }
  345. // if( $m->addCodes($post)){
  346. // return $this->renderSuccess([],'成功');
  347. // }
  348. // return $this->renderError('系统繁忙');
  349. // }
  350. // /**
  351. // *优惠券兑换码列表
  352. // * @return array
  353. // */
  354. // public function couponCodesList(){
  355. // $param = $this->request->param();
  356. // $m = new CouponGenRedeemCodes();
  357. // $list = $m->getAllList($param);
  358. // return $this->renderSuccess(compact('list'),'成功');
  359. // }
  360. // /**
  361. // *优惠券兑换码明细表
  362. // * @return array
  363. // */
  364. // public function codesDetail(){
  365. // $m = new CouponRedeemCodes();
  366. // $list = $m->getAllList($this->request->param('id'));
  367. // return $this->renderSuccess(compact('list'),'成功');
  368. // }
  369. // /**
  370. // * 兑换码导出功能
  371. // * @return array
  372. // * @Time: 2021/10/15 13:43
  373. // */
  374. // public function redeemCodesExport()
  375. // {
  376. // $param = $this->request->param();
  377. // /* if(!isset($param['ids']) || empty($param['ids'])){
  378. // return $this->renderError('请勾选兑换码后再导出');
  379. // }*/
  380. // $model = new CouponRedeemCodes;
  381. // if(isset($param['id']) && $param['id'] > 0){
  382. // $data = $model->redeemCodesExport($param,true);
  383. // //return $this->renderError('请勾选兑换码后再导出');
  384. // }else{
  385. // $data = $model->redeemCodesExport($param);
  386. // }
  387. // //$data = $model->redeemCodesExport($param);
  388. // if (!$data['data'] || !count($data['data'])){
  389. // return $this->renderError('没有数据哟');
  390. // }
  391. // $res = ExportService::export($data['data'],$data['header'],$data['filename'],'兑换码列表');
  392. // return $this->renderSuccess($res,'导出成功');
  393. // }
  394. // /**
  395. // * 优惠券统计表
  396. // * @return array
  397. // */
  398. // public function couponStatistics()
  399. // {
  400. // $params = $this->request->param();
  401. // $m = new CouponModel();
  402. // $list = $m->getStatisticList($params);
  403. // return $this->renderSuccess(compact('list'),'成功');
  404. // }
  405. // /**
  406. // * 优惠券统计表-统计明细
  407. // * @return array
  408. // */
  409. // public function couponStatisticsDetail()
  410. // {
  411. // $params = $this->request->param();
  412. // if (empty($params['couponId'])){
  413. // return $this->renderError('要选择优惠券哟');
  414. // }
  415. // $m = new UserCoupon();
  416. // $list = $m->getStatisticDetail($params);
  417. // return $this->renderSuccess(compact('list'),'成功');
  418. // }
  419. }