/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.ServiceManagement.mapper.SlmServiceObjDeclareHeatMapper;
import market.Slm.ServiceManagement.mapper.SlmServiceObjHandMapper;
import market.Slm.ServiceManagement.service.SlmServiceObjHandService;
import market.Slm.ServiceManagement.vo.SlmServiceObjDeclareHeat;
import market.Slm.ServiceManagement.vo.SlmServiceObjHand;

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="SlmServiceObjHandService")
public class SlmServiceObjHandServiceImpl extends BaseServiceImpl<SlmServiceObjHand,Object> implements SlmServiceObjHandService {

	/**
	 * 查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmServiceObjHand> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjHand annex=sum.getObject();
		try{
			List<SlmServiceObjHand> list=this.getMapper(SlmServiceObjHandMapper.class).findWithCondition(annex, null, " hand_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<SlmServiceObjHand> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjHand hand=sum.getObject();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmServiceObjDeclareHeat heat=this.getMapper(SlmServiceObjDeclareHeatMapper.class).findById(hand.getHeat_no());
			if(heat==null){
				rm.setState("500");
				rm.setMsgInfo("请传入正确的申报编号heat_no");
				return rm;
			}
			if("4".equals(heat.getObjection_flag())){
				heat.setObjection_flag("5"); 			//已处理
				heat.setHand_man_no(userId);
				heat.setHand_man_name(userName);
				heat.setHand_time(new Date());
			}else{
				rm.setState("500");
				rm.setMsgInfo("请选择待处理的数据进行处理");
				return rm;
			}
			String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3);
			hand.setHand_no(id);
			hand.setPay_file_man_no(userId);
			hand.setPay_file_man_name(userName);
			hand.setPay_file_time(new Date());
			this.getMapper(SlmServiceObjHandMapper.class).insert(hand);
			//修改主表
			this.getMapper(SlmServiceObjDeclareHeatMapper.class).update(heat);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改
	 * */
	public ResultModel doUpdateWeb(CommonPage<SlmServiceObjHand> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjHand hand=sum.getObject();
		try{
			SlmServiceObjHand check=this.getMapper(SlmServiceObjHandMapper.class).findById(hand.getHand_no());
			if(check==null){
				rm.setState("500");
				rm.setMsgInfo("未找到异议处理信息");
				return rm;
			}
			SlmServiceObjDeclareHeat heat=this.getMapper(SlmServiceObjDeclareHeatMapper.class).findById(check.getHeat_no());
			if(heat==null){
				rm.setState("500");
				rm.setMsgInfo("未找到异议信息");
				return rm;
			}
			if("45".contains(heat.getObjection_flag())){
				this.getMapper(SlmServiceObjHandMapper.class).update(hand);
				//主表数据修改
				heat.setObjection_flag("5");
				this.getMapper(SlmServiceObjDeclareHeatMapper.class).update(heat);
			}else{
				rm.setState("500");
				rm.setMsgInfo("请修改待处理和已处理的数据");
				return rm;
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}

	/**
	 * 删除 物理删除
	 * */
	public ResultModel doDeleteWeb(CommonPage<SlmServiceObjHand> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjHand hand=sum.getObject();
		try{
			hand=this.getMapper(SlmServiceObjHandMapper.class).findById(hand.getHand_no());
			if(hand==null){
				rm.setState("500");
				rm.setMsgInfo("未找到异议处理信息");
				return rm;
			}
			SlmServiceObjDeclareHeat heat=this.getMapper(SlmServiceObjDeclareHeatMapper.class).findById(hand.getHeat_no());
			if(heat==null){
				rm.setState("500");
				rm.setMsgInfo("未找到异议信息");
				return rm;
			}
			if("5".contains(heat.getObjection_flag())){
				heat.setObjection_flag("4");
				this.getMapper(SlmServiceObjDeclareHeatMapper.class).update(heat);
			}else{
				rm.setState("500");
				rm.setMsgInfo("请删除状态为已处理的数据");
				return rm;
			}
			this.getMapper(SlmServiceObjHandMapper.class).delete(hand.getHand_no());
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}

	//=========================APP eason add 2019-07-15 ================================
	/**
	 * 查询
	 * */
	public ResultModel doQueryApp(CommonPage<SlmServiceObjHand> sum){
		ResultModel rm=new ResultModel();
		SlmServiceObjHand annex=sum.getObject();
		try{
			List<SlmServiceObjHand> list=this.getMapper(SlmServiceObjHandMapper.class).findWithCondition(annex, null, " hand_no desc", 0,0);
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
}
