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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.PlanManagement.mapper.SlmSalePlanConfigMapper;
import market.Slm.PlanManagement.mapper.SlmYearSalePlanAreaMapper;
import market.Slm.PlanManagement.mapper.SlmYearSalePlanLineMapper;
import market.Slm.PlanManagement.service.SlmYearSalePlanLineService;
import market.Slm.PlanManagement.vo.SlmSalePlanConfig;
import market.Slm.PlanManagement.vo.SlmYearSalePlanArea;
import market.Slm.PlanManagement.vo.SlmYearSalePlanLine;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@RemoteService(path="SlmYearSalePlanLineService")
public class SlmYearSalePlanLineServiceImpl extends BaseServiceImpl<SlmYearSalePlanLine,Object> implements SlmYearSalePlanLineService {

	/**
	 * 查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmYearSalePlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanLine line=sum.getObject();
		try{
			List<SlmYearSalePlanLine> list=this.getMapper(SlmYearSalePlanLineMapper.class).findWithCondition(line, null, " plan_line_no ", 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<SlmYearSalePlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanLine line=sum.getObject();
		try{
			//重复性验证
			SlmYearSalePlanLine check=new SlmYearSalePlanLine();
			check.setPlan_heat_no(line.getPlan_heat_no());
			check.setTrack_pk(line.getTrack_pk());
			check.setDelivery_state_code(line.getDelivery_state_code());
			check.setProd_code(line.getProd_code());//.setSteel_name_gather(line.getSteel_name_gather());
			check.setProcess_name(line.getProcess_name());
			int check_count=this.getMapper(SlmYearSalePlanLineMapper.class).count(check);
			if(check_count>0){
				rm.setState("500");
				rm.setMsgInfo("已存在相同分项计划");
				return rm;
			}
			
			//====================eason add 2019-08-28================
			SlmSalePlanConfig config=new SlmSalePlanConfig();
			config.setPlan_variety_no(line.getProd_code());
			List<SlmSalePlanConfig> lconfig=this.getMapper(SlmSalePlanConfigMapper.class).find(config, 0, 0);
			String str="";
			for(SlmSalePlanConfig c:lconfig){
				str+=c.getSteel_name()+"/";
			}
			if(!"".equals(str)){
				line.setSteel_name_gather(str.substring(0,str.length()-1));
			}
			//========================================================
			
			int count=this.getMapper(SlmYearSalePlanLineMapper.class).countWithCondition(null, " plan_heat_no='"+line.getPlan_heat_no()+"'");
			String id=line.getPlan_heat_no()+String.format("%04d", (count+1));
			line.setPlan_line_no(id);
			this.getMapper(SlmYearSalePlanLineMapper.class).insert(line);
			this.getBean(SlmYearSalePlanHeatServiceImpl.class).updateWeight(line.getPlan_heat_no());
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 查询 跟踪
	 * */
	public ResultModel doQueryTrackWeb(CommonPage<SlmYearSalePlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanLine line=sum.getObject();
		List<SlmYearSalePlanLine> list=new ArrayList<SlmYearSalePlanLine>();
		try{
			list=this.getMapper(SlmYearSalePlanLineMapper.class).doQueryTrack(line.getPlan_heat_no());
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改
	 * */
	public ResultModel doUpdateWeb(CommonPage<SlmYearSalePlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanLine line=sum.getObject();
		try{
			SlmYearSalePlanLine check=this.getMapper(SlmYearSalePlanLineMapper.class).findById(line.getPlan_line_no());
			if(check!=null){
				//重复性验证
				SlmYearSalePlanLine rein=new SlmYearSalePlanLine();
				rein.setPlan_heat_no(line.getPlan_heat_no());
				rein.setTrack_pk(line.getTrack_pk());
				rein.setDelivery_state_code(line.getDelivery_state_code());
				rein.setProd_code(line.getProd_code());
				//rein.setSteel_name_gather(line.getSteel_name_gather());
				rein.setProcess_name(line.getProcess_name());
				int check_count=this.getMapper(SlmYearSalePlanLineMapper.class).countWithCondition(rein," plan_line_no!='"+line.getPlan_line_no()+"'");
				if(check_count>0){
					rm.setState("500");
					rm.setMsgInfo("已存在相同分项计划");
					return rm;
				}
				
				//====================eason add 2019-08-28================
				SlmSalePlanConfig config=new SlmSalePlanConfig();
				config.setPlan_variety_no(line.getProd_code());
				List<SlmSalePlanConfig> lconfig=this.getMapper(SlmSalePlanConfigMapper.class).find(config, 0, 0);
				String str="";
				for(SlmSalePlanConfig c:lconfig){
					str+=c.getSteel_name()+"/";
				}
				if(!"".equals(str)){
					line.setSteel_name_gather(str.substring(0,str.length()-1));
				}
				//========================================================
				
				this.getMapper(SlmYearSalePlanLineMapper.class).update(line);
				this.getBean(SlmYearSalePlanHeatServiceImpl.class).updateWeight(check.getPlan_heat_no());
			}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<SlmYearSalePlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanLine one_line=sum.getObject();
		List<SlmYearSalePlanLine> list=sum.getObjectList();
		try{
			if(list==null){
				list=new ArrayList<SlmYearSalePlanLine>();
				list.add(one_line);
			}
			for(SlmYearSalePlanLine line:list){
				SlmYearSalePlanLine check=this.getMapper(SlmYearSalePlanLineMapper.class).findById(line.getPlan_line_no());
				if(check!=null){
					int count=this.getMapper(SlmYearSalePlanAreaMapper.class).countWithCondition(null, " plan_line_no='"+check.getPlan_line_no()+"'");
					if(count>0) throw new Exception("该行信息下存在区域信息，请先删除区域信息");
					String heat_no=check.getPlan_heat_no();
					this.getMapper(SlmYearSalePlanLineMapper.class).delete(check.getPlan_line_no());
					this.getBean(SlmYearSalePlanHeatServiceImpl.class).updateWeight(heat_no);
				}
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("删除失败"+ex.getMessage());
		}
		return rm;
	}

	/**
	 * 查询 预览
	 * */
	public ResultModel doQueryPreviewWeb(CommonPage<SlmYearSalePlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanLine line=sum.getObject();
		List<SlmYearSalePlanLine> list=new ArrayList<SlmYearSalePlanLine>();
		try{
			list=this.getMapper(SlmYearSalePlanLineMapper.class).doQueryPreview(line.getPlan_heat_no());
			List<SlmYearSalePlanArea> area_list=new ArrayList<SlmYearSalePlanArea>();
			SlmYearSalePlanArea area=new SlmYearSalePlanArea();
			for(SlmYearSalePlanLine lines:list){
				area.setPlan_line_no(lines.getPlan_line_no());
				area_list=this.getMapper(SlmYearSalePlanAreaMapper.class).findWithCondition(area, null, " sale_area_no", 0, 0);
				lines.setArea_list(area_list);
			}
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

}