/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-01-09
 * 描述            ：加工计划信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Slm.ServiceManagement.impl;

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

import market.Com.pb.PbMethod;
import market.Com.pb.SpeOptBaseService;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.ServiceManagement.mapper.SlmServiceObjAnnexMapper;
import market.Slm.ServiceManagement.mapper.SlmServiceObjDeclareHeatMapper;
import market.Slm.ServiceManagement.mapper.SlmServiceObjDeclareLineMapper;
import market.Slm.ServiceManagement.service.SlmServiceObjDeclareLineService;
import market.Slm.ServiceManagement.vo.SlmServiceObjAnnex;
import market.Slm.ServiceManagement.vo.SlmServiceObjDeclareHeat;
import market.Slm.ServiceManagement.vo.SlmServiceObjDeclareLine;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;
import com.ibm.icu.text.SimpleDateFormat;

@RemoteService(path="SlmServiceObjDeclareLineService")
public class SlmServiceObjDeclareLineServiceImpl extends BaseServiceImpl<SlmServiceObjDeclareLine,Object> implements SlmServiceObjDeclareLineService {

	/**
	 * 查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmServiceObjDeclareLine> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjDeclareLine line=sum.getObject();
		try{
			List<SlmServiceObjDeclareLine> list=this.getMapper(SlmServiceObjDeclareLineMapper.class).findWithCondition(line, null, " line_no desc", 0,0);
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 新增
	 * */
	public ResultModel doAddWeb(CommonPage<SlmServiceObjDeclareLine> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjDeclareLine line=sum.getObject();
		try{
			if(line.getHeat_no()==null){
				rm.setState("500");
				rm.setMsgInfo("请选中异议信息再新增产品信息");
				return rm;
			}
			String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
			line.setLine_no(id);
			line.setCheck_flag("0");
			this.getMapper(SlmServiceObjDeclareLineMapper.class).insert(line);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 编辑
	 * */
	public ResultModel doUpdateWeb(CommonPage<SlmServiceObjDeclareLine> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjDeclareLine line=sum.getObject();
		try{
			SlmServiceObjDeclareLine check=this.getMapper(SlmServiceObjDeclareLineMapper.class).findById(line.getLine_no());
			if(check==null){
				rm.setState("500");
				rm.setMsgInfo("未找到该产品信息");
				return rm;
			}
			SlmServiceObjDeclareHeat heat=this.getMapper(SlmServiceObjDeclareHeatMapper.class).findById(check.getHeat_no());
			if(!"0".equals(heat.getObjection_flag())){
				rm.setState("500");
				rm.setMsgInfo("无法修改已提交过的数据");
				return rm;
			}
			this.getMapper(SlmServiceObjDeclareLineMapper.class).update(line);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("编辑失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 核实
	 * */
	public ResultModel doCheckWeb(CommonPage<SlmServiceObjDeclareLine> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjDeclareLine line=sum.getObject();
		try{
			if(line.getLine_no()==null){
				rm.setState("500");
				rm.setMsgInfo("请选中需要核实的产品信息");
				return rm;
			}
			SlmServiceObjDeclareLine check=this.getMapper(SlmServiceObjDeclareLineMapper.class).findById(line.getLine_no());
			if(check==null){
				rm.setState("500");
				rm.setMsgInfo("未找到该数据，请检查参数line_no");
				return rm;
			}
			SlmServiceObjDeclareHeat heat=this.getMapper(SlmServiceObjDeclareHeatMapper.class).findById(check.getHeat_no());
			if(heat==null){
				rm.setState("500");
				rm.setMsgInfo("未找到该异议头信息");
				return rm;
			}
			if("23".contains(heat.getObjection_flag())){
				line.setCheck_flag("1"); 		//已核实
				this.getMapper(SlmServiceObjDeclareLineMapper.class).update(line);
				//头表
				if("2".equals(heat.getObjection_flag())){
					SlmServiceObjDeclareLine param=new SlmServiceObjDeclareLine();
					param.setHeat_no(heat.getHeat_no());
					param.setCheck_flag("0");
					int count=this.getMapper(SlmServiceObjDeclareLineMapper.class).count(param);
					if(count>0){}else{
						heat.setObjection_flag("3"); 		//核实
						this.getMapper(SlmServiceObjDeclareHeatMapper.class).update(heat);
					}
				}
				
			}else{
				rm.setState("500");
				rm.setMsgInfo("该异议信息未到立案或核实状态，无法核实");
				return rm;
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 删除 物理删除
	 * 并删除附件
	 * */
	public ResultModel doDeleteWeb(CommonPage<SlmServiceObjDeclareLine> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjDeclareLine line=sum.getObject();
		try{
			line=this.getMapper(SlmServiceObjDeclareLineMapper.class).findById(line.getLine_no());
			if(line==null){
				rm.setState("500");
				rm.setMsgInfo("未找到异议信息");
				return rm;
			}
			SlmServiceObjDeclareHeat heat=this.getMapper(SlmServiceObjDeclareHeatMapper.class).findById(line.getHeat_no());
			if(!"0".equals(heat.getObjection_flag())){
				rm.setState("500");
				rm.setMsgInfo("无法删除已提交过的数据");
				return rm;
			}
			//删除附件信息
			SlmServiceObjAnnex annex=new SlmServiceObjAnnex();
			annex.setObject_no(line.getLine_no());
			List<SlmServiceObjAnnex> annex_list=this.getMapper(SlmServiceObjAnnexMapper.class).find(annex, 0, 0);
			
			this.getMapper(SlmServiceObjDeclareLineMapper.class).delete(line.getLine_no());
			for(SlmServiceObjAnnex annexs:annex_list){
				this.getBean(SpeOptBaseService.class).doDelete(annexs.getAnnex_path());
				this.getMapper(SlmServiceObjAnnexMapper.class).delete(annexs.getAnnex_no());
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}
	
	//=========================APP eason add 2019-07-15 ================================
	/**
	 * 查询
	 * */
	public ResultModel doQueryApp(CommonPage<SlmServiceObjDeclareLine> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjDeclareLine line=sum.getObject();
		try{
			List<SlmServiceObjDeclareLine> list=this.getMapper(SlmServiceObjDeclareLineMapper.class).findWithCondition(line, null, " line_no desc", 0,0);
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 新增
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddApp(CommonPage<SlmServiceObjDeclareLine> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjDeclareLine line=sum.getObject();
		try{
			if(line.getHeat_no()==null){
				rm.setState("500");
				rm.setMsgInfo("请选中异议信息再新增产品信息");
				return rm;
			}
			String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
			line.setLine_no(id);
			line.setCheck_flag("0");
			this.getMapper(SlmServiceObjDeclareLineMapper.class).insert(line);
			
			List<SlmServiceObjAnnex> lsoa=line.getAnnex();
			int i=1;
			String userId=(String)this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName=(String)this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			if(lsoa!=null&&lsoa.size()>0){
				for(SlmServiceObjAnnex annex:lsoa){
					annex.setObject_no(id);
					annex.setAnnex_no(id+String.format("%03d", i++));
					annex.setPrvd_man_no(userId);
					annex.setPrvd_man_name(userName);
					annex.setPrvd_time(new Date());
					if(annex.getAnnex_path()==null){
						throw new Exception("请传入附件路径");
					}
					if(!annex.getAnnex_path().contains("group1/M00")){
						annex.setAnnex_path(this.getBean(SpeOptBaseService.class).doAdd(annex.getAnnex_path()));			
					}
					this.getMapper(SlmServiceObjAnnexMapper.class).insert(annex);
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 删除
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doDeleteApp(CommonPage<SlmServiceObjDeclareLine> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjDeclareLine line=sum.getObject();
		
		SlmServiceObjDeclareHeat heat=this.getMapper(SlmServiceObjDeclareHeatMapper.class).findById(line.getHeat_no());
		if(!"0".equals(heat.getObjection_flag())){
			rm.setState("500");
			rm.setMsgInfo("无法删除已提交过的数据");
			return rm;
		}
		this.getMapper(SlmServiceObjDeclareLineMapper.class).delete(line.getLine_no());
		
		//删除附件信息
		SlmServiceObjAnnex annex=new SlmServiceObjAnnex();
		annex.setObject_no(line.getLine_no());
		List<SlmServiceObjAnnex> annex_list=this.getMapper(SlmServiceObjAnnexMapper.class).find(annex, 0, 0);
		for(SlmServiceObjAnnex annexs:annex_list){
			if(annexs!=null){
				this.getBean(SpeOptBaseService.class).doDelete(annexs.getAnnex_path());
			}
			this.getMapper(SlmServiceObjAnnexMapper.class).delete(annexs.getAnnex_no());
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 编辑
	 * */
	public ResultModel doUpdateApp(CommonPage<SlmServiceObjDeclareLine> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjDeclareLine line=sum.getObject();
		try{
			SlmServiceObjDeclareLine check=this.getMapper(SlmServiceObjDeclareLineMapper.class).findById(line.getLine_no());
			if(check==null){
				rm.setState("500");
				rm.setMsgInfo("未找到该产品信息");
				return rm;
			}
			SlmServiceObjDeclareHeat heat=this.getMapper(SlmServiceObjDeclareHeatMapper.class).findById(check.getHeat_no());
			if(!"0".equals(heat.getObjection_flag())){
				rm.setState("500");
				rm.setMsgInfo("无法修改已提交过的数据");
				return rm;
			}
			this.getMapper(SlmServiceObjDeclareLineMapper.class).update(line);
			
			String userId=(String)this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName=(String)this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			if(check.getAnnex()!=null&&check.getAnnex().size()>0) {
				this.getMapper(SlmServiceObjAnnexMapper.class).doDelete(check.getLine_no());
				int i=1;
				for(SlmServiceObjAnnex annex: check.getAnnex()) {
					annex.setAnnex_no(check.getLine_no()+String.format("%03d", i++));
					annex.setObject_no(check.getLine_no());
					annex.setPrvd_man_no(userId);
					annex.setPrvd_man_name(userName);
					annex.setPrvd_time(new Date());
					if(annex.getAnnex_path()==null){
						throw new Exception("请传入附件路径");
					}
					if(!annex.getAnnex_path().contains("group1/M00")){
						annex.setAnnex_path(this.getBean(SpeOptBaseService.class).doAdd(annex.getAnnex_path()));			
					}
					this.getMapper(SlmServiceObjAnnexMapper.class).insert(annex);
				}
			}
			
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("编辑失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
}

