/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-03-11
 * 描述            ：销售年度计划信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Slm.PlanManagement.impl;
import java.util.Date;
import java.util.List;

import market.Com.pb.DoubleUtil;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.PlanManagement.mapper.SlmMonthSalePlanHeatMapper;
import market.Slm.PlanManagement.mapper.SlmMonthSalePlanLineMapper;
import market.Slm.PlanManagement.service.SlmMonthSalePlanHeatService;
import market.Slm.PlanManagement.vo.SlmMonthSalePlanHeat;
import market.Slm.PlanManagement.vo.SlmMonthSalePlanLine;

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="SlmMonthSalePlanHeatService")
public class SlmMonthSalePlanHeatServiceImpl extends BaseServiceImpl<SlmMonthSalePlanHeat,Object> implements SlmMonthSalePlanHeatService {

	/**
	 * 查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmMonthSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanHeat heat=sum.getObject();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		try{
			List<SlmMonthSalePlanHeat> list=this.getMapper(SlmMonthSalePlanHeatMapper.class).findWithCondition(heat, null, " plan_heat_no desc", (pageIndex-1)*pageSize,pageSize);
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 查询 跟踪
	 * */
	public ResultModel doQueryTrackWeb(CommonPage<SlmMonthSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanHeat heat=sum.getObject();
		try{
			List<SlmMonthSalePlanHeat> list=this.getMapper(SlmMonthSalePlanHeatMapper.class).doQueryTrack(heat);
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 新增
	 * */
	public ResultModel doAddWeb(CommonPage<SlmMonthSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanHeat heat=sum.getObject();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			//查重复
			SlmMonthSalePlanHeat check=new SlmMonthSalePlanHeat();
			check.setPlan_year(heat.getPlan_year());
			check.setPlan_month(heat.getPlan_month());
			check.setTrade_type_no(heat.getTrade_type_no());
			check.setSale_org_no(heat.getSale_org_no());
			check.setValid_flag("1");
			int cou=this.getMapper(SlmMonthSalePlanHeatMapper.class).count(check);
			if(cou>0){
				rm.setState("500");
				rm.setMsgInfo("已存在相同月度计划");
				return rm;
			}
			
			
			String year=new SimpleDateFormat("yyyyMM").format(new Date());
			String maxId=this.getMapper(SlmMonthSalePlanHeatMapper.class).findMaxId(year);
			if(maxId==null){
				year+="0001";
			}else{
				maxId=maxId.substring(6,maxId.length());
				int count=Integer.valueOf(maxId)+1;
				year+=String.format("%04d", count);
			}
			heat.setPlan_heat_no(year);
			heat.setCreate_man_no(userId);
			heat.setCreate_man_name(userName);
			heat.setCreate_time(new Date());
			heat.setPlan_flag("0"); 		//待提交
			heat.setValid_flag("1"); 			//有效
			this.getMapper(SlmMonthSalePlanHeatMapper.class).insert(heat);
			
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改
	 * */
	public ResultModel doUpdateWeb(CommonPage<SlmMonthSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanHeat heat=sum.getObject();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmMonthSalePlanHeat check=this.getMapper(SlmMonthSalePlanHeatMapper.class).findById(heat.getPlan_heat_no());
			if(check!=null){
				if("1".equals(check.getValid_flag())&&"0".equals(check.getPlan_flag())){
					//是否重复
					SlmMonthSalePlanHeat rein=new SlmMonthSalePlanHeat();
					rein.setPlan_year(heat.getPlan_year());
					rein.setPlan_month(heat.getPlan_month());
					rein.setTrade_type_no(heat.getTrade_type_no());
					rein.setSale_org_no(heat.getSale_org_no());
					rein.setValid_flag("1");
					int count=this.getMapper(SlmMonthSalePlanHeatMapper.class).countWithCondition(rein, " plan_heat_no!='"+heat.getPlan_heat_no()+"'");
					if(count>0){
						rm.setState("500");
						rm.setMsgInfo("已存在相同数据");
						return rm;
					}
					heat.setUpdate_time(new Date());
					heat.setUpdate_man_no(userId);
					heat.setUpdate_man_name(userName);
					this.getMapper(SlmMonthSalePlanHeatMapper.class).update(heat);
				}else{
					rm.setMsgInfo("只允许修改待提交的有效数据");
					rm.setState("500");
					return rm;
				}
			}else{
				rm.setMsgInfo("请检查参数");
				rm.setState("500");
				return rm;
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}

	/**
	 * 提交
	 * */
	public ResultModel doSubmitWeb(CommonPage<SlmMonthSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanHeat heat=sum.getObject();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmMonthSalePlanHeat check=this.getMapper(SlmMonthSalePlanHeatMapper.class).findById(heat.getPlan_heat_no());
			if(check!=null){
				if("1".equals(check.getValid_flag())&&"0".equals(check.getPlan_flag())){
					heat.setUpdate_time(new Date());
					heat.setUpdate_man_no(userId);
					heat.setUpdate_man_name(userName);
					heat.setPlan_flag("1");
					this.getMapper(SlmMonthSalePlanHeatMapper.class).update(heat);
				}else{
					rm.setMsgInfo("只允许提交待提交状态的有效数据");
					rm.setState("500");
					return rm;
				}
			}else{
				rm.setMsgInfo("请检查参数");
				rm.setState("500");
				return rm;
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 删除 逻辑删除
	 * */
	public ResultModel doDeleteWeb(CommonPage<SlmMonthSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanHeat heat=sum.getObject();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmMonthSalePlanHeat check=this.getMapper(SlmMonthSalePlanHeatMapper.class).findById(heat.getPlan_heat_no());
			if(check!=null){
				if("1".equals(check.getValid_flag())&&"0".equals(check.getPlan_flag())){
					heat.setDelete_time(new Date());
					heat.setDelete_man_no(userId);
					heat.setDelete_man_name(userName);
					heat.setValid_flag("0");
					this.getMapper(SlmMonthSalePlanHeatMapper.class).update(heat);
				}else{
					rm.setMsgInfo("只允许删除待提交的有效数据");
					rm.setState("500");
					return rm;
				}
			}else{
				rm.setMsgInfo("请检查参数");
				rm.setState("500");
				return rm;
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 启用
	 * */
	public ResultModel doEnableWeb(CommonPage<SlmMonthSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanHeat heat=sum.getObject();
		try{
			SlmMonthSalePlanHeat check=this.getMapper(SlmMonthSalePlanHeatMapper.class).findById(heat.getPlan_heat_no());
			if(check!=null){
				if("0".equals(check.getValid_flag())){
					//是否重复
					SlmMonthSalePlanHeat rein=new SlmMonthSalePlanHeat();
					rein.setPlan_year(check.getPlan_year());
					rein.setPlan_month(check.getPlan_month());
					rein.setTrade_type_no(check.getTrade_type_no());
					rein.setSale_org_no(check.getSale_org_no());
					rein.setValid_flag("1");
					int count=this.getMapper(SlmMonthSalePlanHeatMapper.class).count(rein);
					if(count>0){
						rm.setState("500");
						rm.setMsgInfo("已存在相同的有效数据");
						return rm;
					}
					
					heat.setDelete_man_no("");
					heat.setDelete_man_name("");
					heat.setValid_flag("1");
					this.getMapper(SlmMonthSalePlanHeatMapper.class).update(heat);
				}else{
					rm.setMsgInfo("只允许启用无效数据");
					rm.setState("500");
					return rm;
				}
			}else{
				rm.setMsgInfo("请检查参数");
				rm.setState("500");
				return rm;
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}
	
	
	public ResultModel updateWeight(String id){
		ResultModel rm=new ResultModel();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmMonthSalePlanHeat heat=this.getMapper(SlmMonthSalePlanHeatMapper.class).findById(id);
			if(heat!=null){
				SlmMonthSalePlanLine line=new SlmMonthSalePlanLine();
				line.setPlan_heat_no(id);
				List<SlmMonthSalePlanLine> list=this.getMapper(SlmMonthSalePlanLineMapper.class).find(line,0,0);
				double weight=0d;
				double money=0d;
				for(SlmMonthSalePlanLine lines:list){
					weight=DoubleUtil.add(weight, lines.getSale_total());
					money=DoubleUtil.add(money, lines.getSale_amount());
				}
				heat.setSale_total(weight);
				heat.setSale_amount(money);
				heat.setUpdate_man_no(userId);
				heat.setUpdate_man_name(userName);
				heat.setUpdate_time(new Date());
				this.getMapper(SlmMonthSalePlanHeatMapper.class).update(heat);
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 图谱  共用跟踪方法
	 * */
	public ResultModel doQueryAtlasWeb(CommonPage<SlmMonthSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanHeat heat=sum.getObject();
		try{
			heat.setTrade_type_no(null);
			List<SlmMonthSalePlanHeat> list=this.getMapper(SlmMonthSalePlanHeatMapper.class).doQueryTrack(heat);
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
}