You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
416 lines
20 KiB
PHP
416 lines
20 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API\H5;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class QuestionnairesLogsController extends Controller
|
|
{
|
|
public function delete(Request $request)
|
|
{
|
|
$id = $request->post('id');
|
|
$person_id = $request->post('person');
|
|
$person_info = DB::table('web_user_person')->where('id', $person_id)->first();
|
|
if (!$person_info) {
|
|
return \Yz::echoError('人员信息不存在');
|
|
}
|
|
$log_info = DB::table('questionnaires_logs')->where('id', $id)->where('person_id', $person_id)->first();
|
|
if (!$log_info) {
|
|
return \Yz::echoError('问卷信息不存在');
|
|
}
|
|
DB::table('questionnaires_logs')->where('id', $id)->update(['del' => 1]);
|
|
return \Yz::Return(true, '操作完成');
|
|
}
|
|
public function list(Request $request)
|
|
{
|
|
$person_id = $request->post('person');
|
|
$person_info = DB::table('web_user_person')->where('id', $person_id)->first();
|
|
if (!$person_info) {
|
|
return \Yz::echoError('人员信息不存在');
|
|
}
|
|
$log = DB::table('questionnaires_logs')->select(['*'])
|
|
->selectRaw("IFNULL((select `name` from questionnaires where questionnaires.id = questionnaires_logs.person_id),'') as title")
|
|
->where('items', '!=', '[]')
|
|
->where('person_id', $person_id)
|
|
->where('del', 2)
|
|
->orderBy('id', 'desc')->get();
|
|
$list = [];
|
|
foreach ($log as $value) {
|
|
$person_info = json_decode($value->person_info, true);
|
|
$age = !!$person_info['birthday'] ? date('Y') - date('Y', strtotime($person_info['birthday'])) : 0;
|
|
$check_items = json_decode($value->check_items, true);
|
|
$count = count($check_items);
|
|
$list[] = [
|
|
'id' => $value->id,
|
|
'title' => $value->title,
|
|
'name' => $person_info['name'],
|
|
'age' => $age,
|
|
'created_at' => $value->created_at,
|
|
'count' => $count
|
|
];
|
|
}
|
|
return \Yz::Return(true, '操作完成', [
|
|
'log'=>$log,
|
|
'list' => $list,
|
|
]);
|
|
}
|
|
|
|
public function push(Request $request)
|
|
{
|
|
$id = $request->post('id');
|
|
$person_id = $request->post('person');
|
|
$person_info = DB::table('web_user_person')->where('id', $person_id)->first();
|
|
if (!$person_info) {
|
|
return \Yz::echoError('人员信息不存在');
|
|
}
|
|
$log_info = DB::table('questionnaires_logs')->where('question_id', $id)->where('person_id', $person_id)->orderBy('id', 'desc')->first();
|
|
if (!$log_info) {
|
|
return \Yz::echoError('未查询到答题记录');
|
|
}
|
|
$content = json_decode($log_info->content, true);
|
|
return \Yz::Return(true, '操作完成', [
|
|
'info' => $content,
|
|
]);
|
|
}
|
|
public function info(Request $request)
|
|
{
|
|
$id = $request->post('id');
|
|
$person_id = $request->post('person');
|
|
$person_info = DB::table('web_user_person')->where('id', $person_id)->first();
|
|
if (!$person_info) {
|
|
return \Yz::echoError('人员信息不存在');
|
|
}
|
|
$log_info = DB::table('questionnaires_logs')->where('id', $id)->where('person_id', $person_id)->first();
|
|
if (!$log_info) {
|
|
return \Yz::echoError('问卷信息不存在');
|
|
}
|
|
|
|
$person_info = json_decode($log_info->person_info, true);
|
|
$age = !!$person_info['birthday'] ? date('Y') - date('Y', strtotime($person_info['birthday'])) : 0;
|
|
$sex = !!$person_info['sex'] ? $person_info['sex'] : 1;
|
|
$weight = !!$log_info->weight ? $log_info->weight : 0;
|
|
$height = !!$log_info->height ? $log_info->height : 0;
|
|
$items_ids = json_decode($log_info->check_items, true);
|
|
$check_items = [];
|
|
if (!!count($items_ids)) {
|
|
$items_info = DB::table('question_items')->whereIn('id', $items_ids)->get();
|
|
foreach ($items_info as $key => $value) {
|
|
$check_items[] = [
|
|
'id' => $value->id,
|
|
'name' => $value->name,
|
|
];
|
|
}
|
|
}
|
|
$items = json_decode($log_info->items, true);
|
|
$check_items_array = json_decode($log_info->check_items_array, true);
|
|
$wh = '-';
|
|
if (!!$weight && $height) {
|
|
$wh = round($weight / pow($height / 100, 2), 2);
|
|
}
|
|
return \Yz::Return(true, '操作完成', [
|
|
'date' => date('Y-m-d', strtotime($log_info->created_at)),
|
|
'sex' => $sex,
|
|
'age' => $age,
|
|
'weight' => $wh,
|
|
'items' => $items,
|
|
'check_items' => $check_items,
|
|
'check_items_array' => $check_items_array,
|
|
'price' => [
|
|
'jichu' => self::sum_items($items, $check_items_array['jichu']),
|
|
'tuijian' => self::sum_items($items, $check_items_array['tuijian']),
|
|
'gaoduan' => self::sum_items($items, $check_items_array['gaoduan']),
|
|
],
|
|
'original_price' => [
|
|
'jichu' => self::sum_items($items, $check_items_array['jichu'], false),
|
|
'tuijian' => self::sum_items($items, $check_items_array['tuijian'], false),
|
|
'gaoduan' => self::sum_items($items, $check_items_array['gaoduan'], false),
|
|
],
|
|
'count' => [
|
|
'jichu' => self::item_count($items, $check_items_array['jichu']),
|
|
'tuijian' => self::item_count($items, $check_items_array['tuijian']),
|
|
'gaoduan' => self::item_count($items, $check_items_array['gaoduan']),
|
|
]
|
|
]);
|
|
}
|
|
|
|
public function item_count($items, $check_items)
|
|
{
|
|
if (count($items)) {
|
|
$items_count = DB::table('items')->whereIn('item_id', $items)
|
|
->where('keshi_name', '!=', '材料费')->where('status', 1)->count();
|
|
} else {
|
|
$items_count = 0;
|
|
}
|
|
if (count($check_items['items'])) {
|
|
$check_sum_count = DB::table('items')
|
|
->whereIn('item_id', $check_items['items'])
|
|
->where('keshi_name', '!=', '材料费')->where('status', 1)->count();
|
|
} else {
|
|
$check_sum_count = 0;
|
|
}
|
|
|
|
if (count($check_items['combo_items'])) {
|
|
$combo_count = DB::table('items')
|
|
->whereIn('item_id', $check_items['combo_items'])
|
|
->where('keshi_name', '!=', '材料费')->where('status', 1)->count();
|
|
} else {
|
|
$combo_count = 0;
|
|
}
|
|
$ic = $items_count + $check_sum_count + $combo_count;
|
|
return $ic;
|
|
}
|
|
|
|
public function sum_items($items, $check_items, $zk = true)
|
|
{
|
|
if (count($items)) {
|
|
$sum = DB::table('items')->whereIn('item_id', $items)->where('status', 1)->sum('price');
|
|
} else {
|
|
$sum = 0;
|
|
}
|
|
if (count($check_items['items'])) {
|
|
$check_sum = DB::table('items')->whereIn('item_id', $check_items['items'])->where('status', 1)->sum('price');
|
|
} else {
|
|
$check_sum = 0;
|
|
}
|
|
|
|
if (count($check_items['combo_items'])) {
|
|
$combo = DB::table('items')->whereIn('item_id', $check_items['combo_items'])->where('status', 1)->sum('price');
|
|
} else {
|
|
$combo = 0;
|
|
}
|
|
|
|
if (!!$zk) {
|
|
$zhekou = config('app.globals.Wj_ZheKou');
|
|
} else {
|
|
$zhekou = 1;
|
|
}
|
|
return number_format((($combo + $sum + $check_sum) * $zhekou), 2, '.', '');
|
|
}
|
|
public function submit(Request $request)
|
|
{
|
|
$question_id = $request->post('question_id');
|
|
$content = $request->post('content');
|
|
$use_time = $request->post('use_time');
|
|
$question_info = DB::table('questionnaires')->where('id', $question_id)->first();
|
|
if (!$question_info) {
|
|
return \Yz::echoError('问卷不存在');
|
|
}
|
|
$person_id = $request->post('person_id');
|
|
$person_info = DB::table('web_user_person')->where('id', $person_id)->first();
|
|
if (!$person_info) {
|
|
return \Yz::echoError('人员信息不存在');
|
|
}
|
|
$type = $question_info->type;
|
|
$items = json_decode($question_info->items, true);
|
|
$check_items = [];
|
|
$check_items_array = [
|
|
'jichu' => [
|
|
'combo' => '0',
|
|
'combo_items' => [],
|
|
'items' => []
|
|
],
|
|
'tuijian' => [
|
|
'combo' => '0',
|
|
'combo_items' => [],
|
|
'items' => []
|
|
],
|
|
'gaoduan' => [
|
|
'combo' => '0',
|
|
'combo_items' => [],
|
|
'items' => []
|
|
],
|
|
];
|
|
$question_ids = [];
|
|
$question_map = [];
|
|
foreach ($content as $key => $value) {
|
|
$question_ids[] = $value['id'];
|
|
$question_map["q{$value['id']}"] = $value;
|
|
}
|
|
|
|
if (!!count($question_ids)) {
|
|
$question_questions = DB::table('question_questions')->whereIn('id', $question_ids)->get();
|
|
} else {
|
|
$question_questions = [];
|
|
}
|
|
$weight = 0;
|
|
$height = 0;
|
|
$score = 0;
|
|
foreach ($question_questions as $value) {
|
|
$id = $value->id;
|
|
$option = json_decode($value->option, true);
|
|
if (in_array($value->type, ['input', 'date', 'city'])) {
|
|
if ($option['input']['value'] === '${体重}') {
|
|
if (isset($question_map["q{$id}"])) {
|
|
$weight = $question_map["q{$id}"]['value'];
|
|
}
|
|
}
|
|
|
|
if ($option['input']['value'] === '${身高}') {
|
|
if (isset($question_map["q{$id}"])) {
|
|
$height = $question_map["q{$id}"]['value'];
|
|
}
|
|
}
|
|
} else {
|
|
if (isset($question_map["q{$id}"])) {
|
|
$v = $question_map["q{$id}"]['active'];
|
|
$item_option = $option['select']['value'][$v];
|
|
if ($item_option['type'] === 'score') {
|
|
if (!isset($item_option['score'])) {
|
|
$item_option['score'] = 0;
|
|
}
|
|
foreach ($content as $content_key => $content_item) {
|
|
if ($content_item['id'] == $id) {
|
|
$content[$content_key]['score'] = $item_option['score'];
|
|
}
|
|
}
|
|
$score = $score + $item_option['score'];
|
|
}
|
|
if ($item_option['type'] === 'items') {
|
|
foreach ($item_option['items'] as $item) {
|
|
if (!in_array($item, $check_items)) {
|
|
$check_items_info = DB::table('question_items')->where('id', $item)->first();
|
|
if (!!$check_items_info) {
|
|
$check_items[] = (string)$item;
|
|
|
|
if ($check_items_info->jichu != '0') {
|
|
$jichu_combo_info = DB::table('combos')->where('combo_id', $check_items_info->jichu)->first();
|
|
if (!!$jichu_combo_info) {
|
|
if ($check_items_array['jichu']['combo'] == '0') {
|
|
$items_ids = [];
|
|
$combo_items = json_decode($jichu_combo_info->items, true);
|
|
foreach ($combo_items as $combo_item) {
|
|
$items_ids[] = $combo_item['id'];
|
|
}
|
|
$check_items_array['jichu']['combo'] = $check_items_info->jichu;
|
|
$check_items_array['jichu']['combo_items'] = $items_ids;
|
|
} else {
|
|
$items_ids = [];
|
|
$combo_items = json_decode($jichu_combo_info->items, true);
|
|
foreach ($combo_items as $combo_item) {
|
|
if (
|
|
!in_array($combo_item['id'], $check_items_array['jichu']['combo_items']) &&
|
|
!in_array($combo_item['id'], $check_items_array['jichu']['items'])
|
|
) {
|
|
$items_ids[] = $combo_item['id'];
|
|
}
|
|
}
|
|
$check_items_array['jichu']['items'] = array_merge($check_items_array['jichu']['items'], $items_ids);
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($check_items_info->tuijian != '0') {
|
|
$tuijian_combo_info = DB::table('combos')->where('combo_id', $check_items_info->tuijian)->first();
|
|
if (!!$tuijian_combo_info) {
|
|
if ($check_items_array['tuijian']['combo'] == '0') {
|
|
$items_ids = [];
|
|
$combo_items = json_decode($tuijian_combo_info->items, true);
|
|
foreach ($combo_items as $combo_item) {
|
|
$items_ids[] = $combo_item['id'];
|
|
}
|
|
$check_items_array['tuijian']['combo'] = $check_items_info->tuijian;
|
|
$check_items_array['tuijian']['combo_items'] = $items_ids;
|
|
} else {
|
|
$items_ids = [];
|
|
$combo_items = json_decode($tuijian_combo_info->items, true);
|
|
foreach ($combo_items as $combo_item) {
|
|
if (
|
|
!in_array($combo_item['id'], $check_items_array['tuijian']['combo_items']) &&
|
|
!in_array($combo_item['id'], $check_items_array['tuijian']['items'])
|
|
) {
|
|
$items_ids[] = $combo_item['id'];
|
|
}
|
|
}
|
|
$check_items_array['tuijian']['items'] = array_merge($check_items_array['tuijian']['items'], $items_ids);
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($check_items_info->gaoduan != '0') {
|
|
$gaoduan_combo_info = DB::table('combos')->where('combo_id', $check_items_info->gaoduan)->first();
|
|
if (!!$gaoduan_combo_info) {
|
|
if ($check_items_array['gaoduan']['combo'] == '0') {
|
|
$items_ids = [];
|
|
$combo_items = json_decode($gaoduan_combo_info->items, true);
|
|
foreach ($combo_items as $combo_item) {
|
|
$items_ids[] = $combo_item['id'];
|
|
}
|
|
$check_items_array['gaoduan']['combo'] = $check_items_info->gaoduan;
|
|
$check_items_array['gaoduan']['combo_items'] = $items_ids;
|
|
} else {
|
|
$items_ids = [];
|
|
$combo_items = json_decode($gaoduan_combo_info->items, true);
|
|
foreach ($combo_items as $combo_item) {
|
|
if (
|
|
!in_array($combo_item['id'], $check_items_array['gaoduan']['combo_items']) &&
|
|
!in_array($combo_item['id'], $check_items_array['gaoduan']['items'])
|
|
) {
|
|
$items_ids[] = $combo_item['id'];
|
|
}
|
|
}
|
|
$check_items_array['gaoduan']['items'] = array_merge($check_items_array['gaoduan']['items'], $items_ids);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
preg_match_all('/\d+/', $weight, $matches);
|
|
$weight = $matches[0][0];
|
|
$weight = $weight ? $weight : 0;
|
|
preg_match_all('/\d+/', $height, $matches);
|
|
$height = $matches[0][0];
|
|
$height = $height ? $height : 0;
|
|
$ip = self::getTrustedProxiesIp();
|
|
$ip2region = new \Ip2Region();
|
|
$record = $ip2region->simple($ip);
|
|
$ip_info = '';
|
|
if (!!$record) {
|
|
$ip_info = $record;
|
|
}
|
|
$log_id = DB::table('questionnaires_logs')->insertGetId([
|
|
'question_id' => $question_id,
|
|
'type' => $type,
|
|
'weight' => $weight,
|
|
'height' => $height,
|
|
'use_time' => $use_time,
|
|
'ip' => $ip,
|
|
'ip_info' => $ip_info,
|
|
'score' => $score,
|
|
'order_id' => '0',
|
|
'person_id' => $person_id,
|
|
'person_info' => json_encode($person_info, JSON_UNESCAPED_UNICODE),
|
|
'content' => json_encode($content, JSON_UNESCAPED_UNICODE),
|
|
'items' => json_encode($items, JSON_UNESCAPED_UNICODE),
|
|
'check_items' => json_encode($check_items, JSON_UNESCAPED_UNICODE),
|
|
'check_items_array' => json_encode($check_items_array, JSON_UNESCAPED_UNICODE),
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
]);
|
|
return \Yz::Return(true, '操作完成', [
|
|
'id' => $log_id
|
|
]);
|
|
}
|
|
|
|
public function getTrustedProxiesIp()
|
|
{ //获取用户真实ip
|
|
if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
|
|
$ip = getenv('HTTP_CLIENT_IP');
|
|
} elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
|
|
$ip = getenv('HTTP_X_FORWARDED_FOR');
|
|
} elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
|
|
$ip = getenv('REMOTE_ADDR');
|
|
} elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
}
|
|
$res = preg_match('/[\d\.]{7,15}/', $ip, $matches) ? $matches[0] : '';
|
|
return $res;
|
|
}
|
|
}
|