/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-02-25
 * 描述            ：认证机构与认证人员信息配置
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.qcm.service.impl;

import java.util.Date;
import java.util.List;

import market.Com.mapper.ComBaseInfoMapper;
import market.Com.vo.ComBaseInfo;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.qcm.mapper.QcmZbsOrgMapper;
import market.Qms.qcm.service.QcmZbsOrgService;
import market.Qms.qcm.vo.QcmZbsOrg;

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 = "QcmZbsOrgService")
public class QcmZbsOrgServiceImpl extends BaseServiceImpl<QcmZbsOrg, Object> implements QcmZbsOrgService {

	/**
	 * 根据认证机构代码4096
	 */
	public ResultModel getByOrg(CommonPage<QcmZbsOrg> oci) {
		ResultModel rm = new ResultModel();
		if (oci == null) {
			rm.setState("500");
			rm.setMsgInfo("请传入参数");
			return rm;
		}

		QcmZbsOrg org = oci.getObject();
		if (org == null) {
			rm.setState("500");
			rm.setMsgInfo("请传入参数对象");
			return rm;
		}
		String condition = "1=1";
		if (org.getOrg_code() != null && !"".equals(org.getOrg_code())) {
			condition += " and ORG_CODE='" + org.getOrg_code() + "'";
		}
		List<QcmZbsOrg> orgs = this.getMapper(QcmZbsOrgMapper.class).findWithCondition(null, condition, null, 0, 0);
		rm.setState("200");
		rm.setData(orgs);

		return rm;

	}
	
	
	/**
	 * 根据认证机构代码4096
	 */
	public ResultModel getByOrgName(String name,String validflag) {
		ResultModel rm = new ResultModel();
		String condition="SORT_CODE='4096'";
		if (name!=null && !"".equals(name)) {
			condition+=" and BASE_NAME like '%"+name+"%'";
		}	
		if ("1".equals(validflag)) {
			condition+=" and VALIDFLAG='1'";
		}
		List<ComBaseInfo> orgs=this.getMapper(ComBaseInfoMapper.class).findWithCondition(null, condition, null, 0, 0);
		rm.setState("200");
		rm.setData(orgs);

		return rm;

	}

	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel addOrg(CommonPage<QcmZbsOrg> oci) throws Exception {
		ResultModel rm = new ResultModel();
		String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();

		try {
			if (oci == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}

			List<QcmZbsOrg> org = oci.getObjectList();
			if (org == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数对象");
				return rm;
			}

			for (QcmZbsOrg qcmZbsOrg : org) {
				if (qcmZbsOrg.getOrg_code() == null || "".equals(qcmZbsOrg.getOrg_code())) {
					rm.setState("500");
					rm.setMsgInfo("认证机构代码不能为空");
					return rm;
				}
				if (qcmZbsOrg.getSvr_name() == null || "".equals(qcmZbsOrg.getSvr_name())) {
					rm.setState("500");
					rm.setMsgInfo("联系人不能为空");
					return rm;
				}
			}

			for (QcmZbsOrg qcmZbsOrg : org) {
				QcmZbsOrg old = this.getMapper(QcmZbsOrgMapper.class).findById(qcmZbsOrg.getOrg_code(),
						qcmZbsOrg.getSvr_name());
				if (old == null) {
					qcmZbsOrg.setCreate_name(userName);
					qcmZbsOrg.setCreate_time(new Date());
					this.getMapper(QcmZbsOrgMapper.class).insert(qcmZbsOrg);
				} else {
					qcmZbsOrg.setUpdate_name(userName);
					qcmZbsOrg.setUpdate_time(new Date());
					this.getMapper(QcmZbsOrgMapper.class).update(qcmZbsOrg);
				}

			}
			rm.setState("200");
			rm.setMsgInfo("保存成功");

		} catch (Exception e) {
			throw new Exception("保存失败");
		}

		return rm;
	}

	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel abolishAndRecoveryLanguage(CommonPage<QcmZbsOrg> oci) throws Exception {

		ResultModel rm = new ResultModel();
		String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();

		try {
			if (oci == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}

			List<QcmZbsOrg> languages = oci.getObjectList();
			if (languages == null || languages.size() < 1) {
				rm.setState("500");
				rm.setMsgInfo("请传入需要添加的语种");
				return rm;
			}
			// 数据验证
			for (QcmZbsOrg qcmZbsOrg : languages) {
				if (qcmZbsOrg.getOrg_code() == null || "".equals(qcmZbsOrg.getOrg_code())) {
					rm.setState("500");
					rm.setMsgInfo("认证机构代码不能为空");
					return rm;
				}
				if (qcmZbsOrg.getSvr_name() == null || "".equals(qcmZbsOrg.getSvr_name())) {
					rm.setState("500");
					rm.setMsgInfo("联系人不能为空");
					return rm;
				}
			}

			for (QcmZbsOrg org : languages) {
				if ("1".equals(org.getValidflag())) {
					this.getMapper(QcmZbsOrgMapper.class).update(org);
				} else {
					org.setDelete_name(userName);
					org.setDelete_time(new Date());
					this.getMapper(QcmZbsOrgMapper.class).update(org);
				}
			}
			if ("1".equals(languages.get(0).getValidflag())) {
				rm.setMsgInfo("恢复成功");
			} else {
				rm.setMsgInfo("作废成功");
			}

		} catch (Exception e) {

			throw new Exception("操作失败");
		}

		return rm;
	}

}
