init: 项目初始代码
This commit is contained in:
222
app/api/model/Question.php
Normal file
222
app/api/model/Question.php
Normal file
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
namespace app\api\model;
|
||||
|
||||
use think\facade\Env;
|
||||
|
||||
/**
|
||||
* 用户反馈模型
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class Question extends BaseModel
|
||||
{
|
||||
protected $schema = [
|
||||
'id' => 'int',
|
||||
'platform_id' => 'int',
|
||||
'language_id' => 'int',
|
||||
'site_id' => 'int',
|
||||
'area_id' => 'int',
|
||||
'country_id' => 'int',
|
||||
'name' => 'string',
|
||||
'email' => 'string',
|
||||
'phone' => 'string',
|
||||
'order_sn' => 'string',
|
||||
'product_model' => 'string',
|
||||
'os' => 'string',
|
||||
'desc' => 'string',
|
||||
'is_done' => 'int',
|
||||
'is_send_email' => 'int',
|
||||
'is_send_status' => 'int',
|
||||
'send_time' => 'int',
|
||||
'send_details' => 'string',
|
||||
'create_time' => 'int',
|
||||
'update_time' => 'int'
|
||||
];
|
||||
|
||||
/**
|
||||
* 反馈附件关联
|
||||
* @return \think\model\relation\HasMany
|
||||
*/
|
||||
public function QuestionAttachment()
|
||||
{
|
||||
return $this->hasMany(QuestionAttachment::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列表
|
||||
* @param array $data 查询条件
|
||||
* @param mixed $simple 模糊搜索条件
|
||||
* @param mixed $focus 精准搜索条件
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getAllList($data,$simple = null,$focus = null,$sort = null,$field = null)
|
||||
{
|
||||
$fields = array_keys($this->schema);
|
||||
$collection = $this->field($field)->where(function ($query) use ($data,$fields,$simple,$focus) {
|
||||
if (in_array('platform_id',$fields)) {
|
||||
$query->where('platform_id',$data['platform_id']);
|
||||
}
|
||||
//模糊查询条件
|
||||
if (!empty($simple)) {
|
||||
foreach ($simple as $key => $item) {
|
||||
if (in_array($key,$fields)) {
|
||||
$query->where($key,'like',"%".$item."%");
|
||||
}
|
||||
}
|
||||
}
|
||||
//精准查询
|
||||
if (!empty($focus)) {
|
||||
foreach ($focus as $key => $item) {
|
||||
if (in_array($key,$fields)) {
|
||||
$query->where($key,$item);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (isset($data['page']) && isset($data['size'])) {
|
||||
$count = $collection->count() ?: 0;
|
||||
$page = $collection->limit($data['size'])->page($data['page'])->order($sort)->select()->toArray();
|
||||
if (!empty($page)) {
|
||||
foreach ($page as $key => $item) {
|
||||
$lang = Language::find($data['site_id']);
|
||||
if (!empty($lang)) {
|
||||
if (strtolower($lang['code']) == 'zh-cn' || strtolower($lang['code']) == 'zh_cn') {
|
||||
$page[$key]['country_name'] = '中国';
|
||||
} else {
|
||||
$page[$key]['country_name'] = $this->getCountryName($item['country_id']);
|
||||
}
|
||||
}
|
||||
|
||||
$page[$key]['is_done_name'] = $item['is_done'] == 0 ? '未解决': '已解决';
|
||||
}
|
||||
}
|
||||
return ['page'=>$page,'count'=>$count];
|
||||
} else {
|
||||
$page = $collection->order($sort)->select()->toArray();
|
||||
if (!empty($page)) {
|
||||
foreach ($page as $key => $item) {
|
||||
$page[$key]['country_name'] = $this->getCountryName($item['country_id']);
|
||||
$page[$key]['is_done_name'] = $item['is_done'] == 0 ? '未解决': '已解决';
|
||||
}
|
||||
}
|
||||
return $page;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增数据
|
||||
* @param $data
|
||||
* @return bool|void
|
||||
*/
|
||||
public function addData($data)
|
||||
{
|
||||
$this->startTrans();
|
||||
$question = $this->create($data);
|
||||
//处理数据结构
|
||||
foreach ($data['question_attachment'] as $k => $value) {
|
||||
$value['question_id'] = $question->id;
|
||||
}
|
||||
//模型关联新增
|
||||
$result = $question->QuestionAttachment()->saveAll($data['question_attachment']);
|
||||
if ($result && $question) {
|
||||
$this->commit();
|
||||
return true;
|
||||
} else {
|
||||
$this->rollback();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取详情
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getInfo($data)
|
||||
{
|
||||
$question = $this->find($data['id']);
|
||||
$question['question_attachment'] = $this->QuestionAttachment()->where('question_id',$data['id'])->select()->toArray();
|
||||
if (!empty($question['question_attachment'])) {
|
||||
$newAttach = [];
|
||||
foreach ($question['question_attachment'] as $key1 => $value1) {
|
||||
$newAttach[$key1]['name'] = basename($value1['attach']);
|
||||
// $newAttach[$key1]['url'] = Env::get('QINIU.cdn').$value1['image'];
|
||||
$newAttach[$key1]['url'] = $value1['attach'];
|
||||
}
|
||||
$question['question_attachment'] = $newAttach;
|
||||
}
|
||||
return $question;
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 编辑数据
|
||||
// * @param $data
|
||||
// * @return bool
|
||||
// * @throws \think\db\exception\DataNotFoundException
|
||||
// * @throws \think\db\exception\DbException
|
||||
// * @throws \think\db\exception\ModelNotFoundException
|
||||
// */
|
||||
// public function editData($data)
|
||||
// {
|
||||
// $question = $this->find($data['id']);
|
||||
// $result1 = $question->save($data);
|
||||
// //先将原先的全部删除
|
||||
// $result2 = $question->QuestionAttachment->delete();
|
||||
// $result = false;
|
||||
// if ($result1 && $result2) {
|
||||
// //处理数据结构
|
||||
// $newData = [];
|
||||
// foreach ($data['question_attachment'] as $key => $value) {
|
||||
// $newData[$key]['question_id'] = $data['id'];
|
||||
// $newData[$key]['attach'] = $value['url'];
|
||||
// }
|
||||
// $data['question_attachment'] = $newData;
|
||||
// //新增
|
||||
// $result = $question->QuestionAttachment()->saveAll($data['question_attachment']);
|
||||
// }
|
||||
// return $result;
|
||||
// }
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
* @param $data
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function delData($data)
|
||||
{
|
||||
$this->startTrans();
|
||||
$question = $this->find($data['id']);
|
||||
$result1 = $question->delete();
|
||||
//先将原先的全部删除
|
||||
$result2 = $question->QuestionAttachment->delete();;
|
||||
if ($result1 && $result2) {
|
||||
$this->commit();
|
||||
return true;
|
||||
} else {
|
||||
$this->rollback();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解决-未解决
|
||||
* @param $data
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function isDone($data)
|
||||
{
|
||||
$question = $this->find($data['id']);
|
||||
return $question->save($data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user