WriteOffController.php 8.3 KB

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