ContractServiceImpl.java 45.2 KB
Newer Older
manxiaoqiang committed
1 2
package common.service.impl;

manxiaoqiang committed
3 4 5
import adi.dic.PackageEnum;
import adi.model.ADIUser;
import adi.service.ADIAccountService;
manxiaoqiang committed
6 7 8
import common.model.*;
import common.repository.*;
import common.service.ContractService;
manxiaoqiang committed
9 10 11 12
import common.service.UserService;
import dic.ContractStatusEnum;
import dic.ContractTypeEnum;
import dic.RoleEnum;
manxiaoqiang committed
13 14
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
kangxiaoshan committed
15
import org.springframework.util.StringUtils;
manxiaoqiang committed
16 17
import tkio.model.Account;
import tkio.model.IncrementFlow;
manxiaoqiang committed
18
import tkio.model.PackageType;
manxiaoqiang committed
19 20
import tkio.repository.AccountRepository;
import tkio.repository.IncrementFlowRepository;
manxiaoqiang committed
21
import tkio.repository.PackageTypeRepository;
manxiaoqiang committed
22
import tkio.service.AccountService;
manxiaoqiang committed
23 24 25
import tkio.service.TkioAccountService;
import userio.service.AccountIOService;
import userio.service.IOAccountService;
manxiaoqiang committed
26 27 28 29 30 31 32 33
import util.*;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
manxiaoqiang committed
34
import java.math.BigDecimal;
manxiaoqiang committed
35
import java.math.BigInteger;
manxiaoqiang committed
36
import java.util.*;
manxiaoqiang committed
37 38 39 40 41

/**
 * Created by mxq on 2018/1/16.
 */
@Service
kangxiaoshan committed
42
public class ContractServiceImpl implements ContractService {
manxiaoqiang committed
43

kangxiaoshan committed
44

kangxiaoshan committed
45 46 47 48
    private static final Map<String, String> CONTRACT_TYPE = new HashMap() {{
        put("0", "首次签约");
        put("1", "续约");
        put("2", "合同变更");
kangxiaoshan committed
49 50 51

    }};

manxiaoqiang committed
52 53 54 55 56 57 58 59 60 61 62 63
    @Autowired
    ContractRepository contractRepository;
    @Autowired
    ContractBodyRepository contractBodyRepository;
    @Autowired
    Account4WebRepository account4WebRepository;
    @Autowired
    PackageTypeRepository packageTypeRepository;
    @Autowired
    PriceLevelMoneyRepository priceLevelMoneyRepository;
    @Autowired
    UserRepository userRepository;
manxiaoqiang committed
64 65 66 67
    @Autowired
    ContractChangeRepository contractChangeRepository;
    @Autowired
    ContractMoneyRepository contractMoneyRepository;
manxiaoqiang committed
68 69
    @Autowired
    AccountService accountService;
manxiaoqiang committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    @Autowired
    AccountIOService accountIOService;
    @Autowired
    IOAccount4WebRepository ioAccount4WebRepository;
    @Autowired
    ContractIncrementRepository contractIncrementRepository;
    @Autowired
    TkioAccountService tkioAccountService;
    @Autowired
    IOAccountService ioAccountService;
    @Autowired
    AccountRepository accountRepository;
    @Autowired
    UserService userService;
    @Autowired
    IncrementFlowRepository incrementFlowRepository;
manxiaoqiang committed
86 87
    @Autowired
    BPUContractRepository bpuContractRepository;
88
    @Autowired
manxiaoqiang committed
89
    ADIAccountService adiAccountService;
manxiaoqiang committed
90

kangxiaoshan committed
91 92 93 94 95 96 97
    @Autowired
    TradeTypeRepsitory tradeTypeRepsitory;

    @Autowired
    ContractRelationRepository contractRelationRepository;


manxiaoqiang committed
98
    @Override
manxiaoqiang committed
99
    public Map<String, Object> checkAccount(String email, String platfrom) {
manxiaoqiang committed
100
        Map<String, Object> map = new HashMap<>();
manxiaoqiang committed
101 102
        Account4Web account4Web = null;
        IOAccount4Web ioAccount4Web = null;
kangxiaoshan committed
103
        ADIUser adiUser = null;
kangxiaoshan committed
104 105
        platfrom = "tkio";

kangxiaoshan committed
106
        if ("tkio".equals(platfrom)) {
manxiaoqiang committed
107
            account4Web = account4WebRepository.findByEmail(email);
kangxiaoshan committed
108
        } else if ("io".equals(platfrom)) {
manxiaoqiang committed
109
            ioAccount4Web = ioAccount4WebRepository.findByEmail(email);
kangxiaoshan committed
110
        } else if ("adi".equals(platfrom)) {
manxiaoqiang committed
111
            adiUser = adiAccountService.findOne(email);
manxiaoqiang committed
112
        }
kangxiaoshan committed
113
        if (null == account4Web && null == ioAccount4Web && null == adiUser) {
manxiaoqiang committed
114
            map.put("result", 0);
kangxiaoshan committed
115 116
        } else {
            BigInteger integer = contractRepository.countNumByEmail(email, platfrom);
manxiaoqiang committed
117
            map.put("result", 1);
kangxiaoshan committed
118
            if (platfrom.equals("adi")) {
manxiaoqiang committed
119
                map.put("company", adiUser.getCompany());
kangxiaoshan committed
120
            } else {
manxiaoqiang committed
121 122 123
                map.put("company", null == account4Web ? ioAccount4Web.getCompany() : account4Web.getCompany());
            }

manxiaoqiang committed
124
            map.put("num", integer.intValue());
manxiaoqiang committed
125
            Contract contract = contractRepository.findByPlatformAndEmailLimitVaild(platfrom, email);
kangxiaoshan committed
126
            if (null == contract) {
manxiaoqiang committed
127
                map.put("status", false);
kangxiaoshan committed
128
            } else {
manxiaoqiang committed
129 130
                map.put("status", true);
            }
manxiaoqiang committed
131
        }
manxiaoqiang committed
132

manxiaoqiang committed
133 134 135
        return map;
    }

manxiaoqiang committed
136 137 138
    @Override
    public Boolean checkTime(String email, String platfrom, String type, String product) {
        Boolean flag = false;
kangxiaoshan committed
139
        if (ContractTypeEnum.MAIN.getKey().equals(type)) {
manxiaoqiang committed
140
            Contract contract = contractRepository.findByPlatformAndEmailLimit1(platfrom, email, type);
kangxiaoshan committed
141
            if (null == contract) {
manxiaoqiang committed
142
                flag = true;
kangxiaoshan committed
143
            } else {
manxiaoqiang committed
144
                String beforeDay = DateUtil.getBeforeDays(180);
kangxiaoshan committed
145
                if (beforeDay.compareTo(contract.getEndDate()) > 0) {
manxiaoqiang committed
146 147 148
                    flag = true;
                }
            }
kangxiaoshan committed
149
        } else {
manxiaoqiang committed
150 151
            ContractIncrement increment = contractIncrementRepository.findByCode(product);
            Contract contract = contractRepository.findByPlatformAndEmailAndPricelevelLimit1(platfrom, email, type, increment.getId());
kangxiaoshan committed
152
            if (null == contract) {
manxiaoqiang committed
153
                flag = true;
kangxiaoshan committed
154
            } else {
manxiaoqiang committed
155 156
                String beforeDay = DateUtil.getBeforeDays(180);
                String endDate = contract.getEndDate() == null ? contract.getDs() : contract.getEndDate();
kangxiaoshan committed
157
                if (beforeDay.compareTo(endDate) > 0) {
manxiaoqiang committed
158 159 160 161 162 163 164
                    flag = true;
                }
            }
        }
        return flag;
    }

manxiaoqiang committed
165 166 167 168 169 170 171 172 173
    @Override
    public List<ContractBody> findBody() {
        return contractBodyRepository.findAll();
    }

