363 lines
15 KiB
PHP
363 lines
15 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Admin;
|
||
use App\Models\Live\Anchor;
|
||
use App\Models\ProfileCourtship;
|
||
use App\Models\Server\UserLabel;
|
||
use App\Models\Server\UserLabelRecode;
|
||
use App\Models\Server\UserLabelScoreRecode;
|
||
use App\Models\User;
|
||
use Illuminate\Http\JsonResponse;
|
||
use Illuminate\Http\Request;
|
||
use App\Http\Controllers\Controller;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
|
||
class UserLabelController extends Controller
|
||
{
|
||
/**
|
||
* 批量创建标签
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function createLabelByBatch(Request $request){
|
||
try {
|
||
$lables = $request->all();
|
||
DB::beginTransaction();
|
||
if (is_array($lables)) {
|
||
$title_array = [];
|
||
foreach ($lables as $key => $val) {
|
||
$userLabel = UserLabel::where('title', $val['title'])->where('user_type', Anchor::class)
|
||
->where('user_id', 592)
|
||
->first();
|
||
if(!preg_match("/^[0-9][0-9]*$/" ,$val['value'])){
|
||
return $this->failure('分值必须为0或正整数');
|
||
}
|
||
if($val['value'] >10 || $val['value'] <0){
|
||
return $this->failure('分值不可以大于10分或负分,请重新设置');
|
||
}
|
||
if($val['type'] != '+' && $val['type'] != '-'){
|
||
return $this->failure('type必须为+或-');
|
||
}
|
||
if (!$userLabel) {
|
||
$userLabel = new UserLabel();
|
||
$userLabel->user_type = Anchor::class;
|
||
$userLabel->user_id = 592;
|
||
$userLabel->title = $val['title'];
|
||
$userLabel->type = $val['type'];
|
||
$userLabel->value = $val['value'];
|
||
$userLabel->save();
|
||
} else {
|
||
DB::rollBack();
|
||
return $this->failure('标签'. $val['title'].'已经存在');
|
||
/**
|
||
$userLabel->type = $val['type'];
|
||
$userLabel->value = $val['value'];
|
||
$userLabel->save();
|
||
* **/
|
||
}
|
||
$title_array[] = $val['title'];
|
||
}
|
||
$db_title_array = UserLabel::where('user_type', Anchor::class)
|
||
->where('user_id', 592)
|
||
->pluck('title')
|
||
->toArray();
|
||
if (is_array($title_array) && is_array($db_title_array)) {
|
||
$diff_array = array_diff($db_title_array, $title_array);
|
||
if (is_array($diff_array)) {
|
||
foreach ($diff_array as $array_key => $array_val) {
|
||
$userLabel = UserLabel::where('user_type', Anchor::class)
|
||
->where('user_id', 592)
|
||
->where('title', $array_val)
|
||
->first();
|
||
if ($userLabel) {
|
||
$userLabelRecodes = UserLabelRecode::where('user_type', User::class)
|
||
->where('label_id', $userLabel->id)
|
||
->get();
|
||
//读取原始分值
|
||
$user = User::where('id',$userLabelRecodes->user_id)
|
||
->first();
|
||
foreach ($userLabelRecodes as $key => $val) {
|
||
if($userLabel->type == '+'){
|
||
$variation_score = -$userLabel->value;
|
||
$final_score = $user->score - $userLabel->value;
|
||
}else{
|
||
$variation_score = $userLabel->value;
|
||
$final_score = $user->score + $userLabel->value;
|
||
}
|
||
$userLabelScoreRecode = new UserLabelScoreRecode();
|
||
$userLabelScoreRecode->user_type = User::class;
|
||
$userLabelScoreRecode->label_id = $userLabel->id;
|
||
$userLabelScoreRecode->user_id = $user->id;
|
||
$userLabelScoreRecode->reason = '标签删除';
|
||
$userLabelScoreRecode->source_score = $user->source ?? 0;
|
||
$userLabelScoreRecode->variation_score = $variation_score;
|
||
$userLabelScoreRecode->final_score = $final_score;
|
||
$userLabelScoreRecode->save();
|
||
$user->score = $final_score;
|
||
$user->save();
|
||
}
|
||
UserLabelRecode::where('user_type', User::class)
|
||
->where('label_id', $userLabel->id)
|
||
->delete();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
DB::commit();
|
||
return $this->success('ok');
|
||
}catch (\Exception $e) {
|
||
DB::rollBack();
|
||
$this->getError($e);
|
||
return $this->failure('操作失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 创建单个标签
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function createLabelBySingle(Request $request){
|
||
try {
|
||
$title = $request->title;
|
||
$type = $request->type;
|
||
$value = $request->value;
|
||
if (config('app.debug')) {
|
||
if (!$title || !$type || !isset($value)) {
|
||
return $this->failure('参数不全');
|
||
}
|
||
}
|
||
if(!preg_match("/^[0-9][0-9]*$/" ,$value)){
|
||
return $this->failure('分值必须为0或正整数');
|
||
}
|
||
if($value >10 || $value <0){
|
||
return $this->failure('分值不可以大于10分或负分,请重新设置');
|
||
}
|
||
$userLabel = UserLabel::where('title', $title)->where('user_type', Anchor::class)
|
||
->where('user_id', 592)
|
||
->first();
|
||
if (!$userLabel) {
|
||
$userLabel = new UserLabel();
|
||
$userLabel->user_type = Anchor::class;
|
||
$userLabel->user_id = 592;
|
||
$userLabel->title = $title;
|
||
$userLabel->type = $type;
|
||
$userLabel->value = $value;
|
||
$userLabel->save();
|
||
} else {
|
||
return $this->failure('标签 '. $title.' 已经存在');
|
||
/**
|
||
$userLabel->type = $type;
|
||
$userLabel->value = $value;
|
||
$userLabel->save();
|
||
* **/
|
||
}
|
||
return $this->success('ok');
|
||
}catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('操作失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 查询商家标签
|
||
* @param Request $request
|
||
* @return JsonResponse
|
||
*/
|
||
public function getLabel(Request $request){
|
||
try{
|
||
$nopage = $request->nopage;
|
||
$keyword = $request->keyword;
|
||
$lables = UserLabel::where('user_type', Anchor::class)
|
||
->where('user_id', 592);
|
||
if($keyword){
|
||
$lables = $lables->where('title','like',"%$keyword%");
|
||
}
|
||
if($nopage){
|
||
$lables = $lables->get();
|
||
}else{
|
||
$lables = $lables->paginate();
|
||
}
|
||
return $this->success('ok',$lables);
|
||
}catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('操作失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除标签
|
||
* @param Request $request
|
||
* @return JsonResponse
|
||
*/
|
||
public function deleteSingleTabel(Request $request){
|
||
try {
|
||
DB::beginTransaction();
|
||
$label_id = $request->label_id;
|
||
if (config('app.debug')) {
|
||
if (!$label_id) {
|
||
return $this->failure('label_id不能为空');
|
||
}
|
||
}
|
||
$userLabel = UserLabel::where('id', $label_id)->where('user_type', Anchor::class)
|
||
->where('user_id', 592)
|
||
->first();
|
||
$userLabelRecode = UserLabelRecode::where('user_type', User::class)
|
||
->where('label_id', $userLabel->id)
|
||
->get();
|
||
foreach ($userLabelRecode as $key => $val){
|
||
$user = User::where('id',$val->user_id)
|
||
->first();
|
||
if($userLabel->type == '+'){
|
||
$variation_score = -$userLabel->value;
|
||
$final_score = $user->score - $userLabel->value;
|
||
}else{
|
||
$variation_score = $userLabel->value;
|
||
$final_score = $user->score + $userLabel->value;
|
||
}
|
||
$userLabelScoreRecode = new UserLabelScoreRecode();
|
||
$userLabelScoreRecode->user_type = User::class;
|
||
$userLabelScoreRecode->label_id = $userLabel->id;
|
||
$userLabelScoreRecode->user_id = $user->id;
|
||
$userLabelScoreRecode->reason = '标签删除';
|
||
$userLabelScoreRecode->source_score = $user->source ?? 0;
|
||
$userLabelScoreRecode->variation_score = $variation_score;
|
||
$userLabelScoreRecode->final_score = $final_score;
|
||
$userLabelScoreRecode->save();
|
||
$user->score = $final_score;
|
||
$user->save();
|
||
}
|
||
UserLabelRecode::where('user_type', User::class)
|
||
->where('label_id', $userLabel->id)
|
||
->delete();
|
||
$userLabel->delete();
|
||
DB::commit();
|
||
return $this->success('ok');
|
||
}catch (\Exception $e) {
|
||
DB::rollBack();
|
||
$this->getError($e);
|
||
return $this->failure('操作失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 给用户加标签
|
||
*/
|
||
public function addLabelToUser(Request $request){
|
||
try {
|
||
DB::beginTransaction();
|
||
$data = $request->all();
|
||
if (config('app.debug')) {
|
||
if (!$data['user_id'] || empty($data['label_id'])) {
|
||
return $this->failure('label_id不能为空');
|
||
}
|
||
}
|
||
$user = User::find($data['user_id']);
|
||
if (!$user) {
|
||
return $this->failure('用户不存在');
|
||
}
|
||
foreach ($data['label_id'] as $key => $val) {
|
||
$label_recodes = UserLabelRecode::where('user_type', User::class)->where('user_id', $user->id)
|
||
->where('label_id', $val)
|
||
->first();
|
||
if ($label_recodes) {
|
||
continue;
|
||
}
|
||
$label_recodes = new UserLabelRecode();
|
||
$label_recodes->user_type = User::class;
|
||
$label_recodes->user_id = $user->id;
|
||
$label_recodes->label_id = $val;
|
||
$label_recodes->save();
|
||
$userLabel = UserLabel::where('user_type', Anchor::class)->where('user_id', 592)
|
||
->where('id', $val)
|
||
->first();
|
||
if ($userLabel->type == '+') {
|
||
$variation_score = $userLabel->value;
|
||
$final_score = $user->score + $userLabel->value;
|
||
} else {
|
||
$variation_score = -$userLabel->value;
|
||
$final_score = $user->score - $userLabel->value;
|
||
}
|
||
|
||
$label_score_recode = new UserLabelScoreRecode();
|
||
$label_score_recode->user_type = User::class;
|
||
$label_score_recode->user_id = $user->id;
|
||
$label_score_recode->label_id = $val;
|
||
$label_score_recode->reason = '添加标签';
|
||
$label_score_recode->source_score = $user->score ?? 0;
|
||
$label_score_recode->variation_score = $variation_score;
|
||
$label_score_recode->final_score = $final_score;
|
||
$label_score_recode->save();
|
||
|
||
$user->score = $final_score;
|
||
$user->save();
|
||
}
|
||
//删除用户原有的标签
|
||
$user_label_array = UserLabelRecode::where('user_type', User::class)->where('user_id', $user->id)
|
||
->pluck('label_id')->toArray();
|
||
$diff_array = array_diff($user_label_array,$data['label_id']);
|
||
foreach ($diff_array as $key => $val){
|
||
$userLabel = UserLabel::where('user_type', Anchor::class)->where('user_id', 592)
|
||
->where('id', $val)
|
||
->first();
|
||
if ($userLabel->type == '+') {
|
||
$variation_score = -$userLabel->value;
|
||
$final_score = $user->score - $userLabel->value;
|
||
} else {
|
||
$variation_score = $userLabel->value;
|
||
$final_score = $user->score + $userLabel->value;
|
||
}
|
||
$label_score_recode = new UserLabelScoreRecode();
|
||
$label_score_recode->user_type = User::class;
|
||
$label_score_recode->user_id = $user->id;
|
||
$label_score_recode->label_id = $val;
|
||
$label_score_recode->reason = '标签删除';
|
||
$label_score_recode->source_score = $user->score ?? 0;
|
||
$label_score_recode->variation_score = $variation_score;
|
||
$label_score_recode->final_score = $final_score;
|
||
$label_score_recode->save();
|
||
|
||
$user->score = $final_score;
|
||
$user->save();
|
||
}
|
||
UserLabelRecode::where('user_type', User::class)->where('user_id', $user->id)->whereIn('label_id',$diff_array)
|
||
->delete();
|
||
DB::commit();
|
||
return $this->success('ok');
|
||
}catch (\Exception $e) {
|
||
DB::rollBack();
|
||
$this->getError($e);
|
||
return $this->failure('操作失败');
|
||
}
|
||
}
|
||
|
||
public function procUserMateConditon(Request $request){
|
||
$mate_condtion_ids = ProfileCourtship::whereNotNull('mate_conditon')
|
||
->select('id','mate_conditon')
|
||
->get();
|
||
foreach ($mate_condtion_ids as $key => $val){
|
||
$mate = json_decode($val->mate_conditon,true);
|
||
if(isset($mate['year_income'])){
|
||
unset($mate['year_income']);
|
||
}
|
||
if(isset($mate['location_province'])){
|
||
unset($mate['location_province']);
|
||
}
|
||
if(isset($mate['location_city'])){
|
||
unset($mate['location_city']);
|
||
}
|
||
if(isset($mate['state'])){
|
||
unset($mate['state']);
|
||
}
|
||
if(isset($mate['weight'])){
|
||
unset($mate['weight']);
|
||
}
|
||
$json_mate = json_encode($mate);
|
||
$val->mate_conditon = $json_mate;
|
||
$val->save();
|
||
}
|
||
}
|
||
}
|