package market.Qms.com.service.impl;

import java.util.ArrayList;
import java.util.List;

import market.Qms.com.mapper.PscRSpecMapper;
import market.Qms.com.service.PscRSpecService;
import market.Qms.com.vo.PscRSpec;
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=PscRSpecService.class)
public class PscRSpecServiceImpl extends BaseServiceImpl<PscRSpec, Integer> implements PscRSpecService {

	@Override
	public ReturnMsg<List<PscRSpec>> GetPscRSpec(RequestModel<PscRSpec> spec) {
		ReturnMsg<List<PscRSpec>> msg=new ReturnMsg<>();
		List<PscRSpec> list=new ArrayList<>();
		try{
			PscRSpecMapper mapper=this.getMapper(PscRSpecMapper.class);
			list=mapper.findWithCondition(spec.getModel(), null, "id DESC", spec.getSkipResults(), spec.getPageSize());
			int count=mapper.countWithCondition(spec.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<PscRSpec> SavePscRSpec(PscRSpec spec) {
		// TODO Auto-generated method stub
		ReturnMsg<PscRSpec> msg=new ReturnMsg<>();
		try{
			PscRSpecMapper mapper=this.getMapper(PscRSpecMapper.class);
			if(spec.getId()==null){
				Integer maxId=mapper.GetMaxId();
				if(maxId==null){
					maxId=1;
				}else{
					maxId=maxId+1;
				}
				spec.setId(maxId);
				spec.setValidflag("1");
				mapper.insert(spec);
			}else{
				mapper.update(spec);
			}
			msg.setResult(spec);
			msg.setMessage("保存数据成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setSuccess(false);
			msg.setMessage(ex.getMessage());
		}
		return msg;
	}

	public ReturnMsg DelOrRenewPscRSpec(PscRSpec scr) {
		// TODO Auto-generated method stub
		ReturnMsg msg=new ReturnMsg<>();
		try{
			boolean flag =false;
			Integer id = scr.getId();
			 String validflag = scr.getValidflag();
			 if(validflag.equals("1"))
				 flag=true;
			// TODO Auto-generated method stub
			PscRSpec org=new PscRSpec();
			org.setId(id);
			org.setValidflag(flag?"0":"1");
			this.getMapper(PscRSpecMapper.class).update(org);
			msg.setMessage(flag?"废除成功":"恢复成功");
			msg.setSuccess(true);
		}catch(Exception ex){
			msg.setMessage(ex.getMessage());
			msg.setSuccess(false);
		}
		return msg;
	}

}
