package market.Qms.com.service.impl;

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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.mapper.BaseSteelRSpecMapper;
import market.Qms.com.service.BaseSteelRSpecService;
import market.Qms.com.vo.BaseSteelRSpec;
import market.Qms.common.ReturnMsg;

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

@RemoteService(serviceInterface=BaseSteelRSpecService.class)
public class BaseSteelRSpecServiceImpl extends BaseServiceImpl<BaseSteelRSpec, Integer> implements BaseSteelRSpecService {


	public ResultModel GetSteelSpec(CommonPage<BaseSteelRSpec> steel) {
//		ReturnMsg<List<BaseSteelRSpec>> msg=new ReturnMsg<>();
		List<BaseSteelRSpec> list=new ArrayList<>();
		ResultModel rm=new ResultModel();
//		List<BaseSteel> list=new ArrayList<>();
		int pageIndex=steel.getPageIndex();
		int pageSize=steel.getPageSize();
		try{
			PageModel<BaseSteelRSpec> pm = new PageModel<BaseSteelRSpec>(pageIndex,pageSize);
			BaseSteelRSpecMapper mapper=this.getMapper(BaseSteelRSpecMapper.class);
			String param = "id DESC";
			if(steel.getMemo1() !=null){
				param = null;
				param = steel.getMemo1();
				if (steel.getMemo2().equals("1")) {
					param=param+" DESC";
				}
			}
			list=mapper.findWithCondition(steel.getObject(), null, param, (pageIndex - 1) * pageSize, pageSize);
			int count=mapper.countWithCondition(steel.getObject(),null);
			pm.setRows(list);
			pm.setTotal(count);
			rm.setData(pm);
			rm.setState("200");
			rm.setMsgInfo("获取数据成功");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo(ex.getMessage());
		}
		return rm;
	}

	public ResultModel SaveSteelSpec(CommonPage<BaseSteelRSpec> sum) {
		ResultModel rm=new ResultModel();
		try{
			List<BaseSteelRSpec> list = sum.getObjectList();
			BaseSteelRSpecMapper mapper=this.getMapper(BaseSteelRSpecMapper.class);
			Date date = new Date();
			for(BaseSteelRSpec steel:list)
			{
			if(steel.getId()==null){//新增
				Integer maxCode=mapper.GetMaxCode();
				if(maxCode==null){
					maxCode=1;
				}else{
					maxCode=maxCode+1;
				}
				steel.setCreateTime(date);
				steel.setId(maxCode);
				mapper.insert(steel);
			}else{
				steel.setUpdateTime(date);
				mapper.update(steel);
			}
			}
		
			rm.setState("200");
			rm.setMsgInfo("保存成功");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo(ex.getMessage());
		}
		return rm;
	}

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

}
