/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-05-07
 * 描述            ：供应商开户银行表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Pms.Supp.impl;

import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Supp.mapper.PurSuppBankMapper;
import market.Pms.Supp.service.PurSuppBankService;
import market.Pms.Supp.vo.PurSuppBank;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.service.support.PageModel;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@RemoteService(path="PurSuppBankService")
public class PurSuppBankServiceImpl extends BaseServiceImpl<PurSuppBank,Object> implements PurSuppBankService {
	/**
	 * 查询
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurSuppBank> oci){
		ResultModel rm=new ResultModel();
		PageModel<PurSuppBank> pm=new PageModel<PurSuppBank>(oci.getPageIndex(),oci.getPageSize());
		PurSuppBank queryCls=oci.getObject()==null?(new PurSuppBank()):oci.getObject();
		try{
			pm.setTotal(this.getMapper(PurSuppBankMapper.class).doQueryCount(queryCls));
			pm.setRows(this.getMapper(PurSuppBankMapper.class).doQuery(queryCls, (oci.getPageIndex()-1)*oci.getPageSize(), oci.getPageIndex()*oci.getPageSize()));
		
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("错误："+ex.getMessage());
		}
		
		return rm;
	}
	
	/**
	 * 新增
	 * @param oci
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<PurSuppBank> oci){
		ResultModel rm=new ResultModel();
		rm = ValidData(oci.getObject(),"0");
		if("500".equals(rm.getState())){
			return rm;
		}
		
		int i=1;
		try{
			List<PurSuppBank> queryClsList=this.getMapper(PurSuppBankMapper.class).findWithCondition(null, " bank_code like 'BK%' ", " bank_code desc ", 0, 1);
			if(queryClsList!=null&&queryClsList.size()>0){
				i=Integer.parseInt(queryClsList.get(0).getBank_code().substring(2))+1;
			}
			oci.getObject().setBank_code("BK"+String.format("%08d", i));
			this.getMapper(PurSuppBankMapper.class).insert(oci.getObject());
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("新增失败："+ex.getMessage());
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 修改,其中有效，无效则传编号跟状态即可
	 * @param oci 
	 * memo1 1正常修改  2则为有效无效处理
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<PurSuppBank> oci){
		ResultModel rm=new ResultModel();
		String sType=("1".equals(oci.getMemo1())?"1":"");
		rm = ValidData(oci.getObject(),sType);
		if("500".equals(rm.getState())){
			return rm;
		}
		
		try{
			this.getMapper(PurSuppBankMapper.class).update(oci.getObject());
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("修改失败："+ex.getMessage());
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 公共的数据验证，若需要其它的验证则继续增加
	 * @param oci
	 * @param sType 0新增  1修改
	 * @return
	 */
	public ResultModel ValidData(PurSuppBank oci,String sType){
		ResultModel rm=new ResultModel();
		if(oci==null){
			rm.setState("500");
			rm.setMsgInfo("请传入需操作的数据！");
			return rm;
		}
		
		//需要验证数据是否出现重复的名称
		if(!"".equals(sType)){
			try{
				PurSuppBank validCls=new PurSuppBank();
				validCls.setBank_account(oci.getBank_account());
				validCls.setSupp_code(oci.getSupp_code());
				if("0".equals(sType)){
					ValidName(validCls);
				}else if("1".equals(sType)){
					//先获取是否存在被修改的数据
					PurSuppBank pbd=this.getMapper(PurSuppBankMapper.class).findById(oci.getBank_code());
					if(pbd!=null&&pbd.getBank_code()!=null){
						if(oci.getBank_account()!=null&&!"".equals(oci.getBank_account())&&!oci.getBank_account().equals(pbd.getBank_account())){
							ValidName(validCls);
						}
					}else{
						rm.setState("500");
						rm.setMsgInfo("当前数据已被删除，无法修改！");
						return rm;
					}
				}
				
			}catch(Exception ex){
				rm.setState("500");
				rm.setMsgInfo("数据验证异常："+ex.getMessage());
				return rm;
			}
		}
		
		rm.setState("200");
		return rm;
	}
	/**
	 * 验证是否存在相同名称的数据
	 * @param oci
	 * @throws Exception
	 */
	public void ValidName(PurSuppBank oci) throws Exception{
		int icount=this.getMapper(PurSuppBankMapper.class).count(oci);
		if(icount>0){
			throw new Exception("已存在银行帐号["+oci.getBank_account()+"]!");
		}
	}
}