    @Override
    public String getContractCode(String code) {
        BigInteger integer = contractRepository.countNumByDs(DateUtil.getBeforeDays(0));
        String lastCode = "";
kangxiaoshan committed
174
        if (integer.intValue() < 9) {
manxiaoqiang committed
175
            lastCode = "0" + (integer.intValue() + 1);
kangxiaoshan committed
176
        } else {
manxiaoqiang committed
177 178 179 180 181 182
            lastCode = "" + (integer.intValue() + 1);
        }
        String re = code + "-" + DateUtil.getDays2(0) + lastCode;
        return re;
    }

kangxiaoshan committed
183 184 185
    @Override
    public String getContractCode(String code, String platform, String company) {

kangxiaoshan committed
186
        BigInteger countContract = contractRepository.findcountContract(code);
kangxiaoshan committed
187

kangxiaoshan committed
188
        String lastCode;
kangxiaoshan committed
189

kangxiaoshan committed
190 191 192 193
        if("fake".equals(platform)){
            platform = "FZBWS";
        }

kangxiaoshan committed
194 195
        int codeInt = countContract.intValue();

kangxiaoshan committed
196 197 198 199
        if (codeInt < 9) {
            lastCode = "0" + (codeInt + 1);
        } else {
            lastCode = "" + (codeInt + 1);
kangxiaoshan committed
200
        }
kangxiaoshan committed
201
        return  code + "-" + platform.toUpperCase() + "-" + DateUtil.getDays2(0) + "-" + lastCode;
kangxiaoshan committed
202 203
    }

manxiaoqiang committed
204
    @Override
manxiaoqiang committed
205
    public List findSetmeal(String platform) {
kangxiaoshan committed
206 207 208

        platform = "tkio";

manxiaoqiang committed
209
        List list = new ArrayList();
kangxiaoshan committed
210 211
        if ("adi".equals(platform)) {
            for (int i = 1; i < 3; i++) {
manxiaoqiang committed
212 213
                Map<String, String> packageMap = new HashMap<>();
                packageMap.put("id", i + "");
kangxiaoshan committed
214
                if (i == 1) {
manxiaoqiang committed
215
                    packageMap.put("packageName", PackageEnum.PROBATION.getValue());
kangxiaoshan committed
216
                } else {
manxiaoqiang committed
217 218 219 220
                    packageMap.put("packageName", PackageEnum.FORMAL.getValue());
                }
                list.add(packageMap);
            }
kangxiaoshan committed
221 222 223 224 225 226 227 228 229
        } else {
            if ("tkio".equals(platform) || "io".equals(platform)) {
//            List<PackageType> packageTypeList = packageTypeRepository.findAll();
                List<PackageType> packageTypeList = packageTypeRepository.findIsNewAll();
                for (PackageType p : packageTypeList) {
                    p.setType("main");
                    list.add(p);
                }
                // 增值套餐
kangxiaoshan committed
230 231
//                List<ContractIncrement> increments = contractIncrementRepository.findAll();
//                list.addAll(increments);
kangxiaoshan committed
232 233
            } else if ("userdesk".equals(platform) || "dmp".equals(platform) || "fake".equals(platform)) {
                list = bpuContractRepository.findByProduct(platform);
manxiaoqiang committed
234
            }
manxiaoqiang committed
235 236 237 238 239 240 241 242
        }

        return list;
    }

    @Override
    public List<ContractIncrement> findIncrement() {
        return null;
manxiaoqiang committed
243 244 245 246
    }

    @Override
    public Double getRebat(String product, Integer level, Long money) {
manxiaoqiang committed
247
        Double rebat = 1d;
kangxiaoshan committed
248
        try {
manxiaoqiang committed
249
            PriceLevelMoney levelMoney = priceLevelMoneyRepository.getOne(product, level);
kangxiaoshan committed
250 251
            rebat = money * 1.0 / levelMoney.getMoney();
        } catch (Exception e) {
manxiaoqiang committed
252 253 254
            e.printStackTrace();
        }
        return rebat;
manxiaoqiang committed
255 256 257 258 259 260
    }

    @Override
    public List<User> getSales() {
        return userRepository.findSales();
    }
kangxiaoshan committed
261

manxiaoqiang committed
262 263 264
    @Override
    public Contract build(Contract contract) {
        User user = userRepository.findByEmail(contract.getSaleName());
kangxiaoshan committed
265
        if (null != user) {
manxiaoqiang committed
266 267
            contract.setSale(user.getId());
        }
2  
manxiaoqiang committed
268
        contract.setVirtual(true);
manxiaoqiang committed
269 270 271 272
        Account account = accountRepository.findByEmail(contract.getEmail());
        contract.setCustomerBody(account.getCompany());
        contract.setType("increment");
        contract.setPlatform("tkio");
2  
manxiaoqiang committed
273
        contract.setStartDate(DateUtil.getBeforeDays(0));
manxiaoqiang committed
274 275 276 277 278
        ContractIncrement contractIncrement = contractIncrementRepository.findByCode(contract.getPriceLevelName());
        contract.setPriceLevel(contractIncrement.getId());
        Contract contract1 = create(null, contract);
        return contract1;
    }
kangxiaoshan committed
279

manxiaoqiang committed
280 281
    @Override
    public Contract create(User loginUser, Contract resource) {
kangxiaoshan committed
282

kangxiaoshan committed
283
        if (resource == null) {
kangxiaoshan committed
284 285 286
            return null;
        }

kangxiaoshan committed
287
        Contract contractExist = contractRepository.findByCodePlatform(resource.getContractCode(), resource.getPlatform());
kangxiaoshan committed
288

kangxiaoshan committed
289 290 291 292 293 294 295 296 297
        if (contractExist != null) {
            // 合同编号已存在
            return null;
        }


        resource.setPayMoney(0L);
        resource.setInvoice(0L);
        Long extraFlow = resource.getExtraFlow();
kangxiaoshan committed
298 299 300 301
        if(extraFlow==null){
            extraFlow = 0L;
        }

kangxiaoshan committed
302 303
        resource.setDs(DateUtil.getBeforeDays(0));

kangxiaoshan committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
//        if (resource.getType().equals("main")) {
//            if (resource.getPlatform().equals("tkio")) {
//                if (!(extraFlow.longValue() == 0)) {
//                    if (resource.getPlatform().equals("tkio")) {
//                        IncrementFlow flow = new IncrementFlow();
//                        Account account = accountRepository.findByEmail(resource.getEmail());
//                        flow.setAccount(account.getId());
//                        flow.setFlow(extraFlow);
//                        flow.setEndDate(resource.getEndDate());
//                        flow.setStartDate(resource.getStartDate());
//                        flow.setContractCode(resource.getContractCode());
//                        incrementFlowRepository.save(flow);
//                    }
//                }
//            }
//        }
kangxiaoshan committed
320

kangxiaoshan committed
321
        this.dealContractStatus(resource);
kangxiaoshan committed
322

kangxiaoshan committed
323
        if (resource.getRelationContract() == null) {
kangxiaoshan committed
324 325 326 327 328
            resource.setRelationContract(-1L);
        }

        if (null != loginUser) {
            resource.setCreateAccount(loginUser.getId());
kangxiaoshan committed
329
            resource.setCreateName(loginUser.getName());
kangxiaoshan committed
330 331
        }

kangxiaoshan committed
332 333 334



kangxiaoshan committed
335 336 337
        resource.setCreateTime(new Date());

        //同步修改trackingio的数据
kangxiaoshan committed
338 339 340 341 342 343 344 345 346
//        if (resource.getPlatform().equals("tkio") && resource.getType().equals("main")) {
//            accountService.update(resource);
//        }
//        if (resource.getPlatform().equals("io") && resource.getType().equals("main")) {
//            accountIOService.update(resource);
//        }
//        if (resource.getPlatform().equals("adi")) {
//            adiAccountService.updateAccount(resource);
//        }
kangxiaoshan committed
347 348 349

        resource = contractRepository.save(resource);

kangxiaoshan committed
350 351 352 353 354 355 356
        this.saveContractRelations(resource,resource.getId());

        return resource;
    }


