/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-05-07
 * 描述            ：供应商能力表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Pms.Supp.impl;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Supp.mapper.PurSuppGradeMapper;
import market.Pms.Supp.service.PurSuppGradeService;
import market.Pms.Supp.vo.PurSuppGrade;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path="PurSuppGradeService")
public class PurSuppGradeServiceImpl extends BaseServiceImpl<PurSuppGrade,Object> implements PurSuppGradeService {
	/**
	 * 查询
	 * 若供应商不为空，且pageSize为1000 （供应商不可能有1000种物料供应）
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurSuppGrade> oci){
		ResultModel rm=new ResultModel();
		PageModel<PurSuppGrade> pm=new PageModel<PurSuppGrade>(oci.getPageIndex(),oci.getPageSize());
		PurSuppGrade queryCls=oci.getObject()==null?(new PurSuppGrade()):oci.getObject();
		try{
			pm.setTotal(this.getMapper(PurSuppGradeMapper.class).doQueryCount(queryCls));
			pm.setRows(this.getMapper(PurSuppGradeMapper.class).doQuery(queryCls, (oci.getPageIndex()-1)*oci.getPageSize(), oci.getPageIndex()*oci.getPageSize()));
		
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("错误："+ex.getMessage());
		}
		
		return rm;
	}
	
	/**
	 * 新增
	 * 删除原来的增加当前的
	 * @param oci
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<PurSuppGrade> oci){
		ResultModel rm=new ResultModel();
		
		if(oci.getObjectList()==null||oci.getObjectList().size()==0){
			rm.setState("500");
			rm.setMsgInfo("请传入需操作的数据！");
			return rm;
		}
		
		for(PurSuppGrade pb:oci.getObjectList()){
			rm = ValidData(pb,"0");
			if("500".equals(rm.getState())){
				return rm;
			}
		}
		/*PurSuppGrade psg=new PurSuppGrade();
		psg.setSupp_code(oci.getObjectList().get(0).getSupp_code());
		//删除原来的
		this.getMapper(PurSuppGradeMapper.class).doDelete(psg);*/
		//新增当前选中的
		for(PurSuppGrade pb:oci.getObjectList()){
			this.getMapper(PurSuppGradeMapper.class).insert(pb);
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 删除
	 * @return
	 */
	public ResultModel doDeleteWeb(CommonPage<PurSuppGrade> oci){
		ResultModel rm=new ResultModel();
		rm = ValidData(oci.getObject(),"");
		if("500".equals(rm.getState())){
			return rm;
		}
		
		try{
			this.getMapper(PurSuppGradeMapper.class).doDelete(oci.getObject());
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("删除失败："+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 公共的数据验证，若需要其它的验证则继续增加
	 * @param oci
	 * @param sType 0新增  1修改
	 * @return
	 */
	public ResultModel ValidData(PurSuppGrade oci,String sType){
		ResultModel rm=new ResultModel();
		if(oci==null){
			rm.setState("500");
			rm.setMsgInfo("请传入需操作的数据！");
			return rm;
		}
		
		//需要验证数据是否出现重复的名称
		if(!"".equals(sType)){
			try{
				PurSuppGrade validCls=new PurSuppGrade();
				validCls.setItem_code(oci.getItem_code());
				validCls.setSupp_code(oci.getSupp_code());
				ValidName(validCls);
				
			}catch(Exception ex){
				rm.setState("500");
				rm.setMsgInfo("数据验证异常："+ex.getMessage());
				return rm;
			}
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 验证是否存在相同名称的数据
	 * @param oci
	 * @throws Exception
	 */
	public void ValidName(PurSuppGrade oci) throws Exception{
		int icount=this.getMapper(PurSuppGradeMapper.class).count(oci);
		if(icount>0){
			throw new Exception("已存在相同物料,物料代码为["+oci.getItem_code()+"]!");
		}
	}
}
