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

import market.Com.pb.DoubleUtil;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.PlanManagement.mapper.SlmYearSalePlanAreaMapper;
import market.Slm.PlanManagement.mapper.SlmYearSalePlanLineMapper;
import market.Slm.PlanManagement.service.SlmYearSalePlanAreaService;
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="SlmYearSalePlanAreaService")
public class SlmYearSalePlanAreaServiceImpl extends BaseServiceImpl<SlmYearSalePlanArea,Object> implements SlmYearSalePlanAreaService {

	/**
	 * 查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmYearSalePlanArea> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanArea area=sum.getObject();
		try{
			List<SlmYearSalePlanArea> list=this.getMapper(SlmYearSalePlanAreaMapper.class).findWithCondition(area, null, " plan_area_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 doQueryTrackWeb(CommonPage<SlmYearSalePlanArea> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanArea area=sum.getObject();
		List<SlmYearSalePlanArea> list=new ArrayList<SlmYearSalePlanArea>();
		try{
			list=this.getMapper(SlmYearSalePlanAreaMapper.class).doQueryTrack(area.getPlan_line_no());
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}


	/**
	 * 新增
	 * */
	public ResultModel doAddWeb(CommonPage<SlmYearSalePlanArea> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanArea area=sum.getObject();
		try{
			//查重
			SlmYearSalePlanArea check=new SlmYearSalePlanArea();
			check.setPlan_line_no(area.getPlan_line_no());
			check.setSale_area_no(area.getSale_area_no());
			int check_count=this.getMapper(SlmYearSalePlanAreaMapper.class).count(check);
			if(check_count>0){
				throw new Exception("已存在相同销售区域计划信息");
			}
			SlmYearSalePlanLine line=this.getMapper(SlmYearSalePlanLineMapper.class).findById(area.getPlan_line_no());
				if(line==null) throw new Exception();
			check.setSale_area_no(null);
			List<SlmYearSalePlanArea> check_list=this.getMapper(SlmYearSalePlanAreaMapper.class).find(check, 0, 0);
			double total_money=0d;
			double total_weight=0d;
			for(SlmYearSalePlanArea areas:check_list){
				total_money=DoubleUtil.add(total_money, areas.getSale_amount());
				total_weight=DoubleUtil.add(total_weight, areas.getSale_amount());
			}
			if(line.getSale_amount()<DoubleUtil.add(total_money, area.getSale_amount())) throw new Exception("区域分量金额总和不能超过行信息的金额，最大可新增总额["+DoubleUtil.sub(line.getSale_amount(), total_money)+"]");
			if(line.getSale_total()<DoubleUtil.add(total_weight, area.getSale_total())) throw new Exception("区域分量总重之和不能超过行信息的总重量，最大可新增总量["+DoubleUtil.sub(line.getSale_total(), total_weight)+"]");
			String id=area.getPlan_line_no()+String.format("%02d", (check_list.size()+1));
			area.setPlan_area_no(id);
			this.getMapper(SlmYearSalePlanAreaMapper.class).insert(area);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改
	 * */
	public ResultModel doUpdateWeb(CommonPage<SlmYearSalePlanArea> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanArea area=sum.getObject();
		try{
			SlmYearSalePlanArea check=this.getMapper(SlmYearSalePlanAreaMapper.class).findById(area.getPlan_area_no());
			if(check!=null){
				//查重
				SlmYearSalePlanArea rein=new SlmYearSalePlanArea();
				rein.setPlan_line_no(area.getPlan_line_no());
				rein.setSale_area_no(area.getSale_area_no());
				int check_count=this.getMapper(SlmYearSalePlanAreaMapper.class).countWithCondition(rein," plan_area_no!='"+area.getPlan_area_no()+"'");
				if(check_count>0){
					rm.setState("500");
					rm.setMsgInfo("已存在相同销售区域计划信息");
					return rm;
				}
				this.getMapper(SlmYearSalePlanAreaMapper.class).update(area);
			}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<SlmYearSalePlanArea> sum){
		ResultModel rm=new ResultModel();
		SlmYearSalePlanArea areas=sum.getObject();
		List<SlmYearSalePlanArea> area_list=sum.getObjectList();
		try{
			if(area_list==null){
				area_list=new ArrayList<SlmYearSalePlanArea>();
				area_list.add(areas);
			}
			for(SlmYearSalePlanArea area:area_list){
				SlmYearSalePlanArea check=this.getMapper(SlmYearSalePlanAreaMapper.class).findById(area.getPlan_area_no());
				if(check!=null){
					this.getMapper(SlmYearSalePlanAreaMapper.class).delete(check.getPlan_area_no());
				}else{
//					rm.setMsgInfo("该数据已删除");
//					rm.setState("500");
//					return rm;
				}
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}

}