    private void saveContractRelations(Contract resource,Long contractId){
kangxiaoshan committed
357 358 359 360
        if (resource.getRelationContract() != null && resource.getRelationContract() > 0) {
            //保存关联合同信息
            ContractRelation relation = new ContractRelation();
            relation.setCreateTime(new Date());
kangxiaoshan committed
361
            relation.setMainContract(contractId);
kangxiaoshan committed
362 363 364
            relation.setRelationId(resource.getRelationContract());
            relation.setPlatform(resource.getPlatform());

kangxiaoshan committed
365 366 367 368
            ContractRelation relationOrig =  contractRelationRepository.findByRelationData(relation.getMainContract(),relation.getRelationId(),resource.getPlatform());
            if(relationOrig==null){
                contractRelationRepository.save(relation);
            }
kangxiaoshan committed
369

kangxiaoshan committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
            //处理被关联 合同
            Contract contract = contractRepository.findOne(resource.getRelationContract());
            if (contract.getRelationContract() == null || contract.getRelationContract() == -1) {
                contract.setRelationContract(contractId);
                contractRepository.save(contract);
            }

//            ContractRelation relationOrigNext = contractRelationRepository.findByRelationData(relation.getRelationId(), relation.getMainContract(), resource.getPlatform());
//
//            if (relationOrigNext == null) {
//                // 主动关联
//                ContractRelation relation2 = new ContractRelation();
//                relation2.setCreateTime(new Date());
//                relation2.setMainContract(resource.getRelationContract());
//                relation2.setRelationId(contractId);
//                relation2.setPlatform(resource.getPlatform());
//                contractRelationRepository.save(relation2);
//            }

        }
kangxiaoshan committed
390 391 392 393 394 395 396 397 398 399 400 401
    }


    public Contract create_bak(User loginUser, Contract resource) {

        if (resource == null) {
            return null;
        }

        Contract contractExist = contractRepository.findByCodePlatform(resource.getContractCode(), resource.getPlatform());

        if (contractExist != null) {
kangxiaoshan committed
402 403 404 405 406
            // 合同编号已存在
            return null;
        }

        // 老的逻辑
kangxiaoshan committed
407 408 409 410 411
        String contractCode = getContractCode(resource.getMyBodyCode());

        if (!contractCode.equals(resource.getContractCode())) {
            resource.setContractCode(contractCode);
        }
manxiaoqiang committed
412

manxiaoqiang committed
413 414
        resource.setPayMoney(0L);
        resource.setInvoice(0L);
manxiaoqiang committed
415
        Long extraFlow = resource.getExtraFlow();
kangxiaoshan committed
416
        resource.setDs(DateUtil.getBeforeDays(0));
2  
manxiaoqiang committed
417

kangxiaoshan committed
418 419 420 421 422
        if (resource.getType().equals("main")) {
            if (resource.getPlatform().equals("tkio")) {
                if (!(extraFlow.longValue() == 0)) {
                    if (resource.getPlatform().equals("tkio")) {
                        IncrementFlow flow = new IncrementFlow();
2  
manxiaoqiang committed
423 424 425 426 427 428 429 430
                        Account account = accountRepository.findByEmail(resource.getEmail());
                        flow.setAccount(account.getId());
                        flow.setFlow(extraFlow);
                        flow.setEndDate(resource.getEndDate());
                        flow.setStartDate(resource.getStartDate());
                        flow.setContractCode(resource.getContractCode());
                        incrementFlowRepository.save(flow);
                    }
2  
manxiaoqiang committed
431 432
                }
            }
manxiaoqiang committed
433 434 435 436 437 438 439
            if (DateUtil.getBeforeDays(0).compareTo(resource.getStartDate()) < 0) {
                resource.setDs(DateUtil.getBeforeDays(0));
                resource.setStatus("wait");
            } else {
                resource.setDs(DateUtil.getBeforeDays(0));
                resource.setStatus("executing");
            }
kangxiaoshan committed
440 441 442 443 444
        } else {
//            resource.setDs(DateUtil.getBeforeDays(0));
//            resource.setStatus("wait");
        }

kangxiaoshan committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
//        if (resource.getRebateMoney() != null) {
//            if (resource.getMoney() == null) {
//                resource.setMoney(0L);
//            }
//
//            if (resource.getRebateMoney().compareTo(BigDecimal.ZERO) < 0) {
//                //未回款
//                resource.setStatus(ContractStatusEnum.MONEY_BACK_NONE.getKey());
//
//            }else if(resource.getRebateMoney().compareTo(new BigDecimal(resource.getMoney()+""))< 0){
//                // 部分回款
//                resource.setStatus(ContractStatusEnum.MONEY_BACK_PART.getKey());
//            }else{
//                // 已回款
//                resource.setStatus(ContractStatusEnum.MONEY_BACK_ALL.getKey());
//            }
//
//        } else {
//            //未回款
//            resource.setStatus(ContractStatusEnum.MONEY_BACK_NONE.getKey());
//        }
kangxiaoshan committed
466 467 468 469 470 471 472 473 474 475 476 477 478

        Double rebat = null;
        if (null != resource.getPriceLevel()) {
            rebat = getRebat(resource.getProduct(), resource.getPriceLevel().intValue(), resource.getMoney());
        } else {
            rebat = getRebat(resource.getProduct(), 0, resource.getMoney());
        }

        BigDecimal bg = new BigDecimal(rebat);
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        resource.setRebate(f1);

        if (null != loginUser) {
manxiaoqiang committed
479
            resource.setCreateAccount(loginUser.getId());
kangxiaoshan committed
480
            resource.setCreateName(loginUser.getName());
manxiaoqiang committed
481
        }
manxiaoqiang committed
482
        resource.setCreateTime(new Date());
manxiaoqiang committed
483 484

        //同步修改trackingio的数据
kangxiaoshan committed
485
        if (resource.getPlatform().equals("tkio") && resource.getType().equals("main")) {
manxiaoqiang committed
486 487
            accountService.update(resource);
        }
kangxiaoshan committed
488
        if (resource.getPlatform().equals("io") && resource.getType().equals("main")) {
manxiaoqiang committed
489 490
            accountIOService.update(resource);
        }
kangxiaoshan committed
491
        if (resource.getPlatform().equals("adi")) {
manxiaoqiang committed
492 493
            adiAccountService.updateAccount(resource);
        }
manxiaoqiang committed
494

kangxiaoshan committed
495 496 497 498
        if (resource.getType().equals(ContractTypeEnum.MAIN.getKey())
                || (resource.getType().equals(ContractTypeEnum.INCREMENT.getKey()) && resource.getPriceLevel().equals(1L))) {
            List<Contract> contractList = contractRepository.findByPlatformAndEmail(resource.getPlatform(), resource.getEmail(), resource.getType());
            if (ValidateUtil.isValid(contractList)) {
manxiaoqiang committed
499
                List<Contract> list = new ArrayList<>();
kangxiaoshan committed
500 501 502
                for (Contract contract : contractList) {
                    if (contract.getType().equals(ContractTypeEnum.MAIN.getKey())
                            || (contract.getType().equals(ContractTypeEnum.INCREMENT.getKey()) && contract.getPriceLevel().equals(1L))) {
manxiaoqiang committed
503

kangxiaoshan committed
504 505
                        if (contract.getStatus().equals(ContractStatusEnum.EXECUTING.getKey())
                                || contract.getStatus().equals(ContractStatusEnum.WAIT.getKey())) {
manxiaoqiang committed
506 507 508 509 510 511 512 513 514 515
                            contract.setStatus(ContractStatusEnum.CANCEL.getKey());
                        }
                        list.add(contract);
                    }

                }
                contractRepository.save(contractList);
            }
        }

kangxiaoshan committed
516
        resource = contractRepository.save(resource);
kangxiaoshan committed
517 518


kangxiaoshan committed
519
        if (resource.getRelationContract() != null && resource.getRelationContract() > 0) {
kangxiaoshan committed
520 521 522 523 524 525 526 527 528 529 530
            //保存关联合同信息
            ContractRelation relation = new ContractRelation();
            relation.setCreateTime(new Date());
            relation.setMainContract(resource.getId());
            relation.setRelationId(resource.getRelationContract());
            relation.setPlatform(resource.getPlatform());
            contractRelationRepository.save(relation);

        }

        return resource;
manxiaoqiang committed
531 532 533 534
    }

