CustomScore.php 6.0 KB

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