/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-05-07
 * 描述            ：度量单位转换配置表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Pms.Bs.impl;
import java.util.Date;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Bs.mapper.PurBsUomsRMapper;
import market.Pms.Bs.service.PurBsUomsRService;
import market.Pms.Bs.vo.PurBsUomsR;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="PurBsUomsRService")
public class PurBsUomsRServiceImpl extends BaseServiceImpl<PurBsUomsR,Object> implements PurBsUomsRService {
	/**
	 * 查询
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurBsUomsR> oci){
		ResultModel rm=new ResultModel();
		PurBsUomsR queryCls=oci.getObject()==null?(new PurBsUomsR()):oci.getObject();
		try{
			rm.setData(this.getMapper(PurBsUomsRMapper.class).findWithCondition(queryCls, null, " create_time ", 0, 0));
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("错误："+ex.getMessage());
		}
		
		return rm;
	}
	
	/**
	 * 新增
	 * @param oci
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<PurBsUomsR> oci) throws Exception{
		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(PurBsUomsR pb:oci.getObjectList()){
			rm = ValidData(pb);
			if("500".equals(rm.getState())){
				throw new Exception(rm.getMsgInfo());
			}
			
			PurBsUomsR queryCls=new PurBsUomsR();
			queryCls.setUom_code(pb.getUom_code());
			queryCls.setConvert_unit_code(pb.getConvert_unit_code());
			queryCls.setUom_conefficient(pb.getUom_conefficient());
			int icount=this.getMapper(PurBsUomsRMapper.class).count(queryCls);
			if(icount>0){
				throw new Exception("该度量单位已存在系数为["+queryCls.getUom_conefficient()+"],转换单位代码为["+queryCls.getConvert_unit_code()+"]");
			}
			
			pb.setCreate_name(userName);
			pb.setCreate_userid(userId);
			pb.setCreate_time(new Date());
			this.getMapper(PurBsUomsRMapper.class).insert(pb);
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 修改
	 * @param oci 
	 * memo1 原来的uom_code
	 * memo2 原来的uom_conefficient
	 * memo3 原来的convert_unit_code
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<PurBsUomsR> oci){
		ResultModel rm=new ResultModel();
		rm = ValidData(oci.getObject());
		if("500".equals(rm.getState())){
			return rm;
		}
		
		try{
			//先验证数据是否修改过
			if(!oci.getMemo1().equals(oci.getObject().getUom_code())
					||!oci.getMemo2().equals(oci.getObject().getUom_conefficient())
					||!oci.getMemo3().equals(oci.getObject().getConvert_unit_code())){
				PurBsUomsR queryCls=new PurBsUomsR();
				queryCls.setUom_code(oci.getObject().getUom_code());
				queryCls.setConvert_unit_code(oci.getObject().getConvert_unit_code());
				queryCls.setUom_conefficient(oci.getObject().getUom_conefficient());
				
				int icount=this.getMapper(PurBsUomsRMapper.class).count(queryCls);
				if(icount>0){
					rm.setState("500");
					rm.setMsgInfo("该度量单位已存在系数为["+queryCls.getUom_conefficient()+"],转换单位代码为["+queryCls.getConvert_unit_code()+"]");
					return rm;
				}	
			}
			
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			
			oci.getObject().setUpdate_name(userName);
			oci.getObject().setUpdate_userid(userId);
			oci.getObject().setUpdate_time(new Date());
			this.getMapper(PurBsUomsRMapper.class).doUpdate(oci.getObject(),oci.getMemo2());
			
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("修改失败："+ex.getMessage());
		}
		
		return rm;
	}
	
	/**
	 * 删除
	 * @param oci 
	 * @return
	 */
	public ResultModel doDeleteWeb(CommonPage<PurBsUomsR> oci){
		ResultModel rm=new ResultModel();
		rm = ValidData(oci.getObject());
		if("500".equals(rm.getState())){
			return rm;
		}
		
		try{
			this.getMapper(PurBsUomsRMapper.class).doDelete(oci.getObject());			
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("删除失败："+ex.getMessage());
		}
		
		return rm;
	}
	
	
	/**
	 * 公共的数据验证，若需要其它的验证则继续增加
	 * @param oci
	 * @return
	 */
	public ResultModel ValidData(PurBsUomsR oci){
		ResultModel rm=new ResultModel();
		if(oci==null){
			rm.setState("500");
			rm.setMsgInfo("请传入需操作的数据！");
			return rm;
		}
		rm.setState("200");
		return rm;
	}
}