    @Override
    public Contract update(User loginUser, Contract resource) {
manxiaoqiang committed
535 536
        Map<Long, String> packageMap = getPackageMap();
        Map<Long, String> saleMap = getSaleMap();
manxiaoqiang committed
537 538 539 540 541 542 543 544 545 546 547
        Contract contract = contractRepository.findOne(resource.getId());

        ContractChange cc = new ContractChange();
        cc.setType("update");
        cc.setUser(loginUser.getId());
        cc.setDs(DateUtil.getBeforeDays(0));
        cc.setContent("");
        cc.setEmail(contract.getEmail());
        cc.setCompany(contract.getCustomerBody());
        cc.setContractCode(contract.getContractCode());

kangxiaoshan committed
548 549 550


        this.dealContractStatus(resource);
kangxiaoshan committed
551

manxiaoqiang committed
552

kangxiaoshan committed
553
        if (!resource.getStartDate().equals(contract.getStartDate())) {
manxiaoqiang committed
554
            cc.setContent(cc.getContent() + "开始时间变更 : " + contract.getStartDate() + "-->" + resource.getStartDate());
manxiaoqiang committed
555
            contract.setStartDate(resource.getStartDate());
kangxiaoshan committed
556 557 558 559 560
//            if(DateUtil.getBeforeDays(0).compareTo(resource.getStartDate()) < 0){
//                contract.setStatus("wait");
//            }else{
//                contract.setStatus("executing");
//            }
manxiaoqiang committed
561
        }
kangxiaoshan committed
562
        if (!resource.getEndDate().equals(contract.getEndDate())) {
manxiaoqiang committed
563
            cc.setContent(cc.getContent() + "   结束时间变更 : " + contract.getEndDate() + "-->" + resource.getEndDate());
manxiaoqiang committed
564 565 566 567
            contract.setEndDate(resource.getEndDate());
        }

        boolean newRebat = false;
kangxiaoshan committed
568 569 570
        if (contract.getType().equals(ContractTypeEnum.MAIN.getKey()) && null != resource.getPriceLevel()) {
            if (!resource.getPriceLevel().equals(contract.getPriceLevel())) {
                cc.setContent(cc.getContent() + "  套餐变更:" + packageMap.get(contract.getPriceLevel()) + "-->" + packageMap.get(resource.getPriceLevel()));
manxiaoqiang committed
571 572 573
                newRebat = true;
                contract.setPriceLevel(resource.getPriceLevel());
            }
manxiaoqiang committed
574 575
        }

kangxiaoshan committed
576
        if (!resource.getMoney().equals(contract.getMoney())) {
manxiaoqiang committed
577
            cc.setContent(cc.getContent() + "   金额变更: " + contract.getMoney() + "-->" + resource.getMoney());
manxiaoqiang committed
578 579 580 581
            newRebat = true;
            contract.setMoney(resource.getMoney());
        }

kangxiaoshan committed
582 583 584 585 586 587
        if (!resource.getRebateMoney().equals(contract.getRebateMoney())) {
            cc.setContent(cc.getContent() + "  回款金额变更: " + contract.getMoney() + "-->" + resource.getMoney());
            newRebat = true;
            contract.setMoney(resource.getMoney());
        }

kangxiaoshan committed
588
        if (!resource.getSale().equals(contract.getSale())) {
manxiaoqiang committed
589
            cc.setContent(cc.getContent() + "销售变更:" + saleMap.get(contract.getSale()) + "-->" + saleMap.get(resource.getSale()));
manxiaoqiang committed
590 591 592
            contract.setSale(resource.getSale());
        }

kangxiaoshan committed
593 594 595 596 597 598 599 600 601 602
//        if (newRebat && !"adi".equals(contract.getPlatform())) {
//            Double rebat = getRebat(contract.getProduct(), contract.getPriceLevel().intValue(), contract.getMoney());
//            BigDecimal bg = new BigDecimal(rebat);
//            double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
////            contract.setRebate(f1);
//        }

        if (!resource.getSale().equals(contract.getSale())) {
            cc.setContent(cc.getContent() + "销售变更:" + saleMap.get(contract.getSale()) + "-->" + saleMap.get(resource.getSale()));
            contract.setSale(resource.getSale());
manxiaoqiang committed
603
        }
kangxiaoshan committed
604

manxiaoqiang committed
605
        cc.setPlatform(contract.getPlatform());
manxiaoqiang committed
606 607
        contractChangeRepository.save(cc);

manxiaoqiang committed
608
        //同步修改trackingio的数据
kangxiaoshan committed
609 610 611 612 613 614 615 616 617
//        if (contract.getPlatform().equals("tkio") && resource.getType().equals("main")) {
//            accountService.update(contract);
//        }
//        if (contract.getPlatform().equals("io") && resource.getType().equals("main")) {
//            accountIOService.update(contract);
//        }
//        if (contract.getPlatform().equals("adi")) {
//            adiAccountService.updateAccount(contract);
//        }
manxiaoqiang committed
618

kangxiaoshan committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
//        if (contract.getPlatform().equals("tkio")) {
//            if (contract.getType().equals("main")) {
//                IncrementFlow flow = incrementFlowRepository.findOne(resource.getContractCode());
//                if (null != flow) {
//                    flow.setEndDate(resource.getEndDate());
//                    flow.setStartDate(resource.getStartDate());
//                    if (contract.getExtraFlow() != resource.getExtraFlow()) {
//                        flow.setFlow(resource.getExtraFlow());
//                        incrementFlowRepository.save(flow);
//                    }
//                } else if (resource.getExtraFlow().longValue() > 0) {
//                    IncrementFlow flow1 = new IncrementFlow();
//                    Account account = accountRepository.findByEmail(contract.getEmail());
//                    flow1.setAccount(account.getId());
//                    flow1.setFlow(resource.getExtraFlow());
//                    flow1.setEndDate(resource.getEndDate());
//                    flow1.setStartDate(resource.getStartDate());
//                    flow1.setContractCode(resource.getContractCode());
//                    incrementFlowRepository.save(flow1);
//                }
//            }
//
//        }
manxiaoqiang committed
642

kangxiaoshan committed
643
        contract.setStatus(resource.getStatus());
manxiaoqiang committed
644
        contract.setExtraFlow(resource.getExtraFlow());
kangxiaoshan committed
645 646 647
        contract.setTradeType(resource.getTradeType());
        contract.setInvoiceMoney(resource.getInvoiceMoney());
        contract.setRelationContract(resource.getRelationContract());
kangxiaoshan committed
648
        contract.setRebateMoney(resource.getRebateMoney());
kangxiaoshan committed
649 650
        contract.setDiscountTimeLong(resource.getDiscountTimeLong());

kangxiaoshan committed
651 652 653 654 655 656 657
        contract.setModifyTime(new Date());
        if (null != loginUser) {
            resource.setModifyAccount(loginUser.getId());
        }


        this.saveContractRelations(resource,contract.getId());
manxiaoqiang committed
658

manxiaoqiang committed
659
        return contractRepository.save(contract);
manxiaoqiang committed
660 661
    }

kangxiaoshan committed
662

kangxiaoshan committed
663 664 665 666 667 668
    private void dealContractStatus(Contract resource){
        if (resource.getRebateMoney() != null) {
            if (resource.getMoney() == null) {
                resource.setMoney(0L);
            }

kangxiaoshan committed
669
            if (resource.getRebateMoney().compareTo(BigDecimal.ZERO) <= 0) {
kangxiaoshan committed
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
                //未回款
                resource.setStatus(ContractStatusEnum.MONEY_BACK_NONE.getKey());

            }else if(resource.getRebateMoney().compareTo(new BigDecimal(resource.getMoney()+""))< 0){
                // 部分回款
                resource.setStatus(ContractStatusEnum.MONEY_BACK_PART.getKey());
            }else{
                // 已回款
                resource.setStatus(ContractStatusEnum.MONEY_BACK_ALL.getKey());
            }

        } else {
            //未回款
            resource.setStatus(ContractStatusEnum.MONEY_BACK_NONE.getKey());
        }

kangxiaoshan committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
        if (resource.getRebateMoney() == null) {
            resource.setRebateMoney(new BigDecimal(0));
        }

        if (resource.getInvoiceMoney() == null) {
            resource.setInvoiceMoney(new BigDecimal(0));
        }

        if (resource.getDiscountTimeLong() == null) {
            resource.setDiscountTimeLong(0L);
        }

        if (resource.getExtraFlow() == null) {
            resource.setExtraFlow(0L);
        }
    }
kangxiaoshan committed
702 703


kangxiaoshan committed
704 705 706 707 708 709 710 711 712
    /**
     *
     * @param loginAccount
     * @param startDate
     * @param endDate
     * @param platform
     * @param contractId 用于 获取关联合同列表
     * @return
     */
manxiaoqiang committed
713
    @Override
kangxiaoshan committed
714
    public List<Contract> findAll(User loginAccount, String startDate, String endDate, String platform, String contractId) {
manxiaoqiang committed
715
        List<Contract> result = new ArrayList<>();
manxiaoqiang committed
716
        List<User> sales = userRepository.findAll();
manxiaoqiang committed
717
        Map<Long, String> saleMap = new HashMap<>();
kangxiaoshan committed
718 719
        if (ValidateUtil.isValid(sales)) {
            for (User s : sales) {
manxiaoqiang committed
720 721 722
                saleMap.put(s.getId(), s.getName());
            }
        }
kangxiaoshan committed
723 724 725



manxiaoqiang committed
726 727
        List<PackageType> typeList = packageTypeRepository.findAll();
        Map<Long, String> typeMap = new HashMap<>();
kangxiaoshan committed
728 729
        if (ValidateUtil.isValid(typeList)) {
            for (PackageType s : typeList) {
manxiaoqiang committed
730 731 732
                typeMap.put(s.getId(), s.getPackageName());
            }
        }
manxiaoqiang committed
733 734
        List<ContractIncrement> incrementList = contractIncrementRepository.findAll();
        Map<Long, String> incremenMap = new HashMap<>();
kangxiaoshan committed
735 736
        if (ValidateUtil.isValid(incrementList)) {
            for (ContractIncrement s : incrementList) {
manxiaoqiang committed
737 738 739
                incremenMap.put(s.getId(), s.getPackageName());
            }
        }
manxiaoqiang committed
740 741
        List<BPUContract> bpuContractList = bpuContractRepository.findAll();
        Map<Long, String> bpuMap = new HashMap<>();
kangxiaoshan committed
742 743
        if (ValidateUtil.isValid(bpuContractList)) {
            for (BPUContract s : bpuContractList) {
manxiaoqiang committed
744 745 746
                bpuMap.put(s.getId(), s.getPackageName());
            }
        }
kangxiaoshan committed
747

kangxiaoshan committed
748
        List<String> platforms = new ArrayList<>();
kangxiaoshan committed
749
        Map<String,String> idsDic = new HashMap<>();
kangxiaoshan committed
750
        platforms.add(platform);
kangxiaoshan committed
751

kangxiaoshan committed
752
        if (!StringUtils.isEmpty(contractId)) {
kangxiaoshan committed
753
            //获取 关联合同列表
kangxiaoshan committed
754 755 756 757
            List<String> conIds = new ArrayList<>();
            conIds.add(contractId);
            this.getContractRelationIds(conIds,idsDic);
            idsDic.remove(contractId);
kangxiaoshan committed
758 759 760
            if("fake".equals(platform)){
                platforms.add("tkio");
            }
kangxiaoshan committed
761 762
        }

kangxiaoshan committed
763 764
        List<Contract> contractList = new ArrayList<>();

kangxiaoshan committed
765
        List<String> ids  = new ArrayList<>(idsDic.keySet());
kangxiaoshan committed
766 767

        if (loginAccount.getRole().equals(RoleEnum.SOUTH_BUSSINUSS.getKey()) || loginAccount.getRole().equals(RoleEnum.NORTH_BUSSINUSS.getKey())) {
manxiaoqiang committed
768 769 770

            List<User> userList = userService.findAllSons(loginAccount.getId());
            List<Long> idList = new ArrayList<>();
manxiaoqiang committed
771
            idList.add(loginAccount.getId());
kangxiaoshan committed
772
            for (User u : userList) {
manxiaoqiang committed
773 774 775
                idList.add(u.getId());
            }

kangxiaoshan committed
776 777
//            contractList = contractRepository.findByDsAndRoile(startDate, endDate, platfrom, idList);

kangxiaoshan committed
778 779 780

            if(!StringUtils.isEmpty(contractId)){
                //查看关联合同
kangxiaoshan committed
781
                if (ids != null && ids.size() > 0) {
kangxiaoshan committed
782
                    contractList = contractRepository.findByDsAndRoileRelation(startDate, endDate, platforms, idList, ids);
kangxiaoshan committed
783 784 785
                }

            }else{
kangxiaoshan committed
786 787
                contractList = contractRepository.findByDsAndRoile(startDate, endDate, platform, idList);
            }
kangxiaoshan committed
788 789


kangxiaoshan committed
790
        } else {
kangxiaoshan committed
791
//            contractList = contractRepository.findByDs(startDate, endDate, platform);
kangxiaoshan committed
792 793

            if(!StringUtils.isEmpty(contractId)){
kangxiaoshan committed
794
                if (ids != null && ids.size() > 0) {
kangxiaoshan committed
795
                    contractList = contractRepository.findByDsRelation(startDate, endDate, platforms, ids);
kangxiaoshan committed
796 797
                }
            }else{
kangxiaoshan committed
798 799
                contractList = contractRepository.findByDs(startDate, endDate, platform);
            }
kangxiaoshan committed
800

manxiaoqiang committed
801 802
        }

kangxiaoshan committed
803 804 805
        if (ValidateUtil.isValid(contractList)) {
            for (Contract c : contractList) {

kangxiaoshan committed
806
                if ((c.getExtraFlow() != null && c.getExtraFlow() > 0) ||
kangxiaoshan committed
807 808 809 810 811 812 813 814
                        (c.getDiscountTimeLong() != null && c.getDiscountTimeLong() > 0)) {

                    c.setHasDiscount("有");

                } else {
                    c.setHasDiscount("无");
                }

manxiaoqiang committed
815
                c.setSaleName(saleMap.containsKey(c.getSale()) ? saleMap.get(c.getSale()) : "");
kangxiaoshan committed
816 817 818 819 820 821 822 823
                c.setPriceLevelName(typeMap.containsKey(c.getPriceLevel()) ? typeMap.get(c.getPriceLevel()) : "");
//                if (c.getType().equals(ContractTypeEnum.MAIN.getKey()) && platform.contains("io")) {
//
//                } else if (c.getType().equals(ContractTypeEnum.MAIN.getKey())) {
//                    c.setPriceLevelName(bpuMap.containsKey(c.getPriceLevel()) ? bpuMap.get(c.getPriceLevel()) : "");
//                } else {
//                    c.setPriceLevelName(incremenMap.containsKey(c.getPriceLevel()) ? incremenMap.get(c.getPriceLevel()) : "");
//                }
manxiaoqiang committed
824

manxiaoqiang committed
825
                c.setCreateName(saleMap.containsKey(c.getCreateAccount()) ? saleMap.get(c.getCreateAccount()) : "");
manxiaoqiang committed
826 827 828 829 830
                result.add(c);
            }
        }
        return result;
    }
manxiaoqiang committed
831

kangxiaoshan committed
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856

