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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Order.mapper.PurOrderModeCMapper;
import market.Pms.Order.service.PurOrderModeCService;
import market.Pms.Order.vo.PurOrderClause;
import market.Pms.Order.vo.PurOrderModeC;
import market.Pms.Order.vo.PurOrderModeM;
import market.Pms.Order.vo.PurOrderModeSum;

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;

@RemoteService(path="PurOrderModeCService")
public class PurOrderModeCServiceImpl extends BaseServiceImpl<PurOrderModeC,Object> implements PurOrderModeCService {
	/**
	 * 模板行查询(memo1:1、已匹配当前模板头条款   2、未匹配匹配当前模板头有效条款)
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurOrderModeM> oci){
		ResultModel rm = new ResultModel();
		PageModel<PurOrderClause> pm=new PageModel<PurOrderClause>(oci.getPageIndex(),oci.getPageSize());
		PurOrderModeM queryCls=oci.getObject();
		String memo1 = oci.getMemo1();
		if(queryCls==null || memo1==null || memo1.isEmpty()){
			rm.setData(pm);
			rm.setState("200");
			return rm;
		}
		try{
			if("1".equals(memo1)){ 
				pm.setTotal(this.getBean(PurOrderClauseServiceImpl.class).doQueryMatchedCount(queryCls));
				pm.setRows(this.getBean(PurOrderClauseServiceImpl.class).doQueryMatched(queryCls, (oci.getPageIndex()-1)*oci.getPageSize(), oci.getPageIndex()*oci.getPageSize()));
			}else if("2".equals(memo1)){
				pm.setTotal(this.getBean(PurOrderClauseServiceImpl.class).doQueryInitCount(queryCls));
				pm.setRows(this.getBean(PurOrderClauseServiceImpl.class).doQueryInit(queryCls, (oci.getPageIndex()-1)*oci.getPageSize(), oci.getPageIndex()*oci.getPageSize()));
			}else{
				rm.setData(pm);
				rm.setState("200");
				return rm;
			}
			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<PurOrderModeSum> oci){
		PurOrderModeM pomm = oci.getObject().getPomm();
		List<PurOrderClause> pocList = oci.getObject().getPocList();
		ResultModel rm = new ResultModel();
		try {
			if(pomm!=null && pocList!=null && pocList.size()>0){ // 前端传过来数据不为空
				if(pomm.getMode_code()!=null && !pomm.getMode_code().isEmpty()){  // 模板主键不为空
					for(PurOrderClause poc:pocList){
						if(poc.getClause_code()==null || poc.getClause_code().isEmpty() ){
							rm.setState("500");
							rm.setMsgInfo("新增失败！无效的条款数据"+poc.getClause_code());
							return rm;
						}
						// 新增模板行
						PurOrderModeC pomc = new PurOrderModeC();
						pomc.setMode_code(pomm.getMode_code());
						pomc.setMode_name(pomm.getMode_name());
						pomc.setClause_code(poc.getClause_code());
						pomc.setClause_name(poc.getClause_name());
						pomc.setClause_desc(poc.getClause_desc());
						pomc.setClause_type(poc.getClause_type());
						pomc.setClause_indx(poc.getClause_order_by());
						this.getMapper(PurOrderModeCMapper.class).doInsert(pomc);
					}
				}else{
					rm.setState("500");
					rm.setMsgInfo("新增失败！无效的模板头数据"+pomm.getMode_code());
					return rm;
				}
			}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
	 * @return
	 */
	public ResultModel doDeleteWeb(CommonPage<PurOrderModeSum> oci){
		PurOrderModeM pomm = oci.getObject().getPomm();
		List<PurOrderClause> pocList = oci.getObject().getPocList();
		ResultModel rm = new ResultModel();
		try {
			if(pomm!=null && pocList!=null && pocList.size()>0){ // 前端传过来数据不为空
				if(pomm.getMode_code()!=null && !pomm.getMode_code().isEmpty() &&
						pocList.get(0).getClause_code()!=null && !pocList.get(0).getClause_code().isEmpty()){  // 主键不为空
					// 删除
					this.getMapper(PurOrderModeCMapper.class).doDelete(pomm.getMode_code(),pocList.get(0).getClause_code());
				}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;
		}
		rm.setState("200");
		return rm;
	}
}
