BenefitController.php 13 KB

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