/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-05-14
 * 描述            ：合同模板主表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Pms.Order.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Order.mapper.PurOrderModeMMapper;
import market.Pms.Order.service.PurOrderModeMService;
import market.Pms.Order.vo.PurOrderModeM;

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="PurOrderModeMService")
public class PurOrderModeMServiceImpl extends BaseServiceImpl<PurOrderModeM,Object> implements PurOrderModeMService {
	/**
	 * 模板配置查询
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurOrderModeM> oci){
		ResultModel rm = new ResultModel();
		PageModel<PurOrderModeM> pm=new PageModel<PurOrderModeM>(oci.getPageIndex(),oci.getPageSize());
		PurOrderModeM queryCls=oci.getObject()==null?(new PurOrderModeM()):oci.getObject();
		
		try{
			pm.setTotal(this.getMapper(PurOrderModeMMapper.class).doQueryCount(queryCls));
			pm.setRows(this.getMapper(PurOrderModeMMapper.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;
	}
	
	/**
	 * 模板配置操作 memo1: 1新增  2修改  3启用 4停用
	 * @param oci
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddOrUpdateWeb(CommonPage<PurOrderModeM> oci) {
		PurOrderModeM pom = oci.getObject(); // 模板对象
		String memo1 = oci.getMemo1(); // 操作类型
		ResultModel rm = new ResultModel();
		if(pom==null || memo1==null || memo1.isEmpty()){
			rm.setState("500");
			rm.setMsgInfo("操作的数据无效！");
			return rm;
		}
		try {
			if("1".equals(memo1)){ // 新增
				rm = doAdd(pom);
				return rm;
			}else if("2".equals(memo1)){ // 修改
				rm = doUpdate(pom);
				return rm;
			}else if("3".equals(memo1)){ // 启用
				rm = doEnable(pom);
				return rm;
			}else if("4".equals(memo1)){ // 停用
				rm = doDisable(pom);
				return rm;
			}else{
				rm.setState("500");
				rm.setMsgInfo("无效的操作类型！");
				return rm;
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("操作失败："+ex.getMessage());
			return rm;
		}
	}
	
	// 新增
	public ResultModel doAdd(PurOrderModeM pom) throws Exception{
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if(pom.getMode_name()!=null && !pom.getMode_name().isEmpty()){
			PurOrderModeM mode = new PurOrderModeM();
			mode.setMode_name(pom.getMode_name());
			List<PurOrderModeM> modeList = this.getMapper(PurOrderModeMMapper.class).find(mode, 0, 0);
			if(modeList.size()>0){
				rm.setState("500");
				rm.setMsgInfo("新增失败！当前模板已存在"+pom.getMode_name());
				return rm;
			}
			pom.setMode_code(this.getMapper(PurOrderModeMMapper.class).getNo("OM"+new SimpleDateFormat("yyyyMMdd").format(new Date())));
			pom.setValidflag("1");
			pom.setCreate_userid(userId);
			pom.setCreate_name(userName);
			pom.setCreate_time(new Date());
			this.getMapper(PurOrderModeMMapper.class).insert(pom);
		}else{
			rm.setState("500");
			rm.setMsgInfo("模板名称不能为空！");
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	
	// 修改
	public ResultModel doUpdate(PurOrderModeM pom) throws Exception{
		ResultModel rm = new ResultModel();
		if(pom.getMode_code()!=null && !pom.getMode_code().isEmpty() &&
				pom.getMode_name()!=null && !pom.getMode_name().isEmpty()){
			PurOrderModeM mode = this.getMapper(PurOrderModeMMapper.class).findById(pom.getMode_code());
			if(mode!=null){
				if(pom.getMode_name().equals(mode.getMode_name())){
					doUpdateComm(pom);
				}else{
					// 判断是否有重复的模板名称
					PurOrderModeM poMode = new PurOrderModeM();
					poMode.setMode_name(pom.getMode_name());
					List<PurOrderModeM> poModeList = this.getMapper(PurOrderModeMMapper.class).find(poMode, 0, 0);
					if(poModeList.size()>0){
						rm.setState("500");
						rm.setMsgInfo("存在相同的模板名称"+pom.getMode_name());
						return rm;
					}
					doUpdateComm(pom);
				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("无效的模板数据！");
				return rm;
			}
		}else{
			rm.setState("500");
			rm.setMsgInfo("模板数据不能为空！");
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	
	// 启用
	public ResultModel doEnable(PurOrderModeM pom) throws Exception{
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if(pom.getMode_code()!=null && !pom.getMode_code().isEmpty()){
			PurOrderModeM mode = this.getMapper(PurOrderModeMMapper.class).findById(pom.getMode_code());
			if(mode!=null){
				if("0".equals(mode.getValidflag())){
					mode.setValidflag("1");
					mode.setUpdate_userid(userId);
					mode.setUpdate_name(userName);
					mode.setUpdate_time(new Date());
					this.getMapper(PurOrderModeMMapper.class).update(mode);
				}else{
					rm.setState("500");
					rm.setMsgInfo("启用失败！当前数据是有效数据！");
					return rm;
				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("无效的模板数据！");
				return rm;
			}
		}else{
			rm.setState("500");
			rm.setMsgInfo("模板数据不能为空！");
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	
	// 停用
	public ResultModel doDisable(PurOrderModeM pom) throws Exception{
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if(pom.getMode_code()!=null && !pom.getMode_code().isEmpty()){
			PurOrderModeM mode = this.getMapper(PurOrderModeMMapper.class).findById(pom.getMode_code());
			if(mode!=null){
				if("1".equals(mode.getValidflag())){
					mode.setValidflag("0");
					mode.setDelete_userid(userId);
					mode.setDelete_name(userName);
					mode.setDelete_time(new Date());
					this.getMapper(PurOrderModeMMapper.class).update(mode);
				}else{
					rm.setState("500");
					rm.setMsgInfo("停用失败！当前数据是无效数据！");
					return rm;
				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("无效的模板数据！");
				return rm;
			}
		}else{
			rm.setState("500");
			rm.setMsgInfo("模板数据不能为空！");
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	
	// 修改公共方法
	public void doUpdateComm(PurOrderModeM pom){
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		pom.setUpdate_userid(userId);
		pom.setUpdate_name(userName);
		pom.setUpdate_time(new Date());
		this.getMapper(PurOrderModeMMapper.class).update(pom);
	}
}
