/***文档注释***********************************************
 * 作者            ：      朱泽鹏
 * 创建日期     ：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.metallurgy.mapper.ComBaseStdMapper;
import market.Qms.metallurgy.mapper.ComMscStdCicMapper;
import market.Qms.metallurgy.mapper.ComMscStdDetectionMapper;
import market.Qms.metallurgy.mapper.ComMscStdPicMapper;
import market.Qms.metallurgy.mapper.ComMscStdSpecMapper;
import market.Qms.metallurgy.service.ComBaseStdService;
import market.Qms.metallurgy.vo.ComBaseStd;
import market.Qms.metallurgy.vo.ComMscStdCic;
import market.Qms.metallurgy.vo.ComMscStdDetection;
import market.Qms.metallurgy.vo.ComMscStdPic;
import market.Qms.metallurgy.vo.ComMscStdSpec;

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 = "ComBaseStdService")
public class ComBaseStdServiceImpl extends BaseServiceImpl<ComBaseStd, String>
		implements ComBaseStdService {
	/**
	 * 内控标准管理 查询
	 * 
	 * @param cb
	 * @return
	 */
	public ResultModel doQueryComBaseStd(CommonPage<ComBaseStd> cb) 
	{
		ResultModel rm = new ResultModel();
		int pageIndex = cb.getPageIndex();
		int pageSize = cb.getPageSize();
		PageModel<ComBaseStd> pm = new PageModel<ComBaseStd>(pageIndex,pageSize);
		try 
		{
			ComBaseStd cbs = cb.getObject();
			String condition = " 1=1 ";
			if (cbs != null && "N".equals(cbs.getStdType())) 
			{
				if ("".equals(cbs.getValidflag())) 
				{
					condition += " and std_name like '%" + cbs.getStdName()
							+ "%' " + "and std_type = 'N'";
				} else {
					condition += " and std_name like '%" + cbs.getStdName()
							+ "%' "
							+ " and validflag = '1' and std_type = 'N' ";
				}
				pm.setRows(this.getMapper(ComBaseStdMapper.class).findWithCondition(null, condition," CREATE_TIME desc ",
						(pageIndex - 1) * pageSize, pageSize));
				pm.setTotal(this.getMapper(ComBaseStdMapper.class).countWithCondition(null, condition));
				rm.setData(pm);
				rm.setState("200");
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;

	}

	/**
	 * 内控标准管理 保存
	 * 
	 * @param cbs
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doSaveConBaseStd(List<ComBaseStd> cbss) 
	{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
	
		Date date = new Date();
		try
		{
		if (cbss != null) 
		{
			for (ComBaseStd cbs : cbss) 
			{
				
				if (cbs.getStdCode() == null || "".equals(cbs.getStdCode())) 
				{
					Integer findname = this.getMapper(ComBaseStdMapper.class).findname(cbs.getStdName());
					if(findname > 0){
						rm.setState("500");
						rm.setMsgInfo("存在相同的标准名字!");
						return rm;
					}
					String maxNo = this.getMapper(ComBaseStdMapper.class).getMax();
					cbs.setStdCode(maxNo);
					cbs.setStdType("N");
					cbs.setCreateTime(date);
					cbs.setCreateName(userName);
					this.getMapper(ComBaseStdMapper.class).insert(cbs);
					int cnt = this.getMapper(ComBaseStdMapper.class).countWithCondition(null, "std_code = '"+cbs.getStdCode()+"'");
					if (cnt > 0) 
					{
						rm.setState("200");
						rm.setMsgInfo("新增成功!");
					} else {
						rm.setState("500");
						rm.setMsgInfo("新增失败!");
					}
				} else {
					ComMscStdCic ComMscStdCic = new ComMscStdCic();
//					ComMscStdCic.setStd_code_nk(cbs.getStdCode());
					ComMscStdCic.setStd_name_nk(cbs.getStdName());
					ComMscStdCic.setStd_type(cbs.getStdType());
				ComMscStdCic.setProcess_code(cbs.getProcessCode());
					List<ComMscStdCic> list1 = this.getMapper(ComMscStdCicMapper.class).find(ComMscStdCic, 0, 0);
					ComMscStdPic c2 = new ComMscStdPic();
					c2.setStdNameNk(cbs.getStdName());
//					c2.setStdNameNk(cbs.getStdName());
					c2.setStdType(cbs.getStdType());
					c2.setProcessCode(cbs.getProcessCode());
					List<ComMscStdPic> list2 = this.getMapper(ComMscStdPicMapper.class).find(c2, 0, 0);
					ComMscStdSpec c3 = new ComMscStdSpec();
//					c3.setSpecStdCode(specStdCode);
					c3.setSpecStdCode(cbs.getStdCode());
					c3.setStdType(cbs.getStdType());
					c3.setProcessCode(cbs.getProcessCode());
					List<ComMscStdSpec> list3 = this.getMapper(ComMscStdSpecMapper.class).find(c3, 0, 0);
					ComMscStdDetection c4 = new ComMscStdDetection();
					c4.setStdNameNk(cbs.getStdName());
					c4.setStdType(cbs.getStdType());
					c4.setProcessCode(cbs.getProcessCode());
					List<ComMscStdDetection> list4 = this.getMapper(ComMscStdDetectionMapper.class).find(c4, 0, 0);
					
					if (list1.isEmpty() && list2.isEmpty() && list3.isEmpty() && list4.isEmpty()) 
					{
						Integer findn = this.getMapper(ComBaseStdMapper.class).findn(cbs.getStdName(), cbs.getStdCode());
						if(findn > 0){
							rm.setMsgInfo("存在相同的内控标准号！");
							rm.setState("500");
							return rm;
						}
//						ComBaseStd ComBaseStd = new ComBaseStd();
//						ComBaseStd.setStdCode(cbs.getStdCode());
//						ComBaseStd.setUpdateName(userName);
//						ComBaseStd.setUpdateTime(date);
						cbs.setUpdateName(userName);
						cbs.setUpdateTime(date);
						this.getMapper(ComBaseStdMapper.class).update(cbs);
						rm.setState("200");
						rm.setMsgInfo("更新成功!");
					} else {
						rm.setState("500");
						rm.setMsgInfo("存在子记录不能修改!");
					}

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

	/**
	 * 内控标准管理 作废
	 * 
	 * @param stdcodes
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInvalidConBaseStd(List<String> stdcodes) 
	{
		ResultModel rm = new ResultModel();
		ComBaseStd ComBaseStd = new ComBaseStd();
		ComMscStdCic ComMscStdCic = new ComMscStdCic();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date date = new Date();
		try {
			if (stdcodes.size() > 0) 
			{
				for (String stdcode : stdcodes) 
				{
					ComBaseStd baseStd = this.getMapper(ComBaseStdMapper.class).findById(stdcode);
					if (baseStd.getValidflag().equals("1")) 
					{   
						ComMscStdCic.setStd_code_nk(stdcode);
						List<ComMscStdCic> list = this.getMapper(ComMscStdCicMapper.class).find(ComMscStdCic, 0, 0);
						Integer findvalid = this.getMapper(ComBaseStdMapper.class).findvalid(stdcode);
//						if (!list.isEmpty()) 
//						{
//							for (ComMscStdCic cic : list) {
//								if (States.RECOVER.getValue().equals(cic.getValidflag())) 
//								{
//									rm.setState("500");
//									rm.setMsgInfo("【" + stdcode+ "】子目录具有有效记录,主表不能废除!");
//									continue;
//								}
//							}
//						} else {
						if(findvalid > 0){
							rm.setState("500");
							rm.setMsgInfo("【" + stdcode+ "】子目录具有有效记录,主表不能废除!");
							return rm;
						}
							baseStd.setValidflag("0");
							baseStd.setDeleteName(userName);
							baseStd.setDeleteTime(date);
							this.getMapper(ComBaseStdMapper.class).update(baseStd);
							
						
					}
				}
			}
			rm.setState("200");
			rm.setMsgInfo("废除成功!");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("操作失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 内控标准管理 恢复
	 * 
	 * @param stdcodes
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doRecoverConBaseStd(List<String> stdcodes) 
	{
		ResultModel rm = new ResultModel();
		ComBaseStd ComBaseStd = new ComBaseStd();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try
		{
		if (stdcodes.size() > 0) 
		{
			for (String stdcode : stdcodes) 
			{
				ComBaseStd baseStd = this.getMapper(ComBaseStdMapper.class).findById(stdcode);
				if (baseStd.getValidflag().equals("0")) 
				{
					this.getMapper(ComBaseStdMapper.class).updatec(stdcode);
					rm.setState("200");
					rm.setMsgInfo("【" + stdcode + "】恢复成功!");
				} else {
					rm.setState("500");
					rm.setMsgInfo("【" + stdcode + "】已是有效状态,不能恢复!");
				}
			}
		}
	}catch (Exception ex) {
		rm.setState("500");
		rm.setMsgInfo("操作失败!");
		ex.printStackTrace();
	}
		return rm;

	}

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

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

		public String getValue() 
		{
			return this.value;
		}
	}

}
