package com.product.contract.service;
|
|
import com.product.common.lang.StringUtils;
|
import com.product.contract.config.CmnConst;
|
import com.product.contract.service.ide.IContractInfoService;
|
import com.product.core.dao.BaseDao;
|
import com.product.core.entity.DataTableEntity;
|
import com.product.core.entity.FieldSetEntity;
|
import com.product.core.exception.BaseException;
|
import com.product.core.permission.PermissionService;
|
import com.product.core.service.support.AbstractBaseService;
|
import com.product.core.service.support.QueryFilterService;
|
import com.product.core.spring.context.SpringMVCContextHolder;
|
import com.product.core.transfer.Transactional;
|
import com.product.module.sys.entity.SystemUser;
|
import com.product.util.BaseUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.time.LocalDate;
|
import java.time.format.DateTimeFormatter;
|
import java.util.Date;
|
|
/**
|
* Copyright LX
|
*
|
* @Title: ContractInfoService
|
* @Project: product-server
|
* @date: 2021-06-22 14:11
|
* @author: ZhouJie
|
* @Description: 合同管理
|
*/
|
@Component
|
public class ContractInfoService extends AbstractBaseService implements IContractInfoService {
|
|
@Autowired
|
public BaseDao baseDao;
|
@Autowired
|
PermissionService permissionService;
|
@Autowired
|
QueryFilterService queryFilterService;
|
|
/**
|
* 保存合同信息
|
* @param fs
|
* @return
|
*/
|
@Override
|
@Transactional
|
public String saveContractInfo(FieldSetEntity fs) throws BaseException {
|
if(StringUtils.isEmpty(fs.getString(CmnConst.UUID))){
|
fs.setValue("org_level_uuid", SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid());
|
fs.setValue("created_by", SpringMVCContextHolder.getCurrentUser().getUser_id());
|
fs.setValue("created_utc_datetime",new Date());
|
return baseDao.add(fs);
|
} else {
|
fs.setValue("updated_by", SpringMVCContextHolder.getCurrentUser().getUser_id());
|
fs.setValue("updated_utc_datetime",new Date());
|
baseDao.update(fs);
|
return fs.getString(CmnConst.UUID);
|
}
|
}
|
|
/**
|
* 删除合同信息
|
* @param fs
|
* @return
|
*/
|
@Override
|
@Transactional
|
public boolean deleteContractInfo(FieldSetEntity fs) {
|
String uuid = fs.getUUID();
|
String[] uuids = uuid.split(",");
|
return baseDao.delete(CmnConst.LX_PROJECT_CONTRACT_INFO, BaseUtil.buildQuestionMarkFilter(CmnConst.UUID, uuids.length, true), uuids);
|
}
|
|
/**
|
* 获取合同列表
|
* @param fs
|
* @return
|
*/
|
public DataTableEntity listContract(FieldSetEntity fs) {
|
String queryFilter=permissionService.getDataFilter(CmnConst.ORG_LEVEL_UUID);
|
if(StringUtils.isEmpty(queryFilter)) {
|
queryFilter = CmnConst.CREATED_BY + " = " + SpringMVCContextHolder.getCurrentUser().getUser_id();
|
}
|
if(!BaseUtil.dataTableIsEmpty(fs.getSubDataTable("systemSeniorQueryString"))){
|
queryFilter = queryFilter +" AND "+ queryFilterService.getQueryFilter(fs);
|
}
|
String filter = fs.getString("filter");
|
if(BaseUtil.strIsNull(filter)){
|
filter = queryFilter;
|
}else {
|
filter = filter + " AND " + queryFilter;
|
}
|
//DataTableEntity dt = baseDao.listTable(CmnConst.LX_PROJECT_CONTRACT_INFO,filter,new Object[]{},null,null,fs.getInteger(CmnConst.PAGESIZE), fs.getInteger(CmnConst.CPAGE));
|
DataTableEntity dt = baseDao.listTable(CmnConst.LX_PROJECT_CONTRACT_INFO,filter,new Object[]{},null,null,fs.getInteger(CmnConst.PAGESIZE), fs.getInteger(CmnConst.CPAGE),true);
|
if(dt != null){
|
for (int i = 0; i < dt.getRows(); i++) {
|
FieldSetEntity fsz = dt.getFieldSetEntity(i);
|
DataTableEntity dtsub = fsz.getSubDataTable("product_project_contract_info_sub");
|
double invoice_amount = 0;//开票金额
|
double collection_amount = 0;//回款金额
|
if(dtsub!=null){
|
for (int j = 0; j < dtsub.getRows(); j++) {
|
FieldSetEntity fszsub = dtsub.getFieldSetEntity(j);
|
if(fszsub.getDouble("invoiced")!=null){
|
invoice_amount += fszsub.getDouble("invoiced");
|
}
|
if(fszsub.getDouble("payment_received")!=null){
|
collection_amount += fszsub.getDouble("payment_received");
|
}
|
}
|
}
|
fsz.setValue("invoice_amount",invoice_amount);
|
fsz.setValue("collection_amount",collection_amount);
|
}
|
}
|
baseDao.loadPromptData(dt);
|
return dt;
|
}
|
|
/**
|
* 查询合同详情
|
* @param fs
|
* @return
|
* @throws BaseException
|
*/
|
public FieldSetEntity findContractInfo(FieldSetEntity fs) throws BaseException {
|
FieldSetEntity cfs =baseDao.getFieldSetEntity(CmnConst.LX_PROJECT_CONTRACT_INFO,fs.getUUID(),true);
|
DataTableEntity sub=cfs.getSubDataTable("product_project_contract_info_sub");
|
StringBuilder filter=new StringBuilder(128);
|
filter.append(" uuid='"+cfs.getValues().get("project_uuid")+"'");
|
DataTableEntity product_project_business = baseDao.listTable("product_project_business",filter.toString(),new Object[]{});
|
String guarantee_period = product_project_business.getData().get(0).getValues().get("guarantee_period").toString();
|
StringBuilder dictFilter=new StringBuilder(128);
|
dictFilter.append("dict_name='项目管理项目类型' and dict_value="+product_project_business.getData().get(0).getValues().get("project_type").toString());
|
DataTableEntity product_sys_dict = baseDao.listTable("product_sys_dict", dictFilter.toString(), new Object[]{});
|
//合同有效期:如果是开发项目、实施项目,从项目信息的质保期带过来
|
String dict_label = product_sys_dict.getData().get(0).getValues().get("dict_label").toString();
|
if(dict_label.equals("开发项目") ||dict_label.equals("实施项目")){
|
cfs.setValue("guarantee_period",guarantee_period);
|
cfs.setValue("project_type_now",dict_label);
|
}
|
//结束日期: 如果是开发项目、实施项目,验收日期+合同有效期,维护合同则是签定日期+合同有效期
|
if(dict_label.equals("开发项目") || dict_label.equals("实施项目")){
|
String carefully_check = product_project_business.getData().get(0).getValues().get("carefully_check").toString();
|
String[] dateTime = carefully_check.split(" ");
|
String time=dateTime[0];
|
String date = getDate(time,guarantee_period);
|
cfs.setValue("end_time",date);
|
}
|
return cfs;
|
}
|
|
|
//日期计算方式
|
public String getDate(String dateTime,String carefully_time){
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
LocalDate date = LocalDate.parse(dateTime, formatter);
|
// 加上指定的月份数,例如加3个月
|
LocalDate newDate = date.plusMonths(Integer.parseInt(carefully_time));
|
|
return newDate.format(formatter);
|
}
|
|
/**
|
* 根据合同名查询合同详情
|
* @param fs
|
* @return
|
* @throws BaseException
|
*/
|
public FieldSetEntity getContractInfoByName(FieldSetEntity fs) throws BaseException {
|
FieldSetEntity fss = baseDao.getFieldSetEntity(CmnConst.LX_PROJECT_CONTRACT_INFO,fs.getUUID(),true);
|
FieldSetEntity fsProject = baseDao.getFieldSetEntity(CmnConst.PRODUCT_PROJECT_BUSINESS,fss.getString("project_uuid"),false);
|
FieldSetEntity contractName = baseDao.getFieldSetEntityBySQL("SELECT sum(invoice_amount) invoice_amount FROM product_project_contract_invoice_info WHERE contract_invoice_uuid in(SELECT uuid FROM product_project_contract_invoice where contract_name=?)", new String[]{fs.getString("uuid")},false);
|
if (contractName==null||StringUtils.isEmpty(contractName.getString("invoice_amount"))){
|
fss.setValue("invoice_amount",0);
|
}else {
|
fss.setValue("invoice_amount",contractName.getString("invoice_amount"));
|
}
|
|
fss.setValue("collection_amount",0);
|
fss.setValue("project_num",fsProject.getString("project_num"));
|
fss.setValue("project_name",fsProject.getString("project_name"));
|
return fss;
|
}
|
|
/**
|
* 合同详情页面数据查询
|
* @param fs
|
* @return
|
* @throws BaseException
|
*/
|
public FieldSetEntity getContractInfo(FieldSetEntity fs) throws BaseException {
|
//合同详情
|
FieldSetEntity fss = baseDao.getFieldSetEntity(CmnConst.LX_PROJECT_CONTRACT_INFO,fs.getUUID(),true);
|
//合同变更表
|
FieldSetEntity fschange = baseDao.getFieldSetEntityByFilter(CmnConst.LX_PROJECT_CONTRACT_CHANGE," contract_name=? and created_utc_datetime = (select MAX(created_utc_datetime) from "+CmnConst.LX_PROJECT_CONTRACT_CHANGE+" WHERE contract_name=?) ",new Object[]{fs.getUUID(),fs.getUUID()},true);
|
if(fschange!=null){
|
fss.setValue("account_manager",fschange.getString("account_manager"));
|
fss.setValue("contract_amount",fschange.getString("change_the_amount"));
|
fss.setValue("total_investment",fschange.getInteger("change_the_amount"));
|
}
|
FieldSetEntity fsinvo = null;//开票信息
|
FieldSetEntity fspaym = null;//回款信息
|
DataTableEntity dtproj = null;//项目信息
|
if(fss!=null){
|
fsinvo = baseDao.getFieldSetEntityByFilter(CmnConst.LX_PROJECT_CONTRACT_INVOICE,"contract_name=?",new Object[]{fss.getUUID()},true);
|
fspaym = baseDao.getFieldSetEntityByFilter(CmnConst.LX_PROJECT_CONTRACT_PAYMENT,"contract_name=?",new Object[]{fss.getUUID()},true);
|
dtproj = baseDao.listTable(CmnConst.PRODUCT_PROJECT_BUSINESS," uuid=? ",new Object[]{fss.getString("project_uuid")});
|
}
|
DataTableEntity dt = fss.getSubDataTable(CmnConst.LX_PROJECT_CONTRACT_INFO_SUB);
|
FieldSetEntity fsub = null;
|
int invoiced = 0;//已开票金额
|
int payment_received = 0;//已回款金额
|
if(dt!=null && dt.getRows()>0){
|
for (int i = 0; i < dt.getRows(); i++) {
|
fsub = dt.getFieldSetEntity(i);
|
String invo = fsub.getString("invoiced");
|
String paym = fsub.getString("payment_received");
|
if(BaseUtil.strIsNull(invo)){
|
invo = "0";
|
}
|
if(BaseUtil.strIsNull(paym)){
|
paym = "0";
|
}
|
invoiced += Integer.parseInt(invo);
|
payment_received += Integer.parseInt(paym);
|
}
|
}
|
FieldSetEntity fsperf = null;//履约保证金
|
int earnest_money = 0;//履约保证金金额
|
DataTableEntity dtperf = baseDao.listTable(CmnConst.LX_PROJECT_PERFORMANCE_BOND," project_uuid=? and is_pay=? ",new Object[]{fs.getString("project_uuid"),1});
|
if(dtperf != null){
|
for (int i = 0; i < dtperf.getRows(); i++) {
|
fsperf = dtperf.getFieldSetEntity(i);
|
String earn = fsperf.getString("earnest_money");
|
if(BaseUtil.strIsNull(earn)){
|
earn = "0";
|
}
|
earnest_money += Integer.parseInt(earn);
|
}
|
}
|
int projecting = 0;//在建项目
|
int projected = 0;//完成项目
|
if(dtproj!=null){
|
for (int i = 0; i < dtproj.getRows(); i++) {
|
FieldSetEntity fsproj = dtproj.getFieldSetEntity(i);
|
if(fsproj!=null && "在建".equals(fsproj.getString("project_status"))){
|
projecting++;
|
}else{
|
projected++;
|
}
|
}
|
}
|
fss.setValue("invoiced",invoiced);//已开票金额
|
fss.setValue("uinvoiced",fss.getDouble("contract_amount")-invoiced);//未开票金额
|
fss.setValue("payment_received",payment_received);//已回款金额
|
fss.setValue("upayment_received",fss.getDouble("contract_amount")-payment_received);//未回款金额
|
fss.setValue("project_total",dtproj.getRows());//项目数
|
fss.setValue("projecting",projecting);//在建项目
|
fss.setValue("projected",projected);//完成项目
|
fss.setValue("earnest_money",earnest_money);//已缴纳保证金
|
fss.setValue("uearnest_money","10");//已退保证金
|
if(fsinvo != null) {
|
DataTableEntity invoiceDataTable = fsinvo.getSubDataTable(CmnConst.LX_PROJECT_CONTRACT_INVOICE_INFO);
|
if (!BaseUtil.dataTableIsEmpty(invoiceDataTable)) {
|
baseDao.loadPromptData(invoiceDataTable);
|
fss.addSubDataTable(invoiceDataTable);//开票信息子表
|
}
|
}
|
if(fspaym != null) {
|
DataTableEntity paymentDataTable = fspaym.getSubDataTable(CmnConst.LX_PROJECT_CONTRACT_PAYMENT_COLLECTION);
|
if (!BaseUtil.dataTableIsEmpty(paymentDataTable)) {
|
baseDao.loadPromptData(paymentDataTable);
|
fss.addSubDataTable(paymentDataTable);//回款信息子表
|
}
|
}
|
fss.addSubDataTable(dtproj);//项目信息
|
return fss;
|
}
|
/**
|
* 项目是否已有合同
|
* @param fs
|
* @return
|
* @throws BaseException
|
*/
|
public String isAddContract(FieldSetEntity fs) throws BaseException {
|
String uuid = fs.getUUID();
|
DataTableEntity dataTableEntity = baseDao.listTable(" SELECT * FROM product_project_contract_info where project_uuid = ? ", new String[]{uuid});
|
if (BaseUtil.dataTableIsEmpty(dataTableEntity)) {
|
return "0";
|
} else {
|
return "1";
|
}
|
}
|
|
/**
|
* 获取合同盖章
|
* @param fs
|
* @return
|
* @throws BaseException
|
*/
|
public FieldSetEntity getContractStamp(FieldSetEntity fs) throws BaseException {
|
//合同uuid
|
String uuid = fs.getUUID();
|
//根据条件查询合同盖章
|
FieldSetEntity fieldSetEntity = baseDao.getFieldSetEntityByFilter(CmnConst.LX_PROJECT_CONTRACT_STAMP, " contract_uuid = ? ", new String[] {uuid}, false);
|
FieldSetEntity contractInfoField = this.findContractInfo(fs);
|
if(fieldSetEntity != null){
|
//盖章时间
|
contractInfoField.setValue("stamp_time", fieldSetEntity.getString("stamp_time"));
|
//是否盖章
|
contractInfoField.setValue("is_stamp", fieldSetEntity.getString("is_stamp"));
|
//合同盖章uuid
|
contractInfoField.setValue("stamp_uuid", fieldSetEntity.getString("uuid"));
|
}
|
return contractInfoField;
|
}
|
|
|
@Override
|
@Transactional
|
public boolean saveContractStamp(FieldSetEntity fs) throws BaseException {
|
String stampUuid = fs.getString("stamp_uuid");
|
FieldSetEntity fieldSetEntity = new FieldSetEntity();
|
fieldSetEntity.setTableName(CmnConst.LX_PROJECT_CONTRACT_STAMP);
|
fieldSetEntity.setValue("stamp_time", fs.getString("stamp_time"));
|
fieldSetEntity.setValue("is_stamp", fs.getString("is_stamp"));
|
SystemUser user = SpringMVCContextHolder.getCurrentUser();
|
//为空新增
|
if(BaseUtil.strIsNull(stampUuid)){
|
fieldSetEntity.setValue("contract_uuid" ,fs.getString("uuid"));
|
fieldSetEntity.setValue(CmnConst.ORG_LEVEL_UUID, user.getOrg_level_uuid());
|
fieldSetEntity.setValue(CmnConst.CREATED_BY, user.getUser_id());
|
fieldSetEntity.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
|
baseDao.add(fieldSetEntity);
|
return true;
|
//修改
|
}else {
|
fieldSetEntity.setValue(CmnConst.UUID, stampUuid);
|
fieldSetEntity.setValue(CmnConst.UPDATED_BY, user.getUser_id());
|
fieldSetEntity.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date());
|
return baseDao.update(fieldSetEntity);
|
}
|
}
|
|
/**
|
* 获取合同终止
|
* @param fs
|
* @return
|
* @throws BaseException
|
*/
|
public FieldSetEntity getContractTermination(FieldSetEntity fs) throws BaseException {
|
//合同uuid
|
String uuid = fs.getUUID();
|
//根据条件查询合同盖章
|
FieldSetEntity fieldSetEntity = baseDao.getFieldSetEntityByFilter(CmnConst.LX_PROJECT_CONTRACT_TERMINATION, " contract_uuid = ? ", new String[] {uuid}, false);
|
FieldSetEntity contractInfoField = this.findContractInfo(fs);
|
if(fieldSetEntity != null){
|
//终止时间
|
contractInfoField.setValue("termination_time", fieldSetEntity.getString("termination_time"));
|
//终止原因
|
contractInfoField.setValue("termination_cause", fieldSetEntity.getString("termination_cause"));
|
//合同终止uuid
|
contractInfoField.setValue("termination_uuid", fieldSetEntity.getString("uuid"));
|
}
|
return contractInfoField;
|
}
|
|
|
@Override
|
@Transactional
|
public boolean saveContractTermination(FieldSetEntity fs) throws BaseException {
|
String terminationUuid = fs.getString("termination_uuid");
|
FieldSetEntity fieldSetEntity = new FieldSetEntity();
|
fieldSetEntity.setTableName(CmnConst.LX_PROJECT_CONTRACT_TERMINATION);
|
fieldSetEntity.setValue("termination_time", fs.getString("termination_time"));
|
fieldSetEntity.setValue("termination_cause", fs.getString("termination_cause"));
|
SystemUser user = SpringMVCContextHolder.getCurrentUser();
|
//为空新增
|
if(BaseUtil.strIsNull(terminationUuid)){
|
fieldSetEntity.setValue("contract_uuid" ,fs.getString("uuid"));
|
fieldSetEntity.setValue(CmnConst.ORG_LEVEL_UUID, user.getOrg_level_uuid());
|
fieldSetEntity.setValue(CmnConst.CREATED_BY, user.getUser_id());
|
fieldSetEntity.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
|
baseDao.add(fieldSetEntity);
|
return true;
|
//修改
|
}else {
|
fieldSetEntity.setValue(CmnConst.UUID, terminationUuid);
|
fieldSetEntity.setValue(CmnConst.UPDATED_BY, user.getUser_id());
|
fieldSetEntity.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date());
|
return baseDao.update(fieldSetEntity);
|
}
|
}
|
|
|
/**
|
* 获取合同归档
|
* @param fs
|
* @return
|
* @throws BaseException
|
*/
|
public FieldSetEntity getContractFile(FieldSetEntity fs) throws BaseException {
|
//合同uuid
|
String uuid = fs.getUUID();
|
//根据条件查询合同盖章
|
FieldSetEntity fieldSetEntity = baseDao.getFieldSetEntityByFilter(CmnConst.LX_PROJECT_CONTRACT_FILE, " contract_uuid = ? ", new String[] {uuid}, false);
|
FieldSetEntity contractInfoField = this.findContractInfo(fs);
|
if(fieldSetEntity != null){
|
//收回时间
|
contractInfoField.setValue("recovery_time", fieldSetEntity.getString("recovery_time"));
|
//是否归档
|
contractInfoField.setValue("is_file", fieldSetEntity.getString("is_file"));
|
//归档位置
|
contractInfoField.setValue("file_location", fieldSetEntity.getString("file_location"));
|
//收回类型
|
contractInfoField.setValue("recovery_type", fieldSetEntity.getString("recovery_type"));
|
//合同归档uuid
|
contractInfoField.setValue("file_uuid", fieldSetEntity.getString("uuid"));
|
//合同附件uuid
|
contractInfoField.setValue("attachment_uuid", fieldSetEntity.getString("attachment_uuid"));
|
}
|
return contractInfoField;
|
}
|
|
@Override
|
@Transactional
|
public boolean saveContractFile(FieldSetEntity fs) throws BaseException {
|
String fileUuid = fs.getString("file_uuid");
|
FieldSetEntity fieldSetEntity = new FieldSetEntity();
|
fieldSetEntity.setTableName(CmnConst.LX_PROJECT_CONTRACT_FILE);
|
fieldSetEntity.setValue("recovery_time", fs.getString("recovery_time"));
|
fieldSetEntity.setValue("is_file", fs.getString("is_file"));
|
//归档位置
|
String fileLocation = fs.getString("file_location");
|
fieldSetEntity.setValue("file_location", fileLocation);
|
fieldSetEntity.setValue("recovery_type", fs.getString("recovery_type"));
|
String attachmentUuid = fs.getString("attachment_uuid");
|
//附件uuid
|
fieldSetEntity.setValue("attachment_uuid", attachmentUuid);
|
SystemUser user = SpringMVCContextHolder.getCurrentUser();
|
if(!BaseUtil.strIsNull(fileLocation) && !BaseUtil.strIsNull(attachmentUuid)){
|
FieldSetEntity attFieldSet = baseDao.getFieldSetEntity("product_sys_attachments", attachmentUuid, false);
|
String attachmentTypeUuid = attFieldSet.getString("attachment_type_uuid");
|
FieldSetEntity dictField = baseDao.getFieldSetEntity("product_sys_dict", attachmentTypeUuid, false);
|
FieldSetEntity documentFse = new FieldSetEntity();
|
documentFse.setTableName("product_sys_document");
|
documentFse.setValue("attachments_uuid", attachmentUuid);
|
documentFse.setValue(CmnConst.CREATED_BY, user.getUser_id());
|
documentFse.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
|
documentFse.setValue("directory_uuid", fileLocation);
|
|
documentFse.setValue("file_name", attFieldSet.getString("file_name"));
|
documentFse.setValue("file_size", attFieldSet.getString("attachment_size"));
|
documentFse.setValue("file_type", dictField.getString("dict_label"));
|
baseDao.add(documentFse);
|
}
|
//为空新增
|
if(BaseUtil.strIsNull(fileUuid)){
|
fieldSetEntity.setValue("contract_uuid" ,fs.getString("uuid"));
|
fieldSetEntity.setValue(CmnConst.ORG_LEVEL_UUID, user.getOrg_level_uuid());
|
fieldSetEntity.setValue(CmnConst.CREATED_BY, user.getUser_id());
|
fieldSetEntity.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
|
baseDao.add(fieldSetEntity);
|
return true;
|
//修改
|
}else {
|
fieldSetEntity.setValue(CmnConst.UUID, fileUuid);
|
fieldSetEntity.setValue(CmnConst.UPDATED_BY, user.getUser_id());
|
fieldSetEntity.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date());
|
return baseDao.update(fieldSetEntity);
|
}
|
}
|
}
|