/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-05-07
 * 描述            ：物料分类配置表

 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Pms.Mat.impl;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Mat.mapper.PurMatClassMapper;
import market.Pms.Mat.mapper.PurMatMMapper;
import market.Pms.Mat.service.PurMatClassService;
import market.Pms.Mat.vo.PurMatClass;
import market.Pms.Mat.vo.PurMatM;

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;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="PurMatClassService")
public class PurMatClassServiceImpl extends BaseServiceImpl<PurMatClass,Object> implements PurMatClassService {
	/**
	 * 查询
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurMatClass> oci){
		ResultModel rm=new ResultModel();
		PageModel<PurMatClass> pm=new PageModel<PurMatClass>(oci.getPageIndex(),oci.getPageSize());
		PurMatClass queryCls=oci.getObject()==null?(new PurMatClass()):oci.getObject();
		try{
			pm.setTotal(this.getMapper(PurMatClassMapper.class).doQueryCount(queryCls));
			pm.setRows(this.getMapper(PurMatClassMapper.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<PurMatClass> oci){
		ResultModel rm=new ResultModel();
		
		if(oci.getObjectList()==null||oci.getObjectList().size()==0){
			rm.setState("500");
			rm.setMsgInfo("请传入需操作的数据！");
			return rm;
		}
		
		for(PurMatClass pb:oci.getObjectList()){
			rm = ValidData(pb,"0");
			if("500".equals(rm.getState())){
				return rm;
			}
		}
		
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		int i=1;
		String sCode=(oci.getObjectList().get(0).getParent_code()==null?"":oci.getObjectList().get(0).getParent_code());
		try{
			String sWhere=" parent_code is null ";
			if(!"".equals(sCode)){
				sWhere=" mat_class_code like '"+sCode+"__' ";
			}
			List<PurMatClass> queryClsList=this.getMapper(PurMatClassMapper.class).findWithCondition(null, sWhere, " Mat_class_code desc ", 0, 1);
			if(queryClsList!=null&&queryClsList.size()>0){
				i=Integer.parseInt(queryClsList.get(0).getMat_class_code().substring((sCode.length()==0?1:sCode.length())))+1;
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("新增失败："+ex.getMessage());
			return rm;
		}
		
		for(PurMatClass pb:oci.getObjectList()){
			pb.setSort_code(i+"");
			pb.setMat_class_code("".equals(sCode)?("W"+String.format("%02d", i++)):(sCode+String.format("%02d", i++)));
			pb.setCreate_name(userName);
			pb.setCreate_userid(userId);
			pb.setCreate_time(new Date());
			this.getMapper(PurMatClassMapper.class).insert(pb);
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 修改,其中有效，无效则传编号跟状态即可
	 * @param oci 
	 * memo1 1正常修改  2则为有效无效处理
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<PurMatClass> oci){
		ResultModel rm=new ResultModel();
		String sType=("1".equals(oci.getMemo1())?"1":"");
		rm = ValidData(oci.getObject(),sType);
		if("500".equals(rm.getState())){
			return rm;
		}
		
		try{
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			
			if(!"1".equals(oci.getMemo1())){
				if("1".equals(oci.getObject().getValidflag())){//有效则作废数据制空
					this.getMapper(PurMatClassMapper.class).doUpdate(oci.getObject());
				}else{
					oci.getObject().setDelete_name(userName);
					oci.getObject().setDelete_userid(userId);
					oci.getObject().setDelete_time(new Date());
					this.getMapper(PurMatClassMapper.class).update(oci.getObject());
				}
			}else{
				oci.getObject().setUpdate_name(userName);
				oci.getObject().setUpdate_userid(userId);
				oci.getObject().setUpdate_time(new Date());
				this.getMapper(PurMatClassMapper.class).update(oci.getObject());
			}
			
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("修改失败："+ex.getMessage());
		}
		
		return rm;
	}
	
	/**
	 * 修改排序号
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSaveSortWeb(CommonPage<PurMatClass> oci){
		ResultModel rm=new ResultModel();
		if(oci.getObjectList()==null||oci.getObjectList().size()==0){
			rm.setState("500");
			rm.setMsgInfo("请传入需操作的数据！");
			return rm;
		}
		
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		for(PurMatClass pb:oci.getObjectList()){
			if(pb.getSort_code()!=null&&!"".equals(pb.getSort_code())){
				pb.setUpdate_name(userName);
				pb.setUpdate_userid(userId);
				pb.setUpdate_time(new Date());
				this.getMapper(PurMatClassMapper.class).update(pb);
			}
		}
		
		rm.setState("200");
		return rm;
	}
	
	
	/**
	 * 公共的数据验证，若需要其它的验证则继续增加
	 * @param oci
	 * @param sType 0新增  1修改
	 * @return
	 */
	public ResultModel ValidData(PurMatClass oci,String sType){
		ResultModel rm=new ResultModel();
		if(oci==null){
			rm.setState("500");
			rm.setMsgInfo("请传入需操作的数据！");
			return rm;
		}
		

		//需要验证数据是否出现重复的名称
		if(!"".equals(sType)){
			try{
				PurMatClass validCls=new PurMatClass();
				validCls.setMat_class_name(oci.getMat_class_name().trim());
				if("0".equals(sType)){
					ValidName(validCls);
					//若分类下面有物料信息则不允许增加子分类
					if(oci.getParent_code()!=null&&!"".equals(oci.getParent_code())){
						PurMatM validMCls=new PurMatM();
						validMCls.setMat_class_code(oci.getParent_code());
						int icount=this.getMapper(PurMatMMapper.class).count(validMCls);
						if(icount>0){
							throw new Exception("物料分类["+oci.getParent_name()+"]下面已经存在具体的物料基础信息，无法新增子级分类!");
						}
					}
					
				}else if("1".equals(sType)){
					//先获取是否存在被修改的数据
					PurMatClass pbd=this.getMapper(PurMatClassMapper.class).findById(oci.getMat_class_code());
					if(pbd!=null&&pbd.getMat_class_code()!=null){
						if(!oci.getMat_class_name().equals(pbd.getMat_class_name())){
							ValidName(validCls);
						}
					}else{
						rm.setState("500");
						rm.setMsgInfo("当前数据已被删除，无法修改！");
						return rm;
					}
				}
				
			}catch(Exception ex){
				rm.setState("500");
				rm.setMsgInfo("数据验证异常："+ex.getMessage());
				return rm;
			}
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 验证是否存在相同名称的数据
	 * @param oci
	 * @throws Exception
	 */
	public void ValidName(PurMatClass oci) throws Exception{
		int icount=this.getMapper(PurMatClassMapper.class).count(oci);
		if(icount>0){
			throw new Exception("已存在相同名称的数据!");
		}
	}
}
