/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.PurOrderClauseDMapper;
import market.Pms.Order.mapper.PurOrderClauseMapper;
import market.Pms.Order.mapper.PurOrderMMapper;
import market.Pms.Order.service.PurOrderClauseDService;
import market.Pms.Order.vo.PurOrderClause;
import market.Pms.Order.vo.PurOrderClauseD;
import market.Pms.Order.vo.PurOrderClauseDSum;
import market.Pms.Order.vo.PurOrderM;

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="PurOrderClauseDService")
public class PurOrderClauseDServiceImpl extends BaseServiceImpl<PurOrderClauseD,Object> implements PurOrderClauseDService {
	/**
	 * 合同条款已匹配查询
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurOrderM> oci){
		ResultModel rm = new ResultModel();
		PageModel<PurOrderClauseD> pm=new PageModel<PurOrderClauseD>(oci.getPageIndex(),oci.getPageSize());
		PurOrderM queryCls=oci.getObject();
		if(queryCls.getContract_pk()==null || queryCls.getContract_pk().isEmpty()){
			rm.setData(pm);
			rm.setState("200");
			return rm;
		}
		try{
			pm.setTotal(this.getMapper(PurOrderClauseDMapper.class).doQueryCount(queryCls));
			pm.setRows(this.getMapper(PurOrderClauseDMapper.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
	 */
	public ResultModel doQueryOrderInitWeb(CommonPage<PurOrderClauseDSum> oci){
		ResultModel rm = new ResultModel();
		PageModel<PurOrderClause> pm=new PageModel<PurOrderClause>(oci.getPageIndex(),oci.getPageSize());
		PurOrderM pom=oci.getObject().getPom();
		PurOrderClause poc = oci.getObject().getPoc();
		
		try{
			pm.setTotal(this.getMapper(PurOrderClauseMapper.class).doQueryOrderInitCount(pom,poc));
			pm.setRows(this.getMapper(PurOrderClauseMapper.class).doQueryOrderInit(pom,poc,(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<PurOrderClauseDSum> oci){
		PurOrderM pom = oci.getObject().getPom();
		List<PurOrderClause> pocList = oci.getObject().getPocList();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		try {
			if(pom!=null && pocList!=null && pocList.size()>0){ // 前端传过来数据不为空
				if(pom.getContract_pk()!=null && !pom.getContract_pk().isEmpty()){  // 主键不为空
					PurOrderM order = this.getMapper(PurOrderMMapper.class).findById(pom.getContract_pk());
					if(order!=null){
						for(PurOrderClause poc:pocList){
							PurOrderClauseD clause = new PurOrderClauseD();
							clause.setClause_code(poc.getClause_code());
							clause.setContract_pk(pom.getContract_pk());
							List<PurOrderClauseD> clauseList = this.getMapper(PurOrderClauseDMapper.class).find(clause, 0, 0);
							if(clauseList.size()>0){
								rm.setState("500");
								rm.setMsgInfo("新增失败！当前条款"+poc.getClause_code()+"已存在该合同中！");
								return rm;
							}
							
							// 新增
							PurOrderClauseD pocd = new PurOrderClauseD();
							pocd.setClause_no(this.getMapper(PurOrderClauseDMapper.class).getNo("TK"+new SimpleDateFormat("yyyyMMdd").format(new Date())));// TK+YYYYMMDD+3位流水
							pocd.setContract_pk(order.getContract_pk());
							pocd.setContract_no(order.getContract_no());
							pocd.setClause_code(poc.getClause_code());
							pocd.setClause_name(poc.getClause_name());
							pocd.setClause_desc(poc.getClause_desc());
							pocd.setClause_order_by(poc.getClause_order_by());
							pocd.setCreate_userid(userId);
							pocd.setCreate_name(userName);
							pocd.setCreate_time(new Date());
							this.getMapper(PurOrderClauseDMapper.class).insert(pocd);
						}
						
					}else{
						rm.setState("500");
						rm.setMsgInfo("新增失败！无效的合同数据");
						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;
		}
		rm.setState("200");
		return rm;
	}
	
	
	
	/**
	 * 合同条款已匹配删除
	 * @param oci
	 * @return
	 */
	public ResultModel doDeleteWeb(CommonPage<PurOrderClauseD> oci){
		ResultModel rm = new ResultModel();
		PurOrderClauseD queryCls=oci.getObject();
		if(queryCls.getClause_no()==null || queryCls.getClause_no().isEmpty()){
			rm.setState("500");
			rm.setMsgInfo("删除失败！数据无效！");
			return rm;
		}
		try{
			// 删除
			this.getMapper(PurOrderClauseDMapper.class).delete(queryCls.getClause_no());
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("错误："+ex.getMessage());
		}
		rm.setState("200");
		return rm;
	}
}
