219 lines
9.2 KiB
PHP
219 lines
9.2 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
use app\api\controller\Base;
|
|
use think\Db;
|
|
/**
|
|
* @title 文章机构管理
|
|
*/
|
|
class Organ extends Base
|
|
{
|
|
|
|
public function __construct(\think\Request $request = null) {
|
|
parent::__construct($request);
|
|
}
|
|
|
|
/**
|
|
* 获取文章机构
|
|
* @param file_url 文件地址
|
|
*/
|
|
public function lists(){
|
|
//获取参数
|
|
$aParam = empty($aParam) ? $this->request->post() : $aParam;
|
|
|
|
//获取文章ID
|
|
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
|
|
if(empty($iArticleId)){
|
|
return json_encode(['status' => 2,'msg' => 'Please select the article']);
|
|
}
|
|
//用户ID
|
|
$iUserId = empty($aParam['user_id']) ? 0 : $aParam['user_id'];
|
|
if(empty($iUserId)){
|
|
return json_encode(['status' => 2,'msg' => 'Please select author']);
|
|
}
|
|
$aWhere = ['article_id' => $iArticleId,'user_id' => $iUserId,'state' => ['in',[-1,3]]];
|
|
$aArticle = Db::name('article')->field('article_id')->where($aWhere)->find();
|
|
if(empty($aArticle)){
|
|
return json_encode(['status' => 2,'msg' => 'The article does not exist']);
|
|
}
|
|
if(empty($aArticle)){
|
|
return json_encode(['status' => 2,'msg' => 'The article does not exist']);
|
|
}
|
|
//查询文章机构
|
|
$aWhere = ['article_id' => $iArticleId,'state' => 0];
|
|
$aOragn = Db::name('article_organ')->field('organ_id,article_id,organ_name,sort')->where($aWhere)->select();
|
|
return json_encode(['status' => 1,'data' => $aOragn]);
|
|
}
|
|
/**
|
|
* 添加文章机构
|
|
* @param file_url 文件地址
|
|
*/
|
|
public function add(){
|
|
//获取参数
|
|
$aParam = empty($aParam) ? $this->request->post() : $aParam;
|
|
|
|
//获取文章ID
|
|
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
|
|
if(empty($iArticleId)){
|
|
return json_encode(['status' => 2,'msg' => 'Please select the article']);
|
|
}
|
|
//用户ID
|
|
$iUserId = empty($aParam['user_id']) ? '' : $aParam['user_id'];
|
|
if(empty($iUserId)){
|
|
return json_encode(['status' => 2,'msg' => 'Please select author']);
|
|
}
|
|
//机构名称
|
|
$sOrganName = empty($aParam['organ_name']) ? '' : $aParam['organ_name'];
|
|
if(empty($sOrganName)){
|
|
return json_encode(['status' => 2,'msg' => 'Please enter the name of the institution']);
|
|
}
|
|
|
|
$aWhere = ['article_id' => $iArticleId,'user_id' => $iUserId,'state' => ['in',[-1,3]]];
|
|
$aArticle = Db::name('article')->field('article_id')->where($aWhere)->find();
|
|
if(empty($aArticle)){
|
|
return json_encode(['status' => 2,'msg' => 'The article does not exist']);
|
|
}
|
|
//查询文章机构
|
|
$aWhere = ['article_id' => $iArticleId,'state' => 0,'organ_name' => trim($sOrganName)];
|
|
$aOragn = Db::name('article_organ')->field('organ_id,article_id,organ_name,sort')->where($aWhere)->find();
|
|
if(!empty($aOragn)){
|
|
return json_encode(['status' => 1,'msg' => 'Article organization already exists','data' => $aOragn]);
|
|
}
|
|
|
|
//插入机构表
|
|
$aWhere = ['article_id' => $iArticleId,'state' => 0];
|
|
$aOragn = Db::name('article_organ')->field('max(sort) as sort')->where($aWhere)->find();
|
|
$iSort = empty($aOragn['sort']) ? 0 : $aOragn['sort'];
|
|
$iSort += 1;
|
|
$aInsert = ['article_id' => $iArticleId,'organ_name' => $sOrganName,'create_time' => time(),'sort' => $iSort];
|
|
$iId = Db::name('article_organ')->insertGetId($aInsert);
|
|
if(empty($iId)){
|
|
return json_encode(['status' => 3,'msg' => 'Failed to add article organization']);
|
|
}
|
|
$aInsert['organ_id'] = $iId;
|
|
return json_encode(['status' => 1,'msg' => 'success','data' => $aInsert]);
|
|
}
|
|
/**
|
|
* 修改文章机构
|
|
* @param file_url 文件地址
|
|
*/
|
|
public function modify(){
|
|
//获取参数
|
|
$aParam = empty($aParam) ? $this->request->post() : $aParam;
|
|
|
|
//获取文章ID
|
|
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
|
|
if(empty($iArticleId)){
|
|
return json_encode(['status' => 2,'msg' => 'Please select the article']);
|
|
}
|
|
//用户ID
|
|
$iUserId = empty($aParam['user_id']) ? '' : $aParam['user_id'];
|
|
if(empty($iUserId)){
|
|
return json_encode(['status' => 2,'msg' => 'Please select author']);
|
|
}
|
|
|
|
//机构名称
|
|
$sOrganName = empty($aParam['organ_name']) ? '' : $aParam['organ_name'];
|
|
if(empty($sOrganName)){
|
|
return json_encode(['status' => 2,'msg' => 'Please enter the name of the institution']);
|
|
}
|
|
|
|
//获取机构ID
|
|
$iOrganId = empty($aParam['organ_id']) ? 0 : $aParam['organ_id'];
|
|
if(empty($iOrganId)){
|
|
return json_encode(['status' => 2,'msg' => 'Please select the institution to modify']);
|
|
}
|
|
|
|
//查询文章
|
|
$aWhere = ['article_id' => $iArticleId,'user_id' => $iUserId,'state' => ['in',[-1,3]]];
|
|
$aArticle = Db::name('article')->field('article_id')->where($aWhere)->find();
|
|
if(empty($aArticle)){
|
|
return json_encode(['status' => 2,'msg' => 'The article does not exist']);
|
|
}
|
|
//查询文章机构
|
|
$aWhere = ['article_id' => $iArticleId,'state' => 0,'organ_id' => $iOrganId];
|
|
$aOragn = Db::name('article_organ')->field('organ_id,article_id,organ_name,sort')->where($aWhere)->find();
|
|
if(empty($aOragn)){
|
|
return json_encode(['status' => 3,'msg' => 'Article organization does not exist','data' => $aOragn]);
|
|
}
|
|
//查询是否有重名
|
|
$aWhere = ['article_id' => $iArticleId,'state' => 0,'organ_id' => ['<>',$iOrganId],'organ_name' => trim($sOrganName)];
|
|
$aOragn = Db::name('article_organ')->field('organ_id')->where($aWhere)->find();
|
|
if(!empty($aOragn)){
|
|
return json_encode(['status' => 1,'msg' => 'Article organization already exists','data' => $aOragn]);
|
|
}
|
|
|
|
//更新机构
|
|
$aWhere = ['organ_id' => $iOrganId];
|
|
$aUpdate = ['organ_name' => $sOrganName,'update_time' => time()];
|
|
$result = Db::name('article_organ')->where($aWhere)->limit(1)->update($aUpdate);
|
|
if($result === false){
|
|
return json_encode(['status' => 3,'msg' => 'Failed to update article organization']);
|
|
}
|
|
return json_encode(['status' => 1,'msg' => 'success']);
|
|
}
|
|
/**
|
|
* 删除文章机构
|
|
* @param file_url 文件地址
|
|
*/
|
|
public function remove(){
|
|
//获取参数
|
|
$aParam = empty($aParam) ? $this->request->post() : $aParam;
|
|
|
|
//获取文章ID
|
|
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
|
|
if(empty($iArticleId)){
|
|
return json_encode(['status' => 2,'msg' => 'Please select the article']);
|
|
}
|
|
//用户ID
|
|
$iUserId = empty($aParam['user_id']) ? '' : $aParam['user_id'];
|
|
if(empty($iUserId)){
|
|
return json_encode(['status' => 2,'msg' => 'Please select author']);
|
|
}
|
|
|
|
//获取机构ID
|
|
$iOrganId = empty($aParam['organ_id']) ? 0 : $aParam['organ_id'];
|
|
if(empty($iOrganId)){
|
|
return json_encode(['status' => 2,'msg' => 'Please select the institution to modify']);
|
|
}
|
|
|
|
//查询文章
|
|
$aWhere = ['article_id' => $iArticleId,'user_id' => $iUserId,'state' => ['in',[-1,3]]];
|
|
$aArticle = Db::name('article')->field('article_id')->where($aWhere)->find();
|
|
if(empty($aArticle)){
|
|
return json_encode(['status' => 2,'msg' => 'The article does not exist']);
|
|
}
|
|
//查询文章机构
|
|
$aWhere = ['article_id' => $iArticleId,'state' => 0,'organ_id' => $iOrganId];
|
|
$aOragn = Db::name('article_organ')->field('organ_id,article_id,organ_name,sort')->where($aWhere)->find();
|
|
if(empty($aOragn)){
|
|
return json_encode(['status' => 3,'msg' => 'Article organization does not exist','data' => $aOragn]);
|
|
}
|
|
|
|
//作者关联的机构
|
|
$aWhere = ['organ_id' => $iOrganId,'article_id' => $iArticleId,'state' => 0];
|
|
$aId = Db::name('article_author_organ')->where($aWhere)->column('id');
|
|
|
|
//删除机构
|
|
Db::startTrans();
|
|
//更新机构状态
|
|
$aWhere = ['organ_id' => $iOrganId];
|
|
$aUpdate = ['state' => 1,'update_time' => time()];
|
|
$result = Db::name('article_organ')->where($aWhere)->limit(1)->update($aUpdate);
|
|
if($result === false){
|
|
return json_encode(['status' => 3,'msg' => 'Failed to update article organization']);
|
|
}
|
|
//更新作者关联的机构
|
|
if(!empty($aId)){
|
|
$aWhere = ['id' => ['in',$aId],'state' => 0];
|
|
$aUpdate = ['state' => 1,'update_time' => time()];
|
|
$author_organ_result = Db::name('article_author_organ')->where($aWhere)->limit(count($aId))->update($aUpdate);
|
|
if($author_organ_result === false){
|
|
return json_encode(['status' => 3,'msg' => 'Failed to unbind the relationship between the institution and the author']);
|
|
}
|
|
}
|
|
Db::commit();
|
|
return json_encode(['status' => 1,'msg' => 'success']);
|
|
}
|
|
}
|