zm
2025-03-12 0865fa0747b8fdcd6049ad001e207e012dfde25d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.product.contract.service;
 
import com.product.common.lang.StringUtils;
import com.product.contract.config.CmnConst;
import com.product.contract.service.ide.IContractPaymentService;
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.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
 
import java.util.Date;
 
/**
 * Copyright LX
 *
 * @Title: ContractPaymentService
 * @Project: product-server
 * @date: 2021-07-13 17:22
 * @author: ZhouJie
 * @Description: 合同回款
 */
@Component
public class ContractPaymentService extends AbstractBaseService implements IContractPaymentService {
    @Autowired
    public BaseDao baseDao;
    @Autowired
    PermissionService permissionService;
    @Autowired
    QueryFilterService queryFilterService;
 
    /**
     * 保存合同回款
     * @param fs
     * @return
     * @throws BaseException
     */
    @Override
    @Transactional
    public String saveContractPayment(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());
            String add = baseDao.add(fs, true);
            writeBackPayment(fs);
            return add;
        } else {
            fs.setValue("updated_by", SpringMVCContextHolder.getCurrentUser().getUser_id());
            fs.setValue("updated_utc_datetime",new Date());
            baseDao.update(fs);
            writeBackPayment(fs);
            return fs.getString(CmnConst.UUID);
        }
    }
    private void writeBackPayment(FieldSetEntity fs){
        //回写发票号回款
        DataTableEntity dt = fs.getSubDataTable(CmnConst.LX_PROJECT_CONTRACT_PAYMENT_COLLECTION);
        for (int i = 0; i <dt.getRows() ; i++) {
            FieldSetEntity subFs = dt.getFieldSetEntity(i);
            FieldSetEntity collection_amount_fs = baseDao.getFieldSetBySQL("SELECT sum(collection_amount) collection_amount FROM `product_project_contract_payment_collection` where invoice_num=?", new String[]{subFs.getString("invoice_num")}, false);
            baseDao.executeUpdate("UPDATE product_project_contract_invoice_info set payment_amount=? where invoice_num=?",new String[]{collection_amount_fs.getString("collection_amount"),subFs.getString("invoice_num")});
        }
        //回写条款回款
        FieldSetEntity contractTerms = baseDao.getFieldSetBySQL("SELECT sum(collection_amount) collection_amount FROM `product_project_contract_payment_collection` \n" +
                "where contract_payment_uuid in(SELECT uuid FROM product_project_contract_payment where contract_terms=?)", new String[]{fs.getString("contract_terms")}, false);
        if (contractTerms!=null&&!StringUtils.isEmpty(contractTerms.getString("collection_amount"))) {
            FieldSetEntity contractInfoSub = new FieldSetEntity();
            contractInfoSub.setTableName(CmnConst.LX_PROJECT_CONTRACT_INFO_SUB);
            contractInfoSub.setValue("uuid",fs.getString("contract_terms"));
            contractInfoSub.setValue("payment_amount",contractTerms.getString("collection_amount"));
            baseDao.update(contractInfoSub);
        }
        //回写合同总回款
        FieldSetEntity contractName = baseDao.getFieldSetBySQL("SELECT sum(collection_amount) collection_amount FROM `product_project_contract_payment_collection` \n" +
                "where contract_payment_uuid in(SELECT uuid FROM product_project_contract_payment where contract_name=?)", new String[]{fs.getString("contract_name")}, false);
        if (contractName!=null&&!StringUtils.isEmpty(contractName.getString("collection_amount"))){
            FieldSetEntity contractInfo = new FieldSetEntity();
            contractInfo.setTableName(CmnConst.LX_PROJECT_CONTRACT_INFO);
            contractInfo.setValue("uuid",fs.getString("contract_name"));
            contractInfo.setValue("collection_amount",contractName.getString("collection_amount"));
            baseDao.update(contractInfo);
        }
    }
 
    /**
     * 删除合同回款
     * @param fs
     * @return
     * @throws BaseException
     */
    @Override
    @Transactional
    public boolean deleteContractPayment(FieldSetEntity fs) throws BaseException {
        String uuid = fs.getUUID();
        String[] uuids = uuid.split(",");
        return baseDao.delete(CmnConst.LX_PROJECT_CONTRACT_PAYMENT, BaseUtil.buildQuestionMarkFilter(CmnConst.UUID, uuids.length, true), uuids);
    }
 
    /**
     * 合同回款列表
     * @param fs
     * @return
     */
    public DataTableEntity listContractPayment(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);
        }
        DataTableEntity dt = baseDao.listTable(CmnConst.LX_PROJECT_CONTRACT_PAYMENT,queryFilter,new Object[]{},null,null,fs.getInteger(CmnConst.PAGESIZE), fs.getInteger(CmnConst.CPAGE));
        baseDao.loadPromptData(dt);
        return dt;
    }
    /**
     * 查询合同回款详情
     * @param fs
     * @return
     * @throws BaseException
     */
    public FieldSetEntity findContractPayment(FieldSetEntity fs) throws BaseException {
        return baseDao.getFieldSetEntity(CmnConst.LX_PROJECT_CONTRACT_PAYMENT,fs.getUUID(),true);
    }
 
    /**
     * 回款报表
     * @param fs
     * @return
     * @throws BaseException
     */
    public  DataTableEntity paymentCollectionData()throws BaseException{
       return baseDao.listTable("SELECT b.project_name,g.customer_name,a.contract_name,c.clause_content,d.invoice_num,e.apply_time,d.invoice_amount,f.collection_amount,f.collection_time FROM \n" +
                "product_project_contract_info a \n" +
                "LEFT JOIN product_project_business b on a.project_uuid=b.uuid\n" +
                "LEFT JOIN product_project_contract_info_sub c on a.uuid =c.contract_info_uuid\n" +
                "LEFT JOIN product_project_contract_invoice_info d  on c.uuid=d.contract_terms\n" +
                "LEFT JOIN product_project_contract_invoice e on e.uuid=d.contract_invoice_uuid\n" +
                "LEFT JOIN product_project_contract_payment_collection f on d.invoice_num=f.invoice_num\n" +
                "LEFT JOIN product_project_customer g on b.customer_name=g.uuid",new String[]{});
    }
 
}