/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-03-28
 * 描述            ：CE认证基础信息主表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.qcm.service.impl;

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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.metallurgy.vo.ComBaseStd;
import market.Qms.qcm.mapper.QcmZbsCeDMapper;
import market.Qms.qcm.mapper.QcmZbsCeMMapper;
import market.Qms.qcm.service.QcmZbsCeMService;
import market.Qms.qcm.vo.QcmZbsCE;
import market.Qms.qcm.vo.QcmZbsCeD;
import market.Qms.qcm.vo.QcmZbsCeM;

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 = "QcmZbsCeMService")
public class QcmZbsCeMServiceImpl extends BaseServiceImpl<QcmZbsCeM, Object> implements QcmZbsCeMService {

	/**
	 * 新增接口
	 * 
	 * @param oic
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel addCE(CommonPage<QcmZbsCeM> oic) throws Exception {

		ResultModel rm = new ResultModel();

		try {
			String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();

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

			List<QcmZbsCeM> ces = oic.getObjectList();
			if (ces == null || ces.size() < 1) {
				rm.setState("500");
				rm.setMsgInfo("请传入需要新增的数据");
				return rm;
			}

			for (QcmZbsCeM qcmZbsCeM : ces) {
				if (qcmZbsCeM.getCe_desc() == null || "".equals(qcmZbsCeM.getCe_desc())) {
					rm.setState("500");
					rm.setMsgInfo("CE认证描述不能为空，请确认");
					return rm;
				}
				List<QcmZbsCeM> old = this.getMapper(QcmZbsCeMMapper.class).findWithCondition(null,
						"CE_DESC='" + qcmZbsCeM.getCe_desc() + "'", null, 0, 0);
				if (old != null && old.size() > 0) {
					rm.setState("500");
					rm.setMsgInfo("CE认证描述[" + qcmZbsCeM.getCe_desc() + "]已经存在,请确认");
					return rm;

				}
			}

			int count = this.getBean(QcmJhySampleConsignDItemServiceImpl.class).GetMaxSerialNo("QCM_ZBS_CE_M",
					"CE_CODE", "CE");

			for (QcmZbsCeM qcmZbsCeM : ces) {
				String ce_code = "CE" + String.format("%04d", ++count);
				qcmZbsCeM.setCreate_name(userName);
				qcmZbsCeM.setCreate_time(new Date());
				qcmZbsCeM.setCe_code(ce_code);
				this.getMapper(QcmZbsCeMMapper.class).insert(qcmZbsCeM);

			}

			rm.setState("200");
			rm.setMsgInfo("新增成功");

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("新增失败");
		}

		return rm;
	}

	/**
	 * 查询接口
	 * 
	 * @param oic
	 * @return
	 */

	public ResultModel findCE(CommonPage<QcmZbsCeM> oic) {

		ResultModel rm = new ResultModel();
		try {

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

			QcmZbsCeM cm = oic.getObject();
			if (cm == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入查询对象");
				return rm;
			}
			List<QcmZbsCeM> ces = new ArrayList<QcmZbsCeM>();

			if ("0".equals(cm.getValidflag())) {
				ces = this.getMapper(QcmZbsCeMMapper.class).findWithCondition(null, null, "CE_CODE", 0, 0);
			} else {
				ces = this.getMapper(QcmZbsCeMMapper.class).findWithCondition(null, "VALIDFLAG='1'", "CE_CODE", 0, 0);
			}

			rm.setState("200");
			rm.setData(ces);

		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败");
		}

		return rm;

	}

	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAbolishAndRecover(CommonPage<QcmZbsCeM> oic) throws Exception {
		ResultModel rm = new ResultModel();
		try {
			String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();
			if (oic == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}

			List<QcmZbsCeM> cm = oic.getObjectList();
			if (cm == null || cm.size() < 1) {
				rm.setState("500");
				rm.setMsgInfo("请传入查询对象");
				return rm;
			}

			if (oic.getMemo1() == null || "".equals(oic.getMemo1())) {
				rm.setState("500");
				rm.setMsgInfo("请传入操作标识");
				return rm;
			}

			if ("1".equals(oic.getMemo1())) {

				for (QcmZbsCeM qcmZbsCeM : cm) {
					this.getMapper(QcmZbsCeMMapper.class).update(qcmZbsCeM);
				}
				rm.setState("200");
				rm.setMsgInfo("恢复成功");

			} else {
				for (QcmZbsCeM qcmZbsCeM : cm) {
					qcmZbsCeM.setDelete_name(userName);
					qcmZbsCeM.setDelete_time(new Date());
					this.getMapper(QcmZbsCeMMapper.class).update(qcmZbsCeM);
				}
				rm.setState("200");
				rm.setMsgInfo("作废成功");
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("操作失败");
		}

		return rm;

	}

	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel keepCE(CommonPage<QcmZbsCE> oic) throws Exception {

		ResultModel rm = new ResultModel();

		try {
			String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();
			if (oic == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
			}

			QcmZbsCE object = oic.getObject();
			
			if (object==null) {
				rm.setState("500");
				rm.setMsgInfo("请传入操作对象");
				return rm;
			}
			QcmZbsCeM ce = object.getCe();
			List<ComBaseStd> stds = object.getStds();
			
			if (ce==null) {
				rm.setState("500");
				rm.setMsgInfo("请传入认证对象");
				return rm;
			}

		    this.getMapper(QcmZbsCeDMapper.class).deleteByCeCode(ce.getCe_code());
			
			for (ComBaseStd comBaseStd : stds) {
				QcmZbsCeD cd = new QcmZbsCeD();
				cd.setCe_code(ce.getCe_code());
				cd.setStd_code(comBaseStd.getStdCode());
				cd.setStd_name(comBaseStd.getStdName());
				this.getMapper(QcmZbsCeDMapper.class).insert(cd);

			}
			ce.setUpdate_time(new Date());
			ce.setUpdate_name(userName);
			this.getMapper(QcmZbsCeMMapper.class).update(ce);
			rm.setState("200");
			rm.setMsgInfo("保存成功");

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

		return rm;

	}
	
	
	
	public ResultModel getStd(CommonPage<QcmZbsCeM> oic) throws Exception {
		ResultModel rm=new ResultModel();
		
		if (oic==null) {
			rm.setState("500");
			rm.setMsgInfo("请传入参数");
			return rm;
		}
		
		QcmZbsCeM ce=oic.getObject();
		if (ce==null) {
			rm.setState("500");
			rm.setMsgInfo("请传入操作对象");
			return rm;
		}
		
		if (ce.getCe_code()==null || "".equals(ce.getCe_code())) {
			rm.setState("500");
			rm.setMsgInfo("请传入认证代码");
			return rm;
		}
		
		List<QcmZbsCeD> ed=this.getMapper(QcmZbsCeDMapper.class).findWithCondition(null, "CE_CODE='"+ce.getCe_code()+"'", null, 0, 0);
		
		rm.setData(ed);
		rm.setState("200");	
		return rm;
		
		
	}
	
	
	
	

}
