QuotaController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. $memberId = $request->get('member_id', '');
  181. $componentId = $request->get('component_id', '');
  182. $quotas = MemberQuota::with([
  183. 'member' => function ($query) {
  184. $query->select('member_id', 'member_mobile');
  185. },
  186. 'cert' => function ($query) {
  187. $query->select('join_cert_member_id', 'member_cert_name');
  188. },
  189. 'component' => function ($query) {
  190. $query->select('rule_added_component_id', 'rule_added_component_name');
  191. }
  192. ])->where('join_quota_member_id', $memberId)
  193. ->where('join_member_rule_added_component_id', $componentId)
  194. ->where('member_quota_status', 'USED')
  195. ->orderBy('member_quota_used_json->charge->charge_write_off_time', 'desc')
  196. ->get()
  197. ->toArray();
  198. foreach ($quotas as &$quota) {
  199. $quota['premises'] = '';
  200. $quota['username'] = '';
  201. if (!empty($quota['member_quota_used_json'])) {
  202. $usedJson = json_decode($quota['member_quota_used_json'], true);
  203. $quota['premises'] = $usedJson['charge']['charge_premises'] ?? '';
  204. if (isset($usedJson['charge']['charge_user_id'])) {
  205. $user = SysUser::where('user_id', $usedJson['charge']['charge_user_id'])
  206. ->select('user_id', 'user_name')
  207. ->first();
  208. if ($user) {
  209. $quota['username'] = $user->user_name;
  210. }
  211. }
  212. }
  213. }
  214. return json_success('', $quotas);
  215. }
  216. /**
  217. * @Desc 额度核销
  218. * @Author Gorden
  219. * @Date 2024/6/4 8:32
  220. *
  221. * @param Request $request
  222. * @return Response
  223. */
  224. public function writeOff(Request $request)
  225. {
  226. $params = $request->post();
  227. $memberId = $params['member_id'] ?? [];
  228. $componentId = $params['component_id'] ?? [];
  229. $nbr = $params['nbr'];
  230. $times = $params['times'] ?? '';
  231. $code = $params['sms_code'] ?? '';
  232. if (!$memberId || !$componentId) {
  233. return json_fail("参数异常");
  234. }
  235. $quotas = MemberQuota::where('join_quota_member_id', $memberId[0])
  236. ->where('join_member_rule_added_component_id', $componentId[0])
  237. ->where('member_quota_status', 'PENDING')
  238. ->limit($nbr)
  239. ->get()
  240. ->toArray();
  241. if ($quotas[0]['member_quota_nbr'] != '-99.00' && count($quotas) < intval($nbr)) {
  242. return json_fail("可核销数量不足");
  243. }
  244. // $member = Member::find(current($memberId));
  245. // $mobile = $member->member_mobile;
  246. // $key = "SMS:CODE:QUOTA:" . $mobile;
  247. // $redisCode = Redis::get($key);
  248. // if ($redisCode != $code) {
  249. // return json_fail("验证码错误,请重新输入");
  250. // }
  251. // Redis::del($key);
  252. if (!$times) {
  253. $params['times'] = date('Y-m-d H:i:s');
  254. } else {
  255. $params['times'] = date('Y-m-d H:i:s', strtotime($times));
  256. }
  257. try {
  258. // 生成核销数据
  259. $writeOffData = AddedService::generateWriteOffData($params);
  260. if($quotas[0]['member_quota_nbr'] != '-99.00'){
  261. foreach ($quotas as $quota) {
  262. $writeOffData['member_quota_id'] = $quota['member_quota_id'];
  263. MemberQuota::where('member_quota_id', $quota['member_quota_id'])->update([
  264. 'member_quota_status' => 'USED',
  265. 'member_quota_used_json' => json_encode($writeOffData)
  266. ]);
  267. }
  268. }else{
  269. $quota = $quotas[0];
  270. for($i=0;$i<$nbr;$i++){
  271. $quota['member_quota_id'] = 'MQ'.date('YmdHis').random_string(6,'up');
  272. $quota['member_quota_status'] = 'USED';
  273. $quota['member_quota_used_json'] = json_encode($writeOffData);
  274. $quota['member_quota_addtimes'] = time();
  275. $quota['member_quota_nbr'] = 1;
  276. MemberQuota::insert($quota);
  277. }
  278. }
  279. } catch (\Exception $e) {
  280. dump($e->getMessage());
  281. return json_fail("核销失败");
  282. }
  283. return json_success("核销成功");
  284. }
  285. public static function doWriteOff($params){
  286. $quotas = MemberQuota::where('join_quota_member_id', $params['member_id'])
  287. ->where('join_member_rule_added_component_id', $params['component_id'])
  288. ->where('member_quota_status', 'PENDING')
  289. ->limit($params['nbr'])
  290. ->get()
  291. ->toArray();
  292. if (count($quotas) < intval($params['nbr'])) {
  293. throw new BusinessException('可核销数量不足');
  294. }
  295. if (!$params['times']) {
  296. $params['times'] = date('Y-m-d H:i:s');
  297. } else {
  298. $params['times'] = date('Y-m-d H:i:s', strtotime($params['times']));
  299. }
  300. try {
  301. // 生成核销数据
  302. $writeOffData = AddedService::generateWriteOffData($params);
  303. foreach ($quotas as $quota) {
  304. $writeOffData['member_quota_id'] = $quota['member_quota_id'];
  305. MemberQuota::where('member_quota_id', $quota['member_quota_id'])->update([
  306. 'member_quota_status' => 'USED',
  307. 'member_quota_used_json' => json_encode($writeOffData)
  308. ]);
  309. }
  310. } catch (\Exception $e) {
  311. throw new BusinessException('核销失败');
  312. // return json_fail("核销失败");
  313. }
  314. return json_success("核销成功");
  315. }
  316. }