/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-07-04
 * 描述            ：资材备件库-维修信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Lms.Equipment.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.Lms.Equipment.mapper.YdmEquipmentDetailMapper;
import market.Lms.Equipment.mapper.YdmEquipmentInlistMapper;
import market.Lms.Equipment.mapper.YdmEquipmentMaintainlistMapper;
import market.Lms.Equipment.mapper.YdmEquipmentOutlistMapper;
import market.Lms.Equipment.service.YdmEquipmentMaintainlistService;
import market.Lms.Equipment.vo.YdmEquipmentDetail;
import market.Lms.Equipment.vo.YdmEquipmentInlist;
import market.Lms.Equipment.vo.YdmEquipmentMaintainlist;
import market.Lms.Equipment.vo.YdmEquipmentOutlist;
import market.Qms.common.DataHelper;

import org.springframework.beans.BeanUtils;
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 = "YdmEquipmentMaintainlistService")
public class YdmEquipmentMaintainlistServiceImpl extends
		BaseServiceImpl<YdmEquipmentMaintainlist, Object> implements
		YdmEquipmentMaintainlistService {

	/**
	 * 查询维修数据
	 * 
	 * @param cp
	 * @return
	 */
	public ResultModel doQeuryMaintain(CommonPage<YdmEquipmentMaintainlist> cp) {
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<YdmEquipmentMaintainlist> pm = new PageModel<YdmEquipmentMaintainlist>(
				pageIndex, pageSize);
		try {

			YdmEquipmentMaintainlist yem = new YdmEquipmentMaintainlist();
			yem = cp.getObject();
			String sWhere = "1=1";
			if (cp.getStartTime() != null && !cp.getStartTime().equals("")) {
				String starttime = cp.getStartTime();
				sWhere += " and to_date(Maintain_Date,'yyyy-MM-dd HH24:mi:ss')>=to_date('"
						+ starttime + "','yyyy-MM-dd HH24:mi:ss') ";
			}
			if (cp.getEndTime() != null && !cp.getEndTime().equals("")) {
				String endtime = cp.getEndTime();
				sWhere += " and to_date(Maintain_Date,'yyyy-MM-dd HH24:mi:ss')<=to_date('"
						+ endtime + "','yyyy-MM-dd HH24:mi:ss') ";
			}
			// if (yem.getMat_code() != null
			// && !yem.getMat_code().equals("")) {
			// String Mat_code = yem.getMat_code();
			// sWhere += " and Mat_code = '" + Mat_code + "'";
			// }
			//
			/* if (yem.getSrot_code() != null && !yem.getSrot_code().equals(""))
			 {
			   String Srot_code = yem.getSrot_code();
			   sWhere += " and Srot_code = '" + Srot_code + "'";
			 }*/
			
			 if (yem.getStorage_code() != null
			 && !yem.getStorage_code().equals("")) {
			 String Storage_code = yem.getStorage_code();
			 sWhere += " and Storage_code = '" + Storage_code + "'";
			 }

			// sWhere += " and STATE = '0'";
			// yem.setMaintain_bill_no(null);

			pm.setRows(this.getMapper(YdmEquipmentMaintainlistMapper.class)
					.findWithCondition(yem, sWhere, "MAINTAIN_DATE", 0, 0));
			pm.setTotal(this.getMapper(YdmEquipmentMaintainlistMapper.class)
					.countWithCondition(yem, sWhere));
			rm.setState("200");
			rm.setData(pm);

		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，查询失败!");
			e.printStackTrace();
		}

		return rm;

	}


	/**
	 * 查询维修历史记录
	 * 
	 * @param cp
	 * @return
	 */
	public ResultModel doQeuryMaintainLog(
			CommonPage<YdmEquipmentMaintainlist> cp) {
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		//PageModel<YdmEquipmentMaintainlist> pm = new PageModel<YdmEquipmentMaintainlist>(pageIndex, pageSize);

		try {

			List<YdmEquipmentMaintainlist> listY = this.getMapper(
					YdmEquipmentMaintainlistMapper.class)
					.doQeuryMaintainLog((pageIndex - 1) * pageSize,
							(pageIndex - 1) * pageSize + pageSize,
							cp.getStartTime(), cp.getEndTime(),
							cp.getObject().getState(),
							cp.getObject().getStorage_code(),
							cp.getObject().getSrot_code(),
							cp.getObject().getMat_code());

			rm.setState("200");
			rm.setData(listY);

		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，查询失败!");
			e.printStackTrace();
		}

		return rm;

	}

	/**
	 * 库存维修
	 * 
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel addMainTain(CommonPage<YdmEquipmentMaintainlist> cp)
			throws Exception {
		ResultModel rm = new ResultModel();
		try {
			YdmEquipmentMaintainlist yem = new YdmEquipmentMaintainlist();

			String username = (String) this.getSession().getAttribute(
					HomeService.SESSION_USER_NAME);
			String usernameno = (String) this.getSession().getAttribute(
					HomeService.SESSION_USER_ID);
			String order = (String) this.getSession().getAttribute(
					HomeService.WRK_ORD);
			String team = (String) this.getSession().getAttribute(
					HomeService.WRK_GRP);

			if (cp.getObject() == null || "".equals(cp.getObject())) {
				rm.setData("500");
				throw new Exception("传入的库存数据为空,请检查再操作！");
			}
			yem = cp.getObject();

			if (yem.getObject_no() == null || "".equals(yem.getObject_no())) {
				rm.setData("500");
				throw new Exception("传入的库存编码为空,请检查再操作！");
			}
			if (yem.getQuantity() == null || "".equals(yem.getQuantity())
					|| yem.getQuantity().equals(0)) {
				rm.setData("500");
				throw new Exception("传入的维修数量为空或为 0 ,请检查再操作！");
			}

			YdmEquipmentDetail yed1 = new YdmEquipmentDetail();
			yed1.setObject_no(yem.getObject_no());
			yed1.setState("0");

			YdmEquipmentDetail listyde = this.getMapper(
					YdmEquipmentDetailMapper.class).findById(
					yem.getObject_no(), "0");
			if (listyde == null) {
				rm.setData("500");
				throw new Exception("不存在库存数据 ,请检查再操作！");
			}
			if (listyde.getQuantity() < yem.getQuantity()) {
				rm.setData("500");
				throw new Exception("库存数量少于维修数量 ,请检查再操作！");
			}

			YdmEquipmentMaintainlist yem1 = new YdmEquipmentMaintainlist();

			yem1.setCreate_man_name(username);
			yem1.setCreate_man_no(usernameno);
			yem1.setCreate_time(new Date());
			yem1.setDept_code(listyde.getDept_code());
			yem1.setIn_date(null);
			yem1.setIn_man(null);
			yem1.setIn_time(null);
			yem1.setMaintain_bill_no(this.getMapper(
					YdmEquipmentMaintainlistMapper.class).GetMaintainNo());
			if (yem.getMaintain_date() == null) {
				SimpleDateFormat sdfq = new SimpleDateFormat("yyyy-MM-dd");
				yem1.setMaintain_date(sdfq.format(new Date()));
			} else {
				yem1.setMaintain_date(yem.getMaintain_date());
			}
			yem1.setMaintain_man(username);
			yem1.setMaintain_time(new Date());
			yem1.setMat_code(listyde.getMat_code());
			yem1.setMat_name(listyde.getMat_name());
			yem1.setObject_no(listyde.getObject_no());
			yem1.setPrice(listyde.getPrice());
			yem1.setPurc_order_no(yem.getPurc_order_no());
			yem1.setPurc_order_pk(yem.getPurc_order_pk());
			yem1.setQuantity(yem.getQuantity());
			yem1.setSpec(listyde.getSpec());
			yem1.setSrot_code(listyde.getSrot_code());
			yem1.setSrot_name(listyde.getSrot_name());
			yem1.setState("0");
			yem1.setStorage_code(listyde.getStorage_code());
			yem1.setSupplier_name(listyde.getSupplier_name());
			yem1.setSupplier_no(listyde.getSupplier_no());
			yem1.setUnit(listyde.getUnit());
			// yem1.setUpdate_man_name(listyde.);
			// yem1.setUpdate_man_no(listyde.);
			// yem1.setUpdate_time(listyde.);

			// 插入维修数据
			this.getMapper(YdmEquipmentMaintainlistMapper.class).insert(yem1);

			// 如果库存所存在维修数据 修改数量 不存在插入维修数 减少库存数据
			YdmEquipmentDetail yed2 = new YdmEquipmentDetail();
			yed2 = listyde;
			// yed2.setObject_no(yed1.getObject_no());
			yed2.setState("2");

			YdmEquipmentDetail listyed1 = this.getMapper(
					YdmEquipmentDetailMapper.class).findById(
					yed1.getObject_no(), "2");
			if (listyed1 != null) {
				listyed1.setQuantity(listyed1.getQuantity()
						+ yem1.getQuantity());
				this.getMapper(YdmEquipmentDetailMapper.class).update(listyed1);
			} else {
				yed2.setQuantity(yem1.getQuantity());
				this.getMapper(YdmEquipmentDetailMapper.class).insert(yed2);
			}

			// 如果库存数据维修后数量为0 删除
			YdmEquipmentDetail listyde2 = this.getMapper(
					YdmEquipmentDetailMapper.class).findById(
					yem.getObject_no(), "0");
			if (listyde2.getQuantity().equals(yem.getQuantity())) {
				this.getMapper(YdmEquipmentDetailMapper.class).delete(
						yed1.getObject_no(), yed1.getState());
			} else {
				Double quantity = listyde2.getQuantity() - yem.getQuantity();
				yed1.setQuantity(quantity);
				this.getMapper(YdmEquipmentDetailMapper.class).update(yed1);
			}

			// 插入出库记录
			YdmEquipmentOutlist yeo = new YdmEquipmentOutlist();
			DataHelper.CopyEntity(yem1, yeo);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			//SimpleDateFormat sdff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat sdfff = new SimpleDateFormat("yyyy-MM-dd");
			String out_bill_no = "OT";
			out_bill_no += sdf.format(new Date());
			String maxOutbillno = this.getMapper(
					YdmEquipmentOutlistMapper.class).selectMaxOutbillno(
					out_bill_no);
			if (maxOutbillno == null || "".equals(maxOutbillno)) {
				out_bill_no += "0001";
			} else if (maxOutbillno.length() == 14) {
				String str = maxOutbillno.substring(10);
				int i = Integer.parseInt(str) + 1;
				str = String.format("%04d", i);
				out_bill_no += str;
			} else {
				rm.setData("500");
				rm.setMsgInfo("出库编号异常 :" + maxOutbillno+"请刷新重试");
				throw new Exception("出库编号异常 :" + maxOutbillno+"请刷新重试");
			}
			yeo.setOut_bill_no(out_bill_no);
            yeo.setMaintain_bill_no(yem1.getMaintain_bill_no());
            yeo.setOut_type("500511");
            yeo.setOut_ord(order);
            yeo.setOut_grp(team);
            yeo.setOut_date(sdfff.format(new Date()));
            yeo.setOpr_time(new Date());
            yeo.setOpr_man(username);
            yeo.setCreate_man_name(username);
            yeo.setCreate_man_no(usernameno);
            yeo.setCreate_time(new Date());
            yeo.setArea(yem.getArea());
			this.getMapper(YdmEquipmentOutlistMapper.class).insert(yeo);

			rm.setState("200");
			rm.setMsgInfo("维修成功!");
		} catch (Exception e) {
			throw new Exception("库存维修失败！" + e.getMessage());
		}

		return rm;

	}

	/**
	 * 撤销维修
	 * 
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel deleMainTain(CommonPage<YdmEquipmentMaintainlist> cp)
			throws Exception {
		ResultModel rm = new ResultModel();
		try {
			YdmEquipmentMaintainlist yem = new YdmEquipmentMaintainlist();

			if (cp.getObject() == null || "".equals(cp.getObject())) {
				rm.setData("500");
				throw new Exception("传入的库存数据为空,请检查再操作！");
			}
			yem = cp.getObject();

			if (yem.getMaintain_bill_no() == null
					|| "".equals(yem.getMaintain_bill_no())) {
				rm.setData("500");
				throw new Exception("传入的维修编码为空,请检查再操作！");
			}
			if (yem.getQuantity() == null || "".equals(yem.getQuantity())) {
				rm.setData("500");
				throw new Exception("传入的维修数量为空,请检查再操作！");
			}

			// 删除维修记录
			this.getMapper(YdmEquipmentMaintainlistMapper.class).delete(
					yem.getMaintain_bill_no());

			// 删除出库记录
			this.getMapper(YdmEquipmentOutlistMapper.class)
					.deleteByMaintain_NO(yem.getMaintain_bill_no());

			YdmEquipmentDetail yed = new YdmEquipmentDetail();

			YdmEquipmentDetail yed0 = this.getMapper(
					YdmEquipmentDetailMapper.class).findById(
					yem.getObject_no(), "0");
			YdmEquipmentDetail yed1 = this.getMapper(
					YdmEquipmentDetailMapper.class).findById(
					yem.getObject_no(), "2");

			// if (yed0 == null) {
			// throw new Exception("未找到【 " + yem.getObject_no()
			// + " 】的库存数据 , 请重新操作!");
			// }
			if (yed1 == null) {
				throw new Exception("未找到【 " + yem.getObject_no()
						+ " 】的维修数据 , 请刷新后重新操作!");
			}

			// 不存在在库数据则插入 存在则修改
			if (yed0 == null) {
				yed = yed1;
				yed.setState("0");
				yed.setQuantity(yem.getQuantity());
				this.getMapper(YdmEquipmentDetailMapper.class).insert(yed);
			} else {
				yed = yed0;
				yed.setState("0");
				yed.setQuantity(yed.getQuantity() + yem.getQuantity());
				this.getMapper(YdmEquipmentDetailMapper.class).update(yed);
			}

			// 存在维修库存数量 等于小于撤销 则删除 否则修改
			if (yed1.getQuantity() == yem.getQuantity()) {
				this.getMapper(YdmEquipmentDetailMapper.class).delete(
						yem.getObject_no(), "2");
			} else if(yed1.getQuantity() < yem.getQuantity()){
                    throw new Exception("撤销数量大于正在维修的数量，无法撤销！请刷新重试！");
			}else{
				yed = yed1;
				yed.setState("2");
				yed.setQuantity(yed.getQuantity() - yem.getQuantity());
				this.getMapper(YdmEquipmentDetailMapper.class).update(yed);
			}

			rm.setState("200");
			rm.setMsgInfo("撤销维修成功!");
		} catch (Exception e) {
			throw new Exception("撤销维修失败！" + e.getMessage());
		}

		return rm;
	}

	/**
	 * 维修完成 入库
	 * 
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel IntoDetailMainTain(
			CommonPage<YdmEquipmentMaintainlist> sum) throws Exception {
		ResultModel rm = new ResultModel();
		String user_name = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		String user_id = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String wrk_ord = (String) this.getSession().getAttribute(
				HomeService.WRK_ORD);
		String wrk_grp = (String) this.getSession().getAttribute(
				HomeService.WRK_GRP);
		try {
			YdmEquipmentMaintainlist ydmM = sum.getObject();
			if (ydmM == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数！");
				return rm;
			}
			// 在入库信息表(YDM_EQUIPMENT_INLIST)中新增一条入库记录
			YdmEquipmentInlist ydmIn = new YdmEquipmentInlist();
			// 入库编号
			int count = 0;
			SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
			SimpleDateFormat fmt2 = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat fmt3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String date = fmt.format(new Date());
			List<YdmEquipmentInlist> inList = this.getMapper(
					YdmEquipmentInlistMapper.class).findWithCondition(null,
					"in_bill_no like 'IN" + date + "%'", "in_bill_no desc", 0,
					0);
			if (inList != null && inList.size() > 0) {
				count = Integer.parseInt(inList.get(0).getIn_bill_no()
						.substring(10));
			}
			BeanUtils.copyProperties(ydmM, ydmIn);
			ydmIn.setIn_bill_no("IN" + date + String.format("%04d", count + 1));
			ydmIn.setCreate_man_no(user_id);
			ydmIn.setCreate_man_name(user_name);
			ydmIn.setCreate_time(new Date());
			ydmIn.setIn_date(fmt2.format(new Date()));
			ydmIn.setIn_grp(wrk_grp);
			ydmIn.setIn_ord(wrk_ord);
			ydmIn.setOpr_man(user_name);
			ydmIn.setOpr_time(new Date());
			ydmIn.setIn_type("500409");
			ydmIn.setIn_type_name("维修入库");
			/*this.getMapper(YdmEquipmentInlistMapper.class).insert(ydmIn);*/

			// 在维修信息表(YDM_EQUIPMENT_MAINTAINLIST)中修改对应维修编号的维修记录
			YdmEquipmentMaintainlist yem = new YdmEquipmentMaintainlist();
			yem.setMaintain_bill_no(ydmM.getMaintain_bill_no());
			yem.setState("0");
			List<YdmEquipmentMaintainlist> yemList = this.getMapper(
					YdmEquipmentMaintainlistMapper.class).find(yem, 0, 0);
			//Double Quantity0 = ydmM.getQuantity() == null ? 0d : ydmM.getQuantity();
			if (yemList == null || yemList.size() == 0) {
				throw new Exception("未找到维修编号为：【" + ydmM.getMaintain_bill_no()
						+ "】的在【维修】状态的数据！请刷新重试！");
			}
			String sql = "update YDM_EQUIPMENT_MAINTAINLIST a set a.state='1',a.IN_DATE='"
					+ ydmIn.getIn_date()
					+ "',a.IN_TIME=to_date('"
					+ fmt3.format(new Date())
					+ "','yyyy-mm-dd hh24:mi:ss'),a.IN_MAN='"
					+ user_name
					+ "' where a.MAINTAIN_BILL_NO='"
					+ ydmM.getMaintain_bill_no() + "'";
			this.getMapper(YdmEquipmentMaintainlistMapper.class).doUpdate1(sql);

			// 修改库存信息表（YDM_EQUIPMENT_DETAIL）对应库存编号&&维修的数据， 设置数量(QUANTITY) -= 数量
			Double Quantity = ydmM.getQuantity() == null ? 0d : ydmM.getQuantity();
			YdmEquipmentDetail ydt = new YdmEquipmentDetail();
			ydt.setObject_no(ydmM.getObject_no());
			ydt.setState("2");
			List<YdmEquipmentDetail> ydtList = this.getMapper(
					YdmEquipmentDetailMapper.class).find(ydt, 0, 0);
			if (ydtList == null || ydtList.size() ==0) {
				throw new Exception("未找到库存编号为'"+ydt.getObject_no()+"'的在维修数据，无法操作！");
			}
			Quantity = ydtList.get(0).getQuantity() - Quantity;
			sql = "update YDM_EQUIPMENT_DETAIL a set a.QUANTITY='"
					+ Quantity + "' where a.OBJECT_NO='"
					+ ydmM.getObject_no() + "' and a.state='2'";
			this.getMapper(YdmEquipmentDetailMapper.class).doUpdate1(sql);

			// 库存信息表（YDM_EQUIPMENT_DETAIL）存在库存编号&&在库的数据
			Double Quantity2 = ydmM.getQuantity() == null ? 0d : ydmM
					.getQuantity();
			YdmEquipmentDetail ydt2 = new YdmEquipmentDetail();
			ydt2.setObject_no(ydmM.getObject_no());
			ydt2.setState("0");
			List<YdmEquipmentDetail> ydtList2 = this.getMapper(
					YdmEquipmentDetailMapper.class).find(ydt2, 0, 0);

			// 修改库存信息表（YDM_EQUIPMENT_DETAIL）对应库存编号&&在库的数据， 设置数量(QUANTITY) += 数量
			if (ydtList2 != null && ydtList2.size() > 0) {
				Quantity2 = ydtList2.get(0).getQuantity() + Quantity2;
				sql = "update YDM_EQUIPMENT_DETAIL a set a.QUANTITY='"
						+ Quantity2 + "' where a.OBJECT_NO='"
						+ ydmM.getObject_no() + "' and a.state='0'";
				this.getMapper(YdmEquipmentDetailMapper.class).doUpdate1(sql);
				//如果存在在库的数据，修改入库记录的入库区域为已经在库的区域
				ydmIn.setArea(ydtList2.get(0).getArea());
				this.getMapper(YdmEquipmentInlistMapper.class).insert(ydmIn);
			} else {
				// 在库存信息表（YDM_EQUIPMENT_DETAIL）新增对应库存编号&&在库的数据
				YdmEquipmentDetail ydtt = new YdmEquipmentDetail();
				BeanUtils.copyProperties(ydmM, ydtt);
				//Quantity2 = ydtList2.get(0).getQuantity() + Quantity2;
				ydtt.setQuantity(Quantity2);
				ydtt.setState("0");
				ydtt.setIn_date(ydmIn.getIn_date());
				this.getMapper(YdmEquipmentDetailMapper.class).insert(ydtt);
				this.getMapper(YdmEquipmentInlistMapper.class).insert(ydmIn);
			}

			// 库存编号&&维修 的数量(QUANTITY)=0
			// 在库存信息表（YDM_EQUIPMENT_DETAIL）中删除库存编号&&维修的数据
			List<YdmEquipmentDetail> ydtList3 = this.getMapper(
					YdmEquipmentDetailMapper.class).find(ydt, 0, 0);
			if (ydtList3 != null && ydtList3.size() > 0) {
				if (ydtList3.get(0).getQuantity() != null
						&& ydtList3.get(0).getQuantity() == 0) {
					sql = "delete from YDM_EQUIPMENT_DETAIL a where a.OBJECT_NO='"
							+ ydmM.getObject_no() + "' and a.state='2'";
					this.getMapper(YdmEquipmentDetailMapper.class).doDelete1(
							sql);
				}
			}
			rm.setState("200");
			rm.setMsgInfo("维修入库成功！");
		} catch (Exception e) {
			throw new Exception("维修入库操作失败！" + e.getMessage());
		}
		return rm;
	}

	/**
	 * 撤销维护入库
	 * 
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel deleIntoDetailMainTain(
			CommonPage<YdmEquipmentMaintainlist> cp) throws Exception {

		ResultModel rm = new ResultModel();

		YdmEquipmentMaintainlist yem = new YdmEquipmentMaintainlist();

		if (cp.getObject() == null || "".equals(cp.getObject())) {
			rm.setData("500");
			throw new Exception("传入的维修数据为空,请检查再操作！");
		}
		yem = cp.getObject();

		if (yem.getMaintain_bill_no() == null
				|| "".equals(yem.getMaintain_bill_no())) {
			rm.setData("500");
			throw new Exception("传入的维修编码为空,请检查再操作！");
		}
		if (yem.getQuantity() == null || "".equals(yem.getQuantity())) {
			rm.setData("500");
			throw new Exception("传入的维修数量为空,请检查再操作！");
		}
		YdmEquipmentMaintainlist yem2 = new YdmEquipmentMaintainlist();
		yem2.setMaintain_bill_no(yem.getMaintain_bill_no());
		yem2.setState("1");
		int count = this.getMapper(YdmEquipmentMaintainlistMapper.class).count(yem2);
		if(count ==0 ){
			throw new Exception("选择的维修历史数据状态不为【入库状态】,请刷新重试！");
		}

		YdmEquipmentDetail yed = new YdmEquipmentDetail();

		YdmEquipmentDetail yed0 = this
				.getMapper(YdmEquipmentDetailMapper.class).findById(
						yem.getObject_no(), "0");
		YdmEquipmentDetail yed1 = this
				.getMapper(YdmEquipmentDetailMapper.class).findById(
						yem.getObject_no(), "2");

		if (yed0 == null) {
			rm.setData("500");
			throw new Exception("在库数量为空,请检查再操作！");
		}

		if (yed0.getQuantity() < yem.getQuantity()) {
			rm.setData("500");
			throw new Exception("在库数量小于传入的撤销维修数量,无法完成撤销操作！");
		}

		// 修改库存在库数量 为0时删除
		if (yed0.getQuantity() > yem.getQuantity()) {
			yed = yed0;
			yed.setQuantity(yed.getQuantity() - yem.getQuantity());
			yed.setState("0");
			this.getMapper(YdmEquipmentDetailMapper.class).update(yed);
		} else {
			this.getMapper(YdmEquipmentDetailMapper.class).delete(
					yem.getObject_no(), "0");
		}

		// 修改库存维修数量 为0时插入
		if (yed1 == null) {
			yed = yed0;
			yed.setQuantity(yem.getQuantity());
			yed.setState("2");

			this.getMapper(YdmEquipmentDetailMapper.class).insert(yed);
		} else {

			yed = yed1;
			yed.setQuantity(yed.getQuantity() + yem.getQuantity());
			yed.setState("2");
			this.getMapper(YdmEquipmentDetailMapper.class).update(yed);

		}

		// 删除入库数据
		this.getMapper(YdmEquipmentInlistMapper.class).deleteByMaintain_NO(
				yem.getMaintain_bill_no());

		// 修改维修表数据
		YdmEquipmentMaintainlist yem1 = new YdmEquipmentMaintainlist();
		yem1 = yem;
		yem1.setState("0");
		this.getMapper(YdmEquipmentMaintainlistMapper.class).update(yem1);

		rm.setState("200");
		rm.setMsgInfo("撤销维修入库成功!");
		return rm;
	}

}
