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.
hainan_2024/Laravel/app/Http/Controllers/API/H5/QuestionnairesLogsControlle...

303 lines
15 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, '操作完成', [
'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;
$weight = !!$log_info->weight ? $log_info->weight : 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);
return \Yz::Return(true, '操作完成', [
'date' => date('Y-m-d', strtotime($log_info->created_at)),
'age' => $age,
'weight' => $weight == '0' ? '-' : $weight,
'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']),
]
]);
}
public function sum_items($items, $check_items)
{
$sum = DB::table('items')->whereIn('item_id', $items)->where('status', 1)->sum('price');
$check_sum = DB::table('items')->whereIn('item_id', $check_items['items'])->where('status', 1)->sum('price');
$combo = DB::table('combos')->whereIn('combo_id', [$check_items['combo']])->where('status', 1)->sum('price');
$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');
$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;
foreach ($question_questions as $value) {
$id = $value->id;
$option = json_decode($value->option, true);
if ($value->type === 'input') {
if ($option['input']['value'] === '${体重}') {
if (isset($question_map["q{$id}"])) {
$weight = $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'] === '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;
$log_id = DB::table('questionnaires_logs')->insertGetId([
'question_id' => $question_id,
'type' => $type,
'weight' => $weight,
'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
]);
}
}