BenefitController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\admin\service\order\OrderService;
  4. use app\controller\Curd;
  5. use app\model\Appointment;
  6. use app\model\Member;
  7. use app\model\MemberBenefit;
  8. use app\model\Order;
  9. use app\model\SysDept;
  10. use app\model\SysUser;
  11. use support\Db;
  12. use support\exception\BusinessException;
  13. use support\Redis;
  14. use support\Request;
  15. use Webman\Event\Event;
  16. class BenefitController extends Curd
  17. {
  18. public function __construct()
  19. {
  20. $this->model = new MemberBenefit();
  21. }
  22. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  23. {
  24. $model = $this->model->with('member', 'goods');
  25. foreach ($where as $column => $value) {
  26. if (is_array($value)) {
  27. if ($value[0] === 'like' || $value[0] === 'not like') {
  28. $model = $model->where($column, $value[0], "%$value[1]%");
  29. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  30. $model = $model->where($column, $value[0], $value[1]);
  31. } elseif ($value[0] == 'in' && !empty($value[1])) {
  32. $valArr = $value[1];
  33. if (is_string($value[1])) {
  34. $valArr = explode(",", trim($value[1]));
  35. }
  36. $model = $model->whereIn($column, $valArr);
  37. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  38. $valArr = $value[1];
  39. if (is_string($value[1])) {
  40. $valArr = explode(",", trim($value[1]));
  41. }
  42. $model = $model->whereNotIn($column, $valArr);
  43. } elseif ($value[0] == 'null') {
  44. $model = $model->whereNull($column);
  45. } elseif ($value[0] == 'not null') {
  46. $model = $model->whereNotNull($column);
  47. } elseif ($value[0] !== '' || $value[1] !== '') {
  48. $model = $model->whereBetween($column, $value);
  49. }
  50. } else {
  51. $model = $model->where($column, $value);
  52. }
  53. }
  54. if ($field) {
  55. $model = $model->orderBy($field, $order);
  56. }
  57. return $model;
  58. }
  59. public function my(Request $request)
  60. {
  61. $page = $request->get('page', 1);
  62. $pageSize = $request->get('pageSize', 20);
  63. $id = $request->get('member_id','');
  64. $benefit = MemberBenefit::with([
  65. 'package' => function ($query) {
  66. $query->select('goods_id', 'goods_name');
  67. },
  68. 'goods' => function ($query) {
  69. $query->select('goods_id', 'goods_name');
  70. },
  71. 'order'
  72. ])
  73. ->where('join_benefit_member_id', $id)
  74. ->where('join_benefit_package_id', '<>', '');
  75. $total = $benefit->count('*');
  76. $rows = $benefit->orderBy('member_benefit_addtimes','DESC')
  77. ->forPage($page, $pageSize)
  78. ->get()
  79. ->toArray();
  80. foreach ($rows as &$item){
  81. $item['member_benefit_limit_count'] = intval($item['member_benefit_limit_count']);
  82. $item['member_benefit_used_count'] = intval($item['member_benefit_used_count']);
  83. }
  84. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  85. // return json_success('', $benefit);
  86. }
  87. protected function updateInput(Request $request): array
  88. {
  89. $primary_key = $this->model->getKeyName();
  90. $id = $request->post($primary_key);
  91. $data = $this->inputFilter($request->post());
  92. $model = $this->model->find($id);
  93. if (!$model) {
  94. throw new BusinessException('记录不存在', 2);
  95. }
  96. $data['member_benefit_valid_datetimes'] = date('Y-m-d H:i:s', strtotime($data['member_benefit_valid_datetimes']));
  97. unset($data[$primary_key]);
  98. return [$id, $data];
  99. }
  100. public function writeOff(Request $request)
  101. {
  102. $memberId = $request->post('member_id', '');
  103. $benefitId = $request->post('benefit_id', '');
  104. $nbr = $request->post('nbr');
  105. $times = $request->post('times', '');
  106. $code = $request->post('sms_code', '');
  107. if (!$memberId || !$benefitId) {
  108. return json_fail('参数异常');
  109. }
  110. $member = Member::find($memberId);
  111. $mobile = $member->member_mobile;
  112. $key = "SMS:CODE:QUOTA:" . $mobile;
  113. $redisCode = Redis::get($key);
  114. if ($redisCode != $code) {
  115. return json_fail("验证码错误,请重新输入");
  116. }
  117. $benefit = MemberBenefit::where('member_benefit_id', $benefitId)
  118. ->where('join_benefit_member_id', $memberId)
  119. ->first();
  120. if ($benefit->member_benefit_limit_count - $benefit->member_benefit_used_count < $nbr) {
  121. return json_fail('可核销数量不足');
  122. }
  123. if (!$times) {
  124. $times = date('Y-m-d H:i:s');
  125. } else {
  126. $times = date('Y-m-d H:i:s', strtotime($times));
  127. }
  128. Db::beginTransaction();
  129. try {
  130. // 减额度
  131. $benefitUsed = [];
  132. $appointmentIds = [];
  133. $benefit->member_benefit_used_count = $benefit->member_benefit_used_count + $nbr;
  134. if($benefit->member_benefit_used_count >= $benefit->member_benefit_limit_count){
  135. $benefit->member_benefit_status = 'DONE';
  136. if(!empty($benefit->join_benefit_order_id)){
  137. $benefitUsed[$benefit->join_benefit_order_id][] = $benefitId;
  138. // Order::where('order_id',$benefit->join_benefit_order_id)->update(['order_is_complete'=>'Y','order_status_system'=>'DONE']);
  139. }
  140. }
  141. $benefit->save();
  142. // 记录核销
  143. $params = [
  144. 'dept_premises_id' => $request->post('dept_premises_id',''),
  145. 'order_remark'=>$request->post('remark',''),
  146. 'write_off_member_id' => $request->post('write_off_member_id',''),
  147. 'join_order_member_id' => $memberId,
  148. 'charge_waiter' => $request->post('charge_waiter'),
  149. ];
  150. $writeOffData = OrderService::generateWriteOffData($params);
  151. $appointments = Appointment::where('join_appointment_member_benefit_id', $benefitId)
  152. ->whereIn('appointment_status', ['INIT','WAITING','PENDING','DOING'])
  153. ->limit($nbr)
  154. ->get();
  155. foreach ($appointments as $appointment) {
  156. Appointment::where('appointment_id', $appointment->appointment_id)->update([
  157. 'appointment_status' => 'DONE',
  158. 'appointment_datetime' => $times,
  159. 'appointment_apply_datetime' => $times,
  160. 'appointment_doing_datetime' => $times,
  161. 'appointment_done_datetime' => $times,
  162. 'appointment_done_json' => json_encode($writeOffData)
  163. ]);
  164. }
  165. // 订单服务是否用完了
  166. foreach ($benefitUsed as $key => $item) {
  167. $orderUsedComplete = 'Y';
  168. $orderBenefits = MemberBenefit::where('join_benefit_order_id', $key)->get()->toArray();
  169. foreach ($orderBenefits as $orderBenefit) {
  170. if ($orderBenefit['member_benefit_used_count'] >= $orderBenefit['member_benefit_limit_count']) {
  171. continue;
  172. }
  173. if (!in_array($orderBenefit['member_benefit_id'], $item)) {
  174. $orderUsedComplete = 'N';
  175. }
  176. }
  177. if ($orderUsedComplete == 'Y') {
  178. Order::where('order_id', $key)->update(['order_is_complete' => 'Y', 'order_status_system' => 'DONE']);
  179. }
  180. }
  181. // 核销服务回写process
  182. $dept = [];
  183. if (!empty($request->post('dept_premises_id'))) {
  184. $dept = SysDept::where('dept_id', $request->post('dept_premises_id'))
  185. ->orWhere('dept_name', $request->post('dept_premises_id'))
  186. ->select('dept_id', 'dept_name', 'dept_status', 'dept_category', 'dept_super_id', 'dept_super_path')
  187. ->first();
  188. }
  189. $params['charge_amount'] = $nbr;
  190. $params['goods_id'] = !empty($benefit->join_benefit_goods_id) ? $benefit->join_benefit_goods_id : (!empty($benefit->join_benefit_package_id) ? $benefit->join_benefit_package_id : '');
  191. $params['goods_sku_id'] = $benefit->join_benefit_goods_sku_id;
  192. $params['order_id'] = $benefit->join_benefit_order_id;
  193. $params['appointment_ids'] = $appointmentIds;
  194. $params['dept'] = $dept;
  195. $params['write_off_data'] = OrderService::generateWriteOffDataByOrderProcess($params);
  196. // 触发事件
  197. Event::dispatch('order.write_off', $params);
  198. Redis::del($key);
  199. Db::commit();
  200. _syslog("核销","核销成功");
  201. return json_success('核销成功');
  202. } catch (\Exception $e) {
  203. dump($e->getMessage().$e->getLine());
  204. Db::rollBack();
  205. _syslog("核销","核销失败");
  206. return json_fail('核销失败');
  207. }
  208. }
  209. public static function doWriteOff($paramsData,$appointmentNofee)
  210. {
  211. $benefit = MemberBenefit::where('member_benefit_id', $paramsData['benefit_id'])
  212. ->where('join_benefit_member_id', $paramsData['member_id'])
  213. ->first();
  214. if ($benefit->member_benefit_limit_count - $benefit->member_benefit_used_count < $paramsData['nbr']) {
  215. throw new BusinessException('可核销数量不足');
  216. // return json_fail('可核销数量不足');
  217. }
  218. if (!$paramsData['times']) {
  219. $times = date('Y-m-d H:i:s');
  220. } else {
  221. $times = date('Y-m-d H:i:s', strtotime($paramsData['times']));
  222. }
  223. Db::beginTransaction();
  224. try {
  225. // 减额度
  226. $benefit->member_benefit_used_count = $benefit->member_benefit_used_count + $paramsData['nbr'];
  227. if($benefit->member_benefit_used_count == $benefit->member_benefit_limit_count){
  228. $benefit->member_benefit_status = 'DONE';
  229. if(!empty($benefit->join_benefit_order_id)){
  230. Order::where('order_id',$benefit->join_benefit_order_id)->update(['order_is_complete'=>'Y','order_status_system'=>'DONE']);
  231. }
  232. }
  233. $benefit->save();
  234. // 记录核销
  235. $params = [
  236. 'dept_premises_id' => $paramsData['dept_premises_id'],
  237. 'order_remark'=>$paramsData['remark'],
  238. 'write_off_member_id' => $paramsData['write_off_member_id'],
  239. 'join_order_member_id' => $paramsData['member_id']
  240. ];
  241. $writeOffData = OrderService::generateWriteOffData($params);
  242. $appointments = Appointment::where('join_appointment_member_benefit_id', $paramsData['benefit_id'])
  243. ->whereIn('appointment_status', ['INIT','WAITING','PENDING','DOING'])
  244. ->limit($paramsData['nbr'])
  245. ->get();
  246. foreach ($appointments as $appointment) {
  247. Appointment::where('appointment_id', $appointment->appointment_id)->update([
  248. 'appointment_status' => 'DONE',
  249. 'appointment_datetime' => $times,
  250. 'appointment_apply_datetime' => $times,
  251. 'appointment_doing_datetime' => $times,
  252. 'appointment_done_datetime' => $times,
  253. 'appointment_done_json' => json_encode($writeOffData),
  254. 'appointment_apply_json'=>$appointmentNofee->appointment_apply_json,
  255. 'appointment_process_json'=>$appointmentNofee->appointment_process_json,
  256. 'settlement_mode'=>$appointmentNofee->settlement_mode
  257. ]);
  258. }
  259. Db::commit();
  260. _syslog("核销","核销成功");
  261. return json_success('核销成功');
  262. } catch (\Exception $e) {
  263. dump($e->getFile().'---'.$e->getLine());
  264. Db::rollBack();
  265. _syslog("核销","核销失败");
  266. throw new BusinessException('核销失败');
  267. }
  268. }
  269. public function writeOffList(Request $request)
  270. {
  271. $page = $request->get('page', 1);
  272. $pageSize = $request->get('pageSize', 20);
  273. $memberId = $request->get('member_id', '');
  274. $benefitId = $request->get('benefit_id', '');
  275. $quotas = Appointment::with([
  276. 'member' => function ($query) {
  277. $query->select('member_id', 'member_mobile');
  278. },
  279. 'cert' => function ($query) {
  280. $query->select('join_cert_member_id', 'member_cert_name');
  281. },
  282. // 'goods' => function ($query) {
  283. // $query->select('goods_id', 'goods_name');
  284. // },
  285. 'benefit' => function ($query) {
  286. $query->select('member_benefit_id', 'member_benefit_name');
  287. }
  288. ])->where('join_appointment_member_id', $memberId)
  289. ->where('join_appointment_member_benefit_id', $benefitId)
  290. ->where('appointment_status', 'DONE');
  291. $total = $quotas->count('*');
  292. $rows = $quotas->orderBy('appointment_done_datetime','DESC')
  293. ->forPage($page, $pageSize)
  294. ->get()
  295. ->toArray();
  296. foreach ($rows as &$quota) {
  297. $quota['premises'] = '';
  298. $quota['username'] = '';
  299. if (!empty($quota['appointment_done_json'])) {
  300. $doneJson = json_decode($quota['appointment_done_json'], true);
  301. $quota['premises'] = $doneJson['charge']['charge_premises'] ?? '';
  302. $quota['remark'] = $doneJson['charge']['charge_content'] ?? '';
  303. $quota['charge_waiter'] = $doneJson['charge']['charge_waiter'] ?? '';
  304. if (isset($doneJson['charge']['charge_user_id'])) {
  305. $user = SysUser::where('user_id', $doneJson['charge']['charge_user_id'])
  306. ->select('user_id', 'user_name')
  307. ->first();
  308. if ($user) {
  309. $quota['username'] = $user->user_name;
  310. }
  311. }
  312. }
  313. if (!empty($quota['member']) && !empty($quota['member']['member_mobile'])) {
  314. $quota['member']['member_mobile'] = substr_replace($quota['member']['member_mobile'], '****', 3, 4);
  315. }
  316. }
  317. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  318. // return json_success('', $quotas);
  319. }
  320. public function service(Request $request)
  321. {
  322. $page = $request->get('page', 1);
  323. $pageSize = $request->get('pageSize', 20);
  324. $memberId = $request->get('member_id', '');
  325. $benefit = MemberBenefit::with([
  326. 'goods' => function ($query) {
  327. $query->select('goods_id', 'goods_name');
  328. },
  329. // 'order'
  330. ])
  331. ->where('join_benefit_member_id', $memberId)
  332. ->where('join_benefit_package_id', '');
  333. // ->where('member_benefit_category','SERVICE')
  334. $total = $benefit->count('*');
  335. $rows = $benefit->orderBy('member_benefit_addtimes','DESC')
  336. ->forPage($page, $pageSize)
  337. ->get()
  338. ->toArray();
  339. foreach ($rows as &$item){
  340. $item['member_benefit_limit_count'] = intval($item['member_benefit_limit_count']);
  341. $item['member_benefit_used_count'] = intval($item['member_benefit_used_count']);
  342. }
  343. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  344. // return json_success('', $benefit);
  345. }
  346. }