123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- namespace app\admin\controller\coupon;
- use app\admin\service\member\MemberService;
- use app\controller\Curd;
- use app\model\Coupon;
- use app\model\CouponDetail;
- use support\Request;
- use support\Response;
- class CouponDetailController extends Curd
- {
- public function __construct()
- {
- $this->model = new CouponDetail();
- }
- public function select(Request $request): Response
- {
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- // $where['join_coupon_detail_member_id'] = ['<>', ''];
- $order = $request->get('order', 'desc');
- $field = $field ?? 'coupon_detail_addtimes';
- $couponName = $request->get('coupon_name', '');
- if (!empty($couponName)) {
- $couponIds = Coupon::where('coupon_name', 'like', '%' . $couponName . '%')->pluck('coupon_id')->toArray();
- if (empty($couponIds)) {
- $couponIds = ['default'];
- }
- $where['join_detail_coupon_id'] = ['in', $couponIds];
- }
- $addTimes = $request->get('coupon_detail_addtimes', []);
- if (!empty($addTimes)) {
- $addTimes[0] = isset($addTimes[0]) ? strtotime($addTimes[0]) : time();
- $addTimes[1] = isset($addTimes[1]) ? strtotime($addTimes[1]) : time();
- $where['coupon_detail_addtimes'] = $addTimes;
- }
- $query = $this->doSelect($where, $field, $order);
- return $this->doFormat($query, $format, $limit);
- }
- protected function doSelect(array $where, string $field = null, string $order = 'desc')
- {
- $model = $this->model->with([
- 'detail' => function ($query) {
- $query->select('join_detail_coupon_id', 'coupon_detail_status', 'coupon_detail_gain_datetime', 'coupon_detail_used_datetime', 'coupon_detail_remark');
- },
- 'coupon',
- 'member' => function ($query) {
- $query->select('member_id', 'member_mobile');
- },
- 'cert' => function ($query) {
- $query->select('join_cert_member_id', 'member_cert_name');
- },
- 'info' => function ($query) {
- $query->select('join_info_member_id', 'member_info_nickname');
- }
- ]);
- foreach ($where as $column => $value) {
- if (is_array($value)) {
- if ($value[0] === 'like' || $value[0] === 'not like') {
- $model = $model->where($column, $value[0], "%$value[1]%");
- } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
- $model = $model->where($column, $value[0], $value[1]);
- } elseif ($value[0] == 'in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereIn($column, $valArr);
- } elseif ($value[0] == 'not in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereNotIn($column, $valArr);
- } elseif ($value[0] == 'null') {
- $model = $model->whereNull($column);
- } elseif ($value[0] == 'not null') {
- $model = $model->whereNotNull($column);
- } elseif ($value[0] !== '' || $value[1] !== '') {
- $model = $model->whereBetween($column, $value);
- }
- } else {
- $model = $model->where($column, $value);
- }
- }
- // $model = $model->selectRaw('join_detail_coupon_id,join_coupon_detail_member_id,MAX(coupon_detail_addtimes) as coupon_detail_addtimes,COUNT(*) as total');
- // $model = $model->groupBy('join_coupon_detail_member_id', 'join_detail_coupon_id');
- if ($field) {
- $model = $model->orderBy($field, $order);
- }
- return $model;
- }
- public function afterQuery($items)
- {
- foreach ($items as &$item) {
- $mobile = $certName = $nickname = '';
- if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
- $mobile = $item['member']['member_mobile'];
- }
- if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
- $certName = $item['cert']['member_cert_name'];
- }
- if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
- $nickname = $item['info']['member_info_nickname'];
- }
- $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
- $item['member_used'] = 0;
- foreach ($item['detail'] as $detail) {
- if ($detail['coupon_detail_status'] == 'USED') {
- $item['member_used'] += 1;
- }
- }
- if (!empty($item['coupon_detail_extend_json'])) {
- $couponDetailExtendJson = json_decode($item['coupon_detail_extend_json'], true);
- if (isset($couponDetailExtendJson['gettype'])){
- $item['gettype'] = $couponDetailExtendJson['gettype'];
- }
- }
- }
- return $items;
- }
- /**
- * @Desc 修改状态
- * @Author Gorden
- * @Date 2024/8/22 8:44
- *
- * @param Request $request
- * @return Response
- */
- public function updateStatus(Request $request)
- {
- $detailId = $request->post('coupon_detail_id', '');
- $status = $request->post('status', '');
- $detail = CouponDetail::where('coupon_detail_id', $detailId)->first();
- if (!$detail) {
- return json_fail('数据不存在');
- }
- //状态 INIT|PENDING=等待领取|ACTIVED=可用|WAITING=已占用(待完成)|USED=已使用|DISABLED|CANCEL|EXPIRED
- if (!in_array($status, ['INIT', 'PENDING', 'ACTIVED', 'WAITING', 'USED', 'DISABLED', 'CANCEL', 'EXPIRED'])) {
- return json_fail("状态异常");
- }
- try {
- CouponDetail::where('coupon_detail_id', $detailId)->update(['coupon_detail_status' => $status]);
- return json_success('success');
- } catch (\Exception $e) {
- dump($e->getMessage());
- return json_fail('数据修改失败');
- }
- }
- /**
- * @Desc 详情
- * @Author Gorden
- * @Date 2024/8/22 11:46
- *
- * @param Request $request
- * @return Response
- */
- public function info(Request $request): Response
- {
- $detailId = $request->get('detail_id','');
- if (empty($detailId)){
- return json_fail('参数异常');
- }
- $detail = CouponDetail::where('coupon_detail_id',$detailId)
- ->select('coupon_detail_id','coupon_detail_extend_json')
- ->first();
- if (!empty($detail->coupon_detail_extend_json)){
- $couponDetailExtendJson = json_decode($detail->coupon_detail_extend_json,true);
- if (isset($couponDetailExtendJson['from'])){
- $detail['from'] = MemberService::getMemberNameByMemberId($couponDetailExtendJson['from']);
- }
- }
- return json_success('',$detail);
- }
- }
|