BAOLH
2026-07-16 97d2daf71d472fedfd984696836d3ca67e76f42e
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.quartz.service.impl;
 
 
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.assetManagement.domain.AssetManagement;
import me.zhengjie.modules.assetManagement.repository.AssetManagementRepository;
import me.zhengjie.modules.capital.domain.CapitalAccountCompany;
import me.zhengjie.modules.capital.repository.CapitalAccountCompanyRepository;
import me.zhengjie.modules.capitalFlow.domain.CapitalFlow;
import me.zhengjie.modules.capitalFlow.domain.CapitalFlowExpend;
import me.zhengjie.modules.capitalFlow.domain.CapitalFlowIncome;
import me.zhengjie.modules.capitalFlow.repository.CapitalFlowExpendRepository;
import me.zhengjie.modules.capitalFlow.repository.CapitalFlowIncomeRepository;
import me.zhengjie.modules.capitalFlow.repository.CapitalFlowRepository;
import me.zhengjie.modules.constant.Constant;
import me.zhengjie.modules.contract.domain.ContractPayment;
import me.zhengjie.modules.contract.repository.ContractPaymentRepository;
import me.zhengjie.modules.salary.domain.Salary;
import me.zhengjie.modules.salary.repository.SalaryRepository;
import me.zhengjie.modules.system.domain.SysCompany;
import me.zhengjie.modules.system.repository.SysCompanyRepository;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.modules.system.service.dto.UserQueryCriteria;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;
 
/**
 * @author Zheng Jie
 * @date 2019-01-07
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CapitalServiceImpl{
 
    private final CapitalAccountCompanyRepository capitalAccountCompanyRepository;
    private final UserService userService;
    private final SalaryRepository salaryRepository;
    private final AssetManagementRepository assetManagementRepository;
    private final CapitalFlowRepository capitalFlowRepository;
    private final CapitalFlowExpendRepository capitalFlowExpendRepository;
    private final ContractPaymentRepository contractPaymentRepository;
    private final CapitalFlowIncomeRepository capitalFlowIncomeRepository;
 
    /**
     * 向资金流水和流水指出添加数据
     * @param accountCompanyId
     * @param targetYearMonth
     */
    public void getCapitalInfo(Long accountCompanyId, String targetYearMonth){
        // 判断年度账号Id是否为空
        if(accountCompanyId != null){
            // 单个年度账号流水
            singleAccountCompanyFlow(accountCompanyId, targetYearMonth);
        }else{
            // 查询所有年度账号
            List<CapitalAccountCompany> capitalAccountCompanyList = capitalAccountCompanyRepository.findAll();
            for(int i = 0; i < capitalAccountCompanyList.size(); i++){
                try {
                    // 单个年度账号流水
                    singleAccountCompanyFlow(capitalAccountCompanyList.get(i).getId(), targetYearMonth);
                }catch (Exception e){
                    // 跳过本次循环
                    continue;
                }
            }
        }
    }
 
    /**
     * 单个年度账号流水
     * @param accountCompanyId
     * @param targetYearMonth
     */
    public void singleAccountCompanyFlow(Long accountCompanyId, String targetYearMonth){
        // 日历类
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, Integer.valueOf(targetYearMonth.substring(0,4)));
        // 设置月份
        cal.set(Calendar.MONTH, Integer.valueOf(targetYearMonth.substring(5,7)) - 1);
        // 设置日份
        cal.set(Calendar.DAY_OF_MONTH, 1);
        // 设置时数
        cal.set(Calendar.HOUR_OF_DAY, 0);
        // 设置分数
        cal.set(Calendar.MINUTE, 0);
        // 设置秒数
        cal.set(Calendar.SECOND, 0);
        // 设置毫秒数
        cal.set(Calendar.MILLISECOND, 0);
 
        // 查询资金流水
        CapitalFlow capitalFlow = capitalFlowRepository.findByFlowYearMonthAndAccountCompanyId(new Timestamp(cal.getTime().getTime()), accountCompanyId);
        // 判断是否为空
        if(ObjectUtils.isEmpty(capitalFlow)){
            capitalFlow = new CapitalFlow();
        }
        capitalFlow.setFlowYearMonth(new Timestamp(cal.getTime().getTime()));
        capitalFlow.setAccountCompanyId(accountCompanyId);
        // 资金流水管理添加
        capitalFlowRepository.save(capitalFlow);
 
        // 查询流水支出管理
        List<CapitalFlowExpend> capitalFlowExpends = capitalFlowExpendRepository.findByFlowId(capitalFlow.getId());
        // 流水支出管理不为空,数据全部删除(手动添加的也删除)
        for(int i = 0; i < capitalFlowExpends.size(); i++){
            capitalFlowExpendRepository.delete(capitalFlowExpends.get(i));
        }
 
        // 查询流水收入管理
        List<CapitalFlowIncome> capitalFlowIncomes = capitalFlowIncomeRepository.findByFlowId(capitalFlow.getId());
        // 流水收入管理不为空,数据全部删除
        for(int i = 0; i< capitalFlowIncomes.size(); i++){
            capitalFlowIncomeRepository.delete(capitalFlowIncomes.get(i));
        }
 
