/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-05-14
 * 描述            ：采购订单表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Pms.Order.impl;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Order.mapper.PurOrderCMapper;
import market.Pms.Order.mapper.PurOrderMMapper;
import market.Pms.Order.service.PurOrderCService;
import market.Pms.Order.vo.PurOrderC;
import market.Pms.Order.vo.PurOrderM;
import market.Pms.Order.vo.PurOrderSum;
import market.Pms.Quote.mapper.PurInquiryCMapper;
import market.Pms.Quote.mapper.PurPurchaseCMapper;
import market.Pms.Quote.vo.PurInquiryC;
import market.Pms.Quote.vo.PurPurchaseC;

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="PurOrderCService")
public class PurOrderCServiceImpl extends BaseServiceImpl<PurOrderC,Object> implements PurOrderCService {
	/**
	 * 合同行(订单)信息查询
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurOrderM> oci){
		ResultModel rm = new ResultModel();
		PageModel<PurOrderC> pm=new PageModel<PurOrderC>(oci.getPageIndex(),oci.getPageSize());
		PurOrderM queryCls=oci.getObject();
		if(queryCls.getContract_no()==null || queryCls.getContract_no().isEmpty()){
			rm.setData(pm);
			rm.setState("200");
			return rm;
		}
		
		try{
			pm.setTotal(this.getMapper(PurOrderCMapper.class).doQueryCount(queryCls));
			pm.setRows(this.getMapper(PurOrderCMapper.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<PurOrderSum> 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();
		PurOrderM pom = oci.getObject().getPom();
		List<PurOrderC> pocList = oci.getObject().getPocList();
		if(pom==null || pocList==null || pocList.size()==0){
			rm.setState("500");
			rm.setMsgInfo("请传入需新增的数据！");
			return rm;
		}
		String contract_no = pom.getContract_no();
		String contract_pk = pom.getContract_pk();
		if(contract_no==null || contract_no.isEmpty() || contract_pk==null || contract_pk.isEmpty()){
			rm.setState("500");
			rm.setMsgInfo("当前合同号不存在！");
			return rm;
		}

		PurOrderM  pom2=this.getMapper(PurOrderMMapper.class).findById(contract_pk);
		if(!"10".equals(pom2.getValidflag())){
			rm.setState("500");
			rm.setMsgInfo("当前合同状态不能新增！");
			return rm;
		}
		try {
			for(PurOrderC poc:pocList){
				poc.setOrder_pk(UUID.randomUUID().toString());
				String order_no=contract_no;
				String maxD = this.getMapper(PurOrderCMapper.class).getMaxNo(contract_no);
				if (maxD != null && maxD.length() > 4) {
					String seq = maxD.substring(maxD.length() - 3);
					int iseq = Integer.parseInt(seq) + 1;
					order_no += String.format("%03d", iseq);
				} else {
					order_no += "001";
				}
//				poc.setOrder_no(this.getMapper(PurOrderCMapper.class).getNo(contract_no));
				poc.setOrder_no(order_no);
				poc.setContract_pk(contract_pk);
				poc.setContract_no(contract_no);
				poc.setSupp_code(pom.getSupp_code());
				poc.setSupp_name(pom.getSupp_name());
				poc.setValidflag("10");// 默认10草稿
				poc.setStatus("00"); // 默认00代提报
				poc.setCreate_userid(userId);
				poc.setCreate_name(userName);
				poc.setCreate_time(new Date());
				if(poc.getPurchase_sqe()!=null&&!"".equals(poc.getPurchase_sqe())){
					PurInquiryC  pic=this.getMapper(PurInquiryCMapper.class).findById(poc.getInquiry_sqe());
					poc.setInquiry_no(pic.getInquiry_no());//报价单号
					PurPurchaseC ppc=this.getMapper(PurPurchaseCMapper.class).findById(poc.getPurchase_sqe());
					poc.setPurchase_no(ppc.getPurchase_no());//采购单号
					poc.setPlan_sqe(ppc.getPlan_sqe());//计划行号
					poc.setTax_rate_val("600604");//默认税率16
					poc.setBid_no(poc.getBid_no());
				}
				this.getMapper(PurOrderCMapper.class).insert(poc);
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败："+ex.getMessage());
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 合同行（订单）操作（memo1:1：修改 2：删除  3：提报  4：撤销提报）
	 * @param oci
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdateOrDeleteWeb(CommonPage<PurOrderC> oci){
		ResultModel rm = new ResultModel();
		try {
			PurOrderC poc = oci.getObject();
			String memo1 = oci.getMemo1();
			if(poc!=null && memo1!=null && !memo1.isEmpty()){
				String order_pk = poc.getOrder_pk();
				if(order_pk==null || order_pk.isEmpty()){
					rm.setState("500");
					rm.setMsgInfo("操作失败！当前数据无效！");
					return rm;
				}
				PurOrderC order = this.getMapper(PurOrderCMapper.class).findById(order_pk);
				if(order!=null){
					if("1".equals(memo1)){
						rm = doUpdate(order,poc);
						return rm;
					}else if("2".equals(memo1)){
						rm = doDelete(order);
						return rm;
					}else if("3".equals(memo1)){
						rm = doReport(order);
						return rm;
					}else if("4".equals(memo1)){
						rm = doDisReport(order);
						return rm;
					}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;
		}
	}
	
	// 编辑
	public ResultModel doUpdate(PurOrderC order, PurOrderC 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("10".equals(order.getValidflag()) && ("00".equals(order.getStatus()) || "40".equals(order.getStatus()))){
			poc.setUpdate_userid(userId);
			poc.setUpdate_name(userName);
			poc.setUpdate_time(new Date());
			this.getMapper(PurOrderCMapper.class).update(poc);
		}else{
			rm.setState("500");
			rm.setMsgInfo("操作失败！当前数据不可编辑！");
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	
	// 删除
	public ResultModel doDelete(PurOrderC order) 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("10".equals(order.getValidflag())){
			order.setValidflag("00");
			order.setDelete_userid(userId);
			order.setDelete_name(userName);
			order.setDelete_time(new Date());
			this.getMapper(PurOrderCMapper.class).update(order);
		}else{
			rm.setState("500");
			rm.setMsgInfo("操作失败！当前数据不可删除！");
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	
	// 订单提报
	public ResultModel doReport(PurOrderC order) 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("10".equals(order.getValidflag()) && ("00".equals(order.getStatus()) || "40".equals(order.getStatus()))){
			order.setStatus("10");  // 暂时提报审批通过生效
//			order.setValidflag("20");
			order.setUpdate_userid(userId);
			order.setUpdate_name(userName);
			order.setUpdate_time(new Date());
			this.getMapper(PurOrderCMapper.class).update(order);
		}else{
			rm.setState("500");
			rm.setMsgInfo("操作失败！当前数据不可提报！");
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	
	// 订单撤销提报
	public ResultModel doDisReport(PurOrderC order) 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("10".equals(order.getValidflag()) && "10".equals(order.getStatus())){
			order.setStatus("00"); 
//			order.setValidflag("10");
			order.setUpdate_userid(userId);
			order.setUpdate_name(userName);
			order.setUpdate_time(new Date());
			this.getMapper(PurOrderCMapper.class).update(order);
		}else{
			rm.setState("500");
			rm.setMsgInfo("操作失败！当前数据不可撤销提报！");
			return rm;
		}
		rm.setState("200");
		return rm;
	}
	/**
	 * 采购订单信息表(PUR_ORDER_C)中查询符合条件的采购信息按照显示在采购订单信息表格中
	 * @param[{"object":{"mat_class_code":"产品分类编码(资材备件库)","validflag":"数据状态(20)","item_code":"产品编码","order_no":"采购订单号","supp_code":"供应商代码"},"startTime":"计划到货开始时间","endTime":"计划到货结束时间","pageIndex":"","pageSize":""}]
	 * @return
	 */
	public ResultModel doQueryPurOC(CommonPage<PurOrderC> cp){ResultModel rm = new ResultModel();
	try {
		if (cp != null && cp.getObject() != null
				&& cp.getObject().getValidflag() != null
				&& !"".equals(cp.getObject().getValidflag())
				) {
			// 获取参数
			String startTime = cp.getStartTime();
			String endTime = cp.getEndTime();
			PurOrderC poc = cp.getObject();
			String storage_code = cp.getMemo1();
			// 求时间差
			/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			long m = sdf.parse(endTime).getTime()
					- sdf.parse(startTime).getTime();
			if (m / (1000 * 60 * 60 * 24) > 30) {
				rm.setState("500");
				rm.setMsgInfo("查询时间不能超过30天");
			} else {*/
				// 分页查询
				// PageModel<PurOrderC> pm=new
				// PageModel<PurOrderC>(cp.getPageIndex(),
				// cp.getPageSize());
				// 设置数据集
				// pm.setRows(this.getMapper(PurOrderCMapper.class).doQueryPurOC(poc,
				// startTime, endTime,
				// (cp.getPageIndex()-1)*cp.getPageSize(),
				// cp.getPageIndex()*cp.getPageSize()));
				// 设置总条数
				// pm.setTotal(this.getMapper(PurOrderCMapper.class).doQueryCountPurOC(poc,
				// startTime, endTime));
				// List<PurOrderC>
				// pocList=this.getMapper(PurOrderCMapper.class).doQueryPurOC(poc,
				// startTime, endTime);
				rm.setData(this.getMapper(PurOrderCMapper.class)
						.doQueryPurOC(poc, startTime, endTime,storage_code));
				rm.setState("200");
				rm.setMsgInfo("查询成功！");
			}
/*
		} else {
			rm.setState("500");
			rm.setMsgInfo("参数错误！");
		}*/
	} catch (Exception ex) {
		rm.setState("500");
		rm.setMsgInfo("查询失败！" + ex.getMessage());
	}
	return rm;
}
	
	
	
	
	
}
