123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Http\Requests\Admin\CustomScore as Request;
- use App\Models\Custom;
- use App\Models\CustomScore as Model;
- use Illuminate\Support\Facades\DB;
- use App\Models\FilesManager;
- /**
- * 客户管理
- *
- * @author 刘相欣
- *
- */
- class CustomScore extends Auth{
- protected function _initialize(){
- parent::_initialize();
- $this->assign('breadcrumb1','积分管理');
- $this->assign('breadcrumb2','客户积分');
- }
- /**
- * 添加
- *
- * */
- public function incr(Request $request,Model $Model,Custom $Custom){
- // 接收参数
- $customUid = request('custom_uid',0);
- // 查询数据
- $custom = $Custom->getOne($customUid);
- // 提交
- if( request()->isMethod('post') ){
- // 验证参数
- $request->scene('incr')->validate();
- // 接收数据
- // 接收数据
- $incrScore = request('incr_score',0);
- // 组合数据,写入订单表,子表
- DB::beginTransaction();
- // 写入数据表
- $payId = $Model->trade($customUid,0,$incrScore,4,1);
- // 如果操作失败
- if( isset($payId['error']) ) {
- // 回滚
- DB::rollBack();
- // 结果提示
- return json_send(['code'=>'error','msg'=>$payId['error']]);
- }
- // 提交事务
- DB::commit();
- // 告知结果
- return json_send(['code'=>'success','msg'=>'增加成功','action'=>'add']);
- }
- // 如果不存在
- if( !$custom ) return $this->error('用户不存在');
- // 分配数据
- $this->assign('crumbs','增加积分');
- $this->assign('custom',$custom);
- // 加载模板
- return $this->fetch();
- }
- /**
- * 扣减
- *
- * */
- public function decr(Request $request,Model $Model,Custom $Custom){
- // 接收参数
- $customUid = request('custom_uid',0);
- // 查询数据
- $custom = $Custom->getOne($customUid);
- // 提交
- if( request()->isMethod('post') ){
- // 验证参数
- $request->scene('decr')->validate();
- // 接收数据
- $decrScore = request('decr_score',0);
- // 组合数据,写入订单表,子表
- DB::beginTransaction();
- // 写入数据表
- $payId = $Model->trade($customUid,0,($decrScore*-1),4,2);
- // 如果操作失败
- if( isset($payId['error']) ) {
- // 回滚
- DB::rollBack();
- // 结果提示
- return json_send(['code'=>'error','msg'=>$payId['error']]);
- }
- // 提交事务
- DB::commit();
- // 告知结果
- return json_send(['code'=>'success','msg'=>'扣减成功','action'=>'add']);
- }
- // 如果不存在
- if( !$custom ) return $this->error('用户不存在');
- // 分配数据
- $this->assign('crumbs','扣减积分');
- $this->assign('custom',$custom);
- // 加载模板
- return $this->fetch();
- }
- /**
- * 表格导入
- *
- * */
- public function import_execl( Request $request,Model $Model,Custom $Custom,FilesManager $FilesManager){
- // 验证参数
- $request->scene('import_execl')->validate();
- // 获取表格信息
- $file = request()->file('score_file');
- // 返回结果
- $sheetList = $FilesManager->excelToScore($file);
- // 如果不存在结果
- if( isset($sheetList['error']) ) return json_send(['code'=>'error','msg'=>$sheetList['error']]);
- // 组合数据,写入订单表,子表
- DB::beginTransaction();
- // 循环表格数据
- foreach ($sheetList as $value) {
- // 客户ID
- $customUid = $Custom->codeToId($value['custom_uid']);
- // 如果客户ID有误
- if( !$customUid ) return json_send(['code'=>'error','msg'=>$value['custom_uid'].'编码有误']);
- // 写入数据表
- $payId = $Model->trade($customUid,0,$value['score'],4,($value['score']>0?1:2),$value['description']);
- // 如果操作失败
- if( isset($payId['error']) ) {
- // 回滚
- DB::rollBack();
- // 结果提示
- return json_send(['code'=>'error','msg'=>$value['custom_uid'].'=>'.$payId['error']]);
- }
- }
- // 提交事务
- DB::commit();
- // 提示成功
- return json_send(['code'=>'success','msg'=>'批量导入成功','path'=>'']);
- }
- /**
- * 表格导入
- *
- * */
- public function import_weiban( Request $request,Model $Model,Custom $Custom,FilesManager $FilesManager){
- // 验证参数
- $request->scene('import_execl')->validate();
- // 获取表格信息
- $file = request()->file('score_file');
- // 返回结果
- $sheetList = $FilesManager->weibanToScore($file);
- // 如果不存在结果
- if( isset($sheetList['error']) ) return json_send(['code'=>'error','msg'=>$sheetList['error']]);
- // 通过微伴ID查询用户ID
- $uidList = $Custom->query()->whereIn('weiban_extid',array_column($sheetList,'weiban_extid'))->pluck('uid as custom_uid','weiban_extid')->toArray();
- // 未知用户
- $noUsers = [];
- // 循环列表
- foreach ($sheetList as $key=>$value) {
- // 如果查询不到数据
- if( empty($uidList[$value['weiban_extid']]) ) {
- // 判断
- $noUsers[] = $value['weiban_extid'];
- unset($sheetList[$key]);
- continue;
- }
- // 获取客户ID
- $value['custom_uid'] = $uidList[$value['weiban_extid']];
- // 重组
- $sheetList[$key] = $value;
- }
- // 组合数据,写入订单表,子表
- DB::beginTransaction();
- // 错误信息
- try {
- // 循环表格数据
- foreach ($sheetList as $value) {
- // 写入数据表
- $payId = $Model->trade($value['custom_uid'],0,$value['score'],4,($value['score']>0?1:2),$value['description']);
- // 如果操作失败
- if( isset($payId['error']) ) {
- // 回滚
- DB::rollBack();
- // 结果提示
- return json_send(['code'=>'error','msg'=>$value['weiban_extid'].'=>'.$payId['error']]);
- }
- }
- // 提交事务
- DB::commit();
- // 提示成功
- return json_send(['code'=>'success','msg'=>($noUsers? ' 以下不存用户<br/>'.implode('<br/>',$noUsers) : '批量导入成功'),'path'=>'']);
- } catch (\Throwable $th) {
- // 提示成功
- return json_send(['code'=>'success','msg'=>'批量导入失败','path'=>'','data'=>['error'=>$th->getMessage()]]);
- }
- }
- }
|