123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\store\controller;
- // use app\common\model\coupon\Usercoupon as UserCouponModel;
- // use app\common\model\coupon\Usercoupondetail as UsercoupondetailModel;
- // use app\common\model\coupon\Couponcard as CouponcardModel;
- use app\common\model\ActivityDiscount as ActivityDiscountModel;
- use app\common\model\ActivityDiscountGoods as ActivityDiscountGoodsModel;
- use app\common\service\Export as ExportService;
- use think\facade\Validate;
- use app\common\model\Goods;
- use app\common\model\GoodsSku;
- use app\common\library\helper;
- /**
- * 活动中心 ▸ N件X折 管理
- * Class Files
- * @package app\store\controller
- */
- class ActivityDiscount extends Controller
- {
- /**
- * 添加
- * @return array|string
- */
- public function add()
- {
- // 新增记录
- $model = new ActivityDiscountModel;
- $post = $this->postForm();
- $post['code'] = rand(10000000,99999999);
- if (isset($post['role']) && $post['role'] == 1){
- $post['audit_status_zg'] = 10;
- $post['is_up'] = 1;
- }else{
- $post['audit_status_zg'] = 0;
- }
- $arr = array_column($post['goods'],'limit_stock','goods_id');
- $goods_id = helper::getArrayColumn($post['goods'],'goods_id');
- $goods = \app\store\model\Goods::whereIn('goods_id',$goods_id)->select();
- foreach ($goods as $good){
- if(isset($arr[$good['goods_id']]) && $arr[$good['goods_id']]<=0){
- return $this->renderError('商品 '.$good['goods_name'].' 库存不能设置为0');
- }
- if(isset($arr[$good['goods_id']]) && $good['stock_total']<$arr[$good['goods_id']]){
- return $this->renderError('商品 '.$good['goods_name'].' 库存不足');
- }
- }
- if ($model->addOne($post)) {
- return $this->renderSuccess('添加成功');
- }
- return $this->renderError($model->getError() ?: '添加失败');
- }
- public function edit(){
- // 新增记录
- $model = new ActivityDiscountModel;
- $post = $this->postForm();
- if (isset($post['role']) && $post['role'] == 1){
- $post['audit_status_zg'] = 10;
- $post['is_up'] = 1;
- }else{
- $post['audit_status_zg'] = 0;
- }
- $arr = array_column($post['goods'],'limit_stock','goods_id');
- $goods_id = helper::getArrayColumn($post['goods'],'goods_id');
- $goods = Goods::whereIn('goods_id',$goods_id)->select();
- foreach ($goods as $good){
- if(isset($arr[$good['goods_id']]) && $arr[$good['goods_id']]<=0){
- return $this->renderError('商品 '.$good['goods_name'].' 库存不能设置为0');
- }
- $fullSendGoods = ActivityDiscountGoodsModel::where(['activity_id'=>$post['id'],'goods_id'=>$good['goods_id']])->find();
- if(isset($arr[$good['goods_id']]) && $good['stock_total']<($arr[$good['goods_id']]-$fullSendGoods['purchase_stock'])){
- return $this->renderError('商品 '.$good['goods_name'].' 库存不足');
- }
- }
- $model = ActivityDiscountModel::where('id',$post['id'])->find();
- if ($model->edit($post)) {
- return $this->renderSuccess('修改成功');
- }
- return $this->renderError($model->getError() ?: '添加失败');
- }
- /**
- * 审核
- * @param int $coupon_id
- * @return array|bool
- */
- public function audit()
- {
- // 确认审核
- $postData = $this->postForm();
- $model = ActivityDiscountModel::where('id',$postData['id'])->find();
- if(empty($model)){
- return $this->renderError('找不到数据');
- }
- unset($postData['id']);
- $res ='操作成功';
- $audit_status = $postData['audit_status_zg'];
- if($audit_status==10){
- $res = '已审核通过';
- }
- if($audit_status==20){
- $res = '已审核不通过';
- }
- $postData['audit_time'] = Date("Y-m-d H:i:s",time());
- $postData['audit_user'] = $this->store['user']['real_name'];
- if ($model->audit($postData)) {
- return $this->renderSuccess($postData,$res);
- }
- return $this->renderError($model->getError() ?: '操作失败');
- }
- //活动详情
- public function detail(int $id){
-
- $detail = ActivityDiscountModel::detail($id,['goods','his']);
- $goods_ids = helper::getArrayColumn($detail['goods'],'goods_id');
- $arr = \app\store\model\Goods::whereIn('goods_id',$goods_ids)->column('stock_total','goods_id');
- $newarr = [];
- foreach($detail['goods'] as &$item){
- // 商品信息
- $goods = Goods::detail($item['goods_id'], ['images' => ['file'], 'skuList' => ['image']]);
- if (empty($goods)) {
- $this->error = "商品信息错误";
- continue;
- // return false;
- }
- if(isset($arr[$item['goods_id']])){
- $item['stock_total'] = $arr[$item['goods_id']];
- }
- $goodsInfo['goods_id'] = $item['goods_id'];
- $goodsInfo['limit_stock'] = $item['limit_stock'];
- $goodsInfo['residue_stock'] = $item['residue_stock'];
- $goodsInfo['goods_sku_id'] = $item['goods_sku_id'];
- $goodsInfo['goods_no'] = $goods['goods_no'];
- $goodsInfo['goods_images'] = helper::getArrayColumn($goods['images'], 'file');
- // 商品主图
- $goodsInfo['goods_image'] = current($goodsInfo['goods_images'])['preview_url'];
- $goodsInfo['image_id'] = current($goodsInfo['goods_images'])['file_id'];
- $goodsSku = GoodsSku::detail($item['goods_id'], $item['goods_sku_id']);
- // if(empty($goodsSku)) {
- // $this->error = "商品信息错误";
- // continue;
- // return false;
- // }
- $goodsInfo['goods_price_min'] = $goods['goods_price_min'];
- // $goodsInfo['goods_price'] = $goodsSku['goods_price'];
- $goodsInfo['status'] = $goods['status'];
- $goodsInfo['goods_name'] = $goods['goods_name'];
- unset($goodsInfo['goods_images']);
- $newarr[] = ['goods_info'=>$goodsInfo];
- }
- $detail['goods_list'] = $newarr;
-
- return $this->renderSuccess(compact('detail'));
- }
- /**
- * 启用 禁用
- * @param int $coupon_id
- * @return array|bool
- */
- public function down(int $id){
- // 确认审核
- // $postData = $this->postForm();
- $model = ActivityDiscountModel::where('id',$id)->find();
- if(empty($model)){
- return $this->renderError('找不到数据');
- }
- if($model['is_up']==0){
- return $this->renderError('已经作废了,不要重复操作');
- }
- $postData['is_up'] = 0;
- $postData['end_time'] = Date("Y-m-d H:i:s",time()-1);
- if ($model->down($postData)) {
- return $this->renderSuccess($postData,'作废成功,活动已失效');
- }
- return $this->renderError($model->getError() ?: '操作失败');
- }
- // /**
- // * 添加卡
- // * @return array|string
- // */
- // public function update()
- // {
- // // 新增记录
- // $model = new CouponModel;
- // $post = $this->postForm();
- // // $post['coupon_type'] = CouponModel::ManJian;
- // $post['create_user'] =$this->store['user']['real_name'];
- // if($post['limit_total_type']==10){
- // if(empty($post['total_num'])){
- // return $this->renderError('限制数量total_num参数值要大于0');
- // }
- // }
- // if($post['limit_total_type']==20){
- // $post['total_num'] = $post['total_num']??0;
- // if($post['total_num']>0){
- // return $this->renderError('不限制数量total_num应该为0');
- // }
- // }
- // if ($model->updateOne($post)) {
- // return $this->renderSuccess('修改成功');
- // }
- // return $this->renderError($model->getError() ?: '修改失败');
- // }
-
- // //后台优惠券列表
- // public function couponlist(){
- // $model = new CouponModel;
- // $list = $model->getList($this->request->param());
- // return $this->renderSuccess(compact('list'));
- // }
- /**
- * 列表
- * @return array
- * @throws \think\db\exception\DbException
- */
- public function list()
- {
- $model = new ActivityDiscountModel;
- $list = $model->getList($this->request->param());
- return $this->renderSuccess(compact('list'));
- }
- // //冻结
- // public function frozen($id,$frozen_state=1){
- // $model = new UserCouponModel;
- // $model->where('id',$id)->update(['frozen_state'=>$frozen_state]);
- // return $this->renderSuccess('更新成功');
- // }
- // //兑换明细
- // public function detaillist($user_coupon_id=0){
- // $model = new UsercoupondetailModel;
- // $param['user_coupon_id'] = $user_coupon_id;
- // $list = $model->getAllList($param);
- // return $this->renderSuccess(compact('list'));
- // }
- // //米卡配置列表
- // public function cardlist(){
- // $model = new CouponcardModel;
- // $list = $model->getAllList([]);
- // return $this->renderSuccess(compact('list'));
- // }
- // //添加米卡券
- // public function createcoupon($id,$num=1){
- // $model = new CouponcardModel;
- // $list = $model->addCard($id,$num);
- // return $this->renderSuccess('添加成功');
- // }
- // //添加数据
- // public function addcard(){
- // $param = $this->request->param();
- // $model = new CouponcardModel;
- // $param['created_at'] =Date("Y-m-d H:i:s",time());
- // $param['good_sku_id'] = empty($param['good_sku_id'])?0:$param['good_sku_id'];
- // $model->save($param);
- // return $this->renderSuccess('添加成功');
- // }
- // /**
- // * 编辑文件
- // * @param int $fileId
- // * @return array
- // */
- // public function edit(int $fileId)
- // {
- // // 文件详情
- // $model = UploadFileModel::detail($fileId);
- // // 更新记录
- // if ($model->edit($this->postForm())) {
- // return $this->renderSuccess('更新成功');
- // }
- // return $this->renderError($model->getError() ?: '更新失败');
- // }
- // /**
- // * 删除文件(批量)
- // * @param array $fileIds 文件id集
- // * @return array
- // * @throws \think\Exception
- // * @throws \think\db\exception\DataNotFoundException
- // * @throws \think\db\exception\DbException
- // * @throws \think\db\exception\ModelNotFoundException
- // */
- // public function delete(array $fileIds)
- // {
- // $model = new UploadFileModel;
- // if (!$model->setDelete($fileIds)) {
- // return $this->renderError($model->getError() ?: '操作失败');
- // }
- // return $this->renderSuccess('操作成功');
- // }
- // /**
- // * 移动文件到指定分组(批量)
- // * @param int $groupId
- // * @param array $fileIds
- // * @return array
- // */
- // public function moveGroup(int $groupId, array $fileIds)
- // {
- // $model = new UploadFileModel;
- // if (!$model->moveGroup($groupId, $fileIds)) {
- // return $this->renderError($model->getError() ?: '操作失败');
- // }
- // return $this->renderSuccess('操作成功');
- // }
- /**
- * zq---------------------------------------------------------------------------------------------------------------
- * @Time: 2021/12/29 13:43
- */
- // /**
- // * 生成优惠券的兑换码
- // * @return array
- // */
- // public function genCouponCodes(){
- // $post = $this->postForm();
- // $validate = Validate::rule('couponId', 'require|number|gt:0')
- // ->rule([
- // 'amount' => 'require|number|gt:0',
- // 'giveType' => 'require|number|in:1,2'
- // ]);
- // if (!$validate->check($post)) {
- // return $this->renderError($validate->getError());
- // }
- // //检验优惠券数量是否超发
- // $m = new CouponGenRedeemCodes();
- // /* $flag = $m->sunAmount($post['couponId'],$post['amount']);
- // if($flag){
- // return $this->renderError('优惠券数量已达上限');
- // }*/
- // if($post['giveType'] == 1){
- // if (!isset($post['code']) || !$post['code']){
- // return $this->renderError('兑换码不能为空');
- // }
- // //兑换码匹配汉字数字字母
- // $pattern = "/^[a-zA-Z0-9\x{4e00}-\x{9fa5}]+$/u";
- // if(!preg_match($pattern,$post['code'])){
- // return $this->renderError('兑换码格式不正确');
- // }
- // $codeM = new CouponRedeemCodes();
- // if ($codeM->checkExistedCode($post['code'])){
- // return $this->renderError('兑换码已被使用');
- // }
- // }
- // if( $m->addCodes($post)){
- // return $this->renderSuccess([],'成功');
- // }
- // return $this->renderError('系统繁忙');
- // }
- // /**
- // *优惠券兑换码列表
- // * @return array
- // */
- // public function couponCodesList(){
- // $param = $this->request->param();
- // $m = new CouponGenRedeemCodes();
- // $list = $m->getAllList($param);
- // return $this->renderSuccess(compact('list'),'成功');
- // }
- // /**
- // *优惠券兑换码明细表
- // * @return array
- // */
- // public function codesDetail(){
- // $m = new CouponRedeemCodes();
- // $list = $m->getAllList($this->request->param('id'));
- // return $this->renderSuccess(compact('list'),'成功');
- // }
- // /**
- // * 兑换码导出功能
- // * @return array
- // * @Time: 2021/10/15 13:43
- // */
- // public function redeemCodesExport()
- // {
- // $param = $this->request->param();
- // /* if(!isset($param['ids']) || empty($param['ids'])){
- // return $this->renderError('请勾选兑换码后再导出');
- // }*/
- // $model = new CouponRedeemCodes;
- // if(isset($param['id']) && $param['id'] > 0){
- // $data = $model->redeemCodesExport($param,true);
- // //return $this->renderError('请勾选兑换码后再导出');
- // }else{
- // $data = $model->redeemCodesExport($param);
- // }
- // //$data = $model->redeemCodesExport($param);
- // if (!$data['data'] || !count($data['data'])){
- // return $this->renderError('没有数据哟');
- // }
- // $res = ExportService::export($data['data'],$data['header'],$data['filename'],'兑换码列表');
- // return $this->renderSuccess($res,'导出成功');
- // }
- // /**
- // * 优惠券统计表
- // * @return array
- // */
- // public function couponStatistics()
- // {
- // $params = $this->request->param();
- // $m = new CouponModel();
- // $list = $m->getStatisticList($params);
- // return $this->renderSuccess(compact('list'),'成功');
- // }
- // /**
- // * 优惠券统计表-统计明细
- // * @return array
- // */
- // public function couponStatisticsDetail()
- // {
- // $params = $this->request->param();
- // if (empty($params['couponId'])){
- // return $this->renderError('要选择优惠券哟');
- // }
- // $m = new UserCoupon();
- // $list = $m->getStatisticDetail($params);
- // return $this->renderSuccess(compact('list'),'成功');
- // }
- }
|