WriteOffController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\controller\Curd;
  4. use app\model\Appointment;
  5. use app\model\MemberBenefit;
  6. use app\model\MemberQuota;
  7. use app\model\RuleAddedComponent;
  8. use app\model\SysUser;
  9. use support\Db;
  10. use support\Request;
  11. class WriteOffController extends Curd
  12. {
  13. public function __construct()
  14. {
  15. // $this->model = new ;
  16. }
  17. public function list(Request $request)
  18. {
  19. $page = $request->get('page', 1);
  20. $pageSize = $request->get('pageSize', 50);
  21. $keywords = $request->get('keywords', '');
  22. $userId = $request->get('user_id', '');
  23. $premises = $request->get('premises', '');
  24. $date = $request->get('date', []);
  25. $memberId = $request->get('member_id','');
  26. $appointmentList = Db::table('appointment')
  27. ->leftJoin('member', 'member.member_id', '=', 'appointment.join_appointment_member_id')
  28. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', '=', 'appointment.join_appointment_member_id')
  29. ->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',
  30. 'member.member_mobile', 'member_cert.member_cert_name','appointment_extend_json as extend_json'
  31. )
  32. ->when($keywords != '', function ($query) use ($keywords) {
  33. $query->where('member.member_mobile', 'like', '%' . $keywords . '%');
  34. })->when(!empty($date), function ($query) use ($date) {
  35. $date[0] = date('Y-m-d', strtotime($date[0])) . ' 00:00:00';
  36. $date[1] = date('Y-m-d', strtotime($date[1])) . ' 23:59:59';
  37. $query->whereBetween('appointment.appointment_done_datetime', $date);
  38. })->when($userId != '', function ($query) use ($userId) {
  39. $query->whereJsonContains('appointment.appointment_done_json->charge->charge_user_id', $userId);
  40. })->when($premises != '', function ($query) use ($premises) {
  41. $query->whereJsonContains('appointment.appointment_done_json->charge->charge_premises', $premises);
  42. })->when(!empty($memberId),function ($query) use ($memberId){
  43. $query->where('join_appointment_member_id',$memberId);
  44. })
  45. ->where('appointment_status', 'DONE');
  46. $quotaList = Db::table('member_quota')
  47. ->leftJoin('member', 'member.member_id', '=', 'member_quota.join_quota_member_id')
  48. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', '=', 'member_quota.join_quota_member_id')
  49. ->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",
  50. 'member.member_mobile', 'member_cert.member_cert_name','member_quota_extend_json as extend_json')
  51. ->when($keywords != '', function ($query) use ($keywords) {
  52. $query->where('member.member_mobile', 'like', '%' . $keywords . '%');
  53. })->when(!empty($date), function ($query) use ($date) {
  54. $date[0] = date('Y-m-d', strtotime($date[0])) . ' 00:00:00';
  55. $date[1] = date('Y-m-d', strtotime($date[1])) . ' 23:59:59';
  56. $query->whereBetween('member_quota.member_quota_extend_json->writeOffTime', $date);
  57. })
  58. ->when($userId != '', function ($query) use ($userId) {
  59. $query->whereJsonContains('member_quota.member_quota_used_json->charge->charge_user_id', $userId);
  60. })
  61. ->when($premises != '', function ($query) use ($premises) {
  62. $query->whereJsonContains('member_quota.member_quota_used_json->charge->charge_premises', $premises);
  63. })
  64. ->where('member_quota_status', 'USED')
  65. ->union($appointmentList);
  66. $total = $quotaList->count('*');
  67. $quotaList = $quotaList->orderBy('used_time', 'DESC')
  68. ->forPage($page, $pageSize)
  69. ->get();
  70. $rows = [];
  71. foreach ($quotaList as $item) {
  72. $username = '';
  73. $premises = '';
  74. $benefitName = '';
  75. if (!empty($item->used_json)) {
  76. $usedJson = json_decode($item->used_json, true);
  77. if (isset($usedJson['charge']) && isset($usedJson['charge']['charge_user_id'])) {
  78. $username = SysUser::where('user_id', $usedJson['charge']['charge_user_id'])->value('user_name');
  79. }
  80. if (isset($usedJson['charge']) && isset($usedJson['charge']['charge_premises'])) {
  81. $premises = $usedJson['charge']['charge_premises'];
  82. }
  83. if (substr($item->quota_id, 0, 2) == 'AP') {
  84. $benefitName = MemberBenefit::where('member_benefit_id', $item->benefit_id)->value('member_benefit_name');
  85. } elseif (substr($item->quota_id, 0, 2) == 'MQ') {
  86. $benefitName = RuleAddedComponent::where('rule_added_component_id', $item->benefit_id)->value('rule_added_component_name');
  87. }
  88. }
  89. $remark = [];
  90. if (!empty($item->extend_json)){
  91. $extendJson = json_decode($item->extend_json,true);
  92. if (isset($extendJson['remark'])){
  93. $remark = $extendJson['remark'];
  94. }
  95. }
  96. $classify = "SERVICE";
  97. if (substr($item->quota_id, 0, 2) == "MQ") {
  98. $classify = "QUOTA";
  99. } elseif (!empty($item->classify) && $item->classify == 'PACKAGE') {
  100. $classify = "PACKAGE";
  101. }
  102. $rows[] = [
  103. 'quota_id' => $item->quota_id,
  104. 'username' => $username,
  105. 'premises' => $premises,
  106. 'benefit_name' => $benefitName,
  107. 'used_time' => $item->used_time,
  108. 'member_name' => $item->member_cert_name ? $item->member_cert_name : '',
  109. 'member_mobile' => $item->member_mobile ? $item->member_mobile : '',
  110. 'classify' => $classify,
  111. 'nbr' => 1,
  112. 'remark'=>$remark
  113. ];
  114. }
  115. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  116. }
  117. /**
  118. * @Desc 核销备注
  119. * @Author Gorden
  120. * @Date 2024/9/4 17:05
  121. *
  122. * @param Request $request
  123. * @return \support\Response
  124. */
  125. public function remark(Request $request)
  126. {
  127. $quotaId = $request->post('quota_id', '');
  128. if (!$quotaId) {
  129. return json_fail('参数异常');
  130. }
  131. $extendJson = [];
  132. if (substr($quotaId, 0, 2) == 'AP') {
  133. $appointment = Appointment::where('appointment_id', $quotaId)->first();
  134. if (!empty($appointment->appointment_extend_json)) {
  135. $extendJson = json_decode($appointment->appointment_extend_json, true);
  136. }
  137. } elseif (substr($quotaId, 0, 2) == 'MQ') {
  138. $quota = MemberQuota::where('member_quota_id', $quotaId)->first();
  139. if (!empty($quota->member_quota_extend_json)) {
  140. $extendJson = json_decode($quota->member_quota_extend_json, true);
  141. }
  142. }
  143. if (empty($appointment) && empty($quota)) {
  144. return json_fail("数据异常");
  145. }
  146. try {
  147. $extendJson['remark'] = [
  148. 'premises_id' => $request->post('premises_id', ''),
  149. 'premises_name' => $request->post('premises_name', ''),
  150. 'business' => $request->post('business', ''),
  151. 'service' => $request->post('service', '')
  152. ];
  153. if (!empty($appointment)){
  154. $appointment->appointment_extend_json = json_encode($extendJson,JSON_UNESCAPED_UNICODE);
  155. $appointment->save();
  156. }elseif (!empty($quota)){
  157. $quota->member_quota_extend_json = json_encode($extendJson,JSON_UNESCAPED_UNICODE);
  158. $quota->save();
  159. }
  160. return json_success('success');
  161. } catch (\Exception $e) {
  162. return json_fail('备注保存失败');
  163. }
  164. }
  165. }