CustomScore.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\CustomScore as Request;
  3. use App\Models\Custom;
  4. use App\Models\CustomScore as Model;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Models\FilesManager;
  7. use App\Models\WeiBan\External as WeiBanExternal;
  8. /**
  9. * 客户管理
  10. *
  11. * @author 刘相欣
  12. *
  13. */
  14. class CustomScore extends Auth{
  15. protected function _initialize(){
  16. parent::_initialize();
  17. $this->assign('breadcrumb1','积分管理');
  18. $this->assign('breadcrumb2','客户积分');
  19. }
  20. /**
  21. * 添加
  22. *
  23. * */
  24. public function incr(Request $request,Model $Model,Custom $Custom){
  25. // 接收参数
  26. $customUid = request('custom_uid',0);
  27. // 查询数据
  28. $custom = $Custom->getOne($customUid);
  29. // 提交
  30. if( request()->isMethod('post') ){
  31. // 验证参数
  32. $request->scene('incr')->validate();
  33. // 接收数据
  34. // 接收数据
  35. $incrScore = request('incr_score',0);
  36. // 组合数据,写入订单表,子表
  37. DB::beginTransaction();
  38. // 写入数据表
  39. $payId = $Model->trade($customUid,0,$incrScore,4,1);
  40. // 如果操作失败
  41. if( isset($payId['error']) ) {
  42. // 回滚
  43. DB::rollBack();
  44. // 结果提示
  45. return json_send(['code'=>'error','msg'=>$payId['error']]);
  46. }
  47. // 提交事务
  48. DB::commit();
  49. // 告知结果
  50. return json_send(['code'=>'success','msg'=>'增加成功','action'=>'add']);
  51. }
  52. // 如果不存在
  53. if( !$custom ) return $this->error('用户不存在');
  54. // 分配数据
  55. $this->assign('crumbs','增加积分');
  56. $this->assign('custom',$custom);
  57. // 加载模板
  58. return $this->fetch();
  59. }
  60. /**
  61. * 扣减
  62. *
  63. * */
  64. public function decr(Request $request,Model $Model,Custom $Custom){
  65. // 接收参数
  66. $customUid = request('custom_uid',0);
  67. // 查询数据
  68. $custom = $Custom->getOne($customUid);
  69. // 提交
  70. if( request()->isMethod('post') ){
  71. // 验证参数
  72. $request->scene('decr')->validate();
  73. // 接收数据
  74. $decrScore = request('decr_score',0);
  75. // 组合数据,写入订单表,子表
  76. DB::beginTransaction();
  77. // 写入数据表
  78. $payId = $Model->trade($customUid,0,($decrScore*-1),4,2);
  79. // 如果操作失败
  80. if( isset($payId['error']) ) {
  81. // 回滚
  82. DB::rollBack();
  83. // 结果提示
  84. return json_send(['code'=>'error','msg'=>$payId['error']]);
  85. }
  86. // 提交事务
  87. DB::commit();
  88. // 告知结果
  89. return json_send(['code'=>'success','msg'=>'扣减成功','action'=>'add']);
  90. }
  91. // 如果不存在
  92. if( !$custom ) return $this->error('用户不存在');
  93. // 分配数据
  94. $this->assign('crumbs','扣减积分');
  95. $this->assign('custom',$custom);
  96. // 加载模板
  97. return $this->fetch();
  98. }
  99. /**
  100. * 表格导入
  101. *
  102. * */
  103. public function import_execl( Request $request,Model $Model,Custom $Custom,FilesManager $FilesManager){
  104. // 验证参数
  105. $request->scene('import_execl')->validate();
  106. // 获取表格信息
  107. $file = request()->file('score_file');
  108. // 返回结果
  109. $sheetList = $FilesManager->excelToScore($file);
  110. // 如果不存在结果
  111. if( isset($sheetList['error']) ) return json_send(['code'=>'error','msg'=>$sheetList['error']]);
  112. // 组合数据,写入订单表,子表
  113. DB::beginTransaction();
  114. // 循环表格数据
  115. foreach ($sheetList as $value) {
  116. // 客户ID
  117. $customUid = $Custom->codeToId($value['custom_uid']);
  118. // 如果客户ID有误
  119. if( !$customUid ) return json_send(['code'=>'error','msg'=>$value['custom_uid'].'编码有误']);
  120. // 写入数据表
  121. $payId = $Model->trade($customUid,0,$value['score'],4,($value['score']>0?1:2),$value['description']);
  122. // 如果操作失败
  123. if( isset($payId['error']) ) {
  124. // 回滚
  125. DB::rollBack();
  126. // 结果提示
  127. return json_send(['code'=>'error','msg'=>$value['custom_uid'].'=>'.$payId['error']]);
  128. }
  129. }
  130. // 提交事务
  131. DB::commit();
  132. // 提示成功
  133. return json_send(['code'=>'success','msg'=>'批量导入成功','path'=>'']);
  134. }
  135. /**
  136. * 表格导入
  137. *
  138. * */
  139. public function import_weiban( Request $request,Model $Model,Custom $Custom,FilesManager $FilesManager,WeiBanExternal $WeiBanExternal){
  140. // 验证参数
  141. $request->scene('import_execl')->validate();
  142. // 获取表格信息
  143. $file = request()->file('score_file');
  144. // 返回结果
  145. $sheetList = $FilesManager->weibanToScore($file);
  146. // 如果不存在结果
  147. if( isset($sheetList['error']) ) return json_send(['code'=>'error','msg'=>$sheetList['error']]);
  148. // 通过微伴ID查询用户ID
  149. $uidList = $WeiBanExternal->query()->whereIn('id',array_column($sheetList,'weiban_extid'))->pluck('custom_uid','id')->toArray();
  150. // 循环列表
  151. foreach ($sheetList as $key=>$value) {
  152. // 如果查询不到数据
  153. if( empty($uidList[$value['weiban_extid']]) ) return json_send(['code'=>'error','msg'=>$value['weiban_extid'].'不在系统内']);
  154. // 获取客户ID
  155. $value['custom_uid'] = $uidList[$value['weiban_extid']];
  156. // 重组
  157. $sheetList[$key] = $value;
  158. }
  159. // 组合数据,写入订单表,子表
  160. DB::beginTransaction();
  161. // 循环表格数据
  162. foreach ($sheetList as $value) {
  163. // 写入数据表
  164. $payId = $Model->trade($value['custom_uid'],0,$value['score'],4,($value['score']>0?1:2),$value['description']);
  165. // 如果操作失败
  166. if( isset($payId['error']) ) {
  167. // 回滚
  168. DB::rollBack();
  169. // 结果提示
  170. return json_send(['code'=>'error','msg'=>$value['weiban_extid'].'=>'.$payId['error']]);
  171. }
  172. }
  173. // 提交事务
  174. DB::commit();
  175. // 提示成功
  176. return json_send(['code'=>'success','msg'=>'批量导入成功','path'=>'']);
  177. }
  178. }