2021-10-29 10:15:36 +00:00
package cn.palmte.work.service ;
2021-11-02 09:33:34 +00:00
import cn.palmte.work.bean.* ;
2021-11-15 08:53:43 +00:00
import cn.palmte.work.model.* ;
2021-11-18 09:39:31 +00:00
import cn.palmte.work.utils.InterfaceUtil ;
2021-11-15 08:53:43 +00:00
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSONObject ;
2021-10-29 10:15:36 +00:00
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import top.jfunc.common.db.QueryHelper ;
import top.jfunc.common.db.bean.Page ;
import top.jfunc.common.db.utils.Pagination ;
2021-11-12 07:12:44 +00:00
import top.jfunc.common.utils.CollectionUtil ;
2021-10-29 10:15:36 +00:00
import top.jfunc.common.utils.StrUtil ;
2021-11-19 06:47:59 +00:00
import java.util.* ;
import java.util.stream.Collectors ;
2021-10-29 10:15:36 +00:00
/ * *
* @author xiongshiyan at 2021 / 10 / 29 , contact me with email yanshixiong @ 126. com or phone 15208384257
* /
@Service
public class ProjectService {
@Autowired
private Pagination pagination ;
2021-11-01 11:00:53 +00:00
@Autowired
private ProjectRepository projectRepository ;
2021-11-01 12:07:02 +00:00
@Autowired
2021-11-18 08:07:33 +00:00
private SysConfigService sysConfigService ;
2021-11-12 07:12:44 +00:00
@Autowired
private ActTaskDefService actTaskDefService ;
2021-11-15 08:53:43 +00:00
@Autowired
private ProjectInstanceRelationRepository projectInstanceRelationRepository ;
2021-11-19 06:47:59 +00:00
@Autowired
private DeptRepository deptRepository ;
@Autowired
private ProjectVisibleRepository projectVisibleRepository ;
@Autowired
private SysRoleRepository sysRoleRepository ;
@Autowired
private AdminRepository adminRepository ;
2021-10-29 10:15:36 +00:00
2021-11-18 08:39:59 +00:00
private QueryHelper getQueryHelper ( Map < String , String > searchInfo ) {
2021-10-29 10:15:36 +00:00
/ * "CASE p.type WHEN 1 THEN '工程集成类' WHEN 2 THEN '设备集成类' WHEN 3 THEN '战略合作类' ELSE '未知' AS typeDesc," +
"CASE p.status WHEN 0 THEN '草稿' WHEN 1 THEN '项目创建' WHEN 5 THEN '概算完成' WHEN 10 THEN '预算完成' WHEN 15 THEN '结算中' WHEN 20 THEN '决算完成' ELSE '未知' AS statusDesc," +
"CASE p.approve_status WHEN 0 THEN '待审核' WHEN 1 THEN '审核通过' WHEN 2 THEN '审核不通过' ELSE '未知' AS approveStatusDesc," +
* /
QueryHelper queryHelper = new QueryHelper ( "SELECT p.*" , "project" , "p" ) ;
2021-11-01 03:22:37 +00:00
if ( StrUtil . isNotEmpty ( searchInfo . get ( "status" ) ) & & ! "-1" . equals ( searchInfo . get ( "status" ) ) ) {
queryHelper . addCondition ( "p.status=?" , Integer . parseInt ( searchInfo . get ( "status" ) ) ) ;
2021-10-29 10:15:36 +00:00
}
2021-11-18 08:39:59 +00:00
///
/ * if ( StrUtil . isNotEmpty ( searchInfo . get ( "approveId" ) ) ) {
2021-11-08 08:38:02 +00:00
int approveId = Integer . parseInt ( searchInfo . get ( "approveId" ) ) ;
queryHelper . addCondition ( "p.approve_id=?" , approveId ) ;
2021-11-18 08:39:59 +00:00
} * /
2021-11-01 03:22:37 +00:00
if ( StrUtil . isNotEmpty ( searchInfo . get ( "approveStatus" ) ) & & ! "-1" . equals ( searchInfo . get ( "approveStatus" ) ) ) {
2021-11-02 04:20:20 +00:00
int approveStatus = Integer . parseInt ( searchInfo . get ( "approveStatus" ) ) ;
//四种状态满足其一即可
queryHelper . addCondition ( "(p.approve_status_estimate=? OR p.approve_status_budget=? OR p.approve_status_settle=? OR p.approve_status_final=?)" , approveStatus , approveStatus , approveStatus , approveStatus ) ;
2021-10-29 10:15:36 +00:00
}
2021-11-01 03:22:37 +00:00
if ( StrUtil . isNotEmpty ( searchInfo . get ( "deptId" ) ) & & ! "-1" . equals ( searchInfo . get ( "deptId" ) ) ) {
queryHelper . addCondition ( "p.dept_id=?" , Integer . parseInt ( searchInfo . get ( "deptId" ) ) ) ;
2021-10-29 10:15:36 +00:00
}
2021-11-01 03:22:37 +00:00
if ( StrUtil . isNotEmpty ( searchInfo . get ( "type" ) ) & & ! "-1" . equals ( searchInfo . get ( "type" ) ) ) {
queryHelper . addCondition ( "p.type=?" , Integer . parseInt ( searchInfo . get ( "type" ) ) ) ;
2021-10-29 10:15:36 +00:00
}
queryHelper . addCondition ( StrUtil . isNotEmpty ( searchInfo . get ( "name" ) ) , "p.name LIKE ?" , "%" + searchInfo . get ( "name" ) + "%" ) ;
queryHelper . addCondition ( StrUtil . isNotEmpty ( searchInfo . get ( "creatorName" ) ) , "p.creator_name LIKE ?" , "%" + searchInfo . get ( "creatorName" ) + "%" ) ;
/ * *
* 只 选 择 了 一 个 时 间 的 情 况 , 就 项 目 时 间 包 括 这 个 时 间 的
* /
2021-11-01 11:00:53 +00:00
if ( StrUtil . isNotEmpty ( searchInfo . get ( "startDate" ) ) & & StrUtil . isEmpty ( searchInfo . get ( "endDate" ) ) ) {
String time = searchInfo . get ( "startDate" ) + " 00:00:00" ;
2021-11-01 03:22:37 +00:00
queryHelper . addCondition ( "p.start_date<=? AND p.end_date>=?" , time , time ) ;
2021-10-29 10:15:36 +00:00
}
2021-11-01 11:00:53 +00:00
if ( StrUtil . isNotEmpty ( searchInfo . get ( "endDate" ) ) & & StrUtil . isEmpty ( searchInfo . get ( "startDate" ) ) ) {
String time = searchInfo . get ( "endDate" ) + " 00:00:00" ;
2021-11-01 03:22:37 +00:00
queryHelper . addCondition ( "p.start_date<=? AND p.end_date>=?" , time , time ) ;
2021-10-29 10:15:36 +00:00
}
/ * *
* 两 个 时 间 都 选 了 , 则 包 含 项 目 时 间
* /
2021-11-01 11:00:53 +00:00
if ( StrUtil . isNotEmpty ( searchInfo . get ( "startDate" ) ) & & StrUtil . isNotEmpty ( searchInfo . get ( "endDate" ) ) ) {
String startTime = searchInfo . get ( "startDate" ) + " 00:00:00" ;
String endTime = searchInfo . get ( "endDate" ) + " 23:59:59" ;
2021-11-01 03:22:37 +00:00
queryHelper . addCondition ( "p.start_date>=? AND p.end_date<=?" , startTime , endTime ) ;
2021-10-29 10:15:36 +00:00
}
2021-11-08 08:27:33 +00:00
if ( StrUtil . isNotEmpty ( searchInfo . get ( "startUpdateDate" ) ) ) {
String time = searchInfo . get ( "startUpdateDate" ) + " 00:00:00" ;
queryHelper . addCondition ( "p.last_update_time>=?" , time ) ;
}
if ( StrUtil . isNotEmpty ( searchInfo . get ( "endUpdateDate" ) ) ) {
String time = searchInfo . get ( "endUpdateDate" ) + " 00:00:00" ;
queryHelper . addCondition ( "p.last_update_time<=?" , time ) ;
}
2021-11-18 09:39:31 +00:00
//项目可见性, 根据角色和人员id
Admin admin = InterfaceUtil . getAdmin ( ) ;
int roleId = admin . getRoleId ( ) ;
Integer adminId = admin . getId ( ) ;
2021-11-19 06:47:59 +00:00
//自己创建的肯定能看见
queryHelper . addCondition ( "(p.creator_id=? OR p.id in (SELECT pv1.project_id FROM project_visible pv1 WHERE pv1.type=1 AND pv1.tid=? UNION SELECT pv2.project_id FROM project_visible pv2 WHERE pv2.type=2 AND pv2.tid=?))" , adminId , roleId , adminId ) ;
2021-11-18 09:39:31 +00:00
2021-11-08 08:27:33 +00:00
2021-11-22 02:12:45 +00:00
queryHelper . addDescOrderProperty ( "p.last_update_time" ) ;
2021-10-29 10:15:36 +00:00
return queryHelper ;
}
public Page < Project > list ( Map < String , String > searchInfo , int pageNumber , int pageSize ) {
2021-11-18 08:39:59 +00:00
QueryHelper queryHelper = getQueryHelper ( searchInfo ) ;
2021-10-29 10:15:36 +00:00
return pagination . paginate ( queryHelper . getSql ( ) , Project . class , pageNumber , pageSize ) ;
}
2021-11-08 09:32:25 +00:00
/ * *
* 找 到 待 我 审 核 项 目
* /
public Page < Project > findMyApproveProjects ( Map < String , String > searchInfo , int adminId , int pageNumber , int pageSize ) {
2021-11-18 08:39:59 +00:00
//待审核状态的条件
2021-11-08 09:32:25 +00:00
searchInfo . put ( "approveStatus" , "" + ApproveStatusEnum . APPROVAL_PENDING . getApproveStatus ( ) ) ;
2021-11-18 08:39:59 +00:00
QueryHelper queryHelper = getQueryHelper ( searchInfo ) ;
//在列表基础上增加审核人是我
queryHelper . addCondition ( "p.approve_id=?" , adminId ) ;
return pagination . paginate ( queryHelper . getSql ( ) , Project . class , pageNumber , pageSize ) ;
2021-11-08 09:32:25 +00:00
}
2021-11-02 06:43:42 +00:00
public Project getProject ( int id ) {
return projectRepository . findOne ( id ) ;
}
2021-10-29 10:15:36 +00:00
2021-11-02 06:43:42 +00:00
2021-11-18 08:01:09 +00:00
public Project addProject ( Project project , Admin admin , ApproveStatusEnum approveStatusEnum ) {
2021-11-01 11:00:53 +00:00
project . setTypeDesc ( TypeEnum . parseType ( project . getType ( ) ) . getTypeDesc ( ) ) ;
2021-11-17 02:14:01 +00:00
project . setStatus ( StatusEnum . ESTIMATE_ACCOUNTS . getStatus ( ) ) ;
project . setStatusDesc ( StatusEnum . ESTIMATE_ACCOUNTS . getStatusDesc ( ) ) ;
2021-11-02 04:20:20 +00:00
project . setApproveStatusEstimate ( approveStatusEnum . getApproveStatus ( ) ) ;
2021-11-01 11:00:53 +00:00
project . setCreatorId ( admin . getId ( ) ) ;
project . setCreatorName ( admin . getUserName ( ) ) ;
2021-11-18 08:07:33 +00:00
ProjectConfigBean projectConfigBeanConfig = sysConfigService . getProjectConfigBeanConfig ( ) ;
2021-11-05 04:19:01 +00:00
project . setProjectContributionProfitRateThreshold ( projectConfigBeanConfig . getProjectContributionProfitRateThreshold ( ) ) ;
project . setUnderwrittenTaxRate ( projectConfigBeanConfig . getUnderwrittenTaxRate ( ) ) ;
2021-11-17 03:14:52 +00:00
project . setApproveId ( 0 ) ;
2021-11-01 11:00:53 +00:00
project . setApproveName ( "" ) ;
2021-11-19 06:47:59 +00:00
Dept one = deptRepository . findOne ( admin . getDeptId ( ) ) ;
project . setDeptId ( one . getId ( ) ) ;
project . setDeptName ( one . getName ( ) ) ;
2021-11-01 11:00:53 +00:00
2021-11-08 08:27:33 +00:00
Date now = new Date ( ) ;
project . setCreateTime ( now ) ;
project . setLastUpdateTime ( now ) ;
2021-11-01 11:00:53 +00:00
project = projectRepository . saveAndFlush ( project ) ;
return project ;
}
2021-11-01 12:07:02 +00:00
2021-11-18 08:01:09 +00:00
public Project editProject ( Project project , Project projectInDb ) {
2021-11-16 01:00:26 +00:00
2021-11-01 13:09:10 +00:00
2021-11-02 06:43:42 +00:00
//只有如下可以修改
2021-11-16 01:00:26 +00:00
projectInDb . setStartDate ( project . getStartDate ( ) ) ;
projectInDb . setEndDate ( project . getEndDate ( ) ) ;
projectInDb . setAdvanceInterestAmount ( project . getAdvanceInterestAmount ( ) ) ;
projectInDb . setAdvancePeakAmount ( project . getAdvancePeakAmount ( ) ) ;
projectInDb . setContractAmount ( project . getContractAmount ( ) ) ;
projectInDb . setHuazhiProductAmount ( project . getHuazhiProductAmount ( ) ) ;
projectInDb . setZiguangOtherAmount ( project . getZiguangOtherAmount ( ) ) ;
projectInDb . setMainContractCollectionTerms ( project . getMainContractCollectionTerms ( ) ) ;
2021-11-01 13:09:10 +00:00
2021-11-16 01:00:26 +00:00
/// projectInDb.setApproveStatusEstimate(approveStatusEnum.getApproveStatus());
2021-11-01 13:09:10 +00:00
2021-11-16 01:00:26 +00:00
projectInDb . setLastUpdateTime ( new Date ( ) ) ;
2021-11-08 08:27:33 +00:00
2021-11-16 01:00:26 +00:00
return projectInDb ;
2021-11-02 09:33:34 +00:00
}
2021-11-15 08:53:43 +00:00
public ResponseMsg completeTask ( int projectId , String json ) {
List < ProjectInstanceRelation > relationList = projectInstanceRelationRepository . findByProjectIdOrderByCreateTimeDesc ( projectId ) ;
if ( relationList = = null | | relationList . isEmpty ( ) ) {
return ResponseMsg . buildFailedMsg ( "审核失败" ) ;
}
ProjectInstanceRelation projectInstanceRelation = relationList . get ( 0 ) ;
JSONObject obj = JSON . parseObject ( json ) ;
actTaskDefService . completeTaskByProcInsId ( projectInstanceRelation . getProcessInsId ( ) ,
obj . getIntValue ( "type" ) , obj . getString ( "message" ) ) ;
return ResponseMsg . buildSuccessMsg ( "审核成功" ) ;
}
2021-11-17 06:49:11 +00:00
/ * *
* 更 新 项 目 状 态 和 审 批 状 态
* /
public void updateStatusAndApproveStatus ( int projectId , StatusEnum statusEnum , ApproveStatusEnum approveStatusEnum ) {
Project one = projectRepository . findOne ( projectId ) ;
one . setStatus ( statusEnum . getStatus ( ) ) ;
one . setStatusDesc ( statusEnum . getStatusDesc ( ) ) ;
int approveStatus = approveStatusEnum . getApproveStatus ( ) ;
switch ( statusEnum ) {
case ESTIMATE_ACCOUNTS :
one . setApproveStatusEstimate ( approveStatus ) ;
break ;
case BUDGET_ACCOUNTS :
one . setApproveStatusBudget ( approveStatus ) ;
break ;
case SETTLE_ACCOUNTS :
one . setApproveStatusSettle ( approveStatus ) ;
break ;
case FINAL_ACCOUNTS :
one . setApproveStatusFinal ( approveStatus ) ;
break ;
default :
}
one . setLastUpdateTime ( new Date ( ) ) ;
projectRepository . saveAndFlush ( one ) ;
}
2021-11-19 06:47:59 +00:00
private static final String PREFIX_ROLE = "r-" ;
private static final String PREFIX_USER = "u-" ;
/ * *
* 把 所 有 的 角 色 和 人 员 按 照 层 级 返 回
* /
public List < ZTreeNode > getZTreeNodes ( int projectId ) {
List < SysRole > roleList = sysRoleRepository . findAllRole ( ) ;
List < Admin > adminList = adminRepository . getAllEnable ( ) ;
List < ZTreeNode > zTreeNodes = new ArrayList < > ( roleList . size ( ) + adminList . size ( ) ) ;
List < ProjectVisible > projectVisibles = projectVisibleRepository . findAllByProjectIdEquals ( projectId ) ;
List < ProjectVisible > visibleRoleList = filterByType ( projectVisibles , ProjectVisible . TYPE_ROLE ) ;
List < ProjectVisible > visibleUserList = filterByType ( projectVisibles , ProjectVisible . TYPE_USER ) ;
for ( SysRole sysRole : roleList ) {
zTreeNodes . add ( new ZTreeNode ( PREFIX_ROLE + sysRole . getId ( ) , "0" , sysRole . getName ( ) ,
isChecked ( visibleRoleList , sysRole . getId ( ) ) , false ) ) ;
zTreeNodes . addAll ( filterAdmins ( adminList , sysRole , visibleUserList ) ) ;
}
return zTreeNodes ;
}
private List < ZTreeNode > filterAdmins ( List < Admin > adminList , SysRole sysRole , List < ProjectVisible > visibleUserList ) {
List < Admin > admins = adminList . stream ( )
. filter ( a - > sysRole . getId ( ) = = a . getRoleId ( ) )
. collect ( Collectors . toList ( ) ) ;
if ( CollectionUtil . isNotEmpty ( admins ) ) {
ArrayList < ZTreeNode > zTreeNodes = new ArrayList < > ( admins . size ( ) ) ;
for ( Admin admin : admins ) {
zTreeNodes . add ( new ZTreeNode ( PREFIX_USER + admin . getId ( ) , PREFIX_ROLE + sysRole . getId ( ) , admin . getRealName ( ) ,
isChecked ( visibleUserList , admin . getId ( ) ) , false ) ) ;
}
return zTreeNodes ;
}
return Collections . emptyList ( ) ;
}
private boolean isChecked ( List < ProjectVisible > visibleList , int id ) {
return visibleList . stream ( ) . anyMatch ( pv - > pv . getTid ( ) = = id ) ;
}
private List < ProjectVisible > filterByType ( List < ProjectVisible > projectVisibles , int type ) {
if ( CollectionUtil . isEmpty ( projectVisibles ) ) {
return Collections . emptyList ( ) ;
}
return projectVisibles . stream ( ) . filter ( pv - > pv . getType ( ) = = type ) . collect ( Collectors . toList ( ) ) ;
}
public void saveProjectVisible ( Integer projectId , String [ ] idss ) {
//1.先清除以前的
List < ProjectVisible > projectVisibles = projectVisibleRepository . findAllByProjectIdEquals ( projectId ) ;
if ( CollectionUtil . isNotEmpty ( projectVisibles ) ) {
projectVisibleRepository . deleteInBatch ( projectVisibles ) ;
}
//2.再保存
List < ProjectVisible > pvs = new ArrayList < > ( idss . length ) ;
for ( String s : idss ) {
//只要用户的,角色的不要
if ( s . startsWith ( PREFIX_USER ) ) {
ProjectVisible pv = new ProjectVisible ( ) ;
pv . setProjectId ( projectId ) ;
pv . setType ( ProjectVisible . TYPE_USER ) ;
//去掉前缀
pv . setTid ( Integer . parseInt ( s . substring ( PREFIX_USER . length ( ) ) ) ) ;
pvs . add ( pv ) ;
}
}
projectVisibleRepository . save ( pvs ) ;
}
2021-10-29 10:15:36 +00:00
}