548 lines
23 KiB
PHP
548 lines
23 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Server\H5;
|
|
|
|
use App\Exports\ReportExport;
|
|
use App\Models\MerchantUserImage;
|
|
use App\Models\ReportMerchantUserConfig;
|
|
use App\Models\Server\MerchantUser;
|
|
use App\Server\ReportFile;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ConsultAccount;
|
|
use App\Models\Consultation;
|
|
use App\Models\Course\Course;
|
|
use App\Models\MerchantShop;
|
|
use App\Models\Server\CommunityActivity;
|
|
use App\Models\Server\MerchantReport;
|
|
use App\Models\Server\ReportAnswer;
|
|
use App\Models\Server\ReportQuestion;
|
|
use App\Models\Server\TouristOrder;
|
|
use Illuminate\Support\Facades\Redis;
|
|
use phpDocumentor\Reflection\Types\Object_;
|
|
|
|
class ReportController extends Controller
|
|
{
|
|
|
|
//获取指定的用户的回复列表
|
|
public function getCommitList(Request $request){
|
|
|
|
$m_user_id = $request->merchant_user_id;
|
|
$order_id = $request->order_id;
|
|
$order = TouristOrder::find($order_id);
|
|
if(empty($order)){
|
|
return $this->failure('还未参与活动');
|
|
}
|
|
$type = $order->type;
|
|
$type_id = $order->type_id;
|
|
|
|
$report = MerchantReport::where('type',$type)
|
|
->where('type_id',$type_id)
|
|
->select('id', 'commitd_at')
|
|
->first();
|
|
|
|
|
|
//get report detail
|
|
$report_date_list = json_decode($report->commitd_at, 1);
|
|
|
|
|
|
//获取问题的列表,并且把答案安进去
|
|
$question_list = ReportQuestion::where('type',$type)->where('type_id',$type_id)
|
|
->select('id','type','type_id','title','input_type','title_answer','created_at')
|
|
->orderBy('id','asc')->get();
|
|
$question_id_list = [];
|
|
$question_data_list = [];
|
|
foreach($question_list as $question){
|
|
$question_id_list[] = $question->id;
|
|
if(in_array($question->input_type , ['checkbox', 'radio'])){
|
|
$question->title_answer = json_decode($question->title_answer, 1);
|
|
}
|
|
$question_data_list[$question->id] = $question;
|
|
}
|
|
|
|
//get report answer_list, 按日期与问题ID来排,已经填写的日期也需要加到设置里面
|
|
$answer_list = ReportAnswer::whereIn('question_id', $question_id_list)->where('m_user_id', $m_user_id)
|
|
->select('id', 'question_id', 'title', 'answer', 'answer2', 'pics', 'previous_increment',
|
|
'first_increment', 'commitd_at','report_poster')->get();
|
|
|
|
$answer_data_list = [];
|
|
foreach($answer_list as $answer){
|
|
$date_string = $answer->commitd_at;
|
|
$date = (explode(' ', $date_string))[0];
|
|
$report_date_list[] = $date;
|
|
$answer->pics = json_decode($answer->pics, true);
|
|
$answer_data_list[$date][$answer->question_id] = $answer;
|
|
}
|
|
|
|
|
|
|
|
sort($report_date_list);
|
|
$data_list = [];
|
|
foreach($report_date_list as $date)
|
|
{
|
|
$show_answer = isset($answer_data_list[$date])?1:0;
|
|
$data_list[$date] = [
|
|
'date' => $date,
|
|
'show_answer' => $show_answer
|
|
];
|
|
if($show_answer){
|
|
$q_list = json_decode( json_encode($question_data_list));
|
|
foreach($q_list as $q){
|
|
$answer = isset($answer_data_list[$date][$q->id])?$answer_data_list[$date][$q->id]:'';
|
|
if($q->input_type == 'checkbox' && $answer != ''){
|
|
if(!is_array($answer->answer2)){
|
|
$answer->answer2 = json_decode($answer->answer2, 1);
|
|
}
|
|
}
|
|
$q->answer = $answer;
|
|
}
|
|
$data_list[$date]['question_list'] = array_values(json_decode(json_encode($q_list), 1));
|
|
}
|
|
|
|
}
|
|
|
|
return $this->success('ok', array_values($data_list));
|
|
|
|
}
|
|
//
|
|
//报告问题列表
|
|
public function reportQuestions(Request $request){
|
|
try {
|
|
$type = $request->type;
|
|
$type_id = $request->type_id;
|
|
$result = $this->getModel($type,$type_id);
|
|
if(empty($result)) return $this->success('ok',[]);
|
|
$questions = ReportQuestion::where('type',$type)->where('type_id',$type_id)->select('id','type','type_id','title','input_type','title_answer','created_at')->orderBy('id','asc')->get();
|
|
foreach ($questions as $key => $value) {
|
|
$value->title_answer = json_decode($value->title_answer,true);
|
|
}
|
|
return $this->success('ok',$questions);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure();
|
|
}
|
|
}
|
|
|
|
public function reportQuestionsShare(Request $request, $order_id)
|
|
{
|
|
$order = TouristOrder::find($order_id);
|
|
$commitd_at = $request->input('commitd_at');
|
|
$m_user_id = $request->merchant_user_id;
|
|
//是否完成当前日期的所有报告
|
|
//活动下的所有报告问题
|
|
$question_ids = ReportQuestion::where('type', $order->type)->where('type_id', $order->type_id)->pluck('id')->toArray();
|
|
//当前时间已回答的报告问题
|
|
$an_question_ids = ReportAnswer::where('m_user_id', $m_user_id)->where('m_order_id', $order_id)->where('commitd_at', $commitd_at)->pluck('question_id')->toArray();
|
|
$can_share = count(array_diff($question_ids, $an_question_ids))?0:1;
|
|
return $this->success('ok', compact('can_share'));
|
|
}
|
|
|
|
//报告日期
|
|
public function reportDate(Request $request){
|
|
$type = $request->type;
|
|
$type_id = $request->type_id;
|
|
$result = $this->getModel($type,$type_id);
|
|
if(empty($result)) return $this->success('ok',[]);
|
|
$report = MerchantReport::where('type',$type)->where('type_id',$type_id)->first();
|
|
if(empty($report)) return $this->success('ok',[]);
|
|
$commitd_at = $report->commitd_at ? json_decode($report->commitd_at,true) : [];
|
|
return $this->success('ok',$commitd_at);
|
|
}
|
|
|
|
public function getModel($type,$type_id){
|
|
if($type == 'activity' || $type == 'service') $type = 'community';
|
|
$result = null;
|
|
switch ($type) {
|
|
case 'community':
|
|
$result = CommunityActivity::find($type_id);
|
|
break;
|
|
case 'shop':
|
|
$result = MerchantShop::find($type_id);
|
|
break;
|
|
case 'course':
|
|
$result = Course::find($type_id);
|
|
break;
|
|
case 'consult':
|
|
$result = Consultation::find($type_id);
|
|
$result->merchant_id = ConsultAccount::where('id',$result->consult_account_id)->value('merchant_id');
|
|
break;
|
|
default:
|
|
return $result;
|
|
break;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
//提交
|
|
public function commitReport(Request $request){
|
|
try {
|
|
$m_user_id = $request->merchant_user_id;
|
|
$order_id = $request->order_id;
|
|
$order = TouristOrder::find($order_id);
|
|
$commitd_at2 = $request->commitd_at.' 00:00:00';
|
|
if(empty($order) || !in_array($order->pay_status,[1,4])) return $this->failure('订单未支付,支付后再提交');
|
|
$question_id = $request->question_id;
|
|
$title = $request->title;
|
|
$answer = $request->answer;
|
|
$answer2 = ($request->answer2 && is_array($request->answer2)) ? json_encode($request->answer2) : $request->answer2;
|
|
$pics = $request->pics;
|
|
if(empty(trim($answer)) && empty($request->pics) && empty(trim($answer2))) return $this->success('ok',['status'=>0]);
|
|
$where = [
|
|
'm_user_id'=>$m_user_id,
|
|
'm_order_id'=>$order_id,
|
|
'question_id'=>$question_id,
|
|
'commitd_at'=>$commitd_at2
|
|
];
|
|
$report = [
|
|
'title'=>$title,
|
|
'answer'=>$answer,
|
|
'answer2'=>$answer2,
|
|
'pics'=>!empty($pics) ? json_encode($pics) : null,
|
|
];
|
|
$increment = $this->syncReportComparation(array_merge($where , $report));
|
|
$report['previous_increment'] = $increment['previous_increment'];
|
|
$report['first_increment'] = $increment['first_increment'];
|
|
|
|
ReportAnswer::updateOrCreate($where, $report);
|
|
//是否完成当前日期的所有报告
|
|
//活动下的所有报告问题
|
|
$question_ids = ReportQuestion::where('type', $order->type)->where('type_id', $order->type_id)->pluck('id')->toArray();
|
|
//当前时间已回答的报告问题
|
|
$an_question_ids = ReportAnswer::where('m_user_id', $m_user_id)->where('m_order_id', $order_id)->where('commitd_at', $commitd_at2)->pluck('question_id')->toArray();
|
|
$can_share = count(array_diff($question_ids, $an_question_ids))?0:1;
|
|
return $this->success('ok',['status'=>1, 'increment'=>$increment, 'can_share'=>$can_share]);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure();
|
|
// return $this->success('ok1',$e);
|
|
}
|
|
}
|
|
|
|
public function updateReportPoster(Request $request){
|
|
$m_order_id = $request->order_id;
|
|
$commitd_at = $request->commitd_at.' 00:00:00';
|
|
$report_poster = $request->report_poster;
|
|
if($report_poster) {
|
|
ReportAnswer::where('m_order_id', $m_order_id)->where('commitd_at',$commitd_at)
|
|
->where('m_user_id',$request->merchant_user_id)->update(
|
|
[
|
|
'report_poster' => $report_poster,
|
|
]
|
|
);
|
|
}
|
|
return $this->success('ok');
|
|
}
|
|
|
|
//同步用户的本次记录与上一次和和一次记录的对比值以及对比之后的回馈
|
|
private function syncReportComparation($current_report){
|
|
$previous_increment = 0;
|
|
$first_increment = 0;
|
|
$previous_value = 0;
|
|
$first_value = 0;
|
|
$hint = [
|
|
'title' => '',
|
|
'pic_url' => '',
|
|
];
|
|
$question = ReportQuestion::where('id', $current_report['question_id'])->first();
|
|
if($question->input_type == 'numeric'){
|
|
$previous_report = $this->getPreviousReport($current_report);
|
|
if(!empty($previous_report)){
|
|
$first_report = $this->getFirstReport($current_report);
|
|
$previous_increment = $this->getIncrementPercentage($current_report['answer2'], $previous_report->answer2);
|
|
$first_increment = $this->getIncrementPercentage($current_report['answer2'], $first_report->answer2);
|
|
$previous_value = $previous_report->answer2;
|
|
$first_value = $first_report->answer2;
|
|
$report = MerchantReport::where('type', $question->type)
|
|
->where('type_id', $question->type_id)->first();
|
|
if(!empty($report)){
|
|
if($report->show_hint){
|
|
$hint_info = json_decode($report->hint, 1);
|
|
|
|
$title = isset($hint_info['positive_title'])?$hint_info['positive_title']:'';
|
|
$pic_url = isset($hint_info['positive_pic_url'])?$hint_info['positive_pic_url']:'';
|
|
if($previous_increment < 0){
|
|
$title = isset($hint_info['negative_title'])?$hint_info['negative_title']:'';
|
|
$pic_url = isset($hint_info['negative_pic_url'])?$hint_info['negative_pic_url']:'';
|
|
}elseif($previous_increment == 0){
|
|
$title = isset($hint_info['steady_title'])?$hint_info['steady_title']:'';
|
|
$pic_url = isset($hint_info['steady_pic_url'])?$hint_info['steady_pic_url']:'';
|
|
}
|
|
$hint['title'] = $title;
|
|
$hint['pic_url'] = $pic_url;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
$previous_hint = ($previous_increment>0)?'上升':($previous_increment == 0?'持平':'下降');
|
|
$first_hint = ($first_increment>0)?'上升':($first_increment == 0?'持平':'下降');
|
|
return [
|
|
'previous_increment' => $previous_hint.abs($previous_increment),
|
|
'first_increment' => $first_hint.abs($first_increment),
|
|
'previous_value' => $previous_value,
|
|
'first_value' => $first_value,
|
|
'hint' => $hint
|
|
];
|
|
}
|
|
|
|
//计算对比值的百分比,保留二位小数
|
|
private function getIncrementPercentage($left, $right){
|
|
$percent = 0;
|
|
if($right > 0){
|
|
$percent = number_format((($left - $right) / $right) * 100, 2);
|
|
}
|
|
return $percent;
|
|
}
|
|
|
|
//获得上一次的回答记录
|
|
private function getPreviousReport($current_report){
|
|
return ReportAnswer::where('question_id', $current_report['question_id'])
|
|
->where('m_user_id', $current_report['m_user_id'])
|
|
->where('m_order_id', $current_report['m_order_id'])
|
|
->where('commitd_at', '<', $current_report['commitd_at'])
|
|
->orderby('commitd_at', 'desc')
|
|
->select('id', 'answer2')
|
|
->first();
|
|
}
|
|
|
|
//获得第一次的回答记录
|
|
private function getFirstReport($current_report){
|
|
return ReportAnswer::where('question_id', $current_report['question_id'])
|
|
->where('m_user_id', $current_report['m_user_id'])
|
|
->where('m_order_id', $current_report['m_order_id'])
|
|
->where('commitd_at', '<', $current_report['commitd_at'])
|
|
->select('id', 'answer2')
|
|
->orderby('id', 'asc')
|
|
->first();
|
|
}
|
|
|
|
//查看
|
|
public function userAnswer(Request $request){
|
|
try {
|
|
$order_id = $request->order_id;
|
|
$commitd_at = $request->commitd_at;
|
|
$commitd_at2 = $request->commitd_at.' 00:00:00';
|
|
$order = TouristOrder::find($order_id);
|
|
//这个服务的所有题目
|
|
$questions = ReportQuestion::where('type',$order->type)->where('type_id',$order->type_id)->orderBy('id','asc')->get();
|
|
//这个服务用户提交的题目id
|
|
$question_ids = ReportAnswer::where('m_user_id',$request->merchant_user_id)->where('m_order_id',$order_id);
|
|
if($commitd_at){
|
|
$question_ids = $question_ids->where('commitd_at',$commitd_at);
|
|
}
|
|
$question_ids = $question_ids->pluck('question_id')->toArray();
|
|
foreach ($questions as $key => $question) {
|
|
//当前用户是否提交这个问题的答案
|
|
if(in_array($question->id,$question_ids)){
|
|
$answer = ReportAnswer::where('m_user_id',$request->merchant_user_id)->where('m_order_id',$order_id)->where('question_id',$question->id);
|
|
if($request->commitd_at){
|
|
$answer = $answer->where('commitd_at',$commitd_at2);
|
|
}
|
|
$answer = $answer->first();
|
|
// $question->answer = !empty($answer) ? $answer->answer : '';
|
|
// $question->pics = !empty($answer) ? json_decode($answer->pics,true) : [];
|
|
if(!empty($answer)){
|
|
$question->answer = $answer->answer;
|
|
$question->pics = !empty($answer->pics) ? json_decode($answer->pics,true) : [];
|
|
}else{
|
|
$question->answer = '';
|
|
$question->pics = [];
|
|
}
|
|
}else{
|
|
$question->answer = '';
|
|
$question->pics = [];
|
|
}
|
|
$question->title_answer = json_decode($question->title_answer,true);
|
|
if(in_array($question->input_type,['checkbox']) && isset($answer)){
|
|
$question->answer2 = json_decode($answer->answer2,true);
|
|
}elseif(isset($answer)){
|
|
$question->answer2 = $answer->answer2;
|
|
}else{
|
|
$question->answer2 = '';
|
|
}
|
|
unset($answer);
|
|
}
|
|
return $this->success('ok',$questions);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure();
|
|
}
|
|
}
|
|
|
|
//报告记录
|
|
public function reportRecords(Request $request){
|
|
try {
|
|
$m_user_id = $request->merchant_user_id;
|
|
$order_id = $request->order_id;
|
|
$order = TouristOrder::find($order_id);
|
|
$commitd_at = $request->commitd_at;
|
|
$report = MerchantReport::where('type',$order->type)->where('type_id',$order->type_id)->first();
|
|
$class = !empty($report) ? $report->class : 'one';
|
|
$records = ReportAnswer::with('order.activities')->where('m_user_id',$m_user_id)
|
|
->where('m_order_id',$order_id)->select('id','question_id','commitd_at','m_order_id','report_poster');
|
|
if($commitd_at){
|
|
$records = $records->where('commitd_at',$commitd_at)->first();
|
|
$records->commitd_at = date('Y-m-d',strtotime($records->commitd_at));
|
|
}else{
|
|
$records = $records->groupBy('commitd_at')->orderBy('commitd_at','desc')->paginate();
|
|
foreach ($records as $key => $record) {
|
|
$record->commitd_at = date('Y-m-d',strtotime($record->commitd_at));
|
|
}
|
|
$file = ReportFile::where('order_id', $order_id)->first();
|
|
$file_status = 0;
|
|
$file_url = '';
|
|
if ($file && empty($file->url)) {
|
|
$file_status = 1;
|
|
$file_url = $file->url;
|
|
}elseif ($file && $file->url) {
|
|
$file_status = 2;
|
|
$file_url = $file->url;
|
|
}
|
|
$records[0]['file_url'] = $file_url;
|
|
$records[0]['file_status'] = $file_status;
|
|
}
|
|
return $this->success('ok',compact('class','records'));
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure();
|
|
}
|
|
}
|
|
|
|
public function reportPoster(Request $request, $order_id)
|
|
{
|
|
try {
|
|
$order = TouristOrder::find($order_id);
|
|
if(!$order){
|
|
return $this->failure('订单不存在');
|
|
}
|
|
$desc = $order->desc;
|
|
$report = MerchantReport::where('type',$order->type)->where('type_id',$order->type_id)
|
|
->first();
|
|
$config = ReportMerchantUserConfig::where('order_id',$order_id)
|
|
->where('merchant_user_id',$request->merchant_user_id)
|
|
->first();
|
|
if($config){
|
|
$report_title = $config->report_title;
|
|
$report_pic = json_decode($config->report_pic,true)?:[];
|
|
$config_id = $config->id;
|
|
}else{
|
|
$report_pic = [];
|
|
$report_title = null;
|
|
$config_id = null;
|
|
}
|
|
$obj = (object)array();
|
|
if($config_id){
|
|
$obj->config_id = $config_id;
|
|
}else{
|
|
$obj->config_id = null;
|
|
}
|
|
$user_image = MerchantUserImage::where('type',MerchantUser::class)->where('type_id',$request->merchant_user_id)
|
|
->pluck('image_url')->toArray();
|
|
$report_pic = array_merge($report_pic,$user_image);
|
|
|
|
if(empty($report->share_pic)){
|
|
$pic = MerchantUserImage::DEFAULTPICS;
|
|
if($report_pic) {
|
|
$pic = array_merge($report_pic,$pic);
|
|
}
|
|
$obj->poster_pic = $pic;
|
|
}else{
|
|
$pic = json_decode($report->share_pic,true);
|
|
if(empty($pic)){
|
|
$pic = MerchantUserImage::DEFAULTPICS;
|
|
}
|
|
if($report_pic) {
|
|
$pic = array_merge($report_pic,$pic);
|
|
}
|
|
$obj->poster_pic = $pic;
|
|
}
|
|
if($report_title) {
|
|
$obj->poster_desc = $report_title;
|
|
}else {
|
|
if (empty($report->share_title)) {
|
|
$obj->poster_desc = "挑战运动极限,演绎健美人生";
|
|
|
|
} else {
|
|
$obj->poster_desc = $report->share_title;
|
|
}
|
|
}
|
|
$obj->poster_text = "今天我完成了【".$desc."】目标";
|
|
$wechatUser = session('wechat.oauth_user.new');
|
|
$openid = $wechatUser?$wechatUser->getId():'';
|
|
|
|
$key = 'ActivityDetail_S' . $order->type_id . '_' . $openid;
|
|
$qr_code = Redis::get($key);
|
|
if (!$qr_code) {
|
|
if($report && $report->share_jump_page && $report->share_jump_page == 'first'){
|
|
$url = env('APP_URL') . '/pu/#/information?merchant_id=' . $order->merchant_id;
|
|
}else{
|
|
$url = env('APP_URL') . '/pu/#/activityDetails/' . $order->type_id;
|
|
}
|
|
$url = urlencode($url);
|
|
$url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?from_openid='.$openid.'&merchant_id='.
|
|
$order->merchant_id.'&url='.$url;
|
|
$qr_code = $this->getPreviewQrcode($url);
|
|
Redis::set($key, $qr_code);
|
|
$qr_code = Redis::get($key);
|
|
}
|
|
$obj->current_time = date('Y-m-d H:i');
|
|
$obj->poster_qrcode = $qr_code;
|
|
$obj->merchant_user_name = $order->name;
|
|
return $this->success('ok', $obj);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure();
|
|
}
|
|
|
|
}
|
|
|
|
public function reportFile(Request $request, $order_id)
|
|
{
|
|
try {
|
|
$m_user_id = $request->merchant_user_id;
|
|
$order = TouristOrder::where('id', $order_id)->where('account_id', $m_user_id)->first();
|
|
if (empty($order)) throw new \Exception("生成文件失败,订单不存在");
|
|
ReportFile::updateOrCreate(['order_id'=>$order->id, 'user_id'=>$order->account_id, 'type'=>'activity', 'type_id'=>$order->type_id], ['url'=>'', 'file_type'=>'docx']);
|
|
$start_date = $request->input("start_date");
|
|
$end_date = $request->input('end_date');
|
|
\App\Jobs\ReportExport::dispatch($order, $start_date, $end_date)->onQueue('report.export');
|
|
return $this->success('ok');
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure();
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 用户设置更新配置
|
|
*/
|
|
public function createReportUserConfig(Request $request){
|
|
$config_id = $request->id;
|
|
$m_user_id = $request->merchant_user_id;
|
|
$order_id = $request->order_id;
|
|
if($order_id){
|
|
$config = ReportMerchantUserConfig::where('order_id',$order_id)->where('merchant_user_id',$m_user_id)
|
|
->first();
|
|
if(!$config){
|
|
$config = new ReportMerchantUserConfig();
|
|
}
|
|
}else{
|
|
$config = new ReportMerchantUserConfig();
|
|
|
|
}
|
|
if($request->report_title){
|
|
$config->report_title = $request->report_title;
|
|
}
|
|
if($request->report_pic){
|
|
$config->report_pic = json_encode($request->report_pic);
|
|
}
|
|
if($request->order_id){
|
|
$config->order_id = $request->order_id;
|
|
}
|
|
$config->merchant_user_id = $m_user_id;
|
|
$config->save();
|
|
return $this->success('ok');
|
|
}
|
|
}
|