package com.liang.service.impl;

import com.liang.common.base.BaseController;
import com.liang.dao.PrjContractXgDao;
import com.liang.dao.PrjPayplanDao;
import com.liang.dao.PrjPayplanXgDao;
import com.liang.entity.*;
import com.liang.service.PrjContractXgService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service("prjContractXgService")
public class PrjContractXgServiceImpl extends BaseController implements PrjContractXgService {
    @Resource
    private PrjContractXgDao prjContractXgDao;
    @Resource
    private PrjPayplanXgDao prjPayplanXgDao;
    @Resource
    private PrjPayplanDao prjPayplanDao;

    @Override
    public int insert(PrjContract prjContract) {
        return prjContractXgDao.insert(prjContract);
    }

    @Override
    public int updateSpzt(PrjContract prjContract) {
        return this.prjContractXgDao.updateSpzt(prjContract);
    }

    @Override
    public PrjContractXg queryByhtId(Integer id) {
        return this.prjContractXgDao.queryByhtId(id);
    }

    @Override
    public void deldteById(Integer id) {
        prjContractXgDao.deldteById(id);
    }

    @Override
    @Transactional
    public void doUpdate(Integer htId) {
        //  1.获取合同中间表数据
        PrjContractXg prjContractXg = prjContractXgDao.queryRecordByHtId(htId);
        // 2.获取付款计划中间表数据
        List<PrjPayplan> prjPayplanXgList = prjPayplanXgDao.queryByHtId(htId);
        List<PrjPayplan> prjPayplanUpdateList = new ArrayList<>();//存放已存在的数据
        List<PrjPayplan> prjPayplanAddList = new ArrayList<>();//存放新创建的数据
        //  3.修改合同表中的内容
        int  i =prjContractXgDao.updateXg(prjContractXg);
        //  4.修改付款计划表中的内容
        //判断付款计划是否为空
        if (!prjPayplanXgList.isEmpty()) {
            for (int j = 0; j < prjPayplanXgList.size(); j++) {
                Integer oldid = prjPayplanXgList.get(j).getOldid();
                if (oldid!=null){
                    prjPayplanUpdateList.add(prjPayplanXgList.get(j));
                }else {
                    prjPayplanAddList.add(prjPayplanXgList.get(j));
                }
            }
            for (int a = 0; a < prjPayplanAddList.size(); a++) {
                prjPayplanAddList.get(a).setCjr(getSysUserId());
                prjPayplanAddList.get(a).setCjsj(new Date(System.currentTimeMillis()));
            }
            //清理已删除的数据
            List<PrjPayplan> oldpayPlanList = prjPayplanDao.getPayPlanList(htId);//获取旧付款计划数据
            Iterator<PrjPayplan> iterator = oldpayPlanList.iterator();
            while (iterator.hasNext()) {
                PrjPayplan oldItem = iterator.next();
                boolean found = false;
                for (PrjPayplan newItem : prjPayplanUpdateList) {
                    if (oldItem.getId().equals(newItem.getOldid())) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    prjPayplanDao.deleteById(oldItem.getId());
                }
            }
            //保存新增的付款计划
            if (prjPayplanAddList.size()>0) {
                int num = prjPayplanDao.insertBatch(prjPayplanAddList);
            }
            //修改已存在的付款计划
            for (int k = 0; k < prjPayplanUpdateList.size(); k++) {
                prjPayplanDao.updateXg(prjPayplanUpdateList.get(k));
            }

        }
        if (i>0) {
        // 5.保存成功后删除中间表中的数据
            prjContractXgDao.deldteById(htId);
        // 6.删除付款计划中间表中的数据
            prjPayplanXgDao.deleteBtHtId(htId);
        }
    }

    @Override
    public int updateHtht(PrjContractXg prjContractXg) {
        return this.prjContractXgDao.updateHtht(prjContractXg);
    }
}
