WriteOffController.php 11 KB

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