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

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

@RemoteService(path="SlmMonthSalePlanAreaService")
public class SlmMonthSalePlanAreaServiceImpl extends BaseServiceImpl<SlmMonthSalePlanArea,Object> implements SlmMonthSalePlanAreaService {

	/**
	 * 查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmMonthSalePlanArea> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanArea area=sum.getObject();
		try{
			List<SlmMonthSalePlanArea> list=this.getMapper(SlmMonthSalePlanAreaMapper.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<SlmMonthSalePlanArea> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanArea area=sum.getObject();
		List<SlmMonthSalePlanArea> list=new ArrayList<SlmMonthSalePlanArea>();
		try{
				list=this.getMapper(SlmMonthSalePlanAreaMapper.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<SlmMonthSalePlanArea> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanArea area=sum.getObject();
		try{
			//查重
			SlmMonthSalePlanArea check=new SlmMonthSalePlanArea();
			check.setPlan_line_no(area.getPlan_line_no());
			check.setSale_area_no(area.getSale_area_no());
			int check_count=this.getMapper(SlmMonthSalePlanAreaMapper.class).count(check);
			if(check_count>0){
				rm.setState("500");
				rm.setMsgInfo("已存在相同销售区域计划信息");
				return rm;
			}
			SlmMonthSalePlanLine line=this.getMapper(SlmMonthSalePlanLineMapper.class).findById(area.getPlan_line_no());
			if(line==null){
				rm.setState("500");
				rm.setMsgInfo("行信息不存在");
				return rm;
			}
			//重量不能超额
			check.setSale_area_no(null);
			double total_weight=0d;
			double total_amount=0d;
			List<SlmMonthSalePlanArea> area_list=this.getMapper(SlmMonthSalePlanAreaMapper.class).find(check, 0, 0);
			for(SlmMonthSalePlanArea areas:area_list){
				total_weight=DoubleUtil.add(total_weight, areas.getSale_total());
				total_amount=DoubleUtil.add(total_amount, areas.getSale_amount());
			}
			if(DoubleUtil.add(total_weight, area.getSale_total())>line.getSale_total()){
				rm.setState("500");
				rm.setMsgInfo("区域销售总量不能超过行销售总量，最大可新增总量["+DoubleUtil.sub(line.getSale_total(), total_weight)+"]");
				return rm;
			}
			if(DoubleUtil.add(total_amount, area.getSale_amount())>line.getSale_amount()){
				rm.setState("500");
				rm.setMsgInfo("区域销售总额不能超过行销售总额，最大可新增总额["+DoubleUtil.sub(line.getSale_amount(), total_amount)+"]");
				return rm;
			}
			
			int count=this.getMapper(SlmMonthSalePlanAreaMapper.class).countWithCondition(null, " plan_line_no='"+area.getPlan_line_no()+"'");
			String id=area.getPlan_line_no()+String.format("%02d", (count+1));
			area.setPlan_area_no(id);
			this.getMapper(SlmMonthSalePlanAreaMapper.class).insert(area);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改
	 * */
	public ResultModel doUpdateWeb(CommonPage<SlmMonthSalePlanArea> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanArea area=sum.getObject();
		try{
			SlmMonthSalePlanArea check=this.getMapper(SlmMonthSalePlanAreaMapper.class).findById(area.getPlan_area_no());
			if(check!=null){
				SlmMonthSalePlanLine line=this.getMapper(SlmMonthSalePlanLineMapper.class).findById(check.getPlan_line_no());
				if(line==null){
					rm.setState("500");
					rm.setMsgInfo("行信息不存在");
					return rm;
				}
				SlmMonthSalePlanArea param=new SlmMonthSalePlanArea();
				param.setPlan_line_no(check.getPlan_line_no());
				List<SlmMonthSalePlanArea> area_list=this.getMapper(SlmMonthSalePlanAreaMapper.class).find(param, 0, 0);
				double total_weight=0d;
				double total_amount=0d;
				for(SlmMonthSalePlanArea areas:area_list){
					total_weight=DoubleUtil.add(total_weight, areas.getSale_total());
					total_amount=DoubleUtil.add(total_amount, areas.getSale_amount());
				}
				double sum_weight=DoubleUtil.add(DoubleUtil.sub(total_weight, check.getSale_total()),area.getSale_total());
				if(sum_weight>line.getSale_total()){
					rm.setState("500");
					rm.setMsgInfo("区域销售总量不能超过行销售总量，最大可修改总量["+DoubleUtil.sub(line.getSale_total(), DoubleUtil.sub(total_weight, check.getSale_total()))+"]");
					return rm;
				}
				double sum_amount=DoubleUtil.add(DoubleUtil.sub(total_amount, check.getSale_amount()),area.getSale_amount());
				if(sum_amount>line.getSale_amount()){
					rm.setState("500");
					rm.setMsgInfo("区域销售总额不能超过行销售总额，最大可修改总额["+DoubleUtil.sub(line.getSale_amount(), DoubleUtil.sub(total_amount, check.getSale_amount()))+"]");
					return rm;
				}
				//查重
				SlmMonthSalePlanArea rein=new SlmMonthSalePlanArea();
				rein.setPlan_line_no(area.getPlan_line_no());
				rein.setSale_area_no(area.getSale_area_no());
				int check_count=this.getMapper(SlmMonthSalePlanAreaMapper.class).countWithCondition(rein," plan_area_no!='"+area.getPlan_area_no()+"'");
				if(check_count>0){
					rm.setState("500");
					rm.setMsgInfo("已存在相同销售区域计划信息");
					return rm;
				}
				this.getMapper(SlmMonthSalePlanAreaMapper.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<SlmMonthSalePlanArea> sum){
		ResultModel rm=new ResultModel();
		SlmMonthSalePlanArea areas=sum.getObject();
		List<SlmMonthSalePlanArea> area_list=sum.getObjectList();

		try{
			if(area_list==null){
				area_list=new ArrayList<SlmMonthSalePlanArea>();
				area_list.add(areas);
			}
			for(SlmMonthSalePlanArea area:area_list){
				SlmMonthSalePlanArea check=this.getMapper(SlmMonthSalePlanAreaMapper.class).findById(area.getPlan_area_no());
				if(check!=null){
					this.getMapper(SlmMonthSalePlanAreaMapper.class).delete(check.getPlan_area_no());
				}
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("失败"+ex.getMessage());
		}
		return rm;
	}

}