/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.SlmMonthSalePlanAreaMapper;
import market.Slm.PlanManagement.mapper.SlmMonthSalePlanLineMapper;
import market.Slm.PlanManagement.mapper.SlmSalePlanConfigMapper;
import market.Slm.PlanManagement.service.SlmMonthSalePlanLineService;
import market.Slm.PlanManagement.vo.SlmMonthSalePlanArea;
import market.Slm.PlanManagement.vo.SlmMonthSalePlanLine;
import market.Slm.PlanManagement.vo.SlmSalePlanConfig;

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

@RemoteService(path="SlmMonthSalePlanLineService")
public class SlmMonthSalePlanLineServiceImpl extends BaseServiceImpl<SlmMonthSalePlanLine,Object> implements SlmMonthSalePlanLineService {

	/**
	 * 查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmMonthSalePlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanLine line=sum.getObject();
		try{
			List<SlmMonthSalePlanLine> list=this.getMapper(SlmMonthSalePlanLineMapper.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<SlmMonthSalePlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanLine line=sum.getObject();
		try{
			//重复性验证
			SlmMonthSalePlanLine check=new SlmMonthSalePlanLine();
			check.setPlan_heat_no(line.getPlan_heat_no());
			check.setTrack_pk(line.getTrack_pk());
			check.setDelivery_state_code(line.getDelivery_state_code());
			//check.setSteel_name_gather(line.getSteel_name_gather());
			check.setProd_code(line.getProd_code());
			check.setProcess_name(line.getProcess_name());
			int check_count=this.getMapper(SlmMonthSalePlanLineMapper.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));
			}
			//========================================================
			String id="";
			List<SlmMonthSalePlanLine> sList = this.getMapper(SlmMonthSalePlanLineMapper.class).findWithCondition(null, " plan_heat_no='"+line.getPlan_heat_no()+"'","plan_line_no desc",0,1);
			if (sList!=null && sList.size()>0) {
				String plineno = sList.get(0).getPlan_line_no();
				int seq = Integer.parseInt(plineno.substring(plineno.length()-4))+1;
				id = line.getPlan_heat_no()+String.format("%04d", seq);				
			}else {
				id = line.getPlan_heat_no()+String.format("%04d", 1);
			}			
			line.setPlan_line_no(id);
			this.getMapper(SlmMonthSalePlanLineMapper.class).insert(line);
			this.getBean(SlmMonthSalePlanHeatServiceImpl.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<SlmMonthSalePlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanLine line=sum.getObject();
		List<SlmMonthSalePlanLine> list=new ArrayList<SlmMonthSalePlanLine>();
		try{
			list=this.getMapper(SlmMonthSalePlanLineMapper.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<SlmMonthSalePlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanLine line=sum.getObject();
		try{
			SlmMonthSalePlanLine check=this.getMapper(SlmMonthSalePlanLineMapper.class).findById(line.getPlan_line_no());
			if(check!=null){
				//重复性验证
				SlmMonthSalePlanLine rein=new SlmMonthSalePlanLine();
				rein.setPlan_heat_no(line.getPlan_heat_no());
				rein.setTrack_pk(line.getTrack_pk());
				rein.setDelivery_state_code(line.getDelivery_state_code());
				//rein.setSteel_name_gather(line.getSteel_name_gather());
				rein.setProd_code(line.getProd_code());
				rein.setProcess_name(line.getProcess_name());
				int check_count=this.getMapper(SlmMonthSalePlanLineMapper.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(SlmMonthSalePlanLineMapper.class).update(line);
				this.getBean(SlmMonthSalePlanHeatServiceImpl.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<SlmMonthSalePlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanLine heats=sum.getObject();
		List<SlmMonthSalePlanLine> line_list=sum.getObjectList();
		try{
			if(line_list==null){
				line_list=new ArrayList<SlmMonthSalePlanLine>();
				line_list.add(heats);
			}
			for(SlmMonthSalePlanLine heat:line_list){
				SlmMonthSalePlanLine check=this.getMapper(SlmMonthSalePlanLineMapper.class).findById(heat.getPlan_line_no());
				if(check!=null){
					int count=this.getMapper(SlmMonthSalePlanAreaMapper.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(SlmMonthSalePlanLineMapper.class).delete(check.getPlan_line_no());
					this.getBean(SlmMonthSalePlanHeatServiceImpl.class).updateWeight(heat_no);
				}
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("删除失败"+ex.getMessage());
		}
		return rm;
	}

	/**
	 * 查询 跟踪
	 * */
	public ResultModel doQueryPreviewWeb(CommonPage<SlmMonthSalePlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanLine line=sum.getObject();
		List<SlmMonthSalePlanLine> list=new ArrayList<SlmMonthSalePlanLine>();
		try{
			list=this.getMapper(SlmMonthSalePlanLineMapper.class).doQueryPreview(line.getPlan_heat_no());
			List<SlmMonthSalePlanArea> area_list=new ArrayList<SlmMonthSalePlanArea>();
			SlmMonthSalePlanArea area=new SlmMonthSalePlanArea();
			for(SlmMonthSalePlanLine lines:list){
				area.setPlan_line_no(lines.getPlan_line_no());
				area_list=this.getMapper(SlmMonthSalePlanAreaMapper.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;
	}

}