package com.cardpay.pccredit.intopieces.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cardpay.pccredit.customer.filter.CustomerInforFilter;
import com.cardpay.pccredit.customer.model.CustomerInfor;
import com.cardpay.pccredit.intopieces.constant.Constant;
import com.cardpay.pccredit.intopieces.dao.CustomerApplicationInfoDao;
import com.cardpay.pccredit.intopieces.model.CustomerApplicationInfo;
import com.cardpay.pccredit.riskControl.constant.RiskCreateTypeEnum;
import com.cardpay.pccredit.riskControl.filter.RiskCustomerFilter;
import com.cardpay.pccredit.riskControl.model.RiskCustomer;
import com.wicresoft.jrad.base.auth.IUser;
import com.wicresoft.jrad.base.database.dao.common.CommonDao;
import com.wicresoft.jrad.base.database.id.IDGenerator;
import com.wicresoft.jrad.base.web.security.LoginManager;
import com.wicresoft.util.spring.Beans;

@Service
public class CustomerApplicationInfoService {

	@Autowired
	private CommonDao commonDao;

	@Autowired
	private CustomerApplicationInfoDao customerApplicationInfoDao;

	/*
	 * 保存申请客户信息
	 */
	public void save(CustomerApplicationInfo customerApplicationInfo) {
		commonDao.insertObject(customerApplicationInfo);
	}
	
	public void update(CustomerApplicationInfo customerApplicationInfo,HttpServletRequest request) {
		//更新进件
		commonDao.updateObject(customerApplicationInfo);
		CustomerApplicationInfo applicationInfo = commonDao.findObjectById(CustomerApplicationInfo.class, customerApplicationInfo.getId());
		//获取客户信息
		CustomerInfor info = commonDao.findObjectById(CustomerInfor.class, applicationInfo.getCustomerId());
		String status = customerApplicationInfo.getStatus();
		//拒绝，加入风险名单(没有则添加到风险名单)
		if(status.equals("refuse")){
			RiskCustomerFilter filter = new RiskCustomerFilter();
			filter.setCustomerId(info.getId());
			List<RiskCustomer> oldRisk = commonDao.findObjectsByFilter(RiskCustomer.class, filter).getItems();
			if(oldRisk.size()<1){
				//拒绝原因
				String refuseReason = request.getParameter("decisionRefusereason");
				IUser user = Beans.get(LoginManager.class).getLoggedInUser(request);
				RiskCustomer riskCustomer = new RiskCustomer();
				riskCustomer.setId(IDGenerator.generateID());
				riskCustomer.setCustomerId(info.getId());
				riskCustomer.setRefuseReason(refuseReason);
				riskCustomer.setCreatedTime(new Date());
				riskCustomer.setReportedIdManager(user.getId());
				riskCustomer.setCreatedBy(user.getId());
				riskCustomer.setRiskCreateType(RiskCreateTypeEnum.manual.toString());
				commonDao.insertObject(riskCustomer);
			}
		}
	}

	/*
	 * 删除客户信息
	 */
	public void delete(String id) {
		commonDao.deleteObject(CustomerApplicationInfo.class, id);
	}
	/**
	 * 统计拒绝进件条数
	 * @return
	 */
	public int findCustomerApplicationInfoCount(String userId,String status1,String status2){
		return customerApplicationInfoDao.findCustomerApplicationInfoCount(userId,status1,status2);
	}
}

@@ -2777,16 +2777,176 @@
 %7D%0D%0A%09%09%7D%0D%0A
+%09%09%0D%0A%09%09//%E9%80%80%E5%9B%9E%E9%87%8D%E6%96%B0%E8%B0%83%E6%9F%A5%0D%0A%09%09if(status.equals(%22nopass_re%22))%7B%0D%0A%09%09%09//%E5%88%A0%E9%99%A4%E8%BF%9B%E4%BB%B6%E4%BF%A1%E6%81%AF%0D%0A%09%09%09commonDao.deleteObject(CustomerApplicationInfo.class,customerApplicationInfo.getId());%0D%0A%09%09%7D%0D%0A
 %09%7D%0D%0A%0D%0A%09/

update 审贷决议 如果是退回重新调查操作 需要删除进件信息