package org.cs.mgr.user.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.cs.core.service.impl.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.cs.mgr.user.service.IMgmUserBankService;
import org.cs.mgr.user.dao.IMgmUserBankDao;
import org.cs.mgr.user.model.MgmUser;
import org.cs.mgr.user.model.MgmUserBank;

/**
 * 
 * @ClassName: IMgmUserBankService
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:28:57
 */
@Service("mgmUserBankService")
public class MgmUserBankService extends BaseService<MgmUserBank> implements IMgmUserBankService{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private IMgmUserBankDao mgmUserBankDao;
	
	@Override
	@Transactional(readOnly = true)
	public Pager getMgmUserBanks(Pager page){
		return this.mgmUserBankDao.queryMgmUserBanks(page);
	}
	
	/**
	 * 自动生成
	 * @Title: delMgmUserBank 
	 * @Description: TODO
	 * @param id						主键id
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean delMgmUserBank(Integer id){
	
		MgmUserBank mgmUserBank = this.mgmUserBankDao.findById(id);
		if(mgmUserBank != null){
			mgmUserBank.setIsDelete(true);				
			this.mgmUserBankDao.update(mgmUserBank);
		}
		return true;
	}
	
	
	/**
	 * 自动生成
	 * @Title: addMgmUserBank 
	 * @Description: TODO
	 * @param mgmUserBank						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean addMgmUserBank(MgmUserBank mgmUserBank){
	
		if(mgmUserBank != null){
			this.mgmUserBankDao.insert(mgmUserBank);
		}
		return true;
	}
	
	/**
	 *  自动生成
	 * @Title: updateMgmUserBank 
	 * @Description: TODO
	 * @param mgmUserBank						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean updateMgmUserBank(MgmUserBank mgmUserBank){
	
		MgmUserBank mgmUserBank2 = this.mgmUserBankDao.findById(mgmUserBank.getId());
		if(mgmUserBank2 != null){
			mgmUserBank.mergeTo(mgmUserBank2);
			this.mgmUserBankDao.update(mgmUserBank2);
		}
		
		return true;
	}
	
	/**
	 * @Title: updateUserInfo   
	 * @Description: 银行卡信息
	 * @param: @param map
	 * @param: @return         
	 * @see org.cs.mgr.user.service.IMgmUserService#updateUserInfo(java.util.Map)
	 */
	@Override
	@Transactional(readOnly = false)
	public JSONObject updateUserBank(MgmUserBank mgmUserBank){
		String bankNo=mgmUserBank.getBankNo();
		String username=mgmUserBank.getUsername();
		String bankName=mgmUserBank.getBankName();
		String province=mgmUserBank.getProvince();
		String city=mgmUserBank.getCity();
		String area=mgmUserBank.getArea();
		String bankBranch=mgmUserBank.getBankBranch();
	    String userId=mgmUserBank.getUserId();
	     
	    MgmUserBank mgmUserBanks=mgmUserBankDao.getByPropertie(userId, "userId");
	    
	    if(mgmUserBanks==null){
	    	mgmUserBanks=new MgmUserBank();
	    	mgmUserBanks.setUserId(userId);
	    }
		
		if(StringUtil.isNotBlank(bankNo)){
			if(isNumeric(bankNo)){
				mgmUserBanks.setBankNo(bankNo);
			}else{
				return getJson(0, "", "卡号格式不正确！");	
			}
		}
		
		if(StringUtil.isNotBlank(username)){
			mgmUserBanks.setUsername(username);
		}else{
			return getJson(0, "", "请输入持卡人姓名！");	
		}
		
		if(StringUtil.isNotBlank(bankName)){
			mgmUserBanks.setBankName(bankName);
		}else{
			return getJson(0, "", "请选择开户行！");	
		}
		
		if(StringUtil.isNotBlank(province) && StringUtil.isNotBlank(city) && StringUtil.isNotBlank(area)){
			mgmUserBanks.setProvince(province);
			mgmUserBanks.setCity(city);
			mgmUserBanks.setArea(area);
		}else{
			return getJson(0, "", "请选择开户省市区！");	
		}
		
		if(StringUtil.isNotBlank(bankBranch)){
			mgmUserBanks.setBankBranch(bankBranch);
		}else{
			return getJson(0, "", "请输入支行名称！");	
		}
		
		mgmUserBankDao.update(mgmUserBanks);
		return getJson(1, mgmUserBanks, "修改银行卡成功");	
	}
	
	 /**
	  * 
	  * @Title: getUserBank   
	  * @Description:获取用户银行卡信息   
	  * @param: @param map
	  * @param: @return      
	  * @return: JSONObject      
	  * @throws
	  */
	@Override
	public JSONObject getUserBank(Map<String,String> map){ 
		MgmUserBank mgmUserBanks=mgmUserBankDao.getByPropertie(map.get("userId").toString(), "userId");
		return getJson(1, mgmUserBanks==null?new MgmUserBank():mgmUserBanks, "获取银行卡信息成功");
	}
	
	/**
     * 利用正则表达式判断字符串是否是数字
     * @param str
     * @return
     */
    public boolean isNumeric(String str){
           Pattern pattern = Pattern.compile("[0-9]*");
           Matcher isNum = pattern.matcher(str);
           if( !isNum.matches() ){
               return false;
           }
           return true;
    }
}
