package cn.com.countrygarden.bi.main.service.investment.impl;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.test.Test;
import cn.com.countrygarden.bi.main.service.project.ProjectJointPlanService;
import cn.com.countrygarden.bi.main.service.test.TestService;
import sun.security.rsa.RSAPadding;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mysql.fabric.xmlrpc.base.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.sys.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.countrygarden.bi.main.model.investment.InvestProjectAccount;
import cn.com.countrygarden.bi.main.model.investment.InvestProjectInvestment;
import cn.com.countrygarden.bi.main.model.investment.InvestRegionInvestment;
import cn.com.countrygarden.bi.main.dao.investment.InvestProjectAccountMapper;
import cn.com.countrygarden.bi.main.dao.investment.InvestRegionInvestmentMapper;
import cn.com.countrygarden.bi.main.service.investment.InvestProjectAccountService;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;
import cn.com.countrygarden.bi.main.core.Response;

/**
 * -业务逻辑层实现
 *
 * @author 汤婷
 * @email www.justintoforest@gamail.com
 * @url 
 * @date 2018-10-05
 */
@Service
public class InvestProjectAccountServiceImpl implements InvestProjectAccountService{
    @Autowired
    private InvestProjectAccountMapper investProjectAccountMapper;
    
    @Autowired
    private InvestRegionInvestmentMapper investRegionInvestmentMapper;

    @Override
    public Result queryPageList(InvestProjectAccount investProjectAccount, int page, int size) {
    
        Page<Object> pageList = PageHelper.startPage(page, size);
        Condition condition=new Condition(InvestProjectAccount.class);
        Criteria criteria = condition.createCriteria();
//		TODO 添加条件
		investProjectAccountMapper.selectByCondition(condition);
        PageData data=new PageData(pageList);
        data.setData(pageList.getResult());
        return Response.response(data);
    }

    @Override
    public Result queryById(Integer id) {
        return Response.response(investProjectAccountMapper.selectByPrimaryKey(id));
    }

    @Override
    public Result add(InvestProjectAccount investProjectAccount, AccessToken accessToken) {
        investProjectAccountMapper.insert(investProjectAccount);
        return Response.response();
    }
  
    @Override
    public Result del(Integer id) {
        int count = investProjectAccountMapper.deleteByPrimaryKey(id);
        return Response.response(count);
    }

	

	@Override
	public InvestRegionInvestment addinvestProjectAccountList(InvestRegionInvestment investRegionInvestment,
			List<InvestProjectAccount> investProjectAccountList) {
		if(investProjectAccountList==null||investProjectAccountList.size()<1) {
			return investRegionInvestment;
		}
		List<InvestProjectAccount> projectAccountList=new ArrayList<InvestProjectAccount>();
		for(InvestProjectAccount investProjectAccount:investProjectAccountList) {
			if(investProjectAccount.getInvestProjectInvestmentId()!=null&&investProjectAccount.getInvestProjectInvestmentId()>0) {
		
			investProjectAccount.setInvestRegionInvestmentId(investRegionInvestment.getId());
		
			investProjectAccount.setCreateBy(investRegionInvestment.getCreateBy());
			investProjectAccount.setCreateTime(investRegionInvestment.getCreateTime());
			investProjectAccount.setUpdateBy(investRegionInvestment.getUpdateBy());
			investProjectAccount.setUpdateTime(investRegionInvestment.getUpdateTime());
			projectAccountList.add(investProjectAccount);
			}
		}
		if(projectAccountList.size()>0) {
			investProjectAccountMapper.insertList(projectAccountList);
		}
			investRegionInvestment.setFollowCapitalAccount(investProjectAccountMapper.findFollowCapitalAccountSum(investRegionInvestment.getId()));
			investRegionInvestment.setFollowCapitalAccountTime(new Date());
			investRegionInvestmentMapper.updateByPrimaryKeySelective(investRegionInvestment);
		return investRegionInvestment;
	}

	@Override
	public Result edit(int investProjectInvestmentId, AccessToken accessToken) {
		// TODO Auto-generated method stub
		return null;
	}

}