/***文档注释***********************************************
 * 作者            ： 朱泽鹏
 * 创建日期     ：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.ComMscStdSpecCMapper;
import market.Qms.metallurgy.mapper.ComMscStdSpecMapper;
import market.Qms.metallurgy.service.ComMscStdSpecService;
import market.Qms.metallurgy.vo.ComMscStdSpec;
import market.Qms.metallurgy.vo.ComMscStdSpecC;

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 = "ComMscStdSpecService")
public class ComMscStdSpecServiceImpl extends BaseServiceImpl<ComMscStdSpec, String> implements ComMscStdSpecService {
	/**
	 * 公差标准索 查询
	 * @param cb
	 * @return
	 */
	public ResultModel doQueryComMscStdSpec(CommonPage<ComMscStdSpec> cb) 
	{
		ResultModel rm = new ResultModel();
		int pageIndex = cb.getPageIndex();
		int pageSize = cb.getPageSize();
		PageModel<ComMscStdSpec> pm = new PageModel<ComMscStdSpec>(pageIndex,pageSize);
		try 
		{   
			String condition = null;
			ComMscStdSpec cbs = cb.getObject();
			if ("".equals(cb.getMemo1())) 
			{
				condition = " std_code = '" + cbs.getStdCode() + "'";

			} else {
				condition = " std_code = '" + cbs.getStdCode()
				+ "' and VALIDFLAG <> 0";
			}     									 
			List<ComMscStdSpec> cmss = this.getMapper(ComMscStdSpecMapper.class).findWithCondition(null, condition, " CREATE_TIME desc ",(pageIndex - 1) * pageSize, pageSize);
			// 设置元素个数
			for (ComMscStdSpec cics : cmss) 
			{
				// 有效记录个数
				ComMscStdSpecC cc = new ComMscStdSpecC();
				cc.setSic(cics.getSic());
				Integer count = this.getMapper(ComMscStdSpecCMapper.class).count(cc);
				//						chemCode(cics.getSic());

				cics.setCount(count);

			}
			pm.setRows(cmss);
			pm.setTotal(this.getMapper(ComMscStdSpecMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");
			rm.setMsgInfo("查询成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 公差标准索 保存
	 * @param cms
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveComMscStdSpec(List<ComMscStdSpec> cmss)
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date date = new Date();
		try
		{
			for (ComMscStdSpec cms : cmss) 
			{   
				if(cms.getSic() != null)
				{
					ComMscStdSpec com = this.getMapper(ComMscStdSpecMapper.class).findById(cms.getSic());
					if (com != null) 
					{
						cms.setUpdateName(userName);
						cms.setUpdateTime(date);
						this.getMapper(ComMscStdSpecMapper.class).update(cms);
						rm.setState("200");
						rm.setMsgInfo("更新成功");
					}
				}else {
					// 获取主键
					String sic = this.getMapper(ComMscStdSpecMapper.class).getMaxs();
					cms.setSic(sic);
					cms.setCreateName(userName);
					cms.setCreateTime(new Date());
					cms.setValidflag(Status.RECOVER.getValue());
					if (sic != null) 
					{
						this.getMapper(ComMscStdSpecMapper.class).insert(cms);
						rm.setState("200");
						rm.setMsgInfo("新增成功");
						rm.setData(sic);
					}
				}
			}
		}catch (Exception ex){
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			throw(ex);
		}
		return rm;

	}

	/**
	 * 信息废除A
	 * @param cb
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInvalidA(List<ComMscStdSpec> cb) throws Exception
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date date = new Date();
		try
		{
			if (cb.size() > 0) 
			{
				for (ComMscStdSpec css : cb) 
				{
					ComMscStdSpec st = new ComMscStdSpec();
					st.setSic(css.getSic());
					ComMscStdSpec com = this.getMapper(ComMscStdSpecMapper.class).find(st, 0, 0).get(0);
					if (com.getValidflag().equals("1")) 
					{
						com.setValidflag("0");
						com.setDeleteName(userName);
						com.setDeleteTime(date);
						ComMscStdSpecC ComMscStdSpecC = new ComMscStdSpecC();
						ComMscStdSpecC.setSic(com.getSic());
						List<ComMscStdSpecC> list = this.getMapper(ComMscStdSpecCMapper.class).find(ComMscStdSpecC, 0,0);
						this.getMapper(ComMscStdSpecMapper.class).update(com);
						if (!list.isEmpty()) 
						{
							for (ComMscStdSpecC c : list) 
							{
								if (c.getValidflag().equals("1")) 
								{
									c.setDeleteName(userName);
									c.setDeleteTime(date);
									c.setValidflag("0");
									this.getMapper(ComMscStdSpecCMapper.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;
	}
	
	/**
	 * 公差标内控准索 保存
	 * @param cms
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveComMscStdSpecN(List<ComMscStdSpec> cmss)
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date date = new Date();
		try
		{
			for (ComMscStdSpec cms : cmss) 
			{   
				if(cms.getSic() != null)
				{
					ComMscStdSpec com = this.getMapper(ComMscStdSpecMapper.class).findById(cms.getSic());
					if (com != null) 
					{
						cms.setUpdateName(userName);
						cms.setUpdateTime(date);
						this.getMapper(ComMscStdSpecMapper.class).update(cms);
						rm.setState("200");
						rm.setMsgInfo("更新成功");
					}
				}else {
					// 获取主键
					String sic = this.getMapper(ComMscStdSpecMapper.class).getMax();
					cms.setSic(sic);
					cms.setCreateName(userName);
					cms.setCreateTime(new Date());
					cms.setValidflag(Status.RECOVER.getValue());
					if (sic != null) 
					{
						this.getMapper(ComMscStdSpecMapper.class).insert(cms);
						rm.setState("200");
						rm.setMsgInfo("新增成功");
						rm.setData(sic);
					}
				}
			}
		}catch (Exception ex){
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			throw(ex);
		}
		return rm;

	}

	/**
	 * 信息恢复A
	 * @param cb
	 * @return
	 */
	public ResultModel doRecoverA(List<ComMscStdSpec> cb) throws Exception
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ComMscStdSpecC ComMscStdSpecC = new ComMscStdSpecC();
		try
		{
			if (cb.size() > 0) 
			{
				for (ComMscStdSpec css : cb) 
				{
					ComMscStdSpec st = new ComMscStdSpec();
					st.setSic(css.getSic());
					ComMscStdSpec com = this.getMapper(ComMscStdSpecMapper.class).find(st, 0, 0).get(0);
					if (com.getValidflag().equals("0")) 
					{

						this.getMapper(ComMscStdSpecMapper.class).updates(com.getSic());;
						ComMscStdSpecC.setSic(com.getSic());
						List<ComMscStdSpecC> list = this.getMapper(ComMscStdSpecCMapper.class).find(ComMscStdSpecC, 0,0);
						if (!list.isEmpty()) 
						{
							for (ComMscStdSpecC c : list) 
							{
								if (c.getValidflag().equals("0")) 
								{

									this.getMapper(ComMscStdSpecMapper.class).updatec(c.getSic(), 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<ComMscStdSpec> sum) {
		ResultModel rm =new ResultModel();
		try {
			ComMscStdSpec 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 precisiconTypeCode=com.getPrecisiconTypeCode();
			
			String[] customer=null;
			String[] prod=null;
			String[] steel=null;
			String[] delivery=null;
			String[] precisicon=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(",");
			}
            
            if (precisiconTypeCode!=null && !"".equals(precisiconTypeCode)) {
            	precisicon=precisiconTypeCode.split(",");
			}
            

			String condition="STD_TYPE='N' and VALIDFLAG='1'";
			if (process_code!=null && !"".equals(process_code)) {
					condition+=" and PROCESS_CODE='"+process_code+"'";
			}
				
			List<ComMscStdSpec> specs=this.getMapper(ComMscStdSpecMapper.class).findWithCondition(null, condition, null, 0, 0);
			
			
         ok:   for (int i = 0; i < specs.size(); i++) {
				//过滤适用客户
				if (specs.get(i).getCustomerCode()!=null && !"".equals(specs.get(i).getCustomerCode())) {
					
					if (customer!=null && customer.length>0) {
						for (int j = 0; j < customer.length; j++) {
							if (specs.get(i).getCustomerCode().contains(customer[j])) {
								break;
							}
							
							if (j==customer.length-1) {
								specs.remove(i);
								i--;
								continue ok;
							}
						}
					}
					
					
				}
				
				
				if (specs.get(i).getProdCodeNk()!=null && !"".equals(specs.get(i).getProdCodeNk())) {
					
					if (prod!=null && prod.length>0) {
						for (int j = 0; j < prod.length; j++) {
							if (specs.get(i).getProdCodeNk().contains(prod[j])) {
								break;
							}
							
							if (j==prod.length-1) {
								specs.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
				if (specs.get(i).getSteelCodeNk()!=null && !"".equals(specs.get(i).getSteelCodeNk())) {
					
					if (steel!=null && steel.length>0) {
						for (int j = 0; j < steel.length; j++) {
							if (specs.get(i).getSteelCodeNk().contains(steel[j])) {
								break;
							}
							
							if (j==steel.length-1) {
								specs.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
				if (specs.get(i).getDeliveryStateCode()!=null && !"".equals(specs.get(i).getDeliveryStateCode())) {
					if (delivery!=null && delivery.length>0) {
						for (int j = 0; j < delivery.length; j++) {
							if (specs.get(i).getDeliveryStateCode().contains(delivery[j])) {
								break;
							}
							
							if (j==delivery.length-1) {
								specs.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
				
				if (specs.get(i).getPrecisiconTypeCode()!=null && !"".equals(specs.get(i).getPrecisiconTypeCode())) {
					if (precisicon!=null && precisicon.length>0) {
						for (int j = 0; j < precisicon.length; j++) {
							if (specs.get(i).getPrecisiconTypeCode().contains(precisicon[j])) {
								break;
							}
							
							if (j==precisicon.length-1) {
								specs.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
			}
			
			
			
			
			
			rm.setData(specs);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("查询失败");
		}
		return rm;
	}
	
	
	/**
	 * 公差标准索内控基础配置 查询
	 * @param cb
	 * @return
	 */
	public ResultModel doQueryComMscStdSpecAll(CommonPage<ComMscStdSpec> cb) 
	{
		ResultModel rm = new ResultModel();	
		Integer pageSize= cb.getPageSize()*(cb.getPageIndex()-1);
		Integer pageIndex=cb.getPageSize()*(cb.getPageIndex()-1)+cb.getPageSize();
		ReturnMsg<List<ComMscStdSpec>> msg = new ReturnMsg<>();
		ComMscStdSpec sic=cb.getObject();
		if (sic==null) {
			rm.setMsgInfo("请传入公差索引号");
			rm.setState("500");
			return rm;
		}
		try 
		{
			String validflag=sic.getValidflag();
			String std_type=sic.getStdType();
			String process_desc=sic.getProcessDesc();
			String customer_name=sic.getCustomerName();
			String delivery_state_desc=sic.getDeliveryStateDesc();
			String steel_name_nk=sic.getSteelNameNk();
			String prod_name_nk=sic.getProdNameNk();
			String precisicon_type_desc=sic.getPrecisiconTypeDesc();
			List<ComMscStdSpec> sics=this.getMapper(ComMscStdSpecMapper.class).findByPageNk(validflag,prod_name_nk,steel_name_nk,delivery_state_desc,process_desc,customer_name,std_type,precisicon_type_desc,pageSize,pageIndex);
			List<ComMscStdSpec> total=this.getMapper(ComMscStdSpecMapper.class).findByPageNk(validflag,prod_name_nk,steel_name_nk,delivery_state_desc,process_desc,customer_name,std_type,precisicon_type_desc,null,null);
			msg.setPageIndex(pageIndex);
			msg.setPageSize(pageSize);
			msg.setResult(sics);
			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;
	}
	
	
	
}