/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-02-25
 * 描述            ：质保书多国语言配置表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.qcm.service.impl;

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

import market.Com.impl.ComBasePhyBServiceImpl;
import market.Com.impl.ComBasePhyServiceImpl;
import market.Com.mapper.ComBaseInfoMapper;
import market.Com.vo.ComBaseInfo;
import market.Com.vo.ComBasePhy;
import market.Com.vo.ComBasePhyB;
import market.Com.vo.ComBasePhyadd;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.qcm.mapper.QcmZbsLanguageMapper;
import market.Qms.qcm.service.QcmZbsLanguageService;
import market.Qms.qcm.vo.QcmZbsDataOrd;
import market.Qms.qcm.vo.QcmZbsLanguage;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.service.support.PageModel;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path = "QcmZbsLanguageService")
public class QcmZbsLanguageServiceImpl extends BaseServiceImpl<QcmZbsLanguage, Object>
		implements QcmZbsLanguageService {

	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel addLanguage(CommonPage<QcmZbsLanguage> 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<QcmZbsLanguage> languages = oci.getObjectList();
			if (languages == null || languages.size() < 1) {
				rm.setState("500");
				rm.setMsgInfo("请传入需要添加的语种");
				return rm;
			}

			// 数据验证
			for (QcmZbsLanguage qcmZbsLanguage : languages) {
				if (qcmZbsLanguage.getLng_code() == null || "".equals(qcmZbsLanguage.getLng_code())) {
					rm.setState("500");
					rm.setMsgInfo("语种代码不能为空");
					return rm;
				}

				if (qcmZbsLanguage.getType_code() == null || "".equals(qcmZbsLanguage.getType_code())) {
					rm.setState("500");
					rm.setMsgInfo("分类代码不能为空");
					return rm;
				}

				if (qcmZbsLanguage.getItem_code() == null || "".equals(qcmZbsLanguage.getItem_code())) {
					rm.setState("500");
					rm.setMsgInfo("项目代码不能为空");
					return rm;
				}
			}

			// 新增
			for (QcmZbsLanguage qcmZbsLanguage : languages) {
				QcmZbsLanguage old = this.getMapper(QcmZbsLanguageMapper.class).findById(qcmZbsLanguage.getLng_code(),
						qcmZbsLanguage.getType_code(), qcmZbsLanguage.getItem_code());
				if (old == null) {
					qcmZbsLanguage.setCreate_name(userName);
					qcmZbsLanguage.setCreate_time(new Date());
					this.getMapper(QcmZbsLanguageMapper.class).insert(qcmZbsLanguage);
				} else {
					qcmZbsLanguage.setUpdate_name(userName);
					qcmZbsLanguage.setUpdate_time(new Date());
					this.getMapper(QcmZbsLanguageMapper.class).update(qcmZbsLanguage);
				}

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

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

	}

	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel abolishAndRecoveryLanguage(CommonPage<QcmZbsLanguage> 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<QcmZbsLanguage> languages = oci.getObjectList();
			if (languages == null || languages.size() < 1) {
				rm.setState("500");
				rm.setMsgInfo("请传入需要添加的语种");
				return rm;
			}
			// 数据验证
			for (QcmZbsLanguage qcmZbsLanguage : languages) {
				if (qcmZbsLanguage.getLng_code() == null || "".equals(qcmZbsLanguage.getLng_code())) {
					rm.setState("500");
					rm.setMsgInfo("语种代码不能为空");
					return rm;
				}

				if (qcmZbsLanguage.getType_code() == null || "".equals(qcmZbsLanguage.getType_code())) {
					rm.setState("500");
					rm.setMsgInfo("分类代码不能为空");
					return rm;
				}

				if (qcmZbsLanguage.getItem_code() == null || "".equals(qcmZbsLanguage.getItem_code())) {
					rm.setState("500");
					rm.setMsgInfo("项目代码不能为空");
					return rm;
				}
			}

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

		} catch (Exception e) {

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

		return rm;
	}

	// 获取语言接口

	public ResultModel getAllLanguage() {

		ResultModel rm = new ResultModel();

		List<ComBaseInfo> info = this.getMapper(ComBaseInfoMapper.class).findWithCondition(null, "SORT_CODE='411501'",
				"BASE_CODE", 0, 0);
		rm.setData(info);
		rm.setState("200");

		return rm;

	}

	// 获取分类

	public ResultModel getType() {

		ResultModel rm = new ResultModel();

		List<ComBaseInfo> info = this.getMapper(ComBaseInfoMapper.class).findWithCondition(null, "SORT_CODE='411502'",
				"BASE_CODE", 0, 0);
		rm.setData(info);
		rm.setState("200");

		return rm;

	}

	public ResultModel findByCondition(CommonPage<QcmZbsLanguage> oci) {
		ResultModel rm = new ResultModel();
		if (oci == null) {
			rm.setState("500");
			rm.setMsgInfo("请传入参数");
			return rm;
		}
		QcmZbsLanguage language = oci.getObject();
		String condition = "1=1";
		if (language.getLng_code() != null && !"".equals(language.getLng_code())) {
			condition += " and LNG_CODE='" + language.getLng_code() + "'";
		}

		if (language.getType_code() != null && !"".equals(language.getType_code())) {

			condition += " and TYPE_CODE='" + language.getType_code() + "'";
		}

		if (language.getItem_code() != null && !"".equals(language.getItem_code())) {
			condition += " and ITEM_CODE='" + language.getItem_code() + "'";
		}

		if ("1".equals(language.getValidflag())) {
			condition += " and VALIDFLAG='" + language.getValidflag() + "'";
		}

		List<QcmZbsLanguage> languages = this.getMapper(QcmZbsLanguageMapper.class).findWithCondition(null, condition,
				null, 0, 0);

		rm.setData(languages);
		rm.setState("200");
		return rm;
	}
	
	
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel addAllLanguage(CommonPage<QcmZbsLanguage> oci) throws Exception {
		
		ResultModel rm=new ResultModel();
		try {
			
			String userName=this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();
			
			if (oci==null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数");
				return rm;
				
			}
			
			QcmZbsLanguage languages=oci.getObject();
			if (languages==null) {
				rm.setState("500");
				rm.setMsgInfo("请传入需要添加的对象");
				return rm;
				
			}
			if (languages.getLng_code()==null || "".equals(languages.getLng_code())) {
				rm.setState("500");
				rm.setMsgInfo("请传入语种代码");
				return rm;
			}
			
			if (languages.getType_code()==null || "".equals(languages.getType_code())) {
				rm.setState("500");
				rm.setMsgInfo("请传入分类代码");
				return rm;
			}
			
			List<QcmZbsLanguage> newObject=new ArrayList<QcmZbsLanguage>();
			
			
			if ("41150205".equals(languages.getType_code())) {
				//认证机构
				
				
			}else if ("41150204".equals(languages.getType_code())) {
				//材质项目
				//材质大项
				CommonPage<ComBasePhy> cp=new CommonPage<ComBasePhy>();
				ComBasePhy pb=new ComBasePhy();
				cp.setMemo1("0");
				cp.setObject(pb);
				PageModel<ComBasePhyadd> pm =(PageModel<ComBasePhyadd>) this.getBean(ComBasePhyServiceImpl.class).doQueryPhyBWeb(cp).getData();
				List<ComBasePhyadd> phyB=pm.getRows();
				if (phyB!=null && phyB.size()>0) {
					for (ComBasePhyadd phy : phyB) {
						QcmZbsLanguage tarObject=new QcmZbsLanguage();
						tarObject.setLng_code(languages.getLng_code());
						tarObject.setType_code(languages.getType_code());
						tarObject.setItem_code(phy.getPhy_code_s());
						tarObject.setItem_desc(phy.getPhy_name_s());
						QcmZbsLanguage old = this.getMapper(QcmZbsLanguageMapper.class).findById(languages.getLng_code(),
								languages.getType_code(), phy.getPhy_code_s());
						if (old == null) {
							tarObject.setCreate_name(userName);
							tarObject.setCreate_time(new Date());
							this.getMapper(QcmZbsLanguageMapper.class).insert(tarObject);
							newObject.add(tarObject);
						} 
						
						
						
					}
					
					
				}
	
				
			}else if ("41150203".equals(languages.getType_code())) {
				//材质大项
				CommonPage<ComBasePhyB> cp=new CommonPage<ComBasePhyB>();
				ComBasePhyB pb=new ComBasePhyB();
				cp.setMemo1("0");
				cp.setObject(pb);
			
				PageModel<ComBasePhyB> pm =(PageModel<ComBasePhyB>) this.getBean(ComBasePhyBServiceImpl.class).doQueryPhyBWeb(cp).getData();
				List<ComBasePhyB> phyB=pm.getRows();
				if (phyB!=null && phyB.size()>0) {
					
					for (ComBasePhyB phy : phyB) {
						QcmZbsLanguage tarObject=new QcmZbsLanguage();
						tarObject.setLng_code(languages.getLng_code());
						tarObject.setType_code(languages.getType_code());
						tarObject.setItem_code(phy.getPhy_code_l());
						tarObject.setItem_desc(phy.getPhy_name_l());
						QcmZbsLanguage old = this.getMapper(QcmZbsLanguageMapper.class).findById(languages.getLng_code(),
								languages.getType_code(), phy.getPhy_code_l());
						if (old == null) {
							tarObject.setCreate_name(userName);
							tarObject.setCreate_time(new Date());
							this.getMapper(QcmZbsLanguageMapper.class).insert(tarObject);
							newObject.add(tarObject);
						} 
					}
					
					
				}
				
			}else if ("41150202".equals(languages.getType_code())) {
				//物料信息
				ResultModel tar=new ResultModel();
				tar=this.getBean(QcmZbsTmplItemServiceImpl.class).findTitle("1");
				List<QcmZbsDataOrd> ords=(List<QcmZbsDataOrd>) tar.getData();
				if (ords!=null && ords.size()>0) {
					for (QcmZbsDataOrd ord : ords) {
						QcmZbsLanguage tarObject=new QcmZbsLanguage();
						tarObject.setLng_code(languages.getLng_code());
						tarObject.setType_code(languages.getType_code());
						tarObject.setItem_code(ord.getNames());
						tarObject.setItem_desc(ord.getNotes());
						
						QcmZbsLanguage old = this.getMapper(QcmZbsLanguageMapper.class).findById(languages.getLng_code(),
								languages.getType_code(), ord.getNotes());
						if (old == null) {
							tarObject.setCreate_name(userName);
							tarObject.setCreate_time(new Date());
							this.getMapper(QcmZbsLanguageMapper.class).insert(tarObject);
							newObject.add(tarObject);
						} 
						
					}
					
					
				}
				
			}else if ("41150201".equals(languages.getType_code())) {
				//表头信息
				ResultModel tar=new ResultModel();
				tar=this.getBean(QcmZbsTmplItemServiceImpl.class).findTitle("0");
				List<QcmZbsDataOrd> ords=(List<QcmZbsDataOrd>) tar.getData();
				if (ords!=null && ords.size()>0) {
					for (QcmZbsDataOrd ord : ords) {
						QcmZbsLanguage tarObject=new QcmZbsLanguage();
						tarObject.setLng_code(languages.getLng_code());
						tarObject.setType_code(languages.getType_code());
						tarObject.setItem_code(ord.getNames());
						tarObject.setItem_desc(ord.getNotes());
						
						
						QcmZbsLanguage old = this.getMapper(QcmZbsLanguageMapper.class).findById(languages.getLng_code(),
								languages.getType_code(), ord.getNotes());
						if (old == null) {
							tarObject.setCreate_name(userName);
							tarObject.setCreate_time(new Date());
							this.getMapper(QcmZbsLanguageMapper.class).insert(tarObject);
							newObject.add(tarObject);
						} 
						
					}
					
					
				}
				
			}
			
			
			rm.setState("200");
			rm.setMsgInfo("添加成功");
			rm.setData(newObject);
			
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("批量添加失败");
		}
		
		
		
		return rm;
	}

		
		

}
