/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-07-04
 * 描述            ：资材备件库-入库信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Lms.Equipment.impl;
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.YdmEquipmentInlistSecondMapper;
import market.Lms.Equipment.service.YdmEquipmentInlistSecondService;
import market.Lms.Equipment.vo.YdmEquipmentDetailSecond;
import market.Lms.Equipment.vo.YdmEquipmentInlistSecond;

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="YdmEquipmentInlistSecondService")
public class YdmEquipmentInlistSecondServiceImpl extends BaseServiceImpl<YdmEquipmentInlistSecond,Object> implements YdmEquipmentInlistSecondService {
	
	/**
	 * 查询机组二级库存的入库记录
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryYdmEquipmentInlistSecond(CommonPage<YdmEquipmentInlistSecond> cp){
		ResultModel rm=new ResultModel();
		YdmEquipmentInlistSecond yei = cp.getObject();
		String startTime = cp.getStartTime();
		String endTime = cp.getEndTime();
		try {
			String condition = "";
			if (StringUtils.isNotEmpty(yei.getMachine_code())) {
				condition += " and machine_code = '"+yei.getMachine_code()+"'";
			}
			if (StringUtils.isNotEmpty(yei.getMat_code())) {
				condition += " and mat_code = '"+yei.getMat_code()+"'";
			}
			if (StringUtils.isNotEmpty(yei.getSpec())) {
				condition += " and spec = '"+yei.getSpec()+"'";
			}
			if (StringUtils.isNotEmpty(yei.getSupplier_name())) {
				condition += " and supplier_name = '"+yei.getSupplier_name()+"'";
			}
			if (StringUtils.isNotEmpty(startTime)) {
				condition += " and in_date >= '"+startTime+"'";
			}
			if (StringUtils.isNotEmpty(endTime)) {
				condition += " and in_date <= '"+endTime+"'";
			}
			List<YdmEquipmentInlistSecond> yeiList = this.getMapper(YdmEquipmentInlistSecondMapper.class).findWithCondition(null, condition, "in_date desc,opr_time desc", 0, 0);
			rm.setData(yeiList);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+ex.getMessage());
		}
		return rm;
	}

	/**
	 * 机组库存入库确认功能
	 * @param cp
	 * @return
	 */
	public ResultModel doInsertYdmEquipmentInlistSecond(CommonPage<YdmEquipmentInlistSecond> cp){
		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();
		YdmEquipmentInlistSecond yei = cp.getObject();
		
		String objectno = "";
		//1. 判断机组库存数据是否存在
		YdmEquipmentDetailSecond yed = new YdmEquipmentDetailSecond();
		yed.setMachine_code(yei.getMachine_code());
		yed.setMachine_name(yei.getMachine_name());
		yed.setMat_code(yei.getMat_code());
		yed.setMat_name(yei.getMat_name());
		yed.setSrot_code(yei.getSrot_code());
		yed.setSrot_name(yei.getSrot_name());
		yed.setSpec(yei.getSpec());
		yed.setPrice(yei.getPrice());
		List<YdmEquipmentDetailSecond> yedList = this.getMapper(YdmEquipmentDetailSecondMapper.class).find(yed, 0, 0);
		if (CollectionUtils.isNotEmpty(yedList)) {
			//1.1 存在的话，调整库存数量
			objectno = yedList.get(0).getObject_no();
			String sql = "update ydm_equipment_detail_second set quantity = quantity + "+yei.getQuantity()+" where object_no= '"+objectno+"'";
			this.getMapper(PbModelDbMapper.class).doUpdateSql(sql);
		} else {
			//1.2 不存在的话，新增库存记录
			objectno = "KC"+ new SimpleDateFormat("yyyyMMdd").format(now);
			String sql = "select '"+objectno+"' || lpad(nvl(max(to_number(substr(t.object_no, 11, 4))), 0) + 1, 4, '0') maxno "
					+ "from ydm_equipment_detail_second t "
					+ "where substr(t.object_no, 1, 10) = '"+objectno+"'";
			objectno = this.getMapper(YdmEquipmentDetailSecondMapper.class).getCode(sql);
			yed.setObject_no(objectno);
			yed.setQuantity(yei.getQuantity());
			yed.setUnit(yei.getUnit());
			yed.setState("0");
			this.getMapper(YdmEquipmentDetailSecondMapper.class).insert(yed);
		}		
		
		//2. 新增入库记录
		String inbillno = "IN"+ new SimpleDateFormat("yyyyMMdd").format(now);
		String sql = "select '"+inbillno+"' || lpad(nvl(max(to_number(substr(t.in_bill_no, 11, 5))), 0) + 1, 5, '0') maxno "
				+ " from ydm_equipment_inlist_second t "
				+ " where substr(t.in_bill_no, 1, 10) = '"+inbillno+"'";
		inbillno = this.getMapper(YdmEquipmentInlistSecondMapper.class).getCode(sql);
		yei.setObject_no(objectno);//库存编号
		yei.setIn_bill_no(inbillno);
		yei.setIn_type("500408");
		yei.setIn_ord(workOrd);
		yei.setIn_grp(workGrp);
		yei.setOpr_man(userName);
		yei.setOpr_time(now);
		yei.setCreate_man_name(userName);
		yei.setCreate_time(now);
		this.getMapper(YdmEquipmentInlistSecondMapper.class).insert(yei);
		
		rm.setState("200");
		rm.setMsgInfo("入库确认成功");
		return rm;
	}
	
	/**
	 * 
	 * @param cp
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDeleteYdmEquipmentInlistSecond(CommonPage<YdmEquipmentInlistSecond> cp){
		ResultModel rm = new ResultModel();
		YdmEquipmentInlistSecond yei = cp.getObject();	
		yei = this.getMapper(YdmEquipmentInlistSecondMapper.class).findById(yei.getIn_bill_no());
		if (yei.getQuantity()!=null && yei.getQuantity() > 0) {			
			String sql = "update ydm_equipment_detail_second set quantity = quantity - "+yei.getQuantity()+" where object_no= '"+yei.getObject_no()+"'";
			this.getMapper(PbModelDbMapper.class).doUpdateSql(sql);
		}
		this.getMapper(YdmEquipmentInlistSecondMapper.class).delete(yei.getIn_bill_no());
		rm.setMsgInfo("撤销入库成功");
		rm.setState("200");
		return rm;
	}

}
