package market.Qms.com.service.impl;

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

import market.Qms.com.mapper.BaseStdRSpecMapper;
import market.Qms.com.service.BaseStdRSpecService;
import market.Qms.com.vo.BaseStdRSpec;
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=BaseStdRSpecService.class)
public class BaseStdRSpecServiceImpl extends BaseServiceImpl<BaseStdRSpec, String> implements BaseStdRSpecService {

	@Override
	public ReturnMsg<List<BaseStdRSpec>> GetStdRSpec(RequestModel<BaseStdRSpec> stdSpec) {
		ReturnMsg<List<BaseStdRSpec>> msg=new ReturnMsg<>();
		try{
			BaseStdRSpecMapper mapper=this.getMapper(BaseStdRSpecMapper.class);
			List<BaseStdRSpec> list=mapper.findWithCondition(stdSpec.getModel(), null, "STD_SPEC_ID DESC", stdSpec.getSkipResults(), stdSpec.getPageSize());
			int count=mapper.countWithCondition(stdSpec.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<BaseStdRSpec> SaveStdRSpec(BaseStdRSpec stdSpec) {
		ReturnMsg<BaseStdRSpec> msg=new ReturnMsg<>();
		msg.setResult(stdSpec);
		try{
			BaseStdRSpecMapper mapper=this.getMapper(BaseStdRSpecMapper.class);
			if(DataHelper.IsNullOrSpace(stdSpec.getStdSpecId())){
				String maxCode=mapper.GetMaxCode();
				String code=DataHelper.InitSerial(maxCode,"SP", 7);
				stdSpec.setStdSpecId(code);
				stdSpec.setCreateTime(new Date());
				mapper.insert(stdSpec);
			}else{
				stdSpec.setUpdateTime(new Date());
				mapper.update(stdSpec);
			}
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
			msg.setResult(stdSpec);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

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

}