    private void getContractRelationIds (List<String> contractId,Map<String,String> idsDic){

        //获取 关联合同 id
        List<String> contractIds = new ArrayList<>();

        List<BigInteger> ids = contractRelationRepository.findRelationIds(contractId);

        if (ids != null && ids.size() > 0) {
            for(BigInteger id : ids){

                String idss = id.toString();
                if (idsDic.get(idss) == null) {
                    contractIds.add(idss);
                    idsDic.put(idss,"0");
                }
            }

            if(contractIds.size()>0){
                //递归 查询 id  查询所有相关联的合同
                this.getContractRelationIds(contractIds,idsDic);
            }
        }
    }

kangxiaoshan committed
857

kangxiaoshan committed
858 859 860
    @Override
    public Contract contractStatusUpdate(String platform, String contractId, String status) {

kangxiaoshan committed
861
        if (StringUtils.isEmpty(contractId)) {
kangxiaoshan committed
862 863 864 865 866
            return null;
        }

        Contract contract = contractRepository.findOne(Long.parseLong(contractId));

kangxiaoshan committed
867
        if (ContractStatusEnum.CANCEL.getKey().equals(status)) {
kangxiaoshan committed
868
            contract.setStatus(status);
kangxiaoshan committed
869
        } else {
kangxiaoshan committed
870 871 872 873 874 875
            contract.setStatus(ContractStatusEnum.DELETE.getKey());
        }

        return contractRepository.save(contract);
    }

manxiaoqiang committed
876 877 878 879 880 881
    @Override
    public Contract findOne(String code) {

        List<Contract> result = new ArrayList<>();
        List<User> sales = userRepository.findAll();
        Map<Long, String> saleMap = new HashMap<>();
kangxiaoshan committed
882 883
        if (ValidateUtil.isValid(sales)) {
            for (User s : sales) {
manxiaoqiang committed
884 885 886 887 888
                saleMap.put(s.getId(), s.getName());
            }
        }
        List<PackageType> typeList = packageTypeRepository.findAll();
        Map<Long, String> typeMap = new HashMap<>();
kangxiaoshan committed
889 890
        if (ValidateUtil.isValid(typeList)) {
            for (PackageType s : typeList) {
manxiaoqiang committed
891 892 893
                typeMap.put(s.getId(), s.getPackageName());
            }
        }
2  
manxiaoqiang committed
894 895
        List<ContractIncrement> incrementList = contractIncrementRepository.findAll();
        Map<Long, String> incremenMap = new HashMap<>();
kangxiaoshan committed
896 897
        if (ValidateUtil.isValid(incrementList)) {
            for (ContractIncrement s : incrementList) {
2  
manxiaoqiang committed
898 899 900
                incremenMap.put(s.getId(), s.getPackageName());
            }
        }
manxiaoqiang committed
901 902
        Contract contract = contractRepository.findByCode(code);
        contract.setSaleName(saleMap.containsKey(contract.getSale()) ? saleMap.get(contract.getSale()) : "");
kangxiaoshan committed
903
        if (contract.getType().equals(ContractTypeEnum.MAIN.getKey())) {
2  
manxiaoqiang committed
904
            contract.setPriceLevelName(typeMap.containsKey(contract.getPriceLevel()) ? typeMap.get(contract.getPriceLevel()) : "");
kangxiaoshan committed
905
        } else {
2  
manxiaoqiang committed
906 907
            contract.setPriceLevelName(incremenMap.containsKey(contract.getPriceLevel()) ? incremenMap.get(contract.getPriceLevel()) : "");
        }
manxiaoqiang committed
908
        contract.setCreateName(saleMap.containsKey(contract.getCreateAccount()) ? saleMap.get(contract.getCreateAccount()) : "");
manxiaoqiang committed
909

manxiaoqiang committed
910
        Boolean flag = null;
kangxiaoshan committed
911
        if (!contract.getType().equals(ContractTypeEnum.MAIN.getKey())) {
2  
manxiaoqiang committed
912 913 914 915 916 917
            //人群分析
            if (null == contract.getEndDate()) {
                flag = checkTime(contract.getEmail(), contract.getPlatform(), contract.getType(), "black");
            } else {
                flag = checkTime(contract.getEmail(), contract.getPlatform(), contract.getType(), "analysis");
            }
manxiaoqiang committed
918

2  
manxiaoqiang committed
919 920 921 922 923
            if (flag) {
                contract.setContractType("0");
            } else {
                contract.setContractType("1");
            }
manxiaoqiang committed
924
        }
manxiaoqiang committed
925 926 927
        return contract;
    }

manxiaoqiang committed
928 929 930
    @Override
    public ContractMoney pay(User loginUser, ContractMoney resource) {
        Contract contract = contractRepository.findByCode(resource.getContractCode());
kangxiaoshan committed
931
        if (resource.getType().equals("pay")) {
manxiaoqiang committed
932 933 934 935
            contract.setPayMoney(contract.getPayMoney() + resource.getMoney());
        } else {
            contract.setInvoice(contract.getInvoice() + resource.getMoney());
        }
2  
manxiaoqiang committed
936

kangxiaoshan committed
937
        if (contract.getType().equals(ContractTypeEnum.INCREMENT.getKey())) {
2  
manxiaoqiang committed
938 939
            contract.setStatus("end");
        }
manxiaoqiang committed
940 941
        contractRepository.save(contract);

2  
manxiaoqiang committed
942 943 944 945
        resource.setPlatform(contract.getPlatform());
        resource.setCreateAccount(loginUser.getId());
        resource.setCreateTime(new Date());
        resource.setUser(loginUser.getId());
manxiaoqiang committed
946

kangxiaoshan committed
947
        if (resource.getType().equals("pay") && contract.getPlatform().equals("tkio")) {
manxiaoqiang committed
948
            tkioAccountService.contractPay(loginUser, resource);
kangxiaoshan committed
949
        } else if (resource.getType().equals("pay") && contract.getPlatform().equals("io")) {
manxiaoqiang committed
950 951
            ioAccountService.contractPay(loginUser, resource);
        }
manxiaoqiang committed
952 953 954 955 956
        return contractMoneyRepository.save(resource);
    }

