/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.SlmYearSalePlanHeatMapper;
import market.Slm.PlanManagement.mapper.SlmYearSalePlanLineMapper;
import market.Slm.PlanManagement.service.SlmYearSalePlanHeatService;
import market.Slm.PlanManagement.vo.SlmYearSalePlanHeat;
import market.Slm.PlanManagement.vo.SlmYearSalePlanLine;

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="SlmYearSalePlanHeatService")
public class SlmYearSalePlanHeatServiceImpl extends BaseServiceImpl<SlmYearSalePlanHeat,Object> implements SlmYearSalePlanHeatService {

	/**
	 * 查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmYearSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanHeat heat=sum.getObject();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		try{
			List<SlmYearSalePlanHeat> list=this.getMapper(SlmYearSalePlanHeatMapper.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<SlmYearSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanHeat heat=sum.getObject();
		try{
			List<SlmYearSalePlanHeat> list=this.getMapper(SlmYearSalePlanHeatMapper.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<SlmYearSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanHeat heat=sum.getObject();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			//查重复
			SlmYearSalePlanHeat check=new SlmYearSalePlanHeat();
			check.setPlan_year(heat.getPlan_year());
			check.setTrade_type_no(heat.getTrade_type_no());
			check.setSale_org_no(heat.getSale_org_no());
			check.setValid_flag("1");
			int cou=this.getMapper(SlmYearSalePlanHeatMapper.class).count(check);
			if(cou>0){
				rm.setState("500");
				rm.setMsgInfo("已存在相同年度计划");
				return rm;
			}
			String year=new SimpleDateFormat("yyyy").format(new Date());
			String maxId=this.getMapper(SlmYearSalePlanHeatMapper.class).findMaxId(year);
			if(maxId==null){
				year+="0001";
			}else{
				maxId=maxId.substring(4,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(SlmYearSalePlanHeatMapper.class).insert(heat);
			
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改
	 * */
	public ResultModel doUpdateWeb(CommonPage<SlmYearSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanHeat heat=sum.getObject();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmYearSalePlanHeat check=this.getMapper(SlmYearSalePlanHeatMapper.class).findById(heat.getPlan_heat_no());
			if(check!=null){
				if("1".equals(check.getValid_flag())&&"0".equals(check.getPlan_flag())){
					//是否重复
					SlmYearSalePlanHeat rein=new SlmYearSalePlanHeat();
					rein.setPlan_year(heat.getPlan_year());
					rein.setTrade_type_no(heat.getTrade_type_no());
					rein.setSale_org_no(heat.getSale_org_no());
					rein.setValid_flag("1");
					int count=this.getMapper(SlmYearSalePlanHeatMapper.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(SlmYearSalePlanHeatMapper.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<SlmYearSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanHeat heat=sum.getObject();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmYearSalePlanHeat check=this.getMapper(SlmYearSalePlanHeatMapper.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(SlmYearSalePlanHeatMapper.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<SlmYearSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanHeat heat=sum.getObject();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmYearSalePlanHeat check=this.getMapper(SlmYearSalePlanHeatMapper.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(SlmYearSalePlanHeatMapper.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<SlmYearSalePlanHeat> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanHeat heat=sum.getObject();
		try{
			SlmYearSalePlanHeat check=this.getMapper(SlmYearSalePlanHeatMapper.class).findById(heat.getPlan_heat_no());
			if(check!=null){
				if("0".equals(check.getValid_flag())){
					//是否重复
					SlmYearSalePlanHeat rein=new SlmYearSalePlanHeat();
					rein.setPlan_year(check.getPlan_year());
					rein.setTrade_type_no(check.getTrade_type_no());
					rein.setSale_org_no(check.getSale_org_no());
					rein.setValid_flag("1");
					int count=this.getMapper(SlmYearSalePlanHeatMapper.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(SlmYearSalePlanHeatMapper.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{
			SlmYearSalePlanHeat heat=this.getMapper(SlmYearSalePlanHeatMapper.class).findById(id);
			if(heat!=null){
				SlmYearSalePlanLine line=new SlmYearSalePlanLine();
				line.setPlan_heat_no(id);
				List<SlmYearSalePlanLine> list=this.getMapper(SlmYearSalePlanLineMapper.class).find(line,0,0);
				double weight=0d;
				double money=0d;
				for(SlmYearSalePlanLine 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(SlmYearSalePlanHeatMapper.class).update(heat);
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}
	
}