BenefitController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. try {
  128. Db::beginTransaction();
  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. return json_success('核销成功');
  156. } catch (\Exception $e) {
  157. dump($e->getMessage());
  158. Db::rollBack();
  159. return json_fail('核销失败');
  160. }
  161. }
  162. public static function doWriteOff($paramsData)
  163. {
  164. $benefit = MemberBenefit::where('member_benefit_id', $paramsData['benefit_id'])
  165. ->where('join_benefit_member_id', $paramsData['member_id'])
  166. ->first();
  167. if ($benefit->member_benefit_limit_count - $benefit->member_benefit_used_count < $paramsData['nbr']) {
  168. throw new BusinessException('可核销数量不足');
  169. // return json_fail('可核销数量不足');
  170. }
  171. if (!$paramsData['times']) {
  172. $times = date('Y-m-d H:i:s');
  173. } else {
  174. $times = date('Y-m-d H:i:s', strtotime($paramsData['times']));
  175. }
  176. try {
  177. Db::beginTransaction();
  178. // 减额度
  179. $benefit->member_benefit_used_count = $benefit->member_benefit_used_count + $paramsData['nbr'];
  180. if($benefit->member_benefit_used_count == $benefit->member_benefit_limit_count){
  181. $benefit->member_benefit_status = 'DONE';
  182. if(!empty($benefit->join_benefit_order_id)){
  183. Order::where('order_id',$benefit->join_benefit_order_id)->update(['order_is_complete'=>'Y','order_status_system'=>'DONE']);
  184. }
  185. }
  186. $benefit->save();
  187. // 记录核销
  188. $params = [
  189. 'dept_premises_id' => $paramsData['dept_premises_id'],
  190. 'order_remark'=>$paramsData['remark'],
  191. 'write_off_member_id' => $paramsData['write_off_member_id'],
  192. 'join_order_member_id' => $paramsData['member_id']
  193. ];
  194. $writeOffData = OrderService::generateWriteOffData($params);
  195. $appointments = Appointment::where('join_appointment_member_benefit_id', $paramsData['benefit_id'])
  196. ->where('appointment_status', 'INIT')
  197. ->limit($paramsData['nbr'])
  198. ->get();
  199. foreach ($appointments as $appointment) {
  200. Appointment::where('appointment_id', $appointment->appointment_id)->update([
  201. 'appointment_status' => 'DONE',
  202. 'appointment_datetime' => $times,
  203. 'appointment_apply_datetime' => $times,
  204. 'appointment_doing_datetime' => $times,
  205. 'appointment_done_datetime' => $times,
  206. 'appointment_done_json' => json_encode($writeOffData)
  207. ]);
  208. }
  209. Db::commit();
  210. return json_success('核销成功');
  211. } catch (\Exception $e) {
  212. dump($e->getFile().'---'.$e->getLine());
  213. Db::rollBack();
  214. throw new BusinessException('核销失败');
  215. }
  216. }
  217. public function writeOffList(Request $request)
  218. {
  219. $page = $request->get('page', 1);
  220. $pageSize = $request->get('pageSize', 20);
  221. $memberId = $request->get('member_id', '');
  222. $benefitId = $request->get('benefit_id', '');
  223. $quotas = Appointment::with([
  224. 'member' => function ($query) {
  225. $query->select('member_id', 'member_mobile');
  226. },
  227. 'cert' => function ($query) {
  228. $query->select('join_cert_member_id', 'member_cert_name');
  229. },
  230. // 'goods' => function ($query) {
  231. // $query->select('goods_id', 'goods_name');
  232. // },
  233. 'benefit' => function ($query) {
  234. $query->select('member_benefit_id', 'member_benefit_name');
  235. }
  236. ])->where('join_appointment_member_id', $memberId)
  237. ->where('join_appointment_member_benefit_id', $benefitId)
  238. ->where('appointment_status', 'DONE');
  239. $total = $quotas->count('*');
  240. $rows = $quotas->orderBy('appointment_done_datetime','DESC')
  241. ->forPage($page, $pageSize)
  242. ->get()
  243. ->toArray();
  244. foreach ($rows as &$quota) {
  245. $quota['premises'] = '';
  246. $quota['username'] = '';
  247. if (!empty($quota['appointment_done_json'])) {
  248. $doneJson = json_decode($quota['appointment_done_json'], true);
  249. $quota['premises'] = $doneJson['charge']['charge_premises'] ?? '';
  250. if (isset($doneJson['charge']['charge_user_id'])) {
  251. $user = SysUser::where('user_id', $doneJson['charge']['charge_user_id'])
  252. ->select('user_id', 'user_name')
  253. ->first();
  254. if ($user) {
  255. $quota['username'] = $user->user_name;
  256. }
  257. }
  258. }
  259. }
  260. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  261. // return json_success('', $quotas);
  262. }
  263. public function service(Request $request)
  264. {
  265. $page = $request->get('page', 1);
  266. $pageSize = $request->get('pageSize', 20);
  267. $memberId = $request->get('member_id', '');
  268. $benefit = MemberBenefit::with([
  269. 'goods' => function ($query) {
  270. $query->select('goods_id', 'goods_name');
  271. },
  272. // 'order'
  273. ])
  274. ->where('join_benefit_member_id', $memberId)
  275. ->where('join_benefit_package_id', '');
  276. // ->where('member_benefit_category','SERVICE')
  277. $total = $benefit->count('*');
  278. $rows = $benefit->orderBy('member_benefit_addtimes','DESC')
  279. ->forPage($page, $pageSize)
  280. ->get()
  281. ->toArray();
  282. foreach ($benefit as &$item){
  283. $item['member_benefit_limit_count'] = intval($item['member_benefit_limit_count']);
  284. $item['member_benefit_used_count'] = intval($item['member_benefit_used_count']);
  285. }
  286. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  287. // return json_success('', $benefit);
  288. }
  289. }