/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.PurPurchaseCMapper;
import market.Pms.Quote.mapper.PurPurchaseDMapper;
import market.Pms.Quote.mapper.PurPurchaseEMapper;
import market.Pms.Quote.mapper.PurPurchaseFMapper;
import market.Pms.Quote.mapper.PurPurchaseMMapper;
import market.Pms.Quote.service.PurPurchaseDService;
import market.Pms.Quote.vo.PurPurchaseC;
import market.Pms.Quote.vo.PurPurchaseD;
import market.Pms.Quote.vo.PurPurchaseE;
import market.Pms.Quote.vo.PurPurchaseF;
import market.Pms.Quote.vo.PurPurchaseM;

import org.springframework.transaction.annotation.Transactional;

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 = "PurPurchaseDService")
public class PurPurchaseDServiceImpl extends
		BaseServiceImpl<PurPurchaseD, Object> implements PurPurchaseDService {
	/**
	 * 评标规则信息查询
	 * 
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PurPurchaseD> oci) {
		ResultModel rm = new ResultModel();
		PurPurchaseD ppd = oci.getObject();
		PurPurchaseD ppd2=new PurPurchaseD();
		ppd2.setPurchase_no(ppd.getPurchase_no());
		try {
//			List<PurPurchaseD> list = this.getMapper(PurPurchaseDMapper.class)
//					.find(ppd2, 0, 0);
			List<PurPurchaseD> list = this.getMapper(PurPurchaseDMapper.class)
					.findWithCondition(ppd2, null, "BID_NO  desc", 0, 0);
			rm.setData(list);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败，请联系管理员");
			throw ex;
		}
		return rm;
	}

	/**
	 * 评标规则信息新增
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddWeb(CommonPage<PurPurchaseD> oci) {
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		User user2=this.getMapper(UserMapper.class).findById(userId);
		ResultModel rm = new ResultModel();
		PurPurchaseD ppd = oci.getObject();
		try {
			PurPurchaseM ppm = this.getMapper(PurPurchaseMMapper.class)
					.findById(ppd.getPurchase_no());// 查询采购单号，并判断状态
			//采购单为草稿状态下，并且未提报评审可操作
			if ("10".equals(ppm.getValidflag()) || "00".equals(ppm.getStatus())) {
			String bid_no = "B";
			String maxD = this.getMapper(PurPurchaseDMapper.class)
					.findMaxBidNo(ppd.getPurchase_no());
			if (maxD != null && maxD.length() > 2) {
				String seq = maxD.substring(maxD.length() - 2);
				int iseq = Integer.parseInt(seq) + 1;
				bid_no += String.format("%02d", iseq);
			} else {
				bid_no += "01";
			}
			ppd.setDept_code(user2.getDept_id());//设置部门
			ppd.setDept_desc(user2.getDept_name());
			ppd.setBid_no(bid_no);
			ppd.setCreate_userid(userId);
			ppd.setCreate_name(userName);
			ppd.setCreate_time(new Date());
			this.getMapper(PurPurchaseDMapper.class).insert(ppd);
			rm.setState("200");
			rm.setMsgInfo("新增成功");
			} else {
				rm.setState("500");
				rm.setMsgInfo("当前数据状态不可新增");
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败，请联系管理员");
			throw ex;
		}
		return rm;
	}

	/**
	 * 评标规则信息修改
	 * 
	 * @param oci
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doUpWeb(CommonPage<PurPurchaseD> 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();
		List<PurPurchaseD> ppdlist=oci.getObjectList();
		PurPurchaseD ppd = oci.getObjectList().get(0);
		try {
			PurPurchaseM ppm = this.getMapper(PurPurchaseMMapper.class)
					.findById(ppd.getPurchase_no());// 查询采购单号，并判断状态
			//采购单为草稿状态下，并且未提报评审可操作
			if ("10".equals(ppm.getValidflag()) || "00".equals(ppm.getStatus())) {
				for (PurPurchaseD purP : ppdlist) {	
				PurPurchaseD  purP2 =new PurPurchaseD();
				purP2.setBid_no(purP.getBid_no());
				purP2.setPurchase_no(purP.getPurchase_no());
				PurPurchaseD ppd2 = this.getMapper(PurPurchaseDMapper.class)
						.findPurPurchaseD(purP2);
				ppd2.setBid_std_code(purP.getBid_std_code());
				ppd2.setBid_std_name(purP.getBid_std_name());
				ppd2.setUpdate_userid(userId);
				ppd2.setUpdate_name(userName);
				ppd2.setUpdate_time(new Date());
				this.getMapper(PurPurchaseDMapper.class).update(ppd2);
				}
				rm.setState("200");
				rm.setMsgInfo("修改成功");
			} else {
				rm.setState("500");
				rm.setMsgInfo("当前数据状态不可修改");
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("修改失败，请联系管理员");
			throw ex;
		}
		return rm;
	}
	
	/**
	 * 评标规则信息删除
	 * 
	 * @param oci
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDeleteWeb(CommonPage<PurPurchaseD> oci) throws Exception {
		ResultModel rm = new ResultModel();
		List<PurPurchaseD> ppdlist = oci.getObjectList();
		try {
			for (PurPurchaseD ppd : ppdlist) {		
			PurPurchaseM ppm = this.getMapper(PurPurchaseMMapper.class)
					.findById(ppd.getPurchase_no());// 查询采购单号，并判断状态
			//采购单为草稿状态下，并且未提报评审可操作
			if ("10".equals(ppm.getValidflag()) || "00".equals(ppm.getStatus())) {
				PurPurchaseC  ppc =new PurPurchaseC();
				ppc.setPurchase_no(ppd.getPurchase_no());
				ppc.setBid_no(ppd.getBid_no());
					List<PurPurchaseC> ppc2 = this.getMapper(PurPurchaseCMapper.class).find(ppc,0,0);
				if(ppc2.size()>0){
//					rm.setState("500");
//					rm.setMsgInfo("当前数据"+ppc.getBid_no()+"存在采购单行，不可删除");
					throw new Exception("当前数据"+ppd.getBid_no()+"存在采购单行，不可删除");
				}
				PurPurchaseE  ppe =new PurPurchaseE();
				ppe.setPurchase_no(ppd.getPurchase_no());
				ppe.setBid_no(ppd.getBid_no());
					List<PurPurchaseE> ppe2 = this.getMapper(PurPurchaseEMapper.class).find(ppe,0,0);
					if(ppe2.size()>0){
						throw new Exception("当前数据"+ppd.getBid_no()+"存在供应商信息，不可删除");
					}
				PurPurchaseF  ppf =new PurPurchaseF();
				ppf.setPurchase_no(ppd.getPurchase_no());
				ppf.setBid_no(ppd.getBid_no());
					List<PurPurchaseF> ppf2 = this.getMapper(PurPurchaseFMapper.class).find(ppf,0,0);
					if(ppf2.size()>0){
						throw new Exception("当前数据"+ppd.getBid_no()+"存在评标人，不可删除");
					}
				this.getMapper(PurPurchaseDMapper.class).deleteBidNo(ppd);
				rm.setState("200");
				rm.setMsgInfo("删除成功");
			} else {
				rm.setState("500");
				rm.setMsgInfo("当前状态不可删除");
			}
			}
			
		} catch (Exception ex) {
			throw new Exception(ex.getMessage());
		}
		return rm;
	}
}
