WriteOffController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\controller\Curd;
  4. use app\model\MemberBenefit;
  5. use app\model\RuleAddedComponent;
  6. use app\model\SysUser;
  7. use support\Db;
  8. use support\Request;
  9. class WriteOffController extends Curd
  10. {
  11. public function __construct()
  12. {
  13. // $this->model = new ;
  14. }
  15. public function list(Request $request)
  16. {
  17. $page = $request->get('page', 1);
  18. $pageSize = $request->get('pageSize', 50);
  19. $keywords = $request->get('keywords','');
  20. $userId = $request->get('user_id','');
  21. $premises = $request->get('premises','');
  22. $date = $request->get('date',[]);
  23. $appointmentList = Db::table('appointment')
  24. ->leftJoin('member','member.member_id' ,'=','appointment.join_appointment_member_id')
  25. ->leftJoin('member_cert','member_cert.join_cert_member_id' ,'=','appointment.join_appointment_member_id')
  26. ->select('appointment.appointment_id as quota_id', 'appointment.join_appointment_member_id as member_id','appointment.appointment_classify as classify', 'appointment.appointment_done_json as used_json', 'appointment.appointment_done_datetime as used_time','appointment.join_appointment_member_benefit_id as benefit_id',
  27. 'member.member_mobile','member_cert.member_cert_name'
  28. )
  29. ->when($keywords != '',function($query) use ($keywords){
  30. $query->where('member.member_mobile','like','%'.$keywords.'%');
  31. })->when(!empty($date),function($query) use ($date){
  32. $date[0] = date('Y-m-d',strtotime($date[0])).' 00:00:00';
  33. $date[1] = date('Y-m-d',strtotime($date[1])). ' 23:59:59';
  34. $query->whereBetween('appointment.appointment_done_datetime',$date);
  35. })
  36. ->when($userId != '',function($query) use ($userId){
  37. $query->whereJsonContains('appointment.appointment_done_json->charge->charge_user_id',$userId);
  38. })
  39. ->when($premises != '',function($query) use ($premises){
  40. $query->whereJsonContains('appointment.appointment_done_json->charge->charge_premises',$premises);
  41. })
  42. ->where('appointment_status', 'DONE');
  43. $quotaList = Db::table('member_quota')
  44. ->leftJoin('member','member.member_id' ,'=','member_quota.join_quota_member_id')
  45. ->leftJoin('member_cert','member_cert.join_cert_member_id' ,'=','member_quota.join_quota_member_id')
  46. ->select('member_quota.member_quota_id as quota_id', 'member_quota.join_quota_member_id as member_id','member_quota.member_quota_category as classify', 'member_quota.member_quota_used_json as used_json', "member_quota.member_quota_extend_json->writeOffTime as used_time","member_quota.join_member_rule_added_component_id as benefit_id",
  47. 'member.member_mobile','member_cert.member_cert_name')
  48. ->when($keywords != '',function($query) use ($keywords){
  49. $query->where('member.member_mobile','like','%'.$keywords.'%');
  50. })->when(!empty($date),function($query) use ($date){
  51. $date[0] = date('Y-m-d',strtotime($date[0])).' 00:00:00';
  52. $date[1] = date('Y-m-d',strtotime($date[1])). ' 23:59:59';
  53. $query->whereBetween('member_quota.member_quota_extend_json->writeOffTime',$date);
  54. })
  55. ->when($userId != '',function($query) use ($userId){
  56. $query->whereJsonContains('member_quota.member_quota_used_json->charge->charge_user_id',$userId);
  57. })
  58. ->when($premises != '',function($query) use ($premises){
  59. $query->whereJsonContains('member_quota.member_quota_used_json->charge->charge_premises',$premises);
  60. })
  61. ->where('member_quota_status', 'USED')
  62. // ->toSql();
  63. ->union($appointmentList);
  64. $total = $quotaList->count('*');
  65. $quotaList = $quotaList->orderBy('used_time', 'DESC')
  66. ->forPage($page, $pageSize)
  67. ->get();
  68. $rows = [];
  69. foreach ($quotaList as $item) {
  70. $username = '';
  71. $premises = '';
  72. $benefitName = '';
  73. if (!empty($item->used_json)) {
  74. $usedJson = json_decode($item->used_json,true);
  75. if (isset($usedJson['charge']) && isset($usedJson['charge']['charge_user_id'])){
  76. $username = SysUser::where('user_id',$usedJson['charge']['charge_user_id'])->value('user_name');
  77. }
  78. if (isset($usedJson['charge']) && isset($usedJson['charge']['charge_premises'])){
  79. $premises = $usedJson['charge']['charge_premises'];
  80. }
  81. if (substr($item->quota_id,0,2) == 'AP'){
  82. $benefitName = MemberBenefit::where('member_benefit_id',$item->benefit_id)->value('member_benefit_name');
  83. }elseif (substr($item->quota_id,0,2) == 'MQ'){
  84. $benefitName = RuleAddedComponent::where('rule_added_component_id',$item->benefit_i)->value('rule_added_component_name');
  85. }
  86. }
  87. $classify = "SERVICE";
  88. if(substr($item->quota_id,0,2) == "MQ"){
  89. $classify = "QUOTA";
  90. }elseif(!empty($item->classify) && $item->classify == 'PACKAGE'){
  91. $classify = "PACKAGE";
  92. }
  93. $rows[] = [
  94. 'quota_id' => $item->quota_id,
  95. 'username' => $username,
  96. 'premises' => $premises,
  97. 'benefit_name' => $benefitName,
  98. 'used_time'=>$item->used_time,
  99. 'member_name' => $item->member_cert_name ? $item->member_cert_name.'-' : '',
  100. 'member_mobile' => $item->member_mobile ? $item->member_mobile : '',
  101. 'classify'=>$classify,
  102. 'nbr'=>1
  103. ];
  104. }
  105. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  106. }
  107. }