/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-02-23
 * 描述            ：质保书成分材质方案主表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.qcm.service.impl;

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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.qcm.mapper.QcmZbsTmplCicMapper;
import market.Qms.qcm.mapper.QcmZbsTmplPicMapper;
import market.Qms.qcm.mapper.QcmZbsTmplSchMapper;
import market.Qms.qcm.service.QcmZbsTmplSchService;
import market.Qms.qcm.vo.QcmZbsTmplCic;
import market.Qms.qcm.vo.QcmZbsTmplPic;
import market.Qms.qcm.vo.QcmZbsTmplSch;
import market.Qms.qcm.vo.TmplSchObject;

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 = "QcmZbsTmplSchService")
public class QcmZbsTmplSchServiceImpl extends BaseServiceImpl<QcmZbsTmplSch, Object> implements QcmZbsTmplSchService {

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

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

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

			if (obj == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数！");
				return rm;
			}
			QcmZbsTmplSch sch = obj.getSch();
			List<QcmZbsTmplCic> chemObject = obj.getChemObject();
			List<QcmZbsTmplPic> phyObject = obj.getPhyObject();
			if (sch == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入模板！");
				return rm;
			}
			if (sch.getTmpl_cret_desc() == null || "".equals(sch.getTmpl_cret_desc())) {
				rm.setState("500");
				rm.setMsgInfo("请传入模板名称！");
				return rm;
			}
			List<QcmZbsTmplSch> old = this.getMapper(QcmZbsTmplSchMapper.class).findWithCondition(null,
					"TMPL_CRET_DESC='" + sch.getTmpl_cret_desc() + "'", null, 0, 0);
			if (old != null && old.size() > 0) {
				rm.setState("500");
				rm.setMsgInfo("模板名称已存在，请重新输入！");
				return rm;
			}

			String tmpl_cret_no = "F";
			// 获取流水
			List<QcmZbsTmplSch> big = this.getMapper(QcmZbsTmplSchMapper.class).findWithCondition(null, null,
					"TMPL_CRET_NO desc", 0, 0);
			int count = 0;
			if (big != null && big.size() > 0) {
				String r = big.get(0).getTmpl_cret_no();
				String last = r.substring(1);
				count = Integer.valueOf(last);
				count++;
				tmpl_cret_no += String.format("%07d", count);
			} else {
				tmpl_cret_no += "0000001";
			}

			sch.setTmpl_cret_no(tmpl_cret_no);
			sch.setCreate_name(userName);
			sch.setCreate_time(new Date());
			this.getMapper(QcmZbsTmplSchMapper.class).insert(sch);

			if (chemObject != null && chemObject.size() > 0) {

				for (QcmZbsTmplCic chem : chemObject) {
					chem.setTmpl_cret_no(tmpl_cret_no);
					chem.setCreate_name(userName);
					chem.setCreate_time(new Date());
					this.getMapper(QcmZbsTmplCicMapper.class).insert(chem);

				}

			}

			if (phyObject != null && phyObject.size() > 0) {
				
				for (QcmZbsTmplPic phy : phyObject) {
					phy.setTmpl_cret_no(tmpl_cret_no);
					
					phy.setVal_cnt(String.valueOf(phyObject.size()));
					phy.setCreate_time(new Date());
					phy.setCreate_name(userName);
					this.getMapper(QcmZbsTmplPicMapper.class).insert(phy);

				}
			}

			rm.setState("200");
			rm.setMsgInfo("添加成功");

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("添加失败");
			
		}

		return rm;

	}

	/**
	 * 
	 * 根据名称查询
	 */

	public ResultModel getByName(CommonPage<QcmZbsTmplSch> oic) throws Exception {

		ResultModel rm = new ResultModel();

		try {

			QcmZbsTmplSch ch = oic.getObject();
			String condition = "1=1";
			if (ch.getTmpl_cret_desc() != null && !"".equals(ch.getTmpl_cret_desc())) {
				condition += " and TMPL_CRET_DESC like '%" + ch.getTmpl_cret_desc() + "%'";
			}
			List<QcmZbsTmplSch> sch = this.getMapper(QcmZbsTmplSchMapper.class).findWithCondition(null, condition,
					"TMPL_CRET_NO", 0, 0);
			rm.setState("200");

			rm.setData(sch);
		} catch (Exception e) {
			rm.setMsgInfo("500");
			rm.setState("500");
			return rm;
		}

		return rm;

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

			if (obj == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数！");
				return rm;
			}
			QcmZbsTmplSch sch = obj.getSch();
			List<QcmZbsTmplCic> chemObject = obj.getChemObject();
			List<QcmZbsTmplPic> phyObject = obj.getPhyObject();
			if (sch == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入模板！");
				return rm;
			}
			if (sch.getTmpl_cret_no()==null || "".equals(sch.getTmpl_cret_no())) {
				rm.setState("500");
				rm.setMsgInfo("请传入名称！");
				return rm;
			}
			String tmpl_cret_no=sch.getTmpl_cret_no();
			if (sch.getTmpl_cret_desc() == null || "".equals(sch.getTmpl_cret_desc())) {
				rm.setState("500");
				rm.setMsgInfo("请传入模板名称！");
				return rm;
			}
			List<QcmZbsTmplSch> old = this.getMapper(QcmZbsTmplSchMapper.class).findWithCondition(null,
					"TMPL_CRET_DESC='" + sch.getTmpl_cret_desc() + "'", null, 0, 0);
			if (old != null && old.size() > 0) {
				
				if (!old.get(0).getTmpl_cret_desc().equals(sch.getTmpl_cret_desc())) {
					rm.setState("500");
					rm.setMsgInfo("模板名称已存在，请重新输入！");
					return rm;
				}	
			}
			
			this.getMapper(QcmZbsTmplPicMapper.class).deleteByNo(tmpl_cret_no);
			this.getMapper(QcmZbsTmplCicMapper.class).deleteByNo(tmpl_cret_no);
			if (chemObject != null && chemObject.size() > 0) {

				for (QcmZbsTmplCic chem : chemObject) {
					chem.setTmpl_cret_no(tmpl_cret_no);
					chem.setCreate_name(userName);
					chem.setCreate_time(new Date());
					this.getMapper(QcmZbsTmplCicMapper.class).insert(chem);

				}

				sch.setMod_cic("1");
				
				
			}else{
				sch.setMod_cic("0");
			}

			if (phyObject != null && phyObject.size() > 0) {
				long cont = 1;
				for (QcmZbsTmplPic phy : phyObject) {
					phy.setTmpl_cret_no(tmpl_cret_no);
					phy.setVal_cnt(String.valueOf(phyObject.size()));
					phy.setCreate_time(new Date());
					phy.setCreate_name(userName);
					this.getMapper(QcmZbsTmplPicMapper.class).insert(phy);

				}
				sch.setMod_pic("1");
			}else{
				sch.setMod_pic("0");
			}
			
			this.getMapper(QcmZbsTmplSchMapper.class).update(sch);
			//更新SLM_BASE_ADD_ASK和com_base_additional数据
			this.getMapper(QcmZbsTmplSchMapper.class).updateAddAskByNo(sch.getTmpl_cret_no(),sch.getTmpl_cret_desc());
			this.getMapper(QcmZbsTmplSchMapper.class).updateBaseAddByNo(sch.getTmpl_cret_no(),sch.getTmpl_cret_desc());
			rm.setState("200");
			rm.setMsgInfo("修改成功");
		} catch (Exception e) {
			throw new Exception("修改失败！");
		}
		return rm;
		
	}
		
		
		
		
		
}
