/***文档注释***********************************************
 * 作者            ：谢卫龙
 * 创建日期     ：2018-07-04
 * 描述            ：资材备件库-出库信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Lms.Equipment.impl;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import market.Com.mapper.PbModelDbMapper;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Equipment.mapper.YdmEquipmentDetailSecondMapper;
import market.Lms.Equipment.mapper.YdmEquipmentOutlistSecondMapper;
import market.Lms.Equipment.service.YdmEquipmentOutlisteSecondService;
import market.Lms.Equipment.vo.YdmEquipmentDetailSecond;
import market.Lms.Equipment.vo.YdmEquipmentOutlistSecond;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
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.service.HomeService;
import com.ibm.icu.text.SimpleDateFormat;

@RemoteService(path="YdmEquipmentOutlisteSecondService")
public class YdmEquipmentOutlistSecondServiceImpl extends BaseServiceImpl<YdmEquipmentOutlistSecond,Object> implements YdmEquipmentOutlisteSecondService{
	
	/**
	 * 查询机组二级库存的出库记录
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryYdmEquipmentOutlistSecond(CommonPage<YdmEquipmentOutlistSecond> cp){
		ResultModel rm=new ResultModel();
		YdmEquipmentOutlistSecond yeo = cp.getObject();
		String startTime = cp.getStartTime();
		String endTime = cp.getEndTime();
		try {
			String condition = "";
			if (StringUtils.isNotEmpty(yeo.getMachine_code())) {
				condition += " and machine_code = '"+yeo.getMachine_code()+"'";
			}
			if (StringUtils.isNotEmpty(yeo.getMat_code())) {
				condition += " and mat_code = '"+yeo.getMat_code()+"'";
			}
			if (StringUtils.isNotEmpty(yeo.getSpec())) {
				condition += " and spec = '"+yeo.getSpec()+"'";
			}
			if (StringUtils.isNotEmpty(yeo.getSupplier_name())) {
				condition += " and supplier_name = '"+yeo.getSupplier_name()+"'";
			}
			if (StringUtils.isNotEmpty(startTime)) {
				condition += " and out_date >= '"+startTime+"'";
			}
			if (StringUtils.isNotEmpty(endTime)) {
				condition += " and out_date <= '"+endTime+"'";
			}
			List<YdmEquipmentOutlistSecond> yeoList = this.getMapper(YdmEquipmentOutlistSecondMapper.class).findWithCondition(null, condition, "out_date desc,opr_time desc", 0, 0);
			rm.setData(yeoList);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 出库确认
	 * @param cp
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInsertYdmEquipmentOutlistSecond(CommonPage<YdmEquipmentOutlistSecond> cp) throws Exception{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String workGrp = (String) this.getSession().getAttribute(HomeService.SESSION_WORK_GRP);
		String workOrd = (String) this.getSession().getAttribute(HomeService.SESSION_WORK_ORD);
		Date now = new Date();
		YdmEquipmentOutlistSecond yeo = cp.getObject();
		
		//String objectno = "";
		//1. 判断机组库存数据是否足够
		YdmEquipmentDetailSecond yed = this.getMapper(YdmEquipmentDetailSecondMapper.class).findById(yeo.getObject_no());
		if (yed !=null) {
			if (yed.getQuantity() < yeo.getQuantity()) {			
				throw new Exception("当前库存数量为"+yed.getQuantity()+",库存不足");
			}else {				
				String sql = "update ydm_equipment_detail_second set quantity = quantity - "+yeo.getQuantity()+" where object_no= '"+yeo.getObject_no()+"'";
				this.getMapper(PbModelDbMapper.class).doUpdateSql(sql);
			}
		} 		
		
		//2. 新增出库记录
		String outbillno = "OT"+ new SimpleDateFormat("yyyyMMdd").format(now);
		String sql = "select '"+outbillno+"' || lpad(nvl(max(to_number(substr(t.out_bill_no, 11, 5))), 0) + 1, 5, '0') maxno "
				+ " from ydm_equipment_outlist_second t "
				+ " where substr(t.out_bill_no, 1, 10) = '"+outbillno+"'";
		outbillno = this.getMapper(YdmEquipmentOutlistSecondMapper.class).getCode(sql);
		yeo.setObject_no(yeo.getObject_no());//库存编号
		yeo.setOut_bill_no(outbillno);
		yeo.setOut_type("500509");
		yeo.setOut_ord(workOrd);
		yeo.setOut_grp(workGrp);
		yeo.setOpr_man(userName);
		yeo.setOpr_time(now);
		yeo.setCreate_man_name(userName);
		yeo.setCreate_time(now);
		this.getMapper(YdmEquipmentOutlistSecondMapper.class).insert(yeo);
		
		rm.setState("200");
		rm.setMsgInfo("出库确认成功");
		return rm;
	}
	
	/**
	 * 
	 * @param cp
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDeleteYdmEquipmentOutlistSecond(CommonPage<YdmEquipmentOutlistSecond> cp){
		ResultModel rm = new ResultModel();
		YdmEquipmentOutlistSecond yeo = cp.getObject();	
		yeo = this.getMapper(YdmEquipmentOutlistSecondMapper.class).findById(yeo.getOut_bill_no());
		if (yeo.getQuantity()!=null && yeo.getQuantity() > 0) {			
			String sql = "update ydm_equipment_detail_second set quantity = quantity + "+yeo.getQuantity()+" where object_no= '"+yeo.getObject_no()+"'";
			this.getMapper(PbModelDbMapper.class).doUpdateSql(sql);
		}
		this.getMapper(YdmEquipmentOutlistSecondMapper.class).delete(yeo.getOut_bill_no());
		rm.setMsgInfo("撤销出库成功");
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 各交接班管理界面的保存
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveYdmEquipmentOutlistSecond(CommonPage<YdmEquipmentOutlistSecond> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String workGrp = (String) this.getSession().getAttribute(HomeService.SESSION_WORK_GRP);
		String workOrd = (String) this.getSession().getAttribute(HomeService.SESSION_WORK_ORD);
		Date now = new Date();
		ResultModel rm = new ResultModel();
		List<YdmEquipmentOutlistSecond> yeoList = cp.getObjectList();
		String wrk_date = cp.getMemo1(); //日期
		if (CollectionUtils.isEmpty(yeoList)) {
			throw new Exception("请传入参数");
		}
		
		//先将这天的出库记录给恢复
		String condition = "out_type = '500515' and out_date = '"+wrk_date+"'";
		List<YdmEquipmentOutlistSecond> yList = this.getMapper(YdmEquipmentOutlistSecondMapper.class).findWithCondition(null, condition, "out_bill_no", 0, 0);
		if (CollectionUtils.isNotEmpty(yList)) {
			for (YdmEquipmentOutlistSecond y : yList) {
				String sql = "update ydm_equipment_detail_second set quantity = quantity + "+y.getQuantity()+" where object_no= '"+y.getObject_no()+"'";
				this.getMapper(PbModelDbMapper.class).doUpdateSql(sql);
				this.getMapper(YdmEquipmentOutlistSecondMapper.class).delete(y.getOut_bill_no());
			}
		}
		
		for (YdmEquipmentOutlistSecond yeo : yeoList) {
			//获取当天出库的记录		
			//1. 判断机组库存数据是否足够
			YdmEquipmentDetailSecond yed = this.getMapper(YdmEquipmentDetailSecondMapper.class).findById(yeo.getObject_no());
			if (yed !=null) {
				if (yed.getQuantity() < yeo.getAmount()) {			
					throw new Exception("当前库存数量为"+yed.getQuantity()+",库存不足");
				}else {				
					String sql = "update ydm_equipment_detail_second set quantity = quantity - "+yeo.getAmount()+" where object_no= '"+yeo.getObject_no()+"'";
					this.getMapper(PbModelDbMapper.class).doUpdateSql(sql);
				}
			} 					
			//2. 新增出库记录
			String outbillno = "OT"+ new SimpleDateFormat("yyyyMMdd").format(now);
			String sql = "select '"+outbillno+"' || lpad(nvl(max(to_number(substr(t.out_bill_no, 11, 5))), 0) + 1, 5, '0') maxno "
					+ " from ydm_equipment_outlist_second t "
					+ " where substr(t.out_bill_no, 1, 10) = '"+outbillno+"'";
			outbillno = this.getMapper(YdmEquipmentOutlistSecondMapper.class).getCode(sql);
			yeo.setObject_no(yeo.getObject_no());//库存编号
			yeo.setOut_bill_no(outbillno);
			yeo.setOut_type("500515");//交接班出库
			yeo.setOut_ord(workOrd);
			yeo.setOut_grp(workGrp);
			yeo.setOpr_man(userName);
			yeo.setOpr_time(now);
			yeo.setCreate_man_name(userName);
			yeo.setCreate_time(now);
			yeo.setOut_date(wrk_date);
			yeo.setQuantity(yeo.getAmount());
			this.getMapper(YdmEquipmentOutlistSecondMapper.class).insert(yeo);
		}
		rm.setMsgInfo("保存成功");
		rm.setState("200");
		return rm;
	}
}
