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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Equipment.mapper.YdmEquipmentObjectCmpMapper;
import market.Lms.Equipment.service.YdmEquipmentObjectCmpService;
import market.Lms.Equipment.vo.YdmEquipmentObjectCmp;

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;

@RemoteService(path="YdmEquipmentObjectCmpService")
public class YdmEquipmentObjectCmpServiceImpl extends BaseServiceImpl<YdmEquipmentObjectCmp,Object> implements YdmEquipmentObjectCmpService{
	
	
	/**
	 * 查询机组备件信息
	 * @param cp 
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryYdmEquipmentObjectCmp(CommonPage<YdmEquipmentObjectCmp> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		YdmEquipmentObjectCmp yeoc = cp.getObject();
		try {
			String swhere = "";
			if (StringUtils.isNotEmpty(yeoc.getMachine_code())) {
				swhere += " and machine_code = '"+yeoc.getMachine_code()+"'";
			}
			if (StringUtils.isNotEmpty(yeoc.getValid_flg())) {
				swhere += " and valid_flg = '"+yeoc.getValid_flg()+"'";//是否有效
			}
			if (StringUtils.isNotEmpty(yeoc.getItem_code())) {
				swhere += " and item_code = '"+yeoc.getItem_code()+"'";
			}
			List<YdmEquipmentObjectCmp> lists = this.getMapper(YdmEquipmentObjectCmpMapper.class).findWithCondition(null, swhere, "order_seq", 0, 0);
			rm.setData(lists);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception e) {
			throw new Exception("查询失败:"+e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 新增机组备件信息
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doInsertYdmEquipmentObjectCmp(CommonPage<YdmEquipmentObjectCmp> cp) throws Exception{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		YdmEquipmentObjectCmp yeoc = cp.getObject();
		if (StringUtils.isEmpty(yeoc.getMachine_code()) || StringUtils.isEmpty(yeoc.getMachine_name())) {
			throw new Exception("机组名称不能为空");
		}
		if (StringUtils.isEmpty(yeoc.getItem_code()) || StringUtils.isEmpty(yeoc.getItem_name())) {
			throw new Exception("物料名称不能为空");
		}
		if (StringUtils.isEmpty(yeoc.getSpec())) {
			throw new Exception("规格不能为空");
		}
		YdmEquipmentObjectCmp y = new YdmEquipmentObjectCmp();
		y.setMachine_code(yeoc.getMachine_code());
		y.setItem_code(yeoc.getItem_code());
		y.setSpec(yeoc.getSpec());
		int count = this.getMapper(YdmEquipmentObjectCmpMapper.class).count(y);
		if (count > 0) {
			throw new Exception(yeoc.getMachine_name() + "存在相同物料名称："+ yeoc.getMachine_name());
		}
		
		yeoc.setCreate_man(userName);
		yeoc.setCreate_time(new Date());
		this.getMapper(YdmEquipmentObjectCmpMapper.class).insert(yeoc);
		rm.setState("200");
		rm.setMsgInfo("新增成功");
		return rm;
	}
	
	/**
	 * 删除机组资材备件项目
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doDeleteYdmEquipmentObjectCmp(CommonPage<YdmEquipmentObjectCmp> cp) throws Exception{
		ResultModel rm = new ResultModel();
		YdmEquipmentObjectCmp yeoc = cp.getObject();
		if (StringUtils.isEmpty(yeoc.getMachine_code())) {
			throw new Exception("机组名称不能为空");
		}
		if (StringUtils.isEmpty(yeoc.getItem_code())) {
			throw new Exception("物料名称不能为空");
		}
		if (StringUtils.isEmpty(yeoc.getSpec())) {
			throw new Exception("规格不能为空");
		}		
		int count = this.getMapper(YdmEquipmentObjectCmpMapper.class).count(yeoc);
		if (count==0) {
			throw new Exception("当前机组对应的物料名称不存在，请重新查询");
		}
		
		this.getMapper(YdmEquipmentObjectCmpMapper.class).delete(yeoc.getMachine_code(),yeoc.getItem_code(),yeoc.getSpec());
		rm.setState("200");
		rm.setMsgInfo("删除成功");
		return rm;
	}
	
}