    @Override
    public Contract change(User loginUser, ContractChange resource) {
manxiaoqiang committed
957 958
        Map<Long, String> packageMap = getPackageMap();
        Map<Long, String> saleMap = getSaleMap();
manxiaoqiang committed
959
        Contract contract = contractRepository.findByCode(resource.getContractCode());
kangxiaoshan committed
960
        if (resource.getType().equals("price")) {
manxiaoqiang committed
961
            resource.setContent("套餐变更 :" + packageMap.get(contract.getPriceLevel()) + "-->" + packageMap.get(resource.getLevel())
kangxiaoshan committed
962
                    + " 金额变更 :" + contract.getMoney() + "-->" + (resource.getMoney() + contract.getMoney()));
manxiaoqiang committed
963 964 965 966 967 968 969

            contract.setMoney(resource.getMoney() + contract.getMoney());
            contract.setPriceLevel(resource.getLevel());
            Double rebat = getRebat(contract.getProduct(), contract.getPriceLevel().intValue(), contract.getMoney());
            BigDecimal bg = new BigDecimal(rebat);
            double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            contract.setRebate(f1);
manxiaoqiang committed
970 971

            //同步修改trackingio的数据
kangxiaoshan committed
972
            if (contract.getPlatform().equals("tkio")) {
manxiaoqiang committed
973
                accountService.update(contract);
kangxiaoshan committed
974
            } else if (contract.getPlatform().equals("io")) {
2  
manxiaoqiang committed
975
                accountIOService.update(contract);
manxiaoqiang committed
976
            }
kangxiaoshan committed
977
        } else {
manxiaoqiang committed
978
            //同步修改trackingio的数据
kangxiaoshan committed
979
            if (contract.getPlatform().equals("tkio") && contract.getType().equals("main")) {
manxiaoqiang committed
980
                accountService.forbiden(contract);
kangxiaoshan committed
981
            } else if (contract.getPlatform().equals("io") && contract.getType().equals("main")) {
2  
manxiaoqiang committed
982
                accountIOService.update(contract);
manxiaoqiang committed
983
            }
manxiaoqiang committed
984 985
            contract.setStatus("cancel");
        }
manxiaoqiang committed
986
        resource.setPlatform(contract.getPlatform());
manxiaoqiang committed
987 988 989 990 991 992 993 994 995 996 997 998 999
        resource.setCreateAccount(loginUser.getId());
        resource.setCreateTime(new Date());
        resource.setDs(DateUtil.getBeforeDays(0));
        resource.setUser(loginUser.getId());
        contractChangeRepository.save(resource);

        return contractRepository.save(contract);
    }

