2026-03-10 18:45:36 +08:00
< ? php
namespace app\api\controller ;
use think\Config ;
use think\Controller ;
use think\Db ;
use think\Log ;
use think\Model ;
use think\Request ;
use think\cache\driver\Memcache ;
header ( " Access-Control-Allow-Origin:* " );
header ( " Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE " );
header ( " Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding " );
class Contract extends Common
{
//获取运营合同类型列表
public function GetOperationContractTypeList ()
{
$FirstLeveType = db ( 'contract_type' ) -> where ( 'super_type_id' , 0 ) -> where ( 'type_id' , '>' , 10 ) -> select ();
for ( $i = 0 ; $i < count ( $FirstLeveType ); $i ++ ) {
$SecondLeveType = db ( 'contract_type' ) -> where ( 'super_type_id' , $FirstLeveType [ $i ][ 'type_id' ]) -> select ();
for ( $m = 0 ; $m < count ( $SecondLeveType ); $m ++ )
{
$count = db ( 'contract_info' ) -> where ( 'contract_type' , $SecondLeveType [ $m ][ 'type_id' ]) -> where ( 'del_time' , null ) -> count ();
if ( $count > 0 )
{
$SecondLeveType [ $m ][ 'type_name' ] .= '【' . $count . '】' ;
}
}
for ( $j = 0 ; $j < count ( $SecondLeveType ); $j ++ ) {
$ThridLeveType = db ( 'contract_type' ) -> where ( 'super_type_id' , $SecondLeveType [ $j ][ 'type_id' ]) -> select ();
for ( $k = 0 ; $k < count ( $ThridLeveType ); $k ++ )
{
$count = db ( 'contract_info' ) -> where ( 'contract_type' , $ThridLeveType [ $k ][ 'type_id' ]) -> where ( 'del_time' , null ) -> count ();
if ( $count > 0 )
{
$ThridLeveType [ $k ][ 'type_name' ] .= '【' . $count . '】' ;
}
}
$SecondLeveType [ $j ][ 'sub_type' ] = $ThridLeveType ;
}
$FirstLeveType [ $i ][ 'sub_type' ] = $SecondLeveType ;
}
return $this -> sendSuccess ( $FirstLeveType );
}
//根据合同类型获取合同列表
public function GetOperationContractList ()
{
$param = request () -> post ();
$ContractInfoList = db ( 'contract_info' ) -> where ( 'contract_type' , $param [ 'contract_type' ])
-> where ( 'del_time' , null )
-> select ();
return $this -> sendSuccess ( $ContractInfoList );
}
public function GetCustomerProjectList ()
{
$param = request () -> post ();
2026-03-25 15:18:15 +08:00
$FirstLeveType = db ( 'partner_company' ) -> where ( 'company_type & 0x01' , 1 ) -> field ( 'company_id as id, company_name as name' ) -> order ( 'company_name asc' ) -> select ();
2026-03-10 18:45:36 +08:00
for ( $i = 0 ; $i < count ( $FirstLeveType ); $i ++ )
{
//dump($FirstLeveType[$i]['type_id']);
$SecondLeveType = db ( 'project_info' ) -> where ( 'customer_id' , $FirstLeveType [ $i ][ 'id' ]) -> field ( 'project_id as id, project_name as name' ) -> select ();
//dump($SecondLeveType);
for ( $j = 0 ; $j < count ( $SecondLeveType ); $j ++ )
{
$count = db ( 'contract_info' ) -> where ( 'project_id' , $SecondLeveType [ $j ][ 'id' ]) -> where ( 'contract_category' , 'in' , [ 1 , 3 ]) -> count ();
$SecondLeveType [ $j ][ 'name' ] .= '【' . $count . '】' ;
}
$FirstLeveType [ $i ][ 'customer_projects' ] = $SecondLeveType ;
}
return $this -> sendSuccess ( $FirstLeveType );
}
public function GetDevelopProjectList ()
{
$param = request () -> post ();
$FirstLeveType = db ( 'project_info' ) -> where ( 'project_type' , 3 ) -> field ( 'project_id as id, project_name as name' ) -> select ();
for ( $j = 0 ; $j < count ( $FirstLeveType ); $j ++ )
{
$count = db ( 'contract_info' ) -> where ( 'project_id' , $FirstLeveType [ $j ][ 'id' ]) -> where ( 'contract_type' , 2 ) -> where ( 'del_time' , null ) -> count ();
$FirstLeveType [ $j ][ 'name' ] .= '【' . $count . '】' ;
}
return $this -> sendSuccess ( $FirstLeveType );
}
public function GetProjectContractList ()
{
$param = request () -> post ();
$ContractInfoList = [];
if ( isset ( $param [ 'contract_category' ]))
{
//销售合同/技术协议
if ( 1 == $param [ 'contract_category' ]){
$ContractInfoList = db ( 'contract_info' ) -> where ( 'project_id' , $param [ 'project_id' ])
-> where ( 'contract_type' , 'in' , [ 1 , 2 ])
-> where ( 'contract_category' , 'in' , [ 1 , 3 ])
-> where ( 'del_time' , null )
-> select ();
} elseif ( 2 == $param [ 'contract_category' ]){
$ContractInfoList = db ( 'contract_info' ) -> where ( 'project_id' , $param [ 'project_id' ])
-> where ( 'contract_type' , 'in' , [ 1 , 2 ])
-> where ( 'contract_category' , 'in' , [ 2 , 4 ])
-> where ( 'del_time' , null )
-> select ();
}
if ( 5 == $param [ 'contract_category' ]){
$ContractInfoList = db ( 'contract_info' ) -> where ( 'project_id' , $param [ 'project_id' ])
-> where ( 'contract_type' , 'in' , [ 1 , 2 ])
-> where ( 'contract_category' , $param [ 'contract_category' ])
-> where ( 'del_time' , null )
-> select ();
}
}
else
{
$ContractInfoList = db ( 'contract_info' ) -> where ( 'project_id' , $param [ 'project_id' ])
-> where ( 'contract_type' , $param [ 'contract_type' ])
-> where ( 'contract_category' , 'in' , [ 1 , 3 ])
-> where ( 'del_time' , null )
-> select ();
}
if ( isset ( $param [ 'contract_type' ])){
//采购合同/技术协议
if ( 1 == $param [ 'contract_type' ] || 2 == $param [ 'contract_type' ])
{
if ( isset ( $param [ 'partner_id' ])){
$ContractInfoList = db ( 'contract_info' ) -> where ( 'project_id' , $param [ 'project_id' ])
-> where ( 'contract_type' , $param [ 'contract_type' ])
-> where ( 'partner_id' , $param [ 'partner_id' ])
-> whereIn ( 'contract_category' , $param [ 'contract_category' ])
-> where ( 'del_time' , null )
-> select ();
} else {
$ContractInfoList = db ( 'contract_info' ) -> where ( 'project_id' , $param [ 'project_id' ])
-> where ( 'contract_type' , $param [ 'contract_type' ])
-> where ( 'contract_category' , 'in' , [ 1 , 3 ])
-> where ( 'del_time' , null )
-> select ();
}
}
}
return $this -> sendSuccess ( $ContractInfoList );
}
//添加合同类型
public function AddContractType ()
{
$param = request () -> post ();
$ret = [];
if ( $param [ 'type_id' ] > 0 )
{
//添加子类型
$ret = db ( 'contract_type' ) -> where ( 'type_id' , $param [ 'type_id' ]) -> field ( 'type_id,super_type_id' ) -> find ();
}
else
{
//添加基类型
$ret [ 'type_id' ] = 0 ;
}
if ( $ret != null ) {
$insert = [];
$insert [ 'type_name' ] = $param [ 'type_name' ];
$insert [ 'super_type_id' ] = $ret [ 'type_id' ];
$insert [ 'create_time' ] = date ( 'Y-m-d H:i:s' );
db ( 'contract_type' ) -> insert ( $insert );
}
}
public function AddContractInfo ()
{
$param = request () -> post ();
$insert_info = [];
$insert_info [ 'contract_number' ] = $param [ 'contract_number' ];
$insert_info [ 'contract_name' ] = $param [ 'contract_name' ];
$insert_info [ 'create_time' ] = $param [ 'create_time' ];
if ( isset ( $param [ 'contract_des' ])){
$insert_info [ 'contract_des' ] = $param [ 'contract_des' ];
}
if ( isset ( $param [ 'contract_price' ])) {
$insert_info [ 'contract_price' ] = $param [ 'contract_price' ];
}
if ( isset ( $param [ 'partner_id' ])){
$insert_info [ 'partner_id' ] = $param [ 'partner_id' ];
}
if ( isset ( $param [ 'project_id' ]))
{
$insert_info [ 'project_id' ] = $param [ 'project_id' ]; //合同所属项目ID
$insert_info [ 'contract_category' ] = $param [ 'contract_category' ]; //合同类型 1 商务合同, 2 技术协议
}
if ( isset ( $param [ 'contract_type' ]))
{
$insert_info [ 'contract_type' ] = $param [ 'contract_type' ]; //合同所属项目ID
}
if ( isset ( $param [ 'contract_category' ]))
{
$insert_info [ 'contract_category' ] = $param [ 'contract_category' ];
}
2026-03-17 17:11:14 +08:00
// 先检查键是否存在,再判断值是否为空
if ( ! isset ( $param [ 'contract_url' ]) || $param [ 'contract_url' ] == '' ) {
2026-03-10 18:45:36 +08:00
$insert_info [ 'contract_url' ] = '' ;
2026-03-17 17:11:14 +08:00
} else {
2026-03-17 20:21:37 +08:00
$insert_info [ 'contract_url' ] = $param [ 'contract_url' ];
2026-03-17 17:11:14 +08:00
// 同理, contract_file_name 也建议做存在性检查,避免同理报错
$insert_info [ 'contract_file_name' ] = isset ( $param [ 'contract_file_name' ]) ? $param [ 'contract_file_name' ] : '' ;
2026-03-10 18:45:36 +08:00
}
db ( 'contract_info' ) -> insert ( $insert_info );
return $this -> sendSuccess ( '添加成功' );
}
public function AddProjectContract ()
{
$param = request () -> post ();
$insert_info = [];
$insert_info [ 'contract_number' ] = $param [ 'contract_number' ];
$insert_info [ 'contract_name' ] = $param [ 'contract_name' ];
$insert_info [ 'create_time' ] = $param [ 'create_time' ];
$insert_info [ 'contract_price' ] = $param [ 'contract_price' ];
if ( isset ( $param [ 'partner_id' ])){
$insert_info [ 'partner_id' ] = $param [ 'partner_id' ];
}
if ( isset ( $param [ 'project_id' ]))
{
$insert_info [ 'project_id' ] = $param [ 'project_id' ]; //合同所属项目ID
$insert_info [ 'contract_type' ] = $param [ 'contract_type' ]; //合同类型 1 商务合同, 2 技术协议
}
$insert_info [ 'contract_type' ] = 1 ; //客户项目合同类
if ( isset ( $param [ 'contract_category' ]))
{
$insert_info [ 'contract_category' ] = $param [ 'contract_category' ];
}
if ( $param [ 'contract_url' ] == '' ){
$insert_info [ 'contract_url' ] = '' ;
} else {
$insert_info [ 'contract_url' ] = $param [ 'contract_url' ];
}
db ( 'contract_info' ) -> insert ( $insert_info );
return $this -> sendSuccess ( '添加成功' );
}
public function updateContractInfo ()
{
$param = request () -> post ();
if ( $param != null ){
$update = [];
$update [ 'contract_name' ] = $param [ 'contract_name' ];
$update [ 'contract_category' ] = $param [ 'contract_category' ];
$update [ 'contract_number' ] = $param [ 'contract_number' ];
$update [ 'contract_des' ] = $param [ 'contract_des' ];
if ( $param [ 'contract_url' ] != '' )
{
$update [ 'contract_url' ] = $param [ 'contract_url' ];
}
$update [ 'contract_price' ] = $param [ 'contract_price' ];
$update [ 'create_time' ] = $param [ 'create_time' ];
$update [ 'contract_file_name' ] = $param [ 'contract_file_name' ];
$ret = db ( 'contract_info' ) -> where ( 'contract_id' , $param [ 'contract_id' ]) -> update ( $update );
if ( $ret ){
return $this -> sendSuccess ( '修改成功' );
} else {
return $this -> sendError ( '修改失败' );
}
}
}
public function delContractInfo ()
{
$param = request () -> post ();
// dump($param);
$update = [];
$update [ 'del_time' ] = date ( 'Y-m-d H:i:s' );
$ret = db ( 'contract_info' ) -> where ( 'contract_id' , $param [ 'contract_id' ]) -> update ( $update );
if ( $ret ){
return $this -> sendSuccess ( '删除成功' );
} else {
return $this -> sendError ( '删除失败' );
}
}
2026-03-17 20:21:37 +08:00
// 合同检索
public function searchContracts ()
{
$param = request () -> param ();
$currPage = isset ( $param [ 'currPage' ]) ? $param [ 'currPage' ] : 1 ;
$pageSize = isset ( $param [ 'pageSize' ]) ? $param [ 'pageSize' ] : 20 ;
// 构建查询条件
$where = [ 'del_time' => null ];
// 时间范围筛选
if ( isset ( $param [ 'start_date' ]) && ! empty ( $param [ 'start_date' ])) {
$where [ 'create_time' ] = [ '>=' , $param [ 'start_date' ]];
}
if ( isset ( $param [ 'end_date' ]) && ! empty ( $param [ 'end_date' ])) {
if ( isset ( $where [ 'create_time' ])) {
$where [ 'create_time' ] = [
[ '>=' , $param [ 'start_date' ]],
[ '<=' , $param [ 'end_date' ]]
];
} else {
$where [ 'create_time' ] = [ '<=' , $param [ 'end_date' ]];
}
}
// 合同分类筛选
if ( isset ( $param [ 'contract_category' ]) && $param [ 'contract_category' ] !== '' && $param [ 'contract_category' ] !== '0' ) {
$where [ 'contract_category' ] = $param [ 'contract_category' ];
}
// 查询合同列表
$contractList = db ( 'contract_info' )
-> where ( $where )
-> order ( 'contract_id' , 'desc' )
-> page ( $currPage , $pageSize )
-> select ();
// 遍历合同列表,补充关联信息
foreach ( $contractList as & $contract ) {
// 获取合同类型名称
if ( $contract [ 'contract_type' ]) {
$contractType = db ( 'contract_type' )
-> where ( 'type_id' , $contract [ 'contract_type' ])
-> field ( 'type_name' )
-> find ();
$contract [ 'contract_type_name' ] = $contractType ? $contractType [ 'type_name' ] : '' ;
} else {
$contract [ 'contract_type_name' ] = '' ;
}
// 获取合同分类名称
switch ( $contract [ 'contract_category' ]) {
case 1 :
$contract [ 'contract_category_name' ] = '销售合同' ;
break ;
case 2 :
$contract [ 'contract_category_name' ] = '销售技术协议' ;
break ;
case 3 :
$contract [ 'contract_category_name' ] = '采购合同' ;
break ;
case 4 :
$contract [ 'contract_category_name' ] = '采购及技术协议' ;
break ;
case 5 :
$contract [ 'contract_category_name' ] = '其他' ;
break ;
default :
$contract [ 'contract_category_name' ] = '' ;
}
// 获取项目名称
if ( $contract [ 'project_id' ]) {
$project = db ( 'project_info' )
-> where ( 'project_id' , $contract [ 'project_id' ])
-> field ( 'project_name' )
-> find ();
$contract [ 'project_name' ] = $project ? $project [ 'project_name' ] : '' ;
} else {
$contract [ 'project_name' ] = '' ;
}
// 获取合作伙伴名称
if ( $contract [ 'partner_id' ]) {
$partner = db ( 'partner_company' )
-> where ( 'company_id' , $contract [ 'partner_id' ])
-> field ( 'company_name' )
-> find ();
$contract [ 'partner_name' ] = $partner ? $partner [ 'company_name' ] : '' ;
} else {
$contract [ 'partner_name' ] = '' ;
}
}
unset ( $contract );
// 查询总记录数
$total = db ( 'contract_info' ) -> where ( $where ) -> count ();
$res [ 'list' ] = $contractList ;
$res [ 'total' ] = $total ;
return $this -> sendSuccess ( $res );
}
2026-03-10 18:45:36 +08:00
}