WokerTask/app/common/model/informationg/UserInformationg.php

200 lines
7.2 KiB
PHP
Raw Normal View History

2023-12-27 14:06:33 +08:00
<?php
namespace app\common\model\informationg;
use app\common\model\BaseModel;
use think\facade\Db;
use think\facade\Log;
use app\common\logic\BaseLogic;
use app\common\model\Company;
use app\common\model\user\User;
use app\job\AiAianalyse;
class UserInformationg extends BaseModel
{
/**
* @notes 关联商机信息
* @return \think\model\relation\HasOne
* @author likeadmin
* @date 2023/08/01 15:00
*/
public function informationInfo()
{
return $this->hasOne(\app\common\model\informationg\UserInformationgDemand::class, 'information_id', 'id');
}
public function getExtendAttr($valur, $data)
{
$company_name = Company::where('id', $data['company_id'])->value('company_name');
$nickname = User::where('id', $data['create_user_id'])->value('nickname');
return [
'company_name' => $company_name,
'nickname' => $nickname
];
}
public static function list($param, $page = 1, $size = 10)
{
return self::where($param)->with('company')
->field('id,area_id,area_id as area_name,street_id,street_id as street_name,village_id,village_id as village_name, street_id as street_name,brigade_id,brigade_id as brigade_name,name,phone,age,address,create_time,update_time')
->order('update_time', 'desc')
->page($page)->limit($size)->select();
}
public static function add($param)
{
$family_json = json_encode($param['family']);
$child_arr_json = json_encode($param['child_arr']);
// 插入数据
$data = [
'create_user_id' => $param['admin_id'],
'company_id' => $param['company_id'],
'name' => $param['name'],
'phone' => $param['phone'],
'age' => $param['age'],
'sex' => $param['sex'],
'id_card' => $param['id_card'],
'wechat' => $param['wechat'],
'area_id' => $param['area_id'],
'street_id' => $param['street_id'],
'village_id' => $param['village_id'],
'brigade_id' => $param['brigade_id'],
'address' => $param['address'],
'family' => $family_json,
'child' => $param['child'],
'child_arr' => $child_arr_json,
];
Db::startTrans();
try {
$result = self::create($data);
// foreach ($param['datas'] as $k => $v) {
// self::informationg_demand($v, $result['id'], $param['admin_id']);
// }
// switch($param['data_type']){
// case 1:
// self::informationg_demand($param,$result['id']);
// break;
// }
Db::commit();
} catch (\Exception $e) {
Db::rollback();
BaseLogic::setError($e->getMessage());
return false;
}
return true;
}
public static function informationg_demand($param, $id, $admin_id)
{
$data_field = [];
if (isset($param['card_id']) && $param['card_id'] > 0) {
$category_info = Db::name('category_business')->where('id', $param['card_id'])->field(['pid', 'data_field'])->find();
$category_child = $param['card_id'];
$field_array = json_decode($category_info['data_field'], true);
if (!empty($field_array) && is_array($field_array)) {
// 拼装词语
foreach($param['datas'] as $k => $v) {
if (!empty($field_array[$k]['text'])) {
$key = $field_array[$k]['text'];
if (!empty($field_array[$k]['enum'])) {
$data_field[$key] = $field_array[$k]['enum'][$v] ?? '';
} else {
$data_field[$key] = $v;
}
}
}
}
} else {
$category_id = 0;
$category_child = 0;
}
$data = [
'create_user_id' => $admin_id,
'category_id' => $category_info['pid'] ?? 0,
'category_child' => $category_child,
'data' => json_encode($param['datas']),
'data_field' => json_encode($data_field),
'create_time' => time(),
'update_time' => time(),
'status' => 1,
'information_id' => $id,
];
$res = UserInformationgDemand::create($data);
if ($res) {
queue(AiAianalyse::class, $data);
}
return $res;
}
public static function details($id)
{
$item = self::where('id', $id)->field('*,area_id area_name,street_id street_name,village_id village_name,brigade_id brigade_name')->find();
$item['family'] = json_decode($item['family'], true);
$item['child_arr'] = json_decode($item['child_arr'], true);
$data = UserInformationgDemand::where('information_id', $id)->order('id', 'desc')->select()->toArray();
$item['datas'] = [];
$datas = [];
if ($data) {
foreach ($data as $k => $v) {
$arr = [
'id' => $v['category_child'],
'update_time' => $v['update_time'],
'datas' => $v['data'],
'data_field' => json_decode($v['data_field']),
'ai_question' => $v['ai_question'],
'ai_aianalyse' => $v['ai_aianalyse'],
];
if ($v['data']) {
array_push($datas, $arr);
}
}
$item['datas'] = $datas;
}
return $item;
}
public static function business_opportunity($informationg_id_array)
{
$demand_id_array = UserInformationgDemand::whereIn('information_id', $informationg_id_array)->where('status', 1)->field(['max(id) as demand_id'])->group('information_id')->select()->column('demand_id');
$data = UserInformationgDemand::whereIn('id', $demand_id_array)->column('*', 'information_id');
foreach($data as &$item) {
$item['data'] = json_decode($item['data'], true);
$item['data_field'] = json_decode($item['data_field'], true);
$item['relation_goods'] = [];
}
unset($item);
return $data;
}
public function company()
{
return $this->hasOne(Company::class, 'id', 'company_id')->field(['id', 'company_name', 'admin_id']);
}
public function getCityNameAttr($value)
{
return Db::name('geo_city')->where(['city_code' => $this->city])->value('city_name');
}
public function getAreaNameAttr($value)
{
return Db::name('geo_area')->where(['area_code' => $this->area_id])->value('area_name');
}
public function getStreetNameAttr($value)
{
return Db::name('geo_street')->where(['street_code' => $this->street_id])->value('street_name');
}
public function getVillageNameAttr($value)
{
return Db::name('geo_village')->where(['village_code' => $this->village_id])->value('village_name');
}
public function getBrigadeNameAttr($value)
{
return Db::name('geo_brigade')->where('id', $this->brigade_id)->value('brigade_name');
}
}