123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- namespace app\admin\controller\member;
- use app\admin\validate\life\CinemaIlkValidate;
- use app\admin\validate\member\WellnessRecordValidate;
- use app\controller\Curd;
- use app\model\CinemaIlk as CinemaIlkModel;
- use app\model\Customer;
- use app\model\WellnessRecord;
- use support\exception\BusinessException;
- use support\Request;
- use support\Response;
- class VisitingController extends Curd
- {
- public function __construct()
- {
- $this->model = new WellnessRecord();
- $this->validate = true;
- $this->validateClass = new WellnessRecordValidate();
- }
- /**
- * @Desc 查询列表
- * @Author Gorden
- * @Date 2024/3/21 9:58
- *
- * @param Request $request
- * @return Response
- * @throws \support\exception\BusinessException
- */
- public function select(Request $request): Response
- {
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- $field = $field ?? 'wellness_record_addtimes';
- $order = $request->get('order', 'desc');
- $where['wellness_record_category'] = 'Visiting';
- $m = $request->get('m', 'MEMBER');
- if ($m == 'MEMBER') {
- $isOwner = 'N';
- $query = $this->doSelect($where, $field, $order, $isOwner);
- } elseif ($m == 'OWNER') {
- $isOwner = 'Y';
- $query = $this->doSelect($where, $field, $order, $isOwner);
- } elseif ($m == 'CUSTOMER') {
- $field = 'created_at';
- $query = $this->doSelectCustomer($where, $field, $order);
- }
- return $this->doFormat($query, $format, $limit);
- }
- protected function doSelect(array $where, string $field = null, string $order = 'desc', $isOwner = 'N')
- {
- $model = $this->model->leftJoin('member', 'member.member_id', '=', 'wellness_record.join_wellness_record_member_id')
- ->leftJoin('member_cert', 'member_cert.join_cert_member_id', '=', 'wellness_record.join_wellness_record_member_id')
- ->select('member.member_id', 'member.member_mobile', 'member_cert.member_cert_name', 'wellness_record.*')
- ->where('member.member_is_owner', $isOwner);
- // ->with([
- // 'member' => function ($query) {
- // $query->where('member_is_owner', 'N')->select('member_id', 'member_mobile');
- // },
- // 'cert' => function ($query) {
- // $query->select('join_cert_member_id', 'member_cert_name');
- // }
- // ]);
- 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);
- }
- }
- if ($field) {
- $model = $model->orderBy($field, $order);
- }
- return $model;
- }
- protected function doSelectCustomer(array $where, string $field = null, string $order = 'desc')
- {
- $model = $this->model->leftJoin('customer', 'customer.id', '=', 'wellness_record.join_wellness_record_member_id')
- ->select('customer.name', 'customer.mobile', 'wellness_record.*')
- ->where('customer.name','<>', "");
- // ->with([
- // 'member' => function ($query) {
- // $query->where('member_is_owner', 'N')->select('member_id', 'member_mobile');
- // },
- // 'cert' => function ($query) {
- // $query->select('join_cert_member_id', 'member_cert_name');
- // }
- // ]);
- 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);
- }
- }
- if ($field) {
- $model = $model->orderBy($field, $order);
- }
- return $model;
- }
- protected function afterQuery($items)
- {
- foreach ($items as &$item) {
- if (!empty($item->wellness_record_file_json)) {
- $item->wellness_record_file_json = json_decode($item->wellness_record_file_json, true);
- $wellnessRecordFileJson = [];
- foreach ($item->wellness_record_file_json as $value) {
- $wellnessRecordFileJson[] = [
- 'url' => getenv('STORAGE_DOMAIN') . $value['url'],
- 'name' => $value['name']
- ];
- }
- $item->wellness_record_file_json = $wellnessRecordFileJson;
- }
- if (empty($item->member)) {
- unset($item);
- }
- }
- return $items;
- }
- protected function insertInput(Request $request): array
- {
- $data = $this->inputFilter($request->post());
- $data['wellness_record_datetime'] = date('Y-m-d H:i:s', strtotime($data['wellness_record_datetime']));
- if (!empty($data['wellness_record_file_json'])) {
- $data['wellness_record_file_json'] = json_decode($data['wellness_record_file_json'], true);
- $wellnessRecordFileJson = [];
- foreach ($data['wellness_record_file_json'] as $value) {
- $wellnessRecordFileJson[] = [
- 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $value['url']),
- 'name' => $value['name']
- ];
- }
- $data['wellness_record_file_json'] = json_encode($wellnessRecordFileJson);
- } else {
- $data['wellness_record_file_json'] = '[]';
- }
- return $data;
- }
- protected function updateInput(Request $request): array
- {
- $primary_key = $this->model->getKeyName();
- $id = $request->post($primary_key);
- $data = $this->inputFilter($request->post());
- $data['wellness_record_datetime'] = date('Y-m-d H:i:s', strtotime($data['wellness_record_datetime']));
- if (!empty($data['wellness_record_file_json'])) {
- $data['wellness_record_file_json'] = json_decode($data['wellness_record_file_json'], true);
- $wellnessRecordFileJson = [];
- foreach ($data['wellness_record_file_json'] as $value) {
- $wellnessRecordFileJson[] = [
- 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $value['url']),
- 'name' => $value['name']
- ];
- }
- $data['wellness_record_file_json'] = json_encode($wellnessRecordFileJson);
- } else {
- $data['wellness_record_file_json'] = '[]';
- }
- $model = $this->model->find($id);
- if (!$model) {
- throw new BusinessException('记录不存在', 2);
- }
- unset($data[$primary_key]);
- return [$id, $data];
- }
- }
|