//        // 工资支出总金额
//        Double totalWageExpenditure = 0.0;
//        // 用户检索类
//        UserQueryCriteria userQueryCriteria = new UserQueryCriteria();
//        userQueryCriteria.setEnabled(true);
//        // 获取用户列表
//        List<UserDto> userDtoList = userService.queryAll(userQueryCriteria);
//        for(int i = 0; i < userDtoList.size(); i++){
//            // 判断员工是否属于当前公司
//            if(userDtoList.get(i).getCompanyId().equals(companyId)){
//                // 根据日期和员工查询工资条
//                List<Salary> salaryList = salaryRepository.findByUserIdAndSalaryYearMonth(userDtoList.get(i).getId(), new Timestamp(cal.getTime().getTime()));
//                for(int i1 = 0; i1 < salaryList.size(); i1++){
//                    totalWageExpenditure = totalWageExpenditure + Double.valueOf(salaryList.get(i1).getPreTaxAmount().toString());
//                }
//            }
//        }
//        // 工资支出总金额不为0的话就往流水支出管理添加数据
//        if(!totalWageExpenditure.equals(0.0)){
//            // 调用方法添加
//            saveCapitalFlowExpend(capitalFlow.getId(),totalWageExpenditure, Constant.DAILY_PAYROLL_EXPEND);
//        }
 
 
//        // 资产支出总金额(固定)
//        Double totalAssetsExpenditureFixed = 0.0;
//        // 根据日期和公司查询固定资产
//        List<AssetManagement> assetManagementList = assetManagementRepository.findByDateAndCompany(targetYearMonth.substring(0,7), companyId, Constant.ACCET_TYPE_FIXED);
//        for(int i = 0; i < assetManagementList.size(); i++){
//            totalAssetsExpenditureFixed = totalAssetsExpenditureFixed + Double.valueOf(assetManagementList.get(i).getBuyMoney().toString());
//        }
//        // 固定资产支出总金额不为0的话就往流水支出管理添加数据
//        if(!totalAssetsExpenditureFixed.equals(0.0)){
//            // 调用方法添加
//            saveCapitalFlowExpend(capitalFlow.getId(),totalAssetsExpenditureFixed, Constant.ASSET_FLOW_EXPEND_G);
//        }
 
//        // 资产支出总金额(流动)
//        Double totalAssetsExpenditureFlow = 0.0;
//        // 根据日期和公司查询流动资产
//        List<AssetManagement> assetManagementLists = assetManagementRepository.findByDateAndCompany(targetYearMonth.substring(0,7), companyId, Constant.ACCET_TYPE_FLOW);
//        for(int i = 0; i < assetManagementLists.size(); i++){
//            totalAssetsExpenditureFlow = totalAssetsExpenditureFlow + Double.valueOf(assetManagementLists.get(i).getBuyMoney().toString());
//        }
//        // 流动资产支出总金额不为0的话就往流水支出管理添加数据
//        if(!totalAssetsExpenditureFlow.equals(0.0)){
//            // 调用方法添加
//            saveCapitalFlowExpend(capitalFlow.getId(),totalAssetsExpenditureFlow, Constant.ASSET_FLOW_EXPEND_L);
//        }
 
//        // 合同支出总金额
//        Double contractExpenditure = 0.0;
//        // 根据日期和公司查询合同付款
//        List<ContractPayment> contractPayments = contractPaymentRepository.findByDateAndCompany(targetYearMonth.substring(0,7), companyId, Constant.CONTRACR_EXPENDITURE);
//        for(int i = 0; i < contractPayments.size(); i++){
//            contractExpenditure = contractExpenditure + Double.valueOf(contractPayments.get(i).getPaymentAmount().toString());
//        }
//        // 合同支出总金额不为0的话就往流水支出管理添加数据
//        if(!contractExpenditure.equals(0.0)){
//            // 调用方法添加
//            saveCapitalFlowExpend(capitalFlow.getId(),contractExpenditure, Constant.CONTRACT_EXPENSES);
//        }
 
//        // 合同收入总金额
//        Double contractRevenue = 0.0;
//        // 根据日期和公司查询合同收入
//        List<ContractPayment> contractPaymentList = contractPaymentRepository.findByDateAndCompany(targetYearMonth.substring(0,7), companyId, Constant.CONTRACR_REVENUE);
//        for(int i = 0; i < contractPaymentList.size(); i++){
//            contractRevenue = contractRevenue + Double.valueOf(contractPaymentList.get(i).getPaymentAmount().toString());
//        }
//        // 合同收入总金额不为0就往流水收入管理添加数据
//        if(!contractRevenue.equals(0.0)){
//            CapitalFlowIncome capitalFlowIncome = new CapitalFlowIncome();
//            capitalFlowIncome.setFlowId(capitalFlow.getId());
//            capitalFlowIncome.setAmount(new BigDecimal(contractRevenue));
//            capitalFlowIncome.setTitle(Constant.CONTRACT_EXPENSES_S);
//            capitalFlowIncomeRepository.save(capitalFlowIncome);
//        }
    }
 
 
    /**
     * 流水支出管理添加
     * @param id
     * @param totalExpenditure
     * @param title
     */
    public void saveCapitalFlowExpend(Long id, Double totalExpenditure, String title){
        CapitalFlowExpend capitalFlowExpend = new CapitalFlowExpend();
        capitalFlowExpend.setFlowId(id);
        capitalFlowExpend.setTitle(title);
        capitalFlowExpend.setAmount(new BigDecimal(totalExpenditure));
        capitalFlowExpendRepository.save(capitalFlowExpend);
    }
 
 
}