/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-02-24
 * 描述            ：表头项配置表
 * 注意事项     ：
 * 遗留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.com.service.impl.ComBaseConfigServiceImpl;
import market.Qms.com.vo.ComBaseConfig;
import market.Qms.function.StringFormat;
import market.Qms.qcm.mapper.QcmZbsDataOrdMapper;
import market.Qms.qcm.mapper.QcmZbsTmplItemMapper;
import market.Qms.qcm.service.QcmZbsTmplItemService;
import market.Qms.qcm.vo.QcmZbsDataOrd;
import market.Qms.qcm.vo.QcmZbsTmplItem;

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 = "QcmZbsTmplItemService")
public class QcmZbsTmplItemServiceImpl extends BaseServiceImpl<QcmZbsTmplItem, Object>
		implements QcmZbsTmplItemService {

	public ResultModel findByTmplNo(CommonPage<QcmZbsTmplItem> oci) {

		ResultModel rm = new ResultModel();
		try {

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

			if (item.getTmpl_no() == null || "".equals(item.getTmpl_no())) {
				rm.setState("500");
				rm.setMsgInfo("请传入模板号！");
				return rm;
			}
			List<QcmZbsTmplItem> items = this.getMapper(QcmZbsTmplItemMapper.class).findWithCondition(null,
					"tmpl_no='" + item.getTmpl_no() + "'", null, 0, 0);
			rm.setData(items);
			rm.setState("200");

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

		return rm;

	}

	/**
	 * 添加、修改表头项配置
	 * 
	 * @param oci
	 * @return
	 * @throws Exception
	 *//*
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddhead(CommonPage<QcmZbsTmplItem> 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;
			}
			QcmZbsTmplItem item = oci.getObject();
			if (item == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数！");
				return rm;
			}

			if (item.getTmpl_no() == null || "".equals(item.getTmpl_no())) {
				rm.setState("500");
				rm.setMsgInfo("请传入模板号！");
				return rm;
			}

			if (item.getItem_code() == null || "".equals(item.getItem_code())) {
				rm.setState("500");
				rm.setMsgInfo("请传入项目代码！");
				return rm;
			}

			QcmZbsTmplItem tar = this.getMapper(QcmZbsTmplItemMapper.class).findById(item.getTmpl_no(),
					item.getItem_code());
			if (tar == null) {
				item.setCreate_name(username);
				item.setCreate_time(new Date());
				if (item.getItem_str_col_e() != null && !"".equals(item.getItem_str_col_e())) {
					item.setItem_str_col(String.valueOf(StringFormat.getNumber(item.getItem_str_col_e())));
				}
				this.getMapper(QcmZbsTmplItemMapper.class).insert(item);
				rm.setState("200");
				rm.setMsgInfo("新增成功！");
			} else {
				if (item.getItem_str_col_e() != null && !"".equals(item.getItem_str_col_e())) {
					item.setItem_str_col(String.valueOf(StringFormat.getNumber(item.getItem_str_col_e())));
				}
				item.setUpdate_name(username);
				item.setUpdate_time(new Date());
				this.getMapper(QcmZbsTmplItemMapper.class).update(item);
				rm.setState("200");
				rm.setMsgInfo("修改成功！");
			}

		} catch (Exception e) {
			throw new Exception("新增或修改失败！");
		}

		return rm;

	}*/
	
	/**
	 * 批量添加、修改表头项配置
	 * @param oci
	 * @return
	 * @throws Exception
	 */
	
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddhead(CommonPage<QcmZbsTmplItem> 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;
			}
			List<QcmZbsTmplItem> items=oci.getObjectList();
			//数据验证
			for (QcmZbsTmplItem item : items) {
				if (item == null) {
					rm.setState("500");
					rm.setMsgInfo("请传入参数！");
					return rm;
				}

				if (item.getTmpl_no() == null || "".equals(item.getTmpl_no())) {
					rm.setState("500");
					rm.setMsgInfo("请传入模板号！");
					return rm;
				}

				if (item.getItem_code() == null || "".equals(item.getItem_code())) {
					rm.setState("500");
					rm.setMsgInfo("请传入项目代码！");
					return rm;
				}
				
			}
		
			for (QcmZbsTmplItem item : items) {
				QcmZbsTmplItem tar = this.getMapper(QcmZbsTmplItemMapper.class).findById(item.getTmpl_no(),
						item.getItem_code(),item.getDiv_type());
				if (tar == null) {
					item.setCreate_name(username);
					item.setCreate_time(new Date());
					if (item.getItem_str_col_e() != null && !"".equals(item.getItem_str_col_e())) {
						item.setItem_str_col(String.valueOf(StringFormat.getNumber(item.getItem_str_col_e())));
					}
					this.getMapper(QcmZbsTmplItemMapper.class).insert(item);
					
				} else {
					if (item.getItem_str_col_e() != null && !"".equals(item.getItem_str_col_e())) {
						item.setItem_str_col(String.valueOf(StringFormat.getNumber(item.getItem_str_col_e())));
					}
					item.setUpdate_name(username);
					item.setUpdate_time(new Date());
					this.getMapper(QcmZbsTmplItemMapper.class).update(item);
					
				}
				
			}
			
			rm.setState("200");
			rm.setMsgInfo("操作成功！");

		

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

		return rm;

	}

	/**
	 * 删除表头配置项
	 */

	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel deleteHead(CommonPage<QcmZbsTmplItem> oci) throws Exception {
		ResultModel rm=new ResultModel();
		try {
			
			if (oci == null) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数！");
				return rm;
			}
			List<QcmZbsTmplItem> items = oci.getObjectList();
			if (items== null || items.size()<1 ) {
				rm.setState("500");
				rm.setMsgInfo("请传入参数！");
				return rm;
			}
			
			for (QcmZbsTmplItem item : items) {
				if (item.getTmpl_no()==null ||"".equals(item.getTmpl_no())) {
					rm.setState("500");
					rm.setMsgInfo("请传入模板号！");
					return rm;
				}
				
				if (item.getItem_code()==null || "".equals(item.getItem_code())) {
					rm.setState("500");
					rm.setMsgInfo("请传入项目代码！");
					return rm;
				}
				
				if (item.getDiv_type()==null || "".equals(item.getDiv_type())) {
					rm.setState("500");
					rm.setMsgInfo("请传入类型代码！");
					return rm;
				}
				
			}

			
			for (QcmZbsTmplItem item : items) {
				this.getMapper(QcmZbsTmplItemMapper.class).delete(item.getTmpl_no(),item.getItem_code(),item.getDiv_type());
				
			}
				
			rm.setState("200");
			rm.setMsgInfo("删除成功!");
			
			
			
		} catch (Exception e) {
			throw new Exception("删除失败");
		}
		return rm;
		
	}

	public ResultModel findTitle(String identification) {
		ResultModel rm = new ResultModel();
		List<QcmZbsDataOrd> ords = new ArrayList<>();
		if ("1".equals(identification)) {
			ords = this.getMapper(QcmZbsDataOrdMapper.class).findChem();
			//
			List<String> listconfig_id = new ArrayList<String>();
			listconfig_id.add("QCM0004");
			List<ComBaseConfig> conFig = (List<ComBaseConfig>) this.getBean(ComBaseConfigServiceImpl.class)
					.getBaseConfigInfo(listconfig_id).getData();
			if (conFig != null && conFig.size() > 0) {
				if (conFig.get(0).getConfig_val() != null && !"".equals(conFig.get(0).getConfig_val())) {
					String item = conFig.get(0).getConfig_val();
					String[] items = item.split(",");
					if (items != null && items.length > 0) {
						for (int i = 0; i < ords.size(); i++) {

							for (int j = 0; j < items.length; j++) {
								String string = items[j];
								if (ords.get(i).getNames().equals(string)) {
									break;
								}
								if (j == items.length-1) {
									ords.remove(i);
									i--;
									break;

								}

							}
							
						}
					}

				}

			}

		}

		if ("0".equals(identification)) {
			ords = this.getMapper(QcmZbsDataOrdMapper.class).findTitle();
			List<String> listconfig_id = new ArrayList<String>();
			listconfig_id.add("QCM0003");
			List<ComBaseConfig> conFig = (List<ComBaseConfig>) this.getBean(ComBaseConfigServiceImpl.class)
					.getBaseConfigInfo(listconfig_id).getData();
			if (conFig != null && conFig.size() > 0) {
				if (conFig.get(0).getConfig_val() != null && !"".equals(conFig.get(0).getConfig_val())) {
					String item = conFig.get(0).getConfig_val();
					String[] items = item.split(",");
					if (items != null && items.length > 0) {
						for (int i = 0; i < ords.size(); i++) {

							for (int j = 0; j < items.length; j++) {
								String string = items[j];
								if (ords.get(i).getNames().equals(string)) {
									break;
								}
								if (j == items.length-1) {
									ords.remove(i);
									i--;
									break;

								}

							}
							
						}
					}

				}

			}
		}

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

		return rm;

	}

}