    @Override
    public List<ContractMoney> findPay(String startDate, String endDate, String code) {
        List<User> sales = userRepository.findAll();
        Map<Long, String> saleMap = new HashMap<>();
kangxiaoshan committed
1000 1001
        if (ValidateUtil.isValid(sales)) {
            for (User s : sales) {
manxiaoqiang committed
1002 1003 1004 1005
                saleMap.put(s.getId(), s.getName());
            }
        }

kangxiaoshan committed
1006 1007 1008 1009
        List<ContractMoney> result = new ArrayList<>();
        List<ContractMoney> list = contractMoneyRepository.findByDs(startDate, endDate, code);
        if (ValidateUtil.isValid(list)) {
            for (ContractMoney cm : list) {
manxiaoqiang committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
                cm.setCreateName(saleMap.containsKey(cm.getUser()) ? saleMap.get(cm.getUser()) : "");
                result.add(cm);
            }
        }
        return result;
    }

    @Override
    public List<ContractChange> findChange(String startDate, String endDate, String code) {

        List<User> sales = userRepository.findAll();
        Map<Long, String> saleMap = new HashMap<>();
kangxiaoshan committed
1022 1023
        if (ValidateUtil.isValid(sales)) {
            for (User s : sales) {
manxiaoqiang committed
1024 1025 1026 1027
                saleMap.put(s.getId(), s.getName());
            }
        }

kangxiaoshan committed
1028 1029 1030 1031
        List<ContractChange> result = new ArrayList<>();
        List<ContractChange> list = contractChangeRepository.findByDs(startDate, endDate, code);
        if (ValidateUtil.isValid(list)) {
            for (ContractChange cm : list) {
manxiaoqiang committed
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
                cm.setCreateName(saleMap.containsKey(cm.getUser()) ? saleMap.get(cm.getUser()) : "");
                result.add(cm);
            }
        }
        return result;
    }

