package market.Qms.com.service.impl;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.mapper.BaseDeliverystateRPhyMapper;
import market.Qms.com.service.BaseDeliverystateRPhyService;
import market.Qms.com.vo.BaseDeliverystateRPhy;
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=BaseDeliverystateRPhyService.class)
public class BaseDeliverystateRPhyServiceImpl extends BaseServiceImpl<BaseDeliverystateRPhy, String> implements BaseDeliverystateRPhyService {

	@Override
	public ReturnMsg<List<BaseDeliverystateRPhy>> GetDeliveryRPhy(RequestModel<BaseDeliverystateRPhy> stateRPhy) {
		// TODO Auto-generated method stub
		ReturnMsg<List<BaseDeliverystateRPhy>> msg=new ReturnMsg<>();
		try{
			BaseDeliverystateRPhyMapper mapper=this.getMapper(BaseDeliverystateRPhyMapper.class);
			List<BaseDeliverystateRPhy> list=mapper.find(stateRPhy.getModel(),stateRPhy.getSkipResults(), stateRPhy.getPageSize());
			int count=mapper.countWithCondition(stateRPhy.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;
	}

	
	public ResultModel SaveDeliveryRPhy(CommonPage<BaseDeliverystateRPhy> sum) {
		// TODO Auto-generated method stub
		ResultModel rm = new ResultModel();
//		ReturnMsg<BaseDeliverystateRPhy> msg=new ReturnMsg<>();
		List<BaseDeliverystateRPhy> list = sum.getObjectList();
		for (BaseDeliverystateRPhy _Rphy : list) {
			if(DataHelper.IsNullOrSpace(_Rphy.getPhyCodeS())||_Rphy.getStdmaxUpdate()==null||_Rphy.getStdmaxUpdate()==null){
				rm.setMsgInfo("检验项、下限修正值、上限修正值均不能为空");
				rm.setState("500");
				return rm;
			}
		}

//		msg.setResult(stateRPhy);
		try{
			BaseDeliverystateRPhyMapper mapper=this.getMapper(BaseDeliverystateRPhyMapper.class);
			for(BaseDeliverystateRPhy stateRPhy:list)
			{
			if(DataHelper.IsNullOrSpace(stateRPhy.getId())){
				String maxCode=mapper.GetMaxCode();
				String code=DataHelper.InitSerial(maxCode, "", 7);
				stateRPhy.setId(code);
				stateRPhy.setCreateTime(new Date());
				mapper.insert(stateRPhy);
			}else{
				stateRPhy.setUpdateTime(new Date());
				mapper.update(stateRPhy);
			}
			}
			rm.setMsgInfo("保存数据成功");
			rm.setState("200");
			
		}catch(Exception ex){
			
			rm.setState("500");
			rm.setMsgInfo(ex.getMessage());
		}
		return rm;
	}

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

}
