package market.Qms.com.service.impl;

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

import market.Qms.com.mapper.BaseStdRCustMapper;
import market.Qms.com.service.BaseStdRCustService;
import market.Qms.com.vo.BaseStdRCust;
import market.Qms.common.DataHelper;
import market.Qms.common.RequestModel;
import market.Qms.common.ReturnMsg;

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

@RemoteService(serviceInterface=BaseStdRCustService.class)
public class BaseStdRCustServiceImpl extends BaseServiceImpl<BaseStdRCust, Object> implements BaseStdRCustService {

	@Override
	public ReturnMsg<List<BaseStdRCust>> GetStdRCust(RequestModel<BaseStdRCust> cust) {
		ReturnMsg<List<BaseStdRCust>> msg=new ReturnMsg<>();
		try{
			BaseStdRCustMapper mapper=this.getMapper(BaseStdRCustMapper.class);
			List<BaseStdRCust> list=mapper.find(cust.getModel(),cust.getSkipResults(), cust.getPageSize());
			int count=mapper.countWithCondition(cust.getModel(),null);
			msg.setTotal(count);
			msg.setMessage("获取数据成功");
			msg.setSuccess(true);
			msg.setResult(list);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	@Override
	public ReturnMsg<BaseStdRCust> InsertStdRCust(BaseStdRCust cust) {
		ReturnMsg<BaseStdRCust> msg=new ReturnMsg<>();
		if(DataHelper.IsNullOrSpace(cust.getCustCode())||DataHelper.IsNullOrSpace(cust.getAlphaCode())){
			msg.setMessage("保存数据失败，原因：客户代码或者用途码为空");
			msg.setSuccess(false);
			return msg;
		}
		try{
			BaseStdRCustMapper mapper=this.getMapper(BaseStdRCustMapper.class);
			BaseStdRCust _cust=new BaseStdRCust();
			_cust.setAlphaCode(cust.getAlphaCode());
			_cust.setCustCode(cust.getCustCode());
			List<BaseStdRCust> _listCust=mapper.find(_cust,0,0);
			if(_listCust.size()>0){
				msg.setMessage("同客户代码/用途码的同一信息已经存在");
				msg.setSuccess(false);
				return msg;
			}
			cust.setCreateTime(new Date());
			mapper.insert(cust);
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
			msg.setResult(cust);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ReturnMsg<BaseStdRCust> UpdateStdRCust(BaseStdRCust cust) {
		ReturnMsg<BaseStdRCust> msg=new ReturnMsg<>();
		msg.setResult(cust);
		if(DataHelper.IsNullOrSpace(cust.getCustCode())||DataHelper.IsNullOrSpace(cust.getAlphaCode())){
			msg.setMessage("保存数据失败，原因：客户代码或者用途码为空");
			msg.setSuccess(false);
			return msg;
		}
		try{
			cust.setUpdateTime(new Date());
			BaseStdRCustMapper mapper=this.getMapper(BaseStdRCustMapper.class);
			mapper.update(cust);
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
			msg.setResult(cust);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	@Override
	public ReturnMsg DelOrRenew(String deleteName,String alphaCode, String custCode, Boolean flag) {
		ReturnMsg msg=new ReturnMsg<>();
		try{
			// TODO Auto-generated method stub
			BaseStdRCust cust=new BaseStdRCust();
			cust.setAlphaCode(alphaCode);
			cust.setCustCode(custCode);
			cust.setValidflag(flag?"0":"1");
			if(flag){
				cust.setDeleteTime(new Date());
				cust.setDeleteName(deleteName);
			}else{
				cust.setUpdateTime(new Date());
				cust.setUpdateName(deleteName);
			}
			this.getMapper(BaseStdRCustMapper.class).update(cust);
			msg.setMessage(flag?"废除成功":"恢复成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setMessage(ex.getMessage());
			msg.setSuccess(false);
		}
		return msg;
	}
}
