2022-12-23 02:59:25 +00:00
|
|
|
|
package cn.palmte.work.service;
|
|
|
|
|
|
|
2022-12-23 09:24:05 +00:00
|
|
|
|
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
|
2022-12-23 02:59:25 +00:00
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
2022-12-26 08:43:15 +00:00
|
|
|
|
import java.math.BigDecimal;
|
2022-12-23 09:24:05 +00:00
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
import java.util.HashMap;
|
2022-12-23 02:59:25 +00:00
|
|
|
|
import java.util.List;
|
2022-12-23 09:24:05 +00:00
|
|
|
|
import java.util.Map;
|
2022-12-23 02:59:25 +00:00
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.persistence.EntityManager;
|
|
|
|
|
|
import javax.persistence.TypedQuery;
|
|
|
|
|
|
|
2022-12-26 08:43:15 +00:00
|
|
|
|
import cn.palmte.work.config.activiti.ActProjectTypeEnum;
|
2022-12-23 02:59:25 +00:00
|
|
|
|
import cn.palmte.work.model.Admin;
|
2022-12-26 08:43:15 +00:00
|
|
|
|
import cn.palmte.work.model.Project;
|
|
|
|
|
|
import cn.palmte.work.model.ProjectRepository;
|
|
|
|
|
|
import cn.palmte.work.model.enums.CooperationType;
|
|
|
|
|
|
import cn.palmte.work.model.enums.Enumerable;
|
2022-12-23 02:59:25 +00:00
|
|
|
|
import cn.palmte.work.model.enums.ProcessStatus;
|
2022-12-26 08:43:15 +00:00
|
|
|
|
import cn.palmte.work.model.enums.ProjectType;
|
2022-12-23 09:24:05 +00:00
|
|
|
|
import cn.palmte.work.model.process.ProcurementContract;
|
|
|
|
|
|
import cn.palmte.work.model.process.ProjectProcess;
|
|
|
|
|
|
import cn.palmte.work.model.process.SaleContract;
|
|
|
|
|
|
import cn.palmte.work.model.process.form.SaleContractDetailForm;
|
2022-12-23 02:59:25 +00:00
|
|
|
|
import lombok.RequiredArgsConstructor;
|
2022-12-23 09:24:05 +00:00
|
|
|
|
import lombok.SneakyThrows;
|
2022-12-23 02:59:25 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
|
|
|
|
|
|
* @since 2.0 2022/12/23 09:39
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Service
|
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
|
public class ProjectProcessService {
|
|
|
|
|
|
|
|
|
|
|
|
private final JdbcTemplate jdbcTemplate;
|
|
|
|
|
|
private final EntityManager entityManager;
|
2022-12-23 09:24:05 +00:00
|
|
|
|
private final ProjectInstanceService projectInstanceService;
|
2022-12-26 08:43:15 +00:00
|
|
|
|
private final ProjectRepository projectRepository;
|
2022-12-23 02:59:25 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更新流程 审批人,和状态
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param processId 流程Id
|
|
|
|
|
|
* @param auditId 审批人ID
|
|
|
|
|
|
* @param status 流程状态 可以为空,为空的时候不修改
|
|
|
|
|
|
*/
|
|
|
|
|
|
public void updateAudit(int processId, ProcessStatus status, List<Integer> auditId) {
|
|
|
|
|
|
String currentAudit = getCurrentAudit(auditId);
|
|
|
|
|
|
String currentAuditId = auditId.stream().map(String::valueOf).collect(Collectors.joining(","));
|
|
|
|
|
|
if (status != null) {
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(auditId)) {
|
|
|
|
|
|
jdbcTemplate.update(
|
|
|
|
|
|
"update project_process set current_audit=?,current_audit_id=?,status=? where id=?", currentAudit, currentAuditId, status.getValue(), processId);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
jdbcTemplate.update("update project_process set current_audit=?,current_audit_id=? where id=?", currentAudit, currentAuditId, processId);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String getCurrentAudit(List<Integer> auditId) {
|
|
|
|
|
|
TypedQuery<Admin> query = entityManager.createQuery("from Admin where id in (:ids)", Admin.class);
|
|
|
|
|
|
query.setParameter("ids", auditId);
|
|
|
|
|
|
List<Admin> resultList = query.getResultList();
|
|
|
|
|
|
return resultList.stream().map(Admin::getRealName)
|
|
|
|
|
|
.collect(Collectors.joining(","));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 只更新状态
|
|
|
|
|
|
*/
|
|
|
|
|
|
public void updateProcessStatus(int processId, ProcessStatus status) {
|
|
|
|
|
|
jdbcTemplate.update("update project_process set `status`=? where id=?", status.getValue(), processId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-23 09:24:05 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* 根据流程ID查询销售合同
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param processId 流程ID
|
|
|
|
|
|
* @return 销售合同
|
|
|
|
|
|
*/
|
|
|
|
|
|
public SaleContract findSaleContract(int processId) {
|
|
|
|
|
|
TypedQuery<SaleContract> query = entityManager.createQuery(
|
|
|
|
|
|
"from SaleContract where processId=:processId", SaleContract.class);
|
|
|
|
|
|
query.setParameter("processId", processId);
|
|
|
|
|
|
return query.getSingleResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 根据流程ID查询采购合同
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param processId 流程ID
|
|
|
|
|
|
* @return 采购合同
|
|
|
|
|
|
*/
|
|
|
|
|
|
public ProcurementContract findProcurementContract(int processId) {
|
|
|
|
|
|
TypedQuery<ProcurementContract> query = entityManager.createQuery(
|
|
|
|
|
|
"from ProcurementContract where processId=:processId", ProcurementContract.class);
|
|
|
|
|
|
query.setParameter("processId", processId);
|
|
|
|
|
|
return query.getSingleResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ProjectProcess getById(int id) {
|
|
|
|
|
|
return entityManager.find(ProjectProcess.class, id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更新 销售合同 的 质保期
|
|
|
|
|
|
*/
|
|
|
|
|
|
public void updateIncomeDetails(List<SaleContractDetailForm> incomeDetails) {
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(incomeDetails)) {
|
|
|
|
|
|
jdbcTemplate.batchUpdate("update project_budget_income_detail set expiration_date =? where id =? ",
|
|
|
|
|
|
new BatchPreparedStatementSetter() {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void setValues(PreparedStatement ps, int i) throws SQLException {
|
|
|
|
|
|
SaleContractDetailForm detailForm = incomeDetails.get(i);
|
|
|
|
|
|
ps.setString(1, detailForm.getExpirationDate());
|
|
|
|
|
|
ps.setInt(2, detailForm.getId());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public int getBatchSize() {
|
|
|
|
|
|
return incomeDetails.size();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-26 08:43:15 +00:00
|
|
|
|
public BigDecimal getProjectRepaidAmount(int projectId) {
|
|
|
|
|
|
Map<String, Object> map = jdbcTemplate.queryForMap(
|
|
|
|
|
|
"select ifnull(sum(underwritten_plan), 0) repaidAmount from project_budget_plan_detail where project_id = ?", projectId);
|
|
|
|
|
|
return (BigDecimal) map.values().iterator().next();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-23 09:24:05 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* 发起流程审批
|
|
|
|
|
|
*/
|
|
|
|
|
|
public void startAuditProgress(ProjectProcess entity) {
|
|
|
|
|
|
HashMap<String, Object> variables = new HashMap<>();
|
2022-12-26 08:43:15 +00:00
|
|
|
|
Project project = projectRepository.findById(entity.getProjectId());
|
|
|
|
|
|
// 是否垫资
|
|
|
|
|
|
variables.put("isPrepaid", isProjectPrepaid(project));
|
|
|
|
|
|
// 垫资金额
|
|
|
|
|
|
BigDecimal repaidAmount = getProjectRepaidAmount(entity.getProjectId());
|
|
|
|
|
|
variables.put("repaidAmount", repaidAmount);
|
|
|
|
|
|
// 合同金额
|
|
|
|
|
|
variables.put("contractAmount", project.getContractAmount());
|
|
|
|
|
|
// 项目类型
|
|
|
|
|
|
variables.put("projectType", Enumerable.of(ProjectType.class, project.getType()));
|
|
|
|
|
|
// 合作类型
|
|
|
|
|
|
variables.put("cooperationType", Enumerable.of(CooperationType.class, project.getCooperateType()));
|
|
|
|
|
|
|
2022-12-23 09:24:05 +00:00
|
|
|
|
startAuditProgress(entity, variables);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-26 08:43:15 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* 是否垫资
|
|
|
|
|
|
*/
|
|
|
|
|
|
public boolean isProjectPrepaid(Project project) {
|
|
|
|
|
|
return project.getUnderwrittenMode() == 2 || project.getUnderwrittenMode() == 3;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-23 09:24:05 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* 发起流程审批
|
|
|
|
|
|
*/
|
|
|
|
|
|
@SneakyThrows
|
|
|
|
|
|
public void startAuditProgress(ProjectProcess entity, Map<String, Object> variables) {
|
|
|
|
|
|
if (entity.getStatus() == ProcessStatus.to_be_audit) {
|
|
|
|
|
|
switch (entity.getProcessType()) {
|
|
|
|
|
|
case sale_contract:
|
2022-12-23 09:44:31 +00:00
|
|
|
|
projectInstanceService.startProcessByProjectId(entity.getId(), ActProjectTypeEnum.SALE_CONTRACT, variables);
|
2022-12-23 09:24:05 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case business_procurement:
|
2022-12-23 09:44:31 +00:00
|
|
|
|
projectInstanceService.startProcessByProjectId(entity.getId(), ActProjectTypeEnum.BUSINESS_PURCHASE, variables);
|
2022-12-23 09:24:05 +00:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new UnsupportedOperationException("还不支持");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-23 02:59:25 +00:00
|
|
|
|
}
|