    @Override
    public Contract updatePay(User loginUser, ContractMoney resource) {
        ContractMoney contractMoney = contractMoneyRepository.findOne(resource.getId());
        Contract contract = contractRepository.findByCode(resource.getContractCode());
kangxiaoshan committed
1043
        if (contractMoney.getType().equals("pay")) {
manxiaoqiang committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
            contract.setPayMoney(contract.getPayMoney() + resource.getMoney() - contractMoney.getMoney());
        } else {
            contract.setInvoice(contract.getInvoice() + resource.getMoney() - contractMoney.getMoney());
        }
        Contract save = contractRepository.save(contract);

        contractMoney.setModifyAccount(loginUser.getId());
        contractMoney.setModifyTime(new Date());
        contractMoney.setMoney(resource.getMoney());
        contractMoney.setDs(resource.getDs());
        contractMoneyRepository.save(contractMoney);

        List<User> sales = userRepository.findAll();
        Map<Long, String> saleMap = new HashMap<>();
kangxiaoshan committed
1058 1059
        if (ValidateUtil.isValid(sales)) {
            for (User s : sales) {
manxiaoqiang committed
1060 1061 1062 1063 1064
                saleMap.put(s.getId(), s.getName());
            }
        }
        List<PackageType> typeList = packageTypeRepository.findAll();
        Map<Long, String> typeMap = new HashMap<>();
kangxiaoshan committed
1065 1066
        if (ValidateUtil.isValid(typeList)) {
            for (PackageType s : typeList) {
manxiaoqiang committed
1067 1068 1069 1070 1071 1072 1073 1074 1075
                typeMap.put(s.getId(), s.getPackageName());
            }
        }

        save.setSaleName(saleMap.containsKey(save.getSale()) ? saleMap.get(save.getSale()) : "");
        save.setPriceLevelName(typeMap.containsKey(save.getPriceLevel()) ? typeMap.get(save.getPriceLevel()) : "");
        save.setCreateName(saleMap.containsKey(save.getSale()) ? saleMap.get(save.getSale()) : "");
        return save;
    }
manxiaoqiang committed
1076 1077

    @Override
manxiaoqiang committed
1078
    public List<ContractMoney> findPayAll(String startDate, String endDate, String platfrom) {
manxiaoqiang committed
1079 1080
        List<User> sales = userRepository.findAll();
        Map<Long, String> saleMap = new HashMap<>();
kangxiaoshan committed
1081 1082
        if (ValidateUtil.isValid(sales)) {
            for (User s : sales) {
manxiaoqiang committed
1083 1084 1085 1086
                saleMap.put(s.getId(), s.getName());
            }
        }

kangxiaoshan committed
1087
        List<ContractMoney> result = new ArrayList<>();
manxiaoqiang committed
1088
        List<ContractMoney> list = contractMoneyRepository.findByDsAll(startDate, endDate, platfrom);
kangxiaoshan committed
1089 1090
        if (ValidateUtil.isValid(list)) {
            for (ContractMoney cm : list) {
manxiaoqiang committed
1091 1092 1093 1094 1095 1096
                cm.setCreateName(saleMap.containsKey(cm.getUser()) ? saleMap.get(cm.getUser()) : "");
                result.add(cm);
            }
        }
        return result;
    }
manxiaoqiang committed
1097

kangxiaoshan committed
1098 1099 1100 1101 1102 1103
    @Override
    public List<TradeType> getTradeData(String platform) {
        return tradeTypeRepsitory.findAll();
    }

    @Override
kangxiaoshan committed
1104
    public List<Map> contractCodeAll(String platform, String contractId) {
kangxiaoshan committed
1105 1106 1107 1108 1109 1110 1111

        Map<String,String> data = new HashMap<>();
        data.put("id","-1");
        data.put("contractCode","无");

        List result = new ArrayList();
        result.add(data);
kangxiaoshan committed
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145

        List<String> plats = new ArrayList<>();

        plats.add(platform);

        if("fake".equals(platform)){
            plats.add("tkio");
        }

        List<ContractRelation> ids = null;

        if(!StringUtils.isEmpty(contractId)){
            ids =  contractRelationRepository.findRelationIdsMain(contractId);
        }

        if (ids != null) {

            Map<String,String> filterMap = new HashMap();
            for(ContractRelation relation : ids){
                filterMap.put(relation.getRelationId()+"_","");
            }

            List<Map> resultBefore = contractRepository.contractCodePlatform(plats);
            for (Map  m : resultBefore) {

                String m_id = m.get("id").toString() + "_";
                if (filterMap.get(m_id) == null && !m_id.equals(contractId+"_")) {
                    result.add(m);
                }
            }

        } else {
            result.addAll(contractRepository.contractCodePlatform(plats));
        }
kangxiaoshan committed
1146 1147

        return result;
kangxiaoshan committed
1148
    }
manxiaoqiang committed
1149

kangxiaoshan committed
1150
    public Map<Long, String> getPackageMap() {
manxiaoqiang committed
1151 1152
        List<PackageType> typeList = packageTypeRepository.findAll();
        Map<Long, String> typeMap = new HashMap<>();
kangxiaoshan committed
1153 1154
        if (ValidateUtil.isValid(typeList)) {
            for (PackageType s : typeList) {
manxiaoqiang committed
1155 1156 1157 1158 1159 1160
                typeMap.put(s.getId(), s.getPackageName());
            }
        }
        return typeMap;
    }

kangxiaoshan committed
1161
    public Map<Long, String> getSaleMap() {
manxiaoqiang committed
1162 1163
        List<User> sales = userRepository.findAll();
        Map<Long, String> saleMap = new HashMap<>();
kangxiaoshan committed
1164 1165
        if (ValidateUtil.isValid(sales)) {
            for (User s : sales) {
manxiaoqiang committed
1166 1167 1168 1169 1170
                saleMap.put(s.getId(), s.getName());
            }
        }
        return saleMap;
    }
manxiaoqiang committed
1171

kangxiaoshan committed
1172
    public static Map<String, String> convertBean(Object bean) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
manxiaoqiang committed
1173
        Class type = bean.getClass();
kangxiaoshan committed
1174
        Map<String, String> returnMap = new HashMap<String, String>();
manxiaoqiang committed
1175 1176
        BeanInfo beanInfo = Introspector.getBeanInfo(type);

kangxiaoshan committed
1177 1178
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptors.length; i++) {
manxiaoqiang committed
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
            if (!propertyName.equals("class")) {
                Method readMethod = descriptor.getReadMethod();

                Object invoke = readMethod.invoke(bean, new Object[0]);
                String result = (null == invoke ? null : invoke.toString());

                if (result != null) {
                    returnMap.put(propertyName, result);
                } else {
                    returnMap.put(propertyName, null);
                }


            }
        }
        return returnMap;
    }

kangxiaoshan committed
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211

//    public static void main(String[] args) {
//
//        ContractServiceImpl contractService = new ContractServiceImpl();
//        try {
//            Map data = contractService.dealContractChangeMap(new Contract(), new Contract());
//            System.out.println(data);
//
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }
//
//    }
manxiaoqiang committed
1212
}