/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-09-04
 * 描述            ：采购单入围供应商表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Pms.Quote.impl;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Quote.mapper.PurPurchaseEMapper;
import market.Pms.Quote.mapper.PurPurchaseMMapper;
import market.Pms.Quote.service.PurPurchaseEService;
import market.Pms.Quote.vo.PurPurchaseE;
import market.Pms.Quote.vo.PurPurchaseM;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.mapper.UserMapper;
import com.hnshituo.core.auth.service.HomeService;
import com.hnshituo.core.auth.vo.User;

@RemoteService(path="PurPurchaseEService")
public class PurPurchaseEServiceImpl extends BaseServiceImpl<PurPurchaseE,Object> implements PurPurchaseEService {
	/**
	 *供应商信息查询
	 * 
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurPurchaseE> oci) {
		ResultModel rm = new ResultModel();
		PurPurchaseE ppd = oci.getObject();
		PurPurchaseE ppd2=new PurPurchaseE();
		ppd2.setPurchase_no(ppd.getPurchase_no());
		try {
//			List<PurPurchaseE> list = this.getMapper(PurPurchaseEMapper.class)
//					.find(ppd2, 0, 0);
			String swhere="1=1";
			if(ppd.getSupp_name()!=null&&!"".equals(ppd.getSupp_name())){
				swhere += "and SUPP_NAME like '%" + ppd.getSupp_name() + "%'";
			}
			List<PurPurchaseE> list = this.getMapper(PurPurchaseEMapper.class)
					.findWithCondition(ppd2, swhere, "CREATE_TIME desc", 0, 0);
			rm.setData(list);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	/**
	 *供应商信息新增
	 * 
	 * @param oci
	 * @return
	 * @throws Exception 
	 */
	public ResultModel doAddWeb(CommonPage<PurPurchaseE> oci) throws Exception {
		ResultModel rm = new ResultModel();
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		List<PurPurchaseE> ppdlist = oci.getObjectList();
		String purchase_no=oci.getMemo1();
		String bid_no=oci.getMemo2();
		User user2=this.getMapper(UserMapper.class).findById(userId);
		try {
			PurPurchaseM ppm = this.getMapper(PurPurchaseMMapper.class)
					.findById(purchase_no);// 查询采购单号，并判断状态
			//采购单为草稿状态下，并且未提报评审可操作
			if ("10".equals(ppm.getValidflag()) || "00".equals(ppm.getStatus())) {
			for (PurPurchaseE ppe : ppdlist) {
				ppe.setDept_code(user2.getDept_id());//设置部门
				ppe.setDept_desc(user2.getDept_name());
				ppe.setPurchase_no(purchase_no);//采购单号
				ppe.setBid_no(bid_no);//标段号
				ppe.setCreate_userid(userId);
				ppe.setCreate_name(userName);
				ppe.setCreate_time(new Date());
				PurPurchaseE ppe2=new PurPurchaseE();
				ppe2.setPurchase_no(purchase_no);//采购单号
				ppe2.setBid_no(bid_no);//标段号
				ppe2.setSupp_code(ppe.getSupp_code());//供应商信息
				ppe2.setSupp_name(ppe.getSupp_name());//
				int  count=this.getMapper(PurPurchaseEMapper.class).countWithCondition(ppe2, null);
				if(count!=0){
					throw new Exception("当前供应商"+ppe.getSupp_name()+"已存在该"+bid_no+"标段号");
				}
				this.getMapper(PurPurchaseEMapper.class).insert(ppe);			
			}
			rm.setMsgInfo("新增成功");
			rm.setState("200");
			} else {
				rm.setState("500");
				rm.setMsgInfo("当前数据状态不可新增");
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败，请联系管理员");
//			throw ex;
			throw new Exception(ex.getMessage());
		}
		return rm;
	}
	/**
	 *供应商信息删除
	 * 
	 * @param oci
	 * @return
	 */
	public ResultModel doDeleteWeb(CommonPage<PurPurchaseE> oci) {
		ResultModel rm = new ResultModel();
		List<PurPurchaseE> ppdlist = oci.getObjectList();
		try {
			PurPurchaseM ppm = this.getMapper(PurPurchaseMMapper.class)
					.findById(ppdlist.get(0).getPurchase_no());// 查询采购单号，并判断状态
			//采购单为草稿状态下，并且未提报评审可操作
			if ("10".equals(ppm.getValidflag()) || "00".equals(ppm.getStatus())) {
			for (PurPurchaseE ppe : ppdlist) {
				this.getMapper(PurPurchaseEMapper.class).deleteBidNo(ppe);			
			}
			rm.setState("200");
			rm.setMsgInfo("删除成功！");
			} else {
				rm.setState("500");
				rm.setMsgInfo("当前数据状态不可删除");
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("删除失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	
	/**
	 * 
	 * @author 赵淋支
	 * @date   2018年11月2日
	 * @parameter TODO
	 * @return TODO
	 * @description 采购单编制，采购单审批界面：通过采购单查询供应商
	 */
	public ResultModel doQueryPurchaseE(CommonPage<PurPurchaseE> oci) {
		ResultModel rm = new ResultModel();
		PurPurchaseE ppe = oci.getObject();
		try {
			String condition = "";
			if (ppe!=null) {
				//采购单号
				if (ppe.getPurchase_no()!=null && !"".equals(ppe.getPurchase_no())) {
					condition += " AND E.PURCHASE_NO = '"+ppe.getPurchase_no()+"'";
				}
				//采购单号
				if (ppe.getSupp_name()!=null && !"".equals(ppe.getSupp_name())) {
					condition += " AND E.SUPP_NAME like '%"+ppe.getSupp_name()+"%'";
				}
			}
			List<PurPurchaseE> list = this.getMapper(PurPurchaseEMapper.class)
					.doQueryPurchaseE(condition);
			rm.setData(list);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	
	
}
