/***文档注释***********************************************
 * 作者            ： 朱泽鹏
 * 创建日期     ：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.ComMscStdPhyMapper;
import market.Qms.metallurgy.mapper.ComMscStdPicMapper;
import market.Qms.metallurgy.service.ComMscStdPicService;
import market.Qms.metallurgy.vo.ComMscStdPhy;
import market.Qms.metallurgy.vo.ComMscStdPic;

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

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

				Integer count = this.getMapper(ComMscStdPhyMapper.class).chemCode(cics.getPic());

				cics.setCount(count);
			}
			pm.setRows(cmsp);
			pm.setTotal(this.getMapper(ComMscStdPicMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 材质标准索引信息 保存
	 * @param cms
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveComMscStdPic(List<ComMscStdPic> cmss) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try 
		{
			for (ComMscStdPic cms : cmss) 
			{   
				if(cms.getPic() != null)
				{
					ComMscStdPic ComMscStdPic = this.getMapper(ComMscStdPicMapper.class).findById(cms.getPic());
					if (ComMscStdPic != null) 
					{
						cms.setUpdateName(userName);
						cms.setUpdateTime(new Date());
						cms.setValidflag(Status.RECOVER.getValue());
						this.getMapper(ComMscStdPicMapper.class).update(cms);
						rm.setState("200");
						rm.setMsgInfo("更新成功!");
					}
				} else {
					String pic = this.getMapper(ComMscStdPicMapper.class).getMaxs();
					cms.setPic(pic);
					cms.setCreateName(userName);
					cms.setCreateTime(new Date());
					cms.setValidflag(Status.RECOVER.getValue());// 默认为 1
					this.getMapper(ComMscStdPicMapper.class).insert(cms);
					rm.setState("200");
					rm.setMsgInfo("新增成功!");
					rm.setData(pic);
				}
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;

	}
	
	
	/**
	 * 材质内控标准索引信息 保存
	 * @param cms
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveComMscStdPicN(List<ComMscStdPic> cmss) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try 
		{
			for (ComMscStdPic cms : cmss) 
			{   
				if(cms.getPic() != null)
				{
					ComMscStdPic ComMscStdPic = this.getMapper(ComMscStdPicMapper.class).findById(cms.getPic());
					if (ComMscStdPic != null) 
					{
						cms.setUpdateName(userName);
						cms.setUpdateTime(new Date());
						cms.setValidflag(Status.RECOVER.getValue());
						this.getMapper(ComMscStdPicMapper.class).update(cms);
						rm.setState("200");
						rm.setMsgInfo("更新成功!");
					}
				} else {
					String pic = this.getMapper(ComMscStdPicMapper.class).getMax();
					cms.setPic(pic);
					cms.setCreateName(userName);
					cms.setCreateTime(new Date());
					cms.setValidflag(Status.RECOVER.getValue());// 默认为 1
					this.getMapper(ComMscStdPicMapper.class).insert(cms);
					rm.setState("200");
					rm.setMsgInfo("新增成功!");
					rm.setData(pic);
				}
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;

	}

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

		Date date = new Date();
		try
		{
			if (cb.size() > 0) 
			{
				for (ComMscStdPic s : cb) 
				{
					ComMscStdPic csp = new ComMscStdPic();
					csp.setPic(s.getPic());
					ComMscStdPic com = this.getMapper(ComMscStdPicMapper.class).find(csp, 0, 0).get(0);
					if (com.getValidflag().equals("1")) 
					{
						com.setValidflag(Status.INVALID.getValue());
						ComMscStdPhy ComMscStdPhy = new ComMscStdPhy();
						ComMscStdPhy.setPic(com.getPic());
						List<ComMscStdPhy> list = this.getMapper(ComMscStdPhyMapper.class).find(ComMscStdPhy, 0, 0);
						com.setDeleteName(userName);
						com.setDeleteTime(date);
						this.getMapper(ComMscStdPicMapper.class).update(com);
						if (!list.isEmpty()) 
						{
							for (ComMscStdPhy c : list) 
							{
								if (c.getValidflag().equals("1")) 
								{
									c.setDeleteName(userName);
									c.setDeleteTime(date);
									c.setValidflag(Status.INVALID.getValue());
									this.getMapper(ComMscStdPhyMapper.class).update(c);
									rm.setState("200");
									rm.setMsgInfo("索引信息和标准信息  废除成功!");
								}
							}
						}
					} else {
						rm.setState("500");
						rm.setMsgInfo("已是废除状态!");
					}
				}
			}
			rm.setState("200");
			rm.setMsgInfo("索引信息 废除成功!");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 信息恢复B
	 * @param cb
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doRecover(List<ComMscStdPic> cb) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);

		try
		{
			if (cb.size() > 0) 
			{
				for (ComMscStdPic s : cb) 
				{
					ComMscStdPic csp = new ComMscStdPic();
					csp.setPic(s.getPic());
					ComMscStdPic com = this.getMapper(ComMscStdPicMapper.class).find(csp, 0, 0).get(0);
					if (com.getValidflag().equals("0")) 
					{
						com.setValidflag(Status.RECOVER.getValue());
						this.getMapper(ComMscStdPicMapper.class).updatepic(com.getPic());
						ComMscStdPhy ComMscStdPhy = new ComMscStdPhy();
						ComMscStdPhy.setPic(com.getPic());
						List<ComMscStdPhy> list = this.getMapper(ComMscStdPhyMapper.class).find(ComMscStdPhy, 0, 0);
						if (!list.isEmpty()) 
						{
							for (ComMscStdPhy c : list) 
							{
								if (c.getValidflag().equals("0"))
								{

									this.getMapper(ComMscStdPicMapper.class).updatephy(c.getPic());
									rm.setState("200");
									rm.setMsgInfo("索引信息和标准信息  恢复成功!");
								}
							}
						}

					} else {
						rm.setState("500");
						rm.setMsgInfo("已是恢复状态!");
					}
				}
			}
			rm.setState("200");
			rm.setMsgInfo("信息恢复成功!");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;
	}


	public ResultModel validate(CommonPage<ComMscStdPic> sum){ 
		ResultModel rm = new ResultModel();
		try{
			ComMscStdPic object = sum.getObject();
			if(object.getPic() != null){
				ComMscStdPic cp = new ComMscStdPic();
				cp.setPic(object.getPic());
				List<ComMscStdPic> list = this.getMapper(ComMscStdPicMapper.class).find(cp, 0, 0);
				if(list.isEmpty()){
					rm.setState("500");
					rm.setMsgInfo("该记录不存在");
				}else {
					ComMscStdPic 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;
	}
	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<ComMscStdPic> sum) {
		ResultModel rm =new ResultModel();
		try {
			ComMscStdPic 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'";
			
			if (process_code!=null && !"".equals(process_code)) {
				condition+=" and PROCESS_CODE='"+process_code+"' and VALIDFLAG='1'";
			}
			
			List<ComMscStdPic> pics=this.getMapper(ComMscStdPicMapper.class).findWithCondition(null, condition, null, 0, 0);
			
		ok:	for (int i = 0; i < pics.size(); i++) {
				//过滤适用客户
				if (pics.get(i).getCustomerCode()!=null && !"".equals(pics.get(i).getCustomerCode())) {
					
					if (customer!=null && customer.length>0) {
						for (int j = 0; j < customer.length; j++) {
							if (pics.get(i).getCustomerCode().contains(customer[j])) {
								break;
							}
							
							if (j==customer.length-1) {
								pics.remove(i);
								i--;
								continue ok;
							}
						}
					}
					
					
				}
				
				
				if (pics.get(i).getProdCodeNk()!=null && !"".equals(pics.get(i).getProdCodeNk())) {
					
					if (prod!=null && prod.length>0) {
						for (int j = 0; j < prod.length; j++) {
							if (pics.get(i).getProdCodeNk().contains(prod[j])) {
								break;
							}
							
							if (j==prod.length-1) {
								pics.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
				if (pics.get(i).getSteelCodeNk()!=null && !"".equals(pics.get(i).getSteelCodeNk())) {
					
					if (steel!=null && steel.length>0) {
						for (int j = 0; j < steel.length; j++) {
							if (pics.get(i).getSteelCodeNk().contains(steel[j])) {
								break;
							}
							
							if (j==steel.length-1) {
								pics.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
				if (pics.get(i).getDeliveryStateCode()!=null && !"".equals(pics.get(i).getDeliveryStateCode())) {
					if (delivery!=null && delivery.length>0) {
						for (int j = 0; j < delivery.length; j++) {
							if (pics.get(i).getDeliveryStateCode().contains(delivery[j])) {
								break;
							}
							
							if (j==delivery.length-1) {
								pics.remove(i);
								i--;
								continue ok;
							}
						}
					}
				}
				
			}
			

			rm.setData(pics);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("查询失败");
		}
		return rm;
	}
	
	/**
	 * 材质标准索引信息内控基础信息 查询
	 * @param cb
	 * @return
	 */
	public ResultModel doQueryComMscStdPicAll(CommonPage<ComMscStdPic> cb) 
	{
		ResultModel rm = new ResultModel();	
		Integer pageSize= cb.getPageSize()*(cb.getPageIndex()-1);
		Integer pageIndex=cb.getPageSize()*(cb.getPageIndex()-1)+cb.getPageSize();
		ReturnMsg<List<ComMscStdPic>> msg = new ReturnMsg<>();
		ComMscStdPic pic=cb.getObject();
		try 
		{
			String validflag=pic.getValidflag();
			String std_type=pic.getStdType();
			String process_desc=pic.getProcessDesc();
			String customer_name=pic.getCustomerName();
			String delivery_state_desc=pic.getDeliveryStateDesc();
			String steel_name_nk=pic.getSteelNameNk();
			String prod_name_nk=pic.getProdNameNk();
			List<ComMscStdPic> pics=this.getMapper(ComMscStdPicMapper.class).findByPageNk(validflag,prod_name_nk,steel_name_nk,delivery_state_desc,process_desc,customer_name,std_type,pageSize,pageIndex);
			List<ComMscStdPic> total=this.getMapper(ComMscStdPicMapper.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(pics);
			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;
	}


}