/***文档注释***********************************************
 * 作者            ： 朱泽鹏
 * 创建日期     ：2018-02-6
 * 描述            ：化学元素标准索引表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Qms.metallurgy.impl;
import java.util.Date;
import java.util.List;

import market.Com.mapper.ComBaseSampleMapper;
import market.Com.vo.ComBaseSample;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComMscStdChemMapper;
import market.Qms.metallurgy.mapper.ComMscStdCicMapper;
import market.Qms.metallurgy.service.ComMscStdCicService;
import market.Qms.metallurgy.vo.ComMscStdChem;
import market.Qms.metallurgy.vo.ComMscStdCic;

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 = "ComMscStdCicService")
public class ComMscStdCicServiceImpl extends BaseServiceImpl<ComMscStdCic, Object> implements ComMscStdCicService {
	/**
	 * 成分内控 查询
	 * @param stdcodenk(父级 stdcode) (内控标准号)
	 * @return
	 */
	public ResultModel doQueryComBaseCic(CommonPage<ComMscStdCic> cb) 
	{
		ResultModel rm = new ResultModel();
		int pageIndex = cb.getPageIndex();
		int pageSize = cb.getPageSize();
		PageModel<ComMscStdCic> pm = new PageModel<ComMscStdCic>(pageIndex,pageSize);

		try 
		{
			String condition = null;
			ComMscStdCic cbs = cb.getObject();
			// 内控标记
			if ("".equals(cb.getMemo1())) 
			{
				condition = " std_code_nk = '" + cbs.getStd_code_nk() + "'";

			} else {
				condition = " std_code_nk = '" + cbs.getStd_code_nk()
				+ "' and VALIDFLAG <> 0";
			}
			List<ComMscStdCic> cmsc = this.getMapper(ComMscStdCicMapper.class).findWithCondition(null, condition, " CREATE_TIME desc ",
					(pageIndex - 1) * pageSize, pageSize);
			// 设置元素个数
			for (ComMscStdCic cics : cmsc) 
			{
				// 有效记录个数
				ComMscStdChem c = new ComMscStdChem();
				c.setCic(cics.getCic());
				Integer count = this.getMapper(ComMscStdChemMapper.class).count(c);

				cics.setCount(count);

			}
			pm.setRows(cmsc);
			pm.setTotal(this.getMapper(ComMscStdCicMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	/**
	 * 成分内控 新增或修改
	 * @param cb
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveCic(List<ComMscStdCic> cbs) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			for (ComMscStdCic cb : cbs) 
			{   
				if (cb.getCic() != null || "".equals(cb.getCic())) 
				{
					cb.setUpdate_name(userName);
					cb.setUpdate_time(new Date());
					this.getMapper(ComMscStdCicMapper.class).update(cb);
					rm.setState("200");
					rm.setMsgInfo("修改成功!");
				} else {
					String cic = this.getMapper(ComMscStdCicMapper.class).getMaxNo();
					cb.setCic(cic);
					cb.setStd_type("N");
					cb.setCreate_name(userName);
					cb.setCreate_time(new Date());
					this.getMapper(ComMscStdCicMapper.class).insert(cb);
					rm.setState("200");
					rm.setMsgInfo("新增成功!");
					rm.setData(cic);
				}
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	
	
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveCicG(List<ComMscStdCic> cbs) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			for (ComMscStdCic cb : cbs) 
			{   
				if (cb.getCic() != null || "".equals(cb.getCic())) 
				{
					cb.setUpdate_name(userName);
					cb.setUpdate_time(new Date());
					this.getMapper(ComMscStdCicMapper.class).update(cb);
					rm.setState("200");
					rm.setMsgInfo("修改成功!");
				} else {
					//String cic = this.getMapper(ComMscStdCicMapper.class).getMaxNo();
					String cic = this.getMapper(ComMscStdCicMapper.class).getmaxNOs();
					cb.setCic(cic);
					cb.setStd_type("G");
					cb.setCreate_name(userName);
					cb.setCreate_time(new Date());
					this.getMapper(ComMscStdCicMapper.class).insert(cb);
					rm.setState("200");
					rm.setMsgInfo("新增成功!");
					rm.setData(cic);
				}
			}
		} 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<ComMscStdCic> cb) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ComMscStdChem ComMscStdChem = new ComMscStdChem();
		Date date = new Date();
		try {
			if (cb.size() > 0) 
			{
				for (ComMscStdCic com : cb) 
				{
					if (com.getValidflag().equals("1")) 
					{
						com.setValidflag("0");
						ComMscStdChem.setCic(com.getCic());
						com.setDelete_name(userName);
						com.setDelete_time(date);
						List<ComMscStdChem> list = this.getMapper(ComMscStdChemMapper.class).find(ComMscStdChem,0, 0);
						this.getMapper(ComMscStdCicMapper.class).update(com);
						if (!list.isEmpty()) 
						{
							for (ComMscStdChem c : list) 
							{
								if (c.getValidflag().equals("1")) 
								{
									//									c.setUpdateName(userName);
									//									c.setUpdateTime(new Date());
									//									c.setValidflag(Status.INVALID.getValue());
									c.setDeleteName(userName);
									c.setDeleteTime(date);
									c.setValidflag("0");
									this.getMapper(ComMscStdChemMapper.class).update(c);
									rm.setState("200");
									rm.setMsgInfo("索引信息和标准信息  废除成功!");
								}
							}
						}
						if(Status.INVALID.getValue().equals(this.getMapper(ComMscStdCicMapper.class).findById(com.getCic()).getValidflag()))
						{
							rm.setState("200");
							rm.setMsgInfo("索引信息 废除成功!");
						}
					} else {
						rm.setState("500");
						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 doRecoverCicAndChem(List<ComMscStdCic> cb) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ComMscStdChem ComMscStdChem = new ComMscStdChem();
		try 
		{
			if (cb.size() > 0) 
			{
				for (ComMscStdCic com : cb) 
				{
					if (com.getValidflag().equals("0")) 
					{
						com.setValidflag(Status.RECOVER.getValue());
						this.getMapper(ComMscStdCicMapper.class).updatef(com.getCic());
						ComMscStdChem.setCic(com.getCic());
						List<ComMscStdChem> list = this.getMapper(ComMscStdChemMapper.class).find(ComMscStdChem,0, 0);
						if (!list.isEmpty()) 
						{
							for (ComMscStdChem c : list) 
							{
								if (c.getValidflag().equals("0")) 
								{

									this.getMapper(ComMscStdChemMapper.class).updatec(c.getCic(), c.getIndexSeq());
									rm.setState("200");
									rm.setMsgInfo("索引信息和标准信息  恢复成功!");
								}
							}
						}				
						if(Status.RECOVER.getValue().equals(this.getMapper(ComMscStdCicMapper.class).findById(com.getCic()).getValidflag()))
						{
							rm.setState("200");
							rm.setMsgInfo("索引信息 恢复成功!");
						}
					} else {
						rm.setState("500");
						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 doQuerySample(CommonPage<ComBaseSample> cb) 
	{
		ResultModel rm = new ResultModel();
		int pageIndex = cb.getPageIndex();
		int pageSize = cb.getPageSize();
		PageModel<ComBaseSample> pm = new PageModel<ComBaseSample>(pageIndex,pageSize);
		try{
			List<ComBaseSample> list = this.getMapper(ComBaseSampleMapper.class).findWithCondition(null, null, null, 0, 0);
			pm.setRows(list);
			pm.setTotal(list.size());
			rm.setData(pm);
			rm.setMsgInfo("查询成功！");
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("服务器内部异常!");
			e.printStackTrace();
		}

		return null;

	}
	public ResultModel validate(CommonPage<ComMscStdCic> sum){ 
		ResultModel rm = new ResultModel();
		try{
			ComMscStdCic object = sum.getObject();
			if(object.getCic() != null){
				ComMscStdCic cp = new ComMscStdCic();
				cp.setCic(object.getCic());
				List<ComMscStdCic> list = this.getMapper(ComMscStdCicMapper.class).find(cp, 0, 0);
				if(list.isEmpty()){
					rm.setState("500");
					rm.setMsgInfo("该记录不存在");
				}else {
					ComMscStdCic stdPic = list.get(0);
					if(stdPic.getValidflag().equals("0")){
						rm.setState("500");
						rm.setMsgInfo("该记录无效！");
					}else {
						rm.setState("200");
						rm.setMsgInfo("验证通过！");
					}

				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("该记录无索引号！");
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 冶金规范内控标准
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryBySql(CommonPage<ComMscStdCic> sum) {
		ResultModel rm =new ResultModel();
		try {
			ComMscStdCic com =sum.getObject();
			String process_code =com.getProcess_code();
			String customer_code =com.getCustomer_code();
			String prod_code_nk =com.getProd_code_nk();
			String steel_code_nk =com.getSteel_code_nk();
			String grade_code=com.getGrade_code();
			String delivery_state_code =com.getDelivery_state_code();
			
			String condition="STD_TYPE='N' and VALIDFLAG='1'";
			if (process_code!=null && !"".equals(process_code)) {
					condition+=" and PROCESS_CODE='"+process_code+"'";
			}
			
			List<ComMscStdCic> cics=this.getMapper(ComMscStdCicMapper.class).findWithCondition(null, condition, null, 0, 0);
			
			String[] customer=null;
			String[] prod=null;
			String[] steel=null;
			String[] delivery=null;
			String[] grade=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 (grade_code!=null && !"".equals(grade_code)) {
            	grade=grade_code.split(",");
			}
			
			
         ok:   for (int i = 0; i < cics.size(); i++) {
				//过滤适用客户
				if (cics.get(i).getCustomer_code()!=null && !"".equals(cics.get(i).getCustomer_code())) {
					
					if (customer!=null && customer.length>0) {
						for (int j = 0; j < customer.length; j++) {
							if (cics.get(i).getCustomer_code().contains(customer[j])) {
								break;
							}
							
							if (j==customer.length-1) {
								cics.remove(i);
								i--;
								continue ok;
							}
						}
					}
					
					
				}
				
				
				if (cics.get(i).getProd_code_nk()!=null && !"".equals(cics.get(i).getProd_code_nk())) {
					
					if (prod!=null && prod.length>0) {
						for (int j = 0; j < prod.length; j++) {
							if (cics.get(i).getProd_code_nk().contains(prod[j])) {
								break;
							}
							
							if (j==prod.length-1) {
								cics.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
				if (cics.get(i).getSteel_code_nk()!=null && !"".equals(cics.get(i).getSteel_code_nk())) {
					
					if (steel!=null && steel.length>0) {
						for (int j = 0; j < steel.length; j++) {
							if (cics.get(i).getSteel_code_nk().contains(steel[j])) {
								break;
							}
							
							if (j==steel.length-1) {
								cics.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
				if (cics.get(i).getDelivery_state_code()!=null && !"".equals(cics.get(i).getDelivery_state_code())) {
					if (delivery!=null && delivery.length>0) {
						for (int j = 0; j < delivery.length; j++) {
							if (cics.get(i).getDelivery_state_code().contains(delivery[j])) {
								break;
							}
							
							if (j==delivery.length-1) {
								cics.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
				
				if (cics.get(i).getGrade_code()!=null && !"".equals(cics.get(i).getGrade_code())) {
					if (grade!=null && grade.length>0) {
						for (int j = 0; j < grade.length; j++) {
							if (cics.get(i).getGrade_code().contains(grade[j])) {
								break;
							}
							
							if (j==grade.length-1) {
								cics.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
			}	
			rm.setData(cics);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("查询失败");
		}
		return rm;
	}
	

	/**
	 * 成分内控基础配置 查询
	 * @param stdcodenk(父级 stdcode) (内控标准号)
	 * @return
	 */
	public ResultModel doQueryComBaseCicAll(CommonPage<ComMscStdCic> cb) 
	{
		ResultModel rm = new ResultModel();	
		Integer pageSize= cb.getPageSize()*(cb.getPageIndex()-1);
		Integer pageIndex=cb.getPageSize()*(cb.getPageIndex()-1)+cb.getPageSize();
		ReturnMsg<List<ComMscStdCic>> msg = new ReturnMsg<>();
		ComMscStdCic cic=cb.getObject();

		try 
		{
			String validflag=cic.getValidflag();
			String std_type=cic.getStd_type();
			String process_desc=cic.getProcess_desc();
			String grade_name=cic.getGrade_name();
			String customer_name=cic.getCustomer_name();
			String delivery_state_desc=cic.getDelivery_state_desc();
			String steel_name_nk=cic.getSteel_name_nk();
			String prod_name_nk=cic.getProd_name_nk();
			List<ComMscStdCic> cics=this.getMapper(ComMscStdCicMapper.class).findByPageNk(validflag,prod_name_nk,steel_name_nk,delivery_state_desc,process_desc,grade_name,customer_name,std_type,pageSize,pageIndex);
			List<ComMscStdCic> total=this.getMapper(ComMscStdCicMapper.class).findByPageNk(validflag,prod_name_nk,steel_name_nk,delivery_state_desc,process_desc,grade_name,customer_name,std_type,null,null);
			msg.setPageIndex(pageIndex);
			msg.setPageSize(pageSize);
			msg.setResult(cics);
			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;
	}
	
	                           
}