123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- <?php
- namespace app\store\controller\members;
- use app\common\enum\Setting as SettingEnum;
- use app\common\library\storage\Driver as StorageDriver;
- use app\common\service\Export;
- use app\store\controller\Controller;
- use app\store\model\Coupon as CouponModel;
- use app\store\model\member\MemberCardsAction;
- use app\store\model\member\MemberCooperationCoupon;
- use app\store\model\member\MemberWelfare;
- use app\store\model\member\MemberWelfare as MemberWelfareModel;
- use app\store\model\member\MemberWelfareDraw;
- use app\store\model\Setting as SettingModel;
- use app\store\model\UploadFile;
- use think\facade\Validate;
- use app\common\service\Export as ExportService;
- /**
- * 月度会员福利
- * Class MonthlyWelfare
- * @package app\store\controller\members
- */
- class MonthlyWelfare extends Controller
- {
- /**
- * 添加会员福利(会员专享券)
- * @return array|string
- */
- public function add()
- {
- // 新增记录
- $model = new MemberWelfareModel;
- $param = $this->postForm();
- $param['audit_user'] = $this->store['user']['real_name']?:'';
- $param['audit_admin_id'] = $this->store['user']['store_user_id']?:'';
- $param['number'] = str_replace('.','',microtime(true)); // 生成活动编号
- if ($param['target_type'] == 1 && empty($param['coupon_id'])){
- return $this->renderError('请完善福利内容');
- }
- if ($param['target_type'] == 2 && $param['deploy_type']==1 && empty($param['codes_file'])){
- return $this->renderError('请完善福利内容');
- }
- if ($param['target_type'] == 2 && $param['deploy_type']==2 && empty($param['welfare_name'])){
- return $this->renderError('请完善福利内容');
- }
- $etypes = $param['exchange_type']??[];
- if (is_array($etypes)){
- $etypes = array_map(function($v){
- return intval($v);
- },$etypes);
- }else{
- $etypes = [];
- }
- $param['exchange_type'] = json_encode($etypes);
- $send_times = between_time_format($param['send_times']);
- $param['start_time'] = date('Y-m-01',$send_times['start_time']);
- $nextM = mktime(0,0,0,date('m',$send_times['end_time']) +1,1,date('Y',$send_times['end_time']));
- $param['end_time'] = date('Y-m-d',$nextM - 1 );
- //$param['end_time'] = date('Y-m-d',$send_times['end_time']);
- if($param['target_type'] == 2){//异业福利才有使用有效期
- $use_times = between_time_format($param['use_times']);
- $param['use_start_time'] = date('Y-m-d',$use_times['start_time']);
- $param['use_end_time'] = date('Y-m-d',$use_times['end_time']);
- }else{
- $param['use_start_time']=$param['use_end_time'] = null;
- }
- unset($param['send_times'],$param['use_times']);
- //unset($param['use_times']);
- //dd($param);
- $flag = $model->addOne($param);
- if ($flag == 1) {
- return $this->renderSuccess('添加成功');
- }
- return $this->renderError($model->getError() ?: '添加失败');
- }
- //store/upload/image
- //{"status":200,"message":"图片上传成功","data":{"fileInfo":{"group_id":0,"channel":10,"storage":"aliyun","domain":"http:\/\/img-test.cookhome.club","file_name":"default_unit_logo2.png","file_path":"10001\/20220525\/d3076d109c83f3acc07c31f189407c7c.png","file_size":1923,"file_ext":"png","file_type":10,"store_id":10001,"create_time":"2022-05-25 16:57:23","update_time":"2022-05-25 16:57:23","file_id":"10866"}}}
- /**
- * 会员专享券详情
- * @param int $id
- * @return
- */
- public function detail(int $id)
- {
- $detail = MemberWelfareModel::detail($id,['file','coupon','urls']);
- return $this->renderSuccess(compact('detail'));
- }
- /**
- * 会员专享券编辑
- * @return array|string
- */
- public function update()
- {
- // 新增记录
- $param = $this->postForm();
- $model = MemberWelfareModel::detail($param['id']);
- if ($model->audit_status == 1){
- return $this->renderError( '不支持此操作');
- }
- if ($param['target_type'] == 1 && empty($param['coupon_id'])){
- return $this->renderError('请完善福利内容');
- }
- if ($param['target_type'] == 2 && $param['deploy_type']==1 && empty($param['codes_file'])){
- return $this->renderError('请完善福利内容');
- }
- if ($param['target_type'] == 2 && $param['deploy_type']==2 && empty($param['welfare_name'])){
- return $this->renderError('请完善福利内容');
- }
- $etypes = $param['exchange_type']??[];
- if (is_array($etypes)){
- $etypes = array_map(function($v){
- return intval($v);
- },$etypes);
- }else{
- $etypes = [];
- }
- $param['exchange_type'] = json_encode($etypes);
- $param['audit_user'] = $this->store['user']['real_name']?:'';
- $param['audit_admin_id'] = $this->store['user']['store_user_id']?:'';
- $flag = $model->edit($param);
- if ($flag == 1) {
- return $this->renderSuccess('修改成功');
- }
- return $this->renderError($model->getError() ?: '修改失败');
- }
- /**
- * 会员专享券审核
- * @param
- * @return array|bool
- */
- public function audit()
- {
- // 确认审核
- $postData = $this->postForm();
- $model = MemberWelfareModel::detail($postData['id']);
- if(empty($model)){
- return $this->renderError('找不到');
- }
- $res ='操作成功';
- if($model->audit_status == 1){
- return $this->renderError( '已审核通过,无法再操作');
- }
- $postData['audit_time'] = Date("Y-m-d H:i:s",time());
- $postData['audit_user'] = $this->store['user']['real_name']?:'';
- $postData['audit_admin_id'] = $this->store['user']['store_user_id']?:'';
- $flag = $model->audit($postData);
- if ($flag) {
- if ($postData['audit_status']==1 && $model->deploy_type==1 && !empty($model['codes_file'])){//导入第三方券码
- $file = UploadFile::detail(intval($model['codes_file']));
- if ($postData['audit_status'] == 1 && $model['target_type'] == 2 && $file){
- //审核通过导入券码
- $flagPath = $this->importCodes($model->id,$file->file_path);
- if ($flagPath === false){
- log_record(__METHOD__.'导入券码失败','error');
- return $this->renderError( '系统繁忙,请稍后再试');
- }
- }
- }
- if($postData['audit_status']==1 && $model['target_type'] == 2 && $model->deploy_type==2){//自制券码
- $code = random_character(6);
- $datas = [];
- for ($i = 1; $i <= $model->amount; $i++) {
- $datas[] = [
- 'coupon_id' => 0,
- 'coupon_name' => $model->welfare_name ?? '',
- 'create_time' => time(),
- 'update_time' => time(),
- 'welfare_id' => $postData['id'],
- 'code' => $code,
- 'provider' => $model->provider ?? '',
- ];
- }
- (new MemberCooperationCoupon())->insertAll($datas);
- }
- return $this->renderSuccess($postData,$res);
- }
- return $this->renderError($model->getError() ?: '操作失败');
- }
- /**
- * 会员专享券行动日志
- * @param int $id
- * @return array
- */
- public function actionsList(int $id)
- {
- $model = new MemberCardsAction();
- $list = $model->getLists($id,2);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 会员福利列表
- * @return array
- */
- public function welfareLists(){
- $model = new MemberWelfareModel;
- $params = $this->request->param();
- $list = $model->lists($params);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 编辑序号
- * @return array
- */
- public function editOrder(){
- $postData = $this->postForm();
- $data['id'] = $postData['id'];
- $data['order_at'] = $postData['order_at'];
- $model = new MemberWelfareModel;
- $flag = $model->editOrder($data);
- if ($flag == true){
- return $this->renderSuccess();
- }
- return $this->renderError($model->getError() ?: '操作失败');
- }
- /**
- * 列表删除福利
- * @param $id
- * @return array
- */
- public function delWelfare($id){
- $data['id'] = $id;
- $model = new MemberWelfareModel;
- $flag = $model->del($data);
- if ($flag == true){
- return $this->renderSuccess();
- }
- return $this->renderError($model->getError() ?: '操作失败');
- }
- /**
- * @param $id
- * @param $codesFileUrl
- * @return bool
- * @throws \PhpOffice\PhpSpreadsheet\Exception
- * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private function importCodes($id,$codesFileUrl): bool
- {
- $config = SettingModel::getItem(SettingEnum::STORAGE);
- $storage = new StorageDriver($config);
- $localFile = $storage->download($codesFileUrl);
- $flag = Export::importCooperationCodes($id,$localFile);
- if (file_exists($localFile)){
- unlink($localFile);//处理完删除文件 todo
- }
- return $flag;
- }
- /**
- * 会员专享券列表select
- * @return array
- */
- public function couponlist(){
- $params = $this->request->get();
- $model = new CouponModel;
- $list = $model->getMemTypeList($params);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 异业会员福利领取记录
- * @return array
- */
- public function getDiffWelfareDrawRecords(){
- $model = new MemberWelfareModel;
- $params = $this->request->param();
- $list = $model->listsDiff($params);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 异业会员福利领取记录导出
- * @return array
- * @Time: 2021/10/15 13:43
- */
- public function getDiffwDrawExport()
- {
- $param = $this->request->param();
- /* if(!isset($param['ids']) || empty($param['ids'])){
- return $this->renderError('请勾选兑换码后再导出');
- }*/
- $model = new MemberWelfareModel;
- $data = $model->diffwDrawExportData($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 getDiffWelDrawDetails(){
- $params = $this->request->get();
- $validate = Validate::rule('targetId', 'require|number|gt:0')
- ->rule([
- /* 'checkStatus' => 'require|number|between:0,1',
- 'hasExpired' => 'require|number|between:0,1',*/
- 'mobile' => 'number',
- ]);
- if (!$validate->check($params)) {
- return $this->renderError($validate->getError());
- }
- $model = new MemberWelfareDraw();
- $list = $model->getLists($params);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 异业福利领取明细导出
- * @return array
- * @Time: 2021/10/15 13:43
- */
- public function getDiffwDrawDetExport()
- {
- $params = $this->request->param();
- $validate = Validate::rule('targetId', 'require|number|gt:0')
- ->rule([
- /* 'checkStatus' => 'require|number|between:0,1',
- 'hasExpired' => 'require|number|between:0,1',*/
- 'mobile' => 'number',
- ]);
- if (!$validate->check($params)) {
- return $this->renderError($validate->getError());
- }
- $model = new MemberWelfareDraw;
- $data = $model->diffwDrawExportData($params);
- if (!$data['data'] || !count($data['data'])){
- return $this->renderError('没有数据哟');
- }
- $res = ExportService::export($data['data'],$data['header'],$data['filename'],'异业福利领取明细列表');
- return $this->renderSuccess($res,'导出成功');
- }
- }
|