/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-08-07
 * 描述            ：付款控制方案节点表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Slm.ContractManagement.impl;
import java.util.Date;
import java.util.List;

import market.Com.pb.PbMethod;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.ContractManagement.mapper.SlmContractPaymentConfigureMapper;
import market.Slm.ContractManagement.mapper.SlmContractPaymentPlanMapper;
import market.Slm.ContractManagement.service.SlmContractPaymentConfigureService;
import market.Slm.ContractManagement.vo.SlmContractPaymentConfigure;

import org.springframework.transaction.annotation.Transactional;

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="SlmContractPaymentConfigureService")
public class SlmContractPaymentConfigureServiceImpl extends BaseServiceImpl<SlmContractPaymentConfigure,Object> implements SlmContractPaymentConfigureService {

	/**
	 * 方案节点配置
	 * [{"object":{"plan_no":"方案编号"}}]
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmContractPaymentConfigure> sum){
		ResultModel rm=new ResultModel();
		SlmContractPaymentConfigure plan=sum.getObject();
		try{
			List<SlmContractPaymentConfigure> plan_list=this.getMapper(SlmContractPaymentConfigureMapper.class).findWithCondition(plan, null, " valid_flag desc,create_time desc", 0, 0);
			rm.setData(plan_list);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+e.getMessage());
		}
		return rm;
	}

	/**
	 * 方案节点新增
	 * [{"object":{"plan_no":"客户编号"}}]
	 * @throws Exception 
	 * */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddWeb(CommonPage<SlmContractPaymentConfigure> sum) throws Exception{
		ResultModel rm=new ResultModel();
		SlmContractPaymentConfigure plan=sum.getObject();
		String userId = "admin";//(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = "";//(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmContractPaymentConfigure check=new SlmContractPaymentConfigure();
			check.setPlan_no(plan.getPlan_no());
			check.setPayment_point_no(plan.getPayment_point_no());
			int count=this.getMapper(SlmContractPaymentConfigureMapper.class).count(check);
			if(count>0){
				rm.setState("500");
				rm.setMsgInfo("该方案下已存在"+plan.getPayment_point_name()+"节点");
				return rm;
			}
			/*check.setPayment_point_no(null);
			List<SlmContractPaymentConfigure> config_list=this.getMapper(SlmContractPaymentConfigureMapper.class).find(check,0,0);
			double ratio=0d;
			for(SlmContractPaymentConfigure configs:config_list){
				ratio=DoubleUtil.add(configs.getRatio_value(),ratio);
			}
			ratio=DoubleUtil.add(plan.getRatio_value(),ratio);
			if(ratio>100){
				rm.setState("500");
				rm.setMsgInfo("付款值不得超过100%");
				return rm;
			}
			SlmContractPaymentPlan pay_plan=this.getMapper(SlmContractPaymentPlanMapper.class).findById(plan.getPlan_no());
			if(pay_plan!=null){
				plan.setCustomer_no(pay_plan.getCustomer_no());
				plan.setCustomer_name(pay_plan.getCustomer_name());
			}
			*/
			//===========让方案只有一个有效信息=============================
			check.setValid_flag("0");
			this.getMapper(SlmContractPaymentPlanMapper.class).doUpdateConfigure(check);
			//=============================================================
			
			String strNo=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
			plan.setConfigure_no(strNo+PbMethod.RandomCreate(3));
			plan.setValid_flag("1");
			plan.setCreate_man_no(userId);
			plan.setCreate_man_name(userName);
			plan.setCreate_time(new Date());
			this.getMapper(SlmContractPaymentConfigureMapper.class).insert(plan);

			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("新增失败！"+e.getMessage());
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 方案节点修改
	 * [{"object":{"configure_no":"主键"}}]
	 * */
	public ResultModel doUpdateWeb(CommonPage<SlmContractPaymentConfigure> sum){
		ResultModel rm=new ResultModel();
		SlmContractPaymentConfigure plan=sum.getObject();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmContractPaymentConfigure check=this.getMapper(SlmContractPaymentConfigureMapper.class).findById(plan.getConfigure_no());
			if(check!=null&&"0".equals(check.getValid_flag())){
				rm.setMsgInfo("无法修改已失效的数据，请先启用");
				rm.setState("500");
				return rm;
			}
			/*
			SlmContractPaymentConfigure param=new SlmContractPaymentConfigure();
			param.setPlan_no(check.getPlan_no());
			param.setValid_flag("1");
			List<SlmContractPaymentConfigure> config_list=this.getMapper(SlmContractPaymentConfigureMapper.class).find(param,0,0);
			double ratio=0d;
			for(SlmContractPaymentConfigure configs:config_list){
				ratio=DoubleUtil.add(configs.getRatio_value(),ratio);
			}
			ratio=DoubleUtil.sub(ratio,check.getRatio_value());
			ratio=DoubleUtil.add(ratio,plan.getRatio_value());
			if(ratio>100){
				rm.setState("500");
				rm.setMsgInfo("付款值不得超过100%");
				return rm;
			}
			*/
			plan.setValid_flag(null);
			plan.setUpdate_time(new Date());
			plan.setUpdate_man_no(userId);
			plan.setUpdate_man_name(userName);
			this.getMapper(SlmContractPaymentConfigureMapper.class).update(plan);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败！"+e.getMessage());
		}
		return rm;
	}

	/**
	 * 方案节点启用/停用
	 * [{"object":{"configure_no":"主键"}}]
	 * @throws Exception 
	 * */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doOperateWeb(CommonPage<SlmContractPaymentConfigure> sum) throws Exception{
		ResultModel rm=new ResultModel();
		SlmContractPaymentConfigure plan=sum.getObject();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmContractPaymentConfigure check=this.getMapper(SlmContractPaymentConfigureMapper.class).findById(plan.getConfigure_no());
			if("1".equals(plan.getValid_flag())&&check!=null){
				//=============================所有的先设置为停用====================
				SlmContractPaymentConfigure scpc=new SlmContractPaymentConfigure();
				scpc.setPlan_no(check.getPlan_no());
				scpc.setValid_flag("0");
				this.getMapper(SlmContractPaymentPlanMapper.class).doUpdateConfigure(scpc);
				//==================================================================
				/*
				SlmContractPaymentConfigure param=new SlmContractPaymentConfigure();
				param.setPlan_no(check.getPlan_no());
				param.setValid_flag("1");
				List<SlmContractPaymentConfigure> config_list=this.getMapper(SlmContractPaymentConfigureMapper.class).find(param,0,0);
				double ratio=0d;
				for(SlmContractPaymentConfigure configs:config_list){
					ratio=DoubleUtil.add(configs.getRatio_value(),ratio);
				}
				ratio=DoubleUtil.add(check.getRatio_value(),ratio);
				if(ratio>100){
					rm.setState("500");
					rm.setMsgInfo("付款值不得超过100%");
					return rm;
				}
				*/
			}

			plan.setOperation_time(new Date());
			plan.setOperation_man_no(userId);
			plan.setOperation_man_name(userName);
			this.getMapper(SlmContractPaymentConfigureMapper.class).update(plan);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("操作失败！"+e.getMessage());
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

}
