/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-05-30
 * 描述            ：组织关系对照表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Cm.impl;
import java.util.Date;
import java.util.List;

import market.Cm.mapper.BaseOrgCmpMapper;
import market.Cm.service.BaseOrgCmpService;
import market.Cm.vo.BaseOrgCmp;
import market.Com.mapper.ComBaseMachineMapper;
import market.Com.vo.ComBaseMachine;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

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="BaseOrgCmpService")
public class BaseOrgCmpServiceImpl extends BaseServiceImpl<BaseOrgCmp,Object> implements BaseOrgCmpService {

	/**
	 * 通过(组织代码)查询组织关系
	 * @param cp object[org_code组织代码]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQuaryBaseOrgCmp(CommonPage<BaseOrgCmp> cp) throws Exception{
		ResultModel rm = new ResultModel();
		BaseOrgCmp boc = cp.getObject();
		try {
			String swhere = "";//查询条件
			if (boc.getOrg_code()!=null && !"".equals(boc.getOrg_code())) {
				swhere+=" and org_code = '"+boc.getOrg_code()+"'";//组织代码
			}
			List<BaseOrgCmp> list = this.getMapper(BaseOrgCmpMapper.class).findWithCondition(null, swhere, "cmp_code,create_time desc", 0, 0);
			rm.setData(list);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (Exception e) {
			throw new Exception("查询失败:"+e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 新增组织对应关系
	 * @param cp objectList[{org_code组织编码, cmp_code对照编码}]
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInsertBaseOrgCmp(CommonPage<BaseOrgCmp> cp) throws Exception{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date now = new Date();
		List<BaseOrgCmp> bocList = cp.getObjectList();
		if (cp.getObjectList()==null || cp.getObjectList().size()<=0) {
			throw new Exception("请传入参数");
		}
		for (BaseOrgCmp boc : bocList) {
			//判断是否已经存在相同的对应关系
			BaseOrgCmp BOC = new BaseOrgCmp();
			//BOC.setOrg_code(boc.getOrg_code());
			BOC.setCmp_code(boc.getCmp_code());
			int count = this.getMapper(BaseOrgCmpMapper.class).count(BOC);
			if (count > 0) {
				throw new Exception("对照组织【"+boc.getCmp_name()+"】已经存在组织对照关系，无法重复信息");
			}
			boc.setCreate_man(userName);
			boc.setCreate_time(now);
			this.getMapper(BaseOrgCmpMapper.class).insert(boc);
		}		
		rm.setState("200");
		rm.setMsgInfo("新增成功");
		return rm;
	}
	
	/**
	 * 删除组织对应关系
	 * @param cp object[org_code组织编码, cmp_code对照编码]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doDeleteBaseOrgCmp(CommonPage<BaseOrgCmp> cp) throws Exception{
		ResultModel rm = new ResultModel();
		BaseOrgCmp boc = cp.getObject();
		int count = this.getMapper(BaseOrgCmpMapper.class).count(boc);
		if (count==0) {
			throw new Exception("当前组织对应关系不存在，请重新查询");
		}
		this.getMapper(BaseOrgCmpMapper.class).delete(boc.getOrg_code(),boc.getCmp_code());		
		rm.setState("200");
		rm.setMsgInfo("删除成功");
		return rm;
	}
	
	/**
	 * 查询所有的机台信息
	 * @param cp object["":""]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQuaryComBaseMachine(CommonPage<ComBaseMachine> cp) throws Exception{
		ResultModel rm = new ResultModel();
		try {
			ComBaseMachine cbm = new ComBaseMachine();
			cbm.setValidflag("1");
			List<ComBaseMachine> list = this.getMapper(ComBaseMachineMapper.class).find(cbm, 0, 0);
			rm.setData(list);
			rm.setState("200");
			rm.setMsgInfo("查询机台信息成功");
		} catch (Exception e) {
			throw new Exception("查询失败:"+e.getMessage());
		}
		return rm;
	}
	
}
