QuotaController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\admin\service\added\AddedService;
  4. use app\admin\validate\member\RuleAddedValidate;
  5. use app\admin\validate\member\RulePricingValidate;
  6. use app\controller\Curd;
  7. use app\model\Member;
  8. use app\model\MemberQuota;
  9. use app\model\RuleAdded;
  10. use app\model\RuleAddedComponent;
  11. use app\model\RulePricing;
  12. use app\model\SysSerial;
  13. use app\model\SysUser;
  14. use support\Db;
  15. use support\Redis;
  16. use support\exception\BusinessException;
  17. use support\Request;
  18. use support\Response;
  19. use support\view\Raw;
  20. class QuotaController extends Curd
  21. {
  22. public function __construct()
  23. {
  24. $this->model = new MemberQuota();
  25. // $this->validate = true;
  26. // $this->validateClass = new RuleAddedValidate();
  27. }
  28. public function select(Request $request): Response
  29. {
  30. $page = $request->get('page', 1);
  31. $pageSize = $request->get('pageSize', 20);
  32. $keywords = $request->get('keywords', '');
  33. $memberId = $request->get('member_id', '');
  34. $memberIds = [];
  35. if (!empty($keywords)) {
  36. $memberIds = Member::leftJoin('member_cert', 'member_cert.join_cert_member_id', '=', 'member.member_id')
  37. ->where('member.member_mobile', 'like', '%' . $keywords . '%')
  38. ->orWhere('member_cert.member_cert_name', 'like', '%' . $keywords . '%')
  39. ->pluck('member.member_id')
  40. ->toArray();
  41. if (empty($memberIds)) {
  42. return json_success('', []);
  43. }
  44. }
  45. if (!empty($memberId)) {
  46. $memberIds = $memberId;
  47. }
  48. foreach ($memberIds as &$id) {
  49. $id = "'" . $id . "'";
  50. }
  51. $rows = MemberQuota::select('join_quota_member_id', 'join_member_rule_added_component_id', Db::raw('MAX(`member_quota_addtimes`) as `new_addtimes`'))
  52. // ->when(!empty($memberIds), function ($query) use ($placeholders,$pars) {
  53. // $query->whereRaw("join_quota_member_id in ($placeholders))", $pars);
  54. // })
  55. ->when(!empty($memberIds), function ($query) use ($memberIds) {
  56. $query->whereIn('join_quota_member_id', $memberIds);
  57. })
  58. ->groupBy('join_member_rule_added_component_id', 'join_quota_member_id');
  59. $bindings = $rows->getBindings();
  60. $sql = str_replace('?', '%s', $rows->toSql());
  61. $sql = sprintf($sql, ...$bindings);
  62. $total = MemberQuota::from(Db::raw("({$sql}) t"))->count();
  63. $rows = MemberQuota::from(Db::raw("({$sql}) t"))
  64. ->orderBy('new_addtimes', 'desc')
  65. // ->orderByDesc('new_addtimes')
  66. ->forPage($page, $pageSize)
  67. ->get()
  68. ->toArray();
  69. foreach ($rows as &$row) {
  70. $quota = MemberQuota::with([
  71. 'member' => function ($query) {
  72. $query->select('member_id', 'member_mobile');
  73. },
  74. 'cert' => function ($query) {
  75. $query->select('join_cert_member_id', 'member_cert_name');
  76. },
  77. 'component' => function ($query) {
  78. $query->select('rule_added_component_id', 'rule_added_component_name');
  79. }
  80. ])->where('join_quota_member_id', $row['join_quota_member_id'])
  81. ->where('join_member_rule_added_component_id', $row['join_member_rule_added_component_id'])
  82. ->first()
  83. ->toArray();
  84. $quotaUsed = MemberQuota::where('join_quota_member_id', $row['join_quota_member_id'])
  85. ->where('join_member_rule_added_component_id', $row['join_member_rule_added_component_id'])
  86. ->where('member_quota_status', 'USED')
  87. ->count();
  88. $quota['used'] = $quotaUsed;
  89. if ($quota['member_quota_nbr'] == '-99.00'){
  90. $quota['total'] = '不限次';
  91. $quota['unused'] = '不限次';
  92. }else{
  93. $quotaTotal = MemberQuota::where('join_quota_member_id', $row['join_quota_member_id'])
  94. ->where('join_member_rule_added_component_id', $row['join_member_rule_added_component_id'])
  95. ->count();
  96. $quota['total'] = $quotaTotal;
  97. $quota['unused'] = $quotaTotal - $quotaUsed;
  98. }
  99. $addedId = RuleAddedComponent::where('rule_added_component_id', $row['join_member_rule_added_component_id'])->value('join_component_rule_added_id');
  100. $addedName = RuleAdded::where('rule_added_id', $addedId)->value('rule_added_name');
  101. $quota['added_name'] = $addedName;
  102. $row = $quota;
  103. }
  104. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  105. }
  106. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  107. {
  108. $model = $this->model->with([
  109. 'member' => function ($query) {
  110. $query->select('member_id', 'member_mobile');
  111. },
  112. 'cert' => function ($query) {
  113. $query->select('join_cert_member_id', 'member_cert_name');
  114. },
  115. 'component' => function ($query) {
  116. $query->select('rule_added_component_id', 'rule_added_component_name');
  117. }
  118. ]);
  119. $model = $model->groupBy('join_member_rule_added_component_id');
  120. foreach ($where as $column => $value) {
  121. if (is_array($value)) {
  122. if ($value[0] === 'like' || $value[0] === 'not like') {
  123. $model = $model->where($column, $value[0], "%$value[1]%");
  124. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  125. $model = $model->where($column, $value[0], $value[1]);
  126. } elseif ($value[0] == 'in' && !empty($value[1])) {
  127. $valArr = $value[1];
  128. if (is_string($value[1])) {
  129. $valArr = explode(",", trim($value[1]));
  130. }
  131. $model = $model->whereIn($column, $valArr);
  132. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  133. $valArr = $value[1];
  134. if (is_string($value[1])) {
  135. $valArr = explode(",", trim($value[1]));
  136. }
  137. $model = $model->whereNotIn($column, $valArr);
  138. } elseif ($value[0] == 'null') {
  139. $model = $model->whereNull($column);
  140. } elseif ($value[0] == 'not null') {
  141. $model = $model->whereNotNull($column);
  142. } elseif ($value[0] !== '' || $value[1] !== '') {
  143. $model = $model->whereBetween($column, $value);
  144. }
  145. } else {
  146. $model = $model->where($column, $value);
  147. }
  148. }
  149. if ($field) {
  150. $model = $model->orderBy($field, $order);
  151. }
  152. return $model;
  153. }
  154. // protected function insertInput(Request $request): array
  155. // {
  156. // $data = $this->inputFilter($request->post());
  157. // $data['rule_added_id'] = "RA" . str_pad(SysSerial::getSerial(), 16, '0', STR_PAD_LEFT) . random_string(6, 'up');
  158. //
  159. // return $data;
  160. // }
  161. protected function updateInput(Request $request): array
  162. {
  163. $primary_key = $this->model->getKeyName();
  164. $id = $request->post($primary_key);
  165. $data = $this->inputFilter($request->post());
  166. // if (!empty($data['rule_pricing_goods_json'])) {
  167. // $data['rule_pricing_goods_json'] = json_encode(explode(',', $data['rule_pricing_goods_json']));
  168. // } else {
  169. // $data['rule_pricing_goods_json'] = '[]';
  170. // }
  171. $model = $this->model->find($id);
  172. if (!$model) {
  173. throw new BusinessException('记录不存在', 2);
  174. }
  175. unset($data[$primary_key]);
  176. return [$id, $data];
  177. }
  178. public function writeOffList(Request $request)
  179. {
  180. $page = $request->get('page', 1);
  181. $pageSize = $request->get('pageSize', 20);
  182. $memberId = $request->get('member_id', '');
  183. $componentId = $request->get('component_id', '');
  184. $quotas = MemberQuota::with([
  185. 'member' => function ($query) {
  186. $query->select('member_id', 'member_mobile');
  187. },
  188. 'cert' => function ($query) {
  189. $query->select('join_cert_member_id', 'member_cert_name');
  190. },
  191. 'component' => function ($query) {
  192. $query->select('rule_added_component_id', 'rule_added_component_name');
  193. }
  194. ])->where('join_quota_member_id', $memberId)
  195. ->where('join_member_rule_added_component_id', $componentId)
  196. ->where('member_quota_status', 'USED');
  197. $total = $quotas->count('*');
  198. $rows = $quotas->orderBy('member_quota_used_json->charge->charge_write_off_time', 'desc')
  199. ->forPage($page, $pageSize)
  200. ->get()
  201. ->toArray();
  202. foreach ($rows as &$quota) {
  203. $quota['premises'] = '';
  204. $quota['username'] = '';
  205. if (!empty($quota['member_quota_used_json'])) {
  206. $usedJson = json_decode($quota['member_quota_used_json'], true);
  207. $quota['premises'] = $usedJson['charge']['charge_premises'] ?? '';
  208. if (isset($usedJson['charge']['charge_user_id'])) {
  209. $user = SysUser::where('user_id', $usedJson['charge']['charge_user_id'])
  210. ->select('user_id', 'user_name')
  211. ->first();
  212. if ($user) {
  213. $quota['username'] = $user->user_name;
  214. }
  215. }
  216. }
  217. }
  218. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  219. // return json_success('', $quotas);
  220. }
  221. /**
  222. * @Desc 额度核销
  223. * @Author Gorden
  224. * @Date 2024/6/4 8:32
  225. *
  226. * @param Request $request
  227. * @return Response
  228. */
  229. public function writeOff(Request $request)
  230. {
  231. $params = $request->post();
  232. $memberId = $params['member_id'] ?? [];
  233. $componentId = $params['component_id'] ?? [];
  234. $nbr = $params['nbr'];
  235. $times = $params['times'] ?? '';
  236. $code = $params['sms_code'] ?? '';
  237. if (!$memberId || !$componentId) {
  238. return json_fail("参数异常");
  239. }
  240. $quotas = MemberQuota::where('join_quota_member_id', $memberId[0])
  241. ->where('join_member_rule_added_component_id', $componentId[0])
  242. ->where('member_quota_status', 'PENDING')
  243. ->limit($nbr)
  244. ->get()
  245. ->toArray();
  246. if ($quotas[0]['member_quota_nbr'] != '-99.00' && count($quotas) < intval($nbr)) {
  247. return json_fail("可核销数量不足");
  248. }
  249. $member = Member::find(current($memberId));
  250. $mobile = $member->member_mobile;
  251. $key = "SMS:CODE:QUOTA:" . $mobile;
  252. $redisCode = Redis::get($key);
  253. if ($redisCode != $code) {
  254. return json_fail("验证码错误,请重新输入");
  255. }
  256. Redis::del($key);
  257. if (!$times) {
  258. $params['times'] = date('Y-m-d H:i:s');
  259. } else {
  260. $params['times'] = date('Y-m-d H:i:s', strtotime($times));
  261. }
  262. try {
  263. // 生成核销数据
  264. $writeOffData = AddedService::generateWriteOffData($params);
  265. if($quotas[0]['member_quota_nbr'] != '-99.00'){
  266. foreach ($quotas as $quota) {
  267. $writeOffData['member_quota_id'] = $quota['member_quota_id'];
  268. MemberQuota::where('member_quota_id', $quota['member_quota_id'])->update([
  269. 'member_quota_status' => 'USED',
  270. 'member_quota_used_json' => json_encode($writeOffData)
  271. ]);
  272. }
  273. }else{
  274. $quota = $quotas[0];
  275. for($i=0;$i<$nbr;$i++){
  276. $quota['member_quota_id'] = 'MQ'.date('YmdHis').random_string(6,'up');
  277. $quota['member_quota_status'] = 'USED';
  278. $quota['member_quota_used_json'] = json_encode($writeOffData);
  279. $quota['member_quota_addtimes'] = strtotime($quota['member_quota_addtimes']);
  280. $quota['member_quota_nbr'] = 1;
  281. MemberQuota::insert($quota);
  282. }
  283. }
  284. } catch (\Exception $e) {
  285. dump($e->getMessage());
  286. return json_fail("核销失败");
  287. }
  288. return json_success("核销成功");
  289. }
  290. public static function doWriteOff($params){
  291. $quotas = MemberQuota::where('join_quota_member_id', $params['member_id'])
  292. ->where('join_member_rule_added_component_id', $params['component_id'])
  293. ->where('member_quota_status', 'PENDING')
  294. ->limit($params['nbr'])
  295. ->get()
  296. ->toArray();
  297. if (count($quotas) < intval($params['nbr'])) {
  298. throw new BusinessException('可核销数量不足');
  299. }
  300. if (!$params['times']) {
  301. $params['times'] = date('Y-m-d H:i:s');
  302. } else {
  303. $params['times'] = date('Y-m-d H:i:s', strtotime($params['times']));
  304. }
  305. try {
  306. // 生成核销数据
  307. $writeOffData = AddedService::generateWriteOffData($params);
  308. foreach ($quotas as $quota) {
  309. $writeOffData['member_quota_id'] = $quota['member_quota_id'];
  310. MemberQuota::where('member_quota_id', $quota['member_quota_id'])->update([
  311. 'member_quota_status' => 'USED',
  312. 'member_quota_used_json' => json_encode($writeOffData)
  313. ]);
  314. }
  315. } catch (\Exception $e) {
  316. throw new BusinessException('核销失败');
  317. // return json_fail("核销失败");
  318. }
  319. return json_success("核销成功");
  320. }
  321. }