/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-05-07
 * 描述            ：供应商信息主表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Pms.Supp.impl;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Supp.mapper.PurSuppAptitudeMapper;
import market.Pms.Supp.mapper.PurSuppBankMapper;
import market.Pms.Supp.mapper.PurSuppGradeMapper;
import market.Pms.Supp.mapper.PurSuppMMapper;
import market.Pms.Supp.service.PurSuppMService;
import market.Pms.Supp.vo.PurSuppAptitude;
import market.Pms.Supp.vo.PurSuppBank;
import market.Pms.Supp.vo.PurSuppGrade;
import market.Pms.Supp.vo.PurSuppM;

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="PurSuppMService")
public class PurSuppMServiceImpl extends BaseServiceImpl<PurSuppM,Object> implements PurSuppMService {
	/**
	 * 查询
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurSuppM> oci){
		ResultModel rm=new ResultModel();
		PageModel<PurSuppM> pm=new PageModel<PurSuppM>(oci.getPageIndex(),oci.getPageSize());
		PurSuppM queryCls=oci.getObject()==null?(new PurSuppM()):oci.getObject();
		try{
			pm.setTotal(this.getMapper(PurSuppMMapper.class).doQueryCount(queryCls));
			pm.setRows(this.getMapper(PurSuppMMapper.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<PurSuppM> oci){
		ResultModel rm=new ResultModel();
		
		if(oci.getObjectList()==null||oci.getObjectList().size()==0){
			rm.setState("500");
			rm.setMsgInfo("请传入需操作的数据！");
			return rm;
		}
		
		for(PurSuppM 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;
		try{
			List<PurSuppM> queryClsList=this.getMapper(PurSuppMMapper.class).findWithCondition(null, " supp_code like 'P%' ", " supp_code desc ", 0, 1);
			if(queryClsList!=null&&queryClsList.size()>0){
				i=Integer.parseInt(queryClsList.get(0).getSupp_code().substring(1))+1;
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("新增失败："+ex.getMessage());
			return rm;
		}
		
		for(PurSuppM pb:oci.getObjectList()){
			pb.setSupp_code("P"+String.format("%05d", i++));
			pb.setCreate_name(userName);
			pb.setCreate_userid(userId);
			pb.setCreate_time(new Date());
			pb.setSuspend_status("0");
			this.getMapper(PurSuppMMapper.class).insert(pb);
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 修改,其中有效，无效则传编号跟状态即可
	 * @param oci 
	 * memo1 1正常修改  2则为有效无效处理
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdateWeb(CommonPage<PurSuppM> oci){
		ResultModel rm=new ResultModel();
		String sType=("1".equals(oci.getMemo1())?"1":"");
		rm = ValidData(oci.getObject(),sType);
		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);
		
		if(!"1".equals(oci.getMemo1())){
			if("1".equals(oci.getObject().getValidflag())){//有效则作废数据制空
				this.getMapper(PurSuppMMapper.class).doUpdate(oci.getObject());
			}else{
				oci.getObject().setDelete_name(userName);
				oci.getObject().setDelete_userid(userId);
				oci.getObject().setDelete_time(new Date());
				this.getMapper(PurSuppMMapper.class).update(oci.getObject());
			}
		}else{
			oci.getObject().setUpdate_name(userName);
			oci.getObject().setUpdate_userid(userId);
			oci.getObject().setUpdate_time(new Date());
			this.getMapper(PurSuppMMapper.class).update(oci.getObject());
			//调整子表数据
			
			PurSuppAptitude psa=new PurSuppAptitude();
			psa.setSupp_code(oci.getObject().getSupp_code());
			psa.setSupp_short_name(oci.getObject().getSupp_short_name());
			psa.setSupp_name(oci.getObject().getSupp_name());
			this.getMapper(PurSuppAptitudeMapper.class).doUpdateSupp_name(psa);
			
			PurSuppBank psb=new PurSuppBank();
			psb.setSupp_code(oci.getObject().getSupp_code());
			psb.setSupp_short_name(oci.getObject().getSupp_short_name());
			psb.setSupp_name(oci.getObject().getSupp_name());
			this.getMapper(PurSuppBankMapper.class).doUpdateSupp_name(psb);
			
			PurSuppGrade psg=new PurSuppGrade();
			psg.setSupp_code(oci.getObject().getSupp_code());
			psg.setSupp_short_name(oci.getObject().getSupp_short_name());
			psg.setSupp_name(oci.getObject().getSupp_name());
			this.getMapper(PurSuppGradeMapper.class).doUpdateSupp_name(psg);
		}
		
		rm.setState("200");

		
		return rm;
	}
	
	
	/**
	 * 修改 挂起，冻结
	 * @param oci 
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUspendStatusUpdateWeb(CommonPage<PurSuppM> oci){
		ResultModel rm=new ResultModel();
		if(oci.getObjectList()==null){
			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(PurSuppM pb:oci.getObjectList()){
			pb.setUpdate_name(userName);
			pb.setUpdate_userid(userId);
			pb.setUpdate_time(new Date());
			this.getMapper(PurSuppMMapper.class).update(pb);	
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 公共的数据验证，若需要其它的验证则继续增加
	 * @param oci
	 * @param sType 0新增  1修改
	 * @return
	 */
	public ResultModel ValidData(PurSuppM oci,String sType){
		ResultModel rm=new ResultModel();
		if(oci==null){
			rm.setState("500");
			rm.setMsgInfo("请传入需操作的数据！");
			return rm;
		}
		
		//需要验证数据是否出现重复的名称
		if(!"".equals(sType)){
			try{
				PurSuppM validCls=new PurSuppM();
				validCls.setSupp_name(oci.getSupp_name().trim());
				if("0".equals(sType)){
					ValidName(validCls);
				}else if("1".equals(sType)){
					//先获取是否存在被修改的数据
					PurSuppM pbd=this.getMapper(PurSuppMMapper.class).findById(oci.getSupp_code());
					if(pbd!=null&&pbd.getSupp_code()!=null){
						if(!oci.getSupp_name().equals(pbd.getSupp_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(PurSuppM oci) throws Exception{
		int icount=this.getMapper(PurSuppMMapper.class).count(oci);
		if(icount>0){
			throw new Exception("已存在["+oci.getSupp_name()+"]名称的数据!");
		}
	}
}
