/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.PurOrderAppendixMapper;
import market.Pms.Order.mapper.PurOrderMMapper;
import market.Pms.Order.service.PurOrderAppendixService;
import market.Pms.Order.vo.PurOrderAppendix;
import market.Pms.Order.vo.PurOrderAppendixSum;
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="PurOrderAppendixService")
public class PurOrderAppendixServiceImpl extends BaseServiceImpl<PurOrderAppendix,Object> implements PurOrderAppendixService {
	/**
	 * 合同附件初始化查询
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurOrderAppendix> oci){
		ResultModel rm = new ResultModel();
		PageModel<PurOrderAppendix> pm=new PageModel<PurOrderAppendix>(oci.getPageIndex(),oci.getPageSize());
		PurOrderAppendix queryCls=oci.getObject();
		if(queryCls.getContract_pk()==null || queryCls.getContract_pk().isEmpty()){
			rm.setData(pm);
			rm.setState("200");
			return rm;
		}
		try{
			PurOrderAppendix poa = new PurOrderAppendix();
			poa.setContract_pk(queryCls.getContract_pk());
			List<PurOrderAppendix> poadList = this.getMapper(PurOrderAppendixMapper.class).findWithCondition(poa, null, "appendix_code", (oci.getPageIndex()-1)*oci.getPageSize(),  oci.getPageIndex()*oci.getPageSize());
			if(poadList.size()>0){
				pm.setTotal(poadList.size());
				pm.setRows(poadList);
			}else{
				pm.setTotal(0);
				pm.setRows(null);
			}
			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<PurOrderAppendixSum> oci){
		PurOrderM pom = oci.getObject().getPom();
		List<PurOrderAppendix> poaList = oci.getObject().getPoaList();
		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 && poaList!=null && poaList.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(PurOrderAppendix poa:poaList){
							if(poa.getAppendix_name()==null || poa.getAppendix_name().isEmpty()){
								rm.setState("500");
								rm.setMsgInfo("新增失败！数据无效！");
								return rm;
							}
							// 新增
							poa.setAppendix_code(this.getMapper(PurOrderAppendixMapper.class).getNo("FJ"+new SimpleDateFormat("yyyyMMdd").format(new Date())));//(FJ+YYYYMMDD+2位流水)
							poa.setContract_pk(order.getContract_pk());
							poa.setContract_no(order.getContract_no());
							poa.setCreate_userid(userId);
							poa.setCreate_name(userName);
							poa.setCreate_time(new Date());
							this.getMapper(PurOrderAppendixMapper.class).insert(poa);
						}
						
					}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<PurOrderAppendix> oci){
		ResultModel rm = new ResultModel();
		PurOrderAppendix queryCls=oci.getObject();
		if(queryCls.getAppendix_code()==null || queryCls.getAppendix_code().isEmpty()){
			rm.setState("500");
			rm.setMsgInfo("删除失败！数据无效！");
			return rm;
		}
		try{
			PurOrderAppendix poa = this.getMapper(PurOrderAppendixMapper.class).findById(queryCls.getAppendix_code());
			if(poa!=null){
				// 删除表数据
				this.getMapper(PurOrderAppendixMapper.class).delete(queryCls.getAppendix_code());
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("错误："+ex.getMessage());
		}
		rm.setState("200");
		return rm;
	}
	
}
