/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.PurOrderClauseMapper;
import market.Pms.Order.service.PurOrderClauseService;
import market.Pms.Order.vo.PurOrderClause;
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="PurOrderClauseService")
public class PurOrderClauseServiceImpl extends BaseServiceImpl<PurOrderClause,Object> implements PurOrderClauseService {
	/**
	 * 条款配置查询
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurOrderClause> oci){
		ResultModel rm = new ResultModel();
		PageModel<PurOrderClause> pm=new PageModel<PurOrderClause>(oci.getPageIndex(),oci.getPageSize());
		PurOrderClause queryCls=oci.getObject()==null?(new PurOrderClause()):oci.getObject();
		
		try{
			pm.setTotal(this.getMapper(PurOrderClauseMapper.class).doQueryCount(queryCls));
			pm.setRows(this.getMapper(PurOrderClauseMapper.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<PurOrderClause> oci) {
		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(oci.getObjectList()==null||oci.getObjectList().size()==0){
			rm.setState("500");
			rm.setMsgInfo("请传入需新增的数据！");
			return rm;
		}
		try {
			for(PurOrderClause poc:oci.getObjectList()){
				// 判断名称是否相同
				PurOrderClause clause = new PurOrderClause();
				clause.setClause_name(poc.getClause_name());
				List<PurOrderClause> clauseList = this.getMapper(PurOrderClauseMapper.class).find(clause, 0, 0);
				if(clauseList.size()>0){
					rm.setState("500");
					rm.setMsgInfo("新增失败！存在相同的条款名称"+poc.getClause_name());
					return rm;
				}
				// 判断当日内序号是否相同
				Long clauseOrderBy = poc.getClause_order_by();
				String clauseCode = "OC" + new SimpleDateFormat("yyyyMMdd").format(new Date());
				List<PurOrderClause> clauseOrderByList = this.getMapper(PurOrderClauseMapper.class).doQueryOrderByWeb(clauseOrderBy,clauseCode);
				if(clauseOrderByList.size()>0){
					rm.setState("500");
					rm.setMsgInfo("新增失败！存在相同的条款序号"+poc.getClause_order_by());
					return rm;
				}
				poc.setClause_code(this.getMapper(PurOrderClauseMapper.class).getNo("OC"+new SimpleDateFormat("yyyyMMdd").format(new Date())));
				poc.setValidflag("1");
				poc.setCreate_userid(userId);
				poc.setCreate_name(userName);
				poc.setCreate_time(new Date());
				this.getMapper(PurOrderClauseMapper.class).insert(poc);
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败："+ex.getMessage());
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 条款配置操作(memo1：  1编辑  2启用 3停用)
	 * @param oci
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<PurOrderClause> oci){
		PurOrderClause poc = oci.getObject(); // 抬头对象
		String memo1 = oci.getMemo1(); // 操作类型
		ResultModel rm = new ResultModel();
		try {
			if(poc!=null && memo1!=null && !memo1.isEmpty()){
				if("1".equals(memo1)){ // 修改
					rm = doUpdate(poc);
					return rm;
				}else if("2".equals(memo1)){ // 启用
					rm = doEnable(poc);
					return rm;
				}else if("3".equals(memo1)){ // 停用
					rm = doDisable(poc);
					return rm;
				}else{
					rm.setState("500");
					rm.setMsgInfo("无效的操作类型！");
					return rm;
				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("操作失败！当前数据无效！");
				return rm;
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("操作失败："+ex.getMessage());
			return rm;
		}
	}
	
	// 修改
	public ResultModel doUpdate(PurOrderClause poc) throws Exception {
		ResultModel rm = new ResultModel();
		if(poc.getClause_code()!=null && !poc.getClause_code().isEmpty()){ // 判断主键条款代码不为空
			PurOrderClause clause = this.getMapper(PurOrderClauseMapper.class).findById(poc.getClause_code());
			if(clause!=null){ // 判断数据不为空
				if("1".equals(clause.getValidflag())){ // 判断数据有效
					if(poc.getClause_name().equals(clause.getClause_name())){ // 判断不能编辑已存在的条款名称
						// 更新数据
						doUpdateClause(poc);
					}else{
						PurOrderClause poClause = new PurOrderClause();
						poClause.setClause_name(poc.getClause_name());
						List<PurOrderClause> poClauseList = this.getMapper(PurOrderClauseMapper.class).find(poClause, 0, 0);
						if(poClauseList.size()>0){
							rm.setState("500");
							rm.setMsgInfo("编辑失败！已存在相同的条款名称"+poc.getClause_name());
							return rm;
						}
						// 更新数据
						doUpdateClause(poc);
					}
				}else{
					rm.setState("500");
					rm.setMsgInfo("编辑失败！该数据为无效数据！");
					return rm;
				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("编辑失败！该数据不存在！");
				return rm;
			}
		}
		rm.setState("200");
		return rm;
	}
	
	// 启用
	public ResultModel doEnable(PurOrderClause poc)  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(poc.getClause_code()!=null && !poc.getClause_code().isEmpty()){ // 主键不为空
			PurOrderClause clause = this.getMapper(PurOrderClauseMapper.class).findById(poc.getClause_code());
			if(clause!=null){ // 数据库数据不为空
				if("0".equals(clause.getValidflag())){ // 判断是否为有效数据
					clause.setValidflag("1");
					clause.setUpdate_userid(userId);
					clause.setUpdate_name(userName);
					clause.setUpdate_time(new Date());
					this.getMapper(PurOrderClauseMapper.class).update(clause);
				}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(PurOrderClause poc) 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(poc.getClause_code()!=null && !poc.getClause_code().isEmpty()){ // 主键不为空
			PurOrderClause clause = this.getMapper(PurOrderClauseMapper.class).findById(poc.getClause_code());
			if(clause!=null){ // 数据库数据不为空
				if("1".equals(clause.getValidflag())){ // 判断是否为有效数据
					clause.setValidflag("0");
					clause.setDelete_userid(userId);
					clause.setDelete_name(userName);
					clause.setDelete_time(new Date());
					this.getMapper(PurOrderClauseMapper.class).update(clause);
				}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 doUpdateClause(PurOrderClause poc){
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		poc.setUpdate_userid(userId);
		poc.setUpdate_name(userName);
		poc.setUpdate_time(new Date());
		this.getMapper(PurOrderClauseMapper.class).update(poc);
	}

	// 模板行新增初始化查询总条数
	public long doQueryInitCount(PurOrderModeM queryCls) {
		return this.getMapper(PurOrderClauseMapper.class).doQueryInitCount(queryCls);
	}

	// 模板行新增初始化查询
	public List<PurOrderClause> doQueryInit(PurOrderModeM queryCls, int i, int j) {
		return this.getMapper(PurOrderClauseMapper.class).doQueryInit(queryCls,i,j);
	}

	// 模板行查询已匹配条款总条数
	public long doQueryMatchedCount(PurOrderModeM queryCls) {
		return this.getMapper(PurOrderClauseMapper.class).doQueryMatchedCount(queryCls);
	}

	// 模板行已匹配条款查询
	public List<PurOrderClause> doQueryMatched(PurOrderModeM queryCls, int i,
			int j) {
		return this.getMapper(PurOrderClauseMapper.class).doQueryMatched(queryCls,i,j);
	}
	
}
