/***文档注释***********************************************
 * 作者            ： 朱泽鹏
 * 创建日期     ：2018-02-6
 * 描述            ：探伤要求主表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Qms.metallurgy.impl;

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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComMscStdDetectionCMapper;
import market.Qms.metallurgy.mapper.ComMscStdDetectionMapper;
import market.Qms.metallurgy.service.ComMscStdDetectionService;
import market.Qms.metallurgy.vo.ComMscStdDetection;
import market.Qms.metallurgy.vo.ComMscStdDetectionC;

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 = "ComMscStdDetectionService")
public class ComMscStdDetectionServiceImpl extends BaseServiceImpl<ComMscStdDetection, String> implements ComMscStdDetectionService {
	/**
	 * 探伤标准索引信息 查询
	 * @param cb
	 * @return
	 */
	public ResultModel doQueryComMscStdDetection(CommonPage<ComMscStdDetection> cb) 
	{
		ResultModel rm = new ResultModel();
		int pageIndex = cb.getPageIndex();
		int pageSize = cb.getPageSize();
		PageModel<ComMscStdDetection> pm = new PageModel<ComMscStdDetection>(pageIndex, pageSize);
		try 
		{   
			String condition = null;
			ComMscStdDetection cbs = cb.getObject();
			if ("".equals(cb.getMemo1())) 
			{
				condition = " std_code_nk = '" + cbs.getStdCodeNk() + "'";

			} else {
				condition = " std_code_nk = '" + cbs.getStdCodeNk()
				+ "' and VALIDFLAG <> 0";
			}
			List<ComMscStdDetection> cmsp = this.getMapper(ComMscStdDetectionMapper.class).findWithCondition(null, condition, " CREATE_TIME desc ",
					(pageIndex - 1) * pageSize, pageSize);
			// 设置元素个数
			for (ComMscStdDetection cics : cmsp) 
			{
				// 有效记录个数
				ComMscStdDetectionC ct = new ComMscStdDetectionC();
				ct.setDic(cics.getDic());
				Integer count = this.getMapper(ComMscStdDetectionCMapper.class).count(ct);

				cics.setCount(count);

			}
			pm.setRows(cmsp);
			pm.setTotal(this.getMapper(ComMscStdDetectionMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");

		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveComMscStdDetection(List<ComMscStdDetection> coms) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date date = new Date();
		try 
		{
			for (ComMscStdDetection com : coms) 
			{
				if(com.getDic() != null)
				{
					com.setUpdateName(userName);
					com.setUpdateTime(date);
					this.getMapper(ComMscStdDetectionMapper.class).update(com);

				} else {
					String dic = this.getMapper(ComMscStdDetectionMapper.class).getMax();
					com.setDic(dic);
					com.setCreateName(userName);
					com.setCreateTime(new Date());
					this.getMapper(ComMscStdDetectionMapper.class).insert(com);
					rm.setState("200");
					rm.setData(dic);
				}
			}
			rm.setState("200");
			rm.setMsgInfo("保存成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;

	}
	
	
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveComMscStdDetectionN(List<ComMscStdDetection> coms) 
	{
		ResultModel rm = new ResultModel();
		String userName =(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date date = new Date();
		try 
		{
			for (ComMscStdDetection com : coms) 
			{
				if(com.getDic() != null)
				{
					com.setUpdateName(userName);
					com.setUpdateTime(date);
					this.getMapper(ComMscStdDetectionMapper.class).update(com);

				} else {
					String dic = this.getMapper(ComMscStdDetectionMapper.class).getMax();
					com.setDic(dic);
					com.setCreateName(userName);
					com.setCreateTime(new Date());
					this.getMapper(ComMscStdDetectionMapper.class).insert(com);
					rm.setState("200");
					rm.setData(dic);
				}
			}
			rm.setState("200");
			rm.setMsgInfo("保存成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;

	}

	/**
	 * 信息废除A
	 * @param cb
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInvalidA(List<ComMscStdDetection> cb) throws Exception
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);

		try
		{
			Date date = new Date();
			if (cb.size() > 0) 
			{
				for (ComMscStdDetection ccs : cb) 
				{
					ComMscStdDetection ct = new ComMscStdDetection();
					ct.setDic(ccs.getDic());
					ComMscStdDetection com = new ComMscStdDetection();
					com = this.getMapper(ComMscStdDetectionMapper.class).find(ct, 0, 0).get(0);
					if (com.getValidflag().equals("1")) 
					{
						com.setValidflag("0");
						com.setDeleteName(userName);
						com.setDeleteTime(date);
						ComMscStdDetectionC ComMscStdDetectionC = new ComMscStdDetectionC();
						ComMscStdDetectionC.setDic(com.getDic());
						List<ComMscStdDetectionC> list = this.getMapper(ComMscStdDetectionCMapper.class).find(ComMscStdDetectionC, 0, 0);
						this.getMapper(ComMscStdDetectionMapper.class).update(com);
						if (!list.isEmpty()) 
						{
							for (ComMscStdDetectionC c : list) 
							{
								if (c.getValidflag().equals("1")) 
								{
									c.setDeleteName(userName);
									c.setDeleteTime(date);
									c.setValidflag("0");
									this.getMapper(ComMscStdDetectionCMapper.class).update(c);
								}
							}
						}

					} else {
						rm.setState("500");
						rm.setMsgInfo("已是废除状态!");
					}
				}
			}
			rm.setState("200");
			rm.setMsgInfo("索引信息和标准信息  废除成功!");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	/**
	 * 信息恢复A
	 * @param cb
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doRecoverA(List<ComMscStdDetection> cb) throws Exception
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);

		try
		{
			if (cb.size() > 0) 
			{
				for (ComMscStdDetection ccs : cb) 
				{
					ComMscStdDetection ct = new ComMscStdDetection();
					ct.setDic(ccs.getDic());
					ComMscStdDetection com = new ComMscStdDetection();
					com = this.getMapper(ComMscStdDetectionMapper.class).find(ct, 0, 0).get(0);
					if (com.getValidflag().equals("0")) 
					{
						this.getMapper(ComMscStdDetectionMapper.class).updatec(com.getDic());;
						ComMscStdDetectionC ComMscStdDetectionC = new ComMscStdDetectionC();
						ComMscStdDetectionC.setDic(com.getDic());
						List<ComMscStdDetectionC> list = this.getMapper(ComMscStdDetectionCMapper.class).find(ComMscStdDetectionC, 0, 0);
						if (list.size() > 0) 
						{
							for (ComMscStdDetectionC c : list) 
							{
								if (c.getValidflag().equals("0")) 
								{
									this.getMapper(ComMscStdDetectionCMapper.class).updatec(c.getDic(), c.getIndexSeq());
								}
							}
						} 					
					}else {
						rm.setState("500");
						rm.setMsgInfo("该记录已经恢复!");
						return rm;
					}
				}
			}
			rm.setState("200");
			rm.setMsgInfo("索引信息和标准信息  恢复成功!");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	enum Status {
		INVALID("0"), RECOVER("1");
		private String value;

		private Status(String val) {
			this.value = val;
		}

		public String getValue() {
			return this.value;
		}
	}
	
	public ResultModel doQueryBySql(CommonPage<ComMscStdDetection> sum) {
		ResultModel rm =new ResultModel();
		try {
			ComMscStdDetection com =sum.getObject();
			String process_code =com.getProcessCode();
			String customer_code =com.getCustomerCode();
			String prod_code_nk =com.getProdCodeNk();
			String steel_code_nk =com.getSteelCodeNk();
			String delivery_state_code =com.getDeliveryStateCode();
			
			
			String[] customer=null;
			String[] prod=null;
			String[] steel=null;
			String[] delivery=null;
			
			if (customer_code!=null && !"".equals(customer_code)) {
				customer=process_code.split(",");
			}
			
            if (prod_code_nk!=null && !"".equals(prod_code_nk)) {
            	prod=prod_code_nk.split(",");
			}
            
            
            if (steel_code_nk!=null && !"".equals(steel_code_nk)) {
            	steel=steel_code_nk.split(",");
			}
            
            if (delivery_state_code!=null && !"".equals(delivery_state_code)) {
            	delivery=delivery_state_code.split(",");
			}
            
            
			
			String condition="STD_TYPE='N' and VALIDFLAG='1'";
			
			if (process_code!=null && !"".equals(process_code)) {
				condition+=" and PROCESS_CODE='"+process_code+"'";
			}
			
			List<ComMscStdDetection> detection=this.getMapper(ComMscStdDetectionMapper.class).findWithCondition(null, condition, null, 0, 0);
			
			ok :for (int i = 0; i < detection.size(); i++) {
				//过滤适用客户
				if (detection.get(i).getCustomerCode()!=null && !"".equals(detection.get(i).getCustomerCode())) {
					
					if (customer!=null && customer.length>0) {
						for (int j = 0; j < customer.length; j++) {
							if (detection.get(i).getCustomerCode().contains(customer[j])) {
								break;
							}
							
							if (j==customer.length-1) {
								detection.remove(i);
								i--;
								continue ok;
							}
						}
					}
					
					
				}
				
				
				if (detection.get(i).getProdCodeNk()!=null && !"".equals(detection.get(i).getProdCodeNk())) {
					
					if (prod!=null && prod.length>0) {
						for (int j = 0; j < prod.length; j++) {
							if (detection.get(i).getProdCodeNk().contains(prod[j])) {
								break;
							}
							
							if (j==prod.length-1) {
								detection.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
				if (detection.get(i).getSteelCodeNk()!=null && !"".equals(detection.get(i).getSteelCodeNk())) {
					
					if (steel!=null && steel.length>0) {
						for (int j = 0; j < steel.length; j++) {
							if (detection.get(i).getSteelCodeNk().contains(steel[j])) {
								break;
							}
							
							if (j==steel.length-1) {
								detection.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
				if (detection.get(i).getDeliveryStateCode()!=null && !"".equals(detection.get(i).getDeliveryStateCode())) {
					if (delivery!=null && delivery.length>0) {
						for (int j = 0; j < delivery.length; j++) {
							if (detection.get(i).getDeliveryStateCode().contains(delivery[j])) {
								break;
							}
							
							if (j==delivery.length-1) {
								detection.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
			}
			
			rm.setData(this.getMapper(ComMscStdDetectionMapper.class).doFindDeteBySql(process_code, customer_code, prod_code_nk, steel_code_nk, delivery_state_code));
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("查询失败");
		}
		return rm;
	}
	
	/**
	 * 探伤标准索引信息 查询
	 * @param cb
	 * @return
	 */
	public ResultModel doQueryComMscStdDetectionAll(CommonPage<ComMscStdDetection> cb) 
	{
		ResultModel rm = new ResultModel();	
		Integer pageSize= cb.getPageSize()*(cb.getPageIndex()-1);
		Integer pageIndex=cb.getPageSize()*(cb.getPageIndex()-1)+cb.getPageSize();
		ReturnMsg<List<ComMscStdDetection>> msg = new ReturnMsg<>();
		ComMscStdDetection dic=cb.getObject();
		try 
		{
			
			String validflag=dic.getValidflag();
			String std_type=dic.getStdType();
			String process_desc=dic.getProcessDesc();
			String customer_name=dic.getCustomerName();
			String delivery_state_desc=dic.getDeliveryStateDesc();
			String steel_name_nk=dic.getSteelNameNk();
			String prod_name_nk=dic.getProdNameNk();
			List<ComMscStdDetection> dics=this.getMapper(ComMscStdDetectionMapper.class).findByPageNk(validflag,prod_name_nk,steel_name_nk,delivery_state_desc,process_desc,customer_name,std_type,pageSize,pageIndex);
			List<ComMscStdDetection> total=this.getMapper(ComMscStdDetectionMapper.class).findByPageNk(validflag,prod_name_nk,steel_name_nk,delivery_state_desc,process_desc,customer_name,std_type,null,null);
			
			msg.setPageIndex(pageIndex);
			msg.setPageSize(pageSize);
			msg.setResult(dics);
			if (total!=null  && total.size()>0) {
				msg.setTotal(total.size());
			}else{
				msg.setTotal(0);
			}
			
			msg.setSuccess(true);
			
			rm.setData(msg);
			rm.setState("200");		
			
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	
	
	
}