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

import java.util.Date;
import java.util.List;
import java.util.UUID;

import market.Com.pb.PbMethod;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.ContractManagement.mapper.SlmContractPaymentConfigMapper;
import market.Slm.ContractManagement.mapper.SlmContractPaymentPlanMapper;
import market.Slm.ContractManagement.service.SlmContractPaymentPlanService;
import market.Slm.ContractManagement.vo.SlmContractPaymentConfig;
import market.Slm.ContractManagement.vo.SlmContractPaymentConfigure;
import market.Slm.ContractManagement.vo.SlmContractPaymentPlan;

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="SlmContractPaymentPlanService")
public class SlmContractPaymentPlanServiceImpl extends BaseServiceImpl<SlmContractPaymentPlan,Object> implements SlmContractPaymentPlanService {

	/**
	 * 方案查询
	 * [{"object":{"customer_no":"客户编号","valid_flag":"传1只查有效，不传查所有"}}]
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmContractPaymentPlan> sum){
		ResultModel rm=new ResultModel();
		SlmContractPaymentPlan plan=sum.getObject();
		try{
			String sWhere= " 1=1 ";
			if(plan.getPlan_name()!=null&&!"".equals(plan.getPlan_name())){
				sWhere+=" and plan_name like '%"+plan.getPlan_name()+"%'";
				plan.setPlan_name(null);
			}
			List<SlmContractPaymentPlan> plan_list=this.getMapper(SlmContractPaymentPlanMapper.class).findWithCondition(plan, sWhere, " valid_flag desc,create_time desc", 0, 0);
			
			rm.setData(plan_list);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 查询对应客户的方案信息
	 * eason add 2019-06-29
	 * @return
	 */
	public ResultModel doQueryCustPlanWeb(CommonPage<SlmContractPaymentPlan> sum){
		ResultModel rm=new ResultModel();
		SlmContractPaymentPlan plan=sum.getObject();
		try{
			List<SlmContractPaymentPlan> plan_list=this.getMapper(SlmContractPaymentPlanMapper.class).doQueryCustPlan(plan);
			rm.setData(plan_list);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 方案新增
	 * [{"object":{"customer_no":"客户编号","plan_name":"方案名称"}}]
	 * eason edit 2019-06-29
	 * @throws Exception 
	 * */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddWeb(CommonPage<SlmContractPaymentPlan> sum) throws Exception{
		ResultModel rm=new ResultModel();
		SlmContractPaymentPlan plan=sum.getObject();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmContractPaymentPlan scpp=new SlmContractPaymentPlan();
			scpp.setPlan_name(plan.getPlan_name());
			int icount = this.getMapper(SlmContractPaymentPlanMapper.class).count(scpp);
			if(icount>0){
				rm.setState("500");
				rm.setMsgInfo("存在相同名字的方案！");
				return rm;
			}
			
			String strNo=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
			plan.setValid_flag("1");
			plan.setPlan_no(strNo+PbMethod.RandomCreate(3));
			plan.setCreate_man_no(userId);
			plan.setCreate_man_name(userName);
			plan.setCreate_time(new Date());
			
			if(plan.getCustomer_no()!=null&&!"".equals(plan.getCustomer_no())){
				String customerNo=plan.getCustomer_no();
				String customerName=plan.getCustomer_name();
				
				SlmContractPaymentConfig config=new SlmContractPaymentConfig();
				config.setCreate_man_name(userId);
				config.setCreate_man_no(userName);
				config.setCreate_time(new Date());
				config.setSeq(UUID.randomUUID().toString());
				config.setPlan_no(plan.getPlan_no());
				config.setPlan_name(plan.getPlan_name());
				config.setCustomer_no(customerNo);
				config.setCustomer_name(customerName);
				this.getMapper(SlmContractPaymentConfigMapper.class).insert(config);
				
				plan.setCustomer_no(null);
				plan.setCustomer_name(null);
			}
			
			this.getMapper(SlmContractPaymentPlanMapper.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":{"plan_no":"主键"}}]
	 * */
	public ResultModel doUpdateWeb(CommonPage<SlmContractPaymentPlan> sum){
		ResultModel rm=new ResultModel();
		SlmContractPaymentPlan plan=sum.getObject();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmContractPaymentPlan check=this.getMapper(SlmContractPaymentPlanMapper.class).findById(plan.getPlan_no());
			if(check!=null&&"0".equals(check.getValid_flag())){
				rm.setMsgInfo("无法修改已失效的数据，请先启用");
				rm.setState("500");
				return rm;
			}
			
			SlmContractPaymentPlan scpp=new SlmContractPaymentPlan();
			scpp.setPlan_name(plan.getPlan_name());
			int icount = this.getMapper(SlmContractPaymentPlanMapper.class).countWithCondition(scpp, " plan_no!='"+plan.getPlan_no()+"' ");
			if(icount>0){
				rm.setState("500");
				rm.setMsgInfo("存在相同名字的方案！");
				return rm;
			}
			
			plan.setValid_flag(null);
			plan.setUpdate_time(new Date());
			plan.setUpdate_man_no(userId);
			plan.setUpdate_man_name(userName);
			this.getMapper(SlmContractPaymentPlanMapper.class).update(plan);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 方案启用停用
	 * [{"object":{"plan_no":"主键","valid_flag":"1启用 0停用"}}]
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doOperateWeb(CommonPage<SlmContractPaymentPlan> sum) throws Exception{
		ResultModel rm=new ResultModel();
		SlmContractPaymentPlan plan=sum.getObject();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			SlmContractPaymentPlan check=this.getMapper(SlmContractPaymentPlanMapper.class).findById(plan.getPlan_no());
			if(check.getValid_flag().equals(plan.getValid_flag())){
				rm.setState("500");
				rm.setMsgInfo("该方案已"+transFlag(check.getValid_flag())+"，请刷新页面");
				return rm;
			}
			if("0".equals(plan.getValid_flag())){
				//若为停用则判断是否与客户有关联，如果没关联则可停用，有关联则提示
				SlmContractPaymentConfig spc=new SlmContractPaymentConfig();
				spc.setPlan_no(check.getPlan_no());
				int icount = this.getMapper(SlmContractPaymentConfigMapper.class).count(spc);
				if (icount > 0){
					rm.setState("500");
					rm.setMsgInfo("该方案与客户进行了关联，无法停用");
					return rm;
				}
				// 若方案停用则方案下的节点也停用
				SlmContractPaymentConfigure scpc=new SlmContractPaymentConfigure();
				scpc.setPlan_no(check.getPlan_no());
				scpc.setValid_flag("0");
				this.getMapper(SlmContractPaymentPlanMapper.class).doUpdateConfigure(scpc);
			}
			
			check.setValid_flag(plan.getValid_flag());
			check.setOperation_man_no(userId);
			check.setOperation_man_name(userName);
			check.setOperation_time(new Date());
			this.getMapper(SlmContractPaymentPlanMapper.class).update(check);
			rm.setState("200");
		} catch (Exception e) {
			throw new Exception("操作失败"+e.getMessage());
		}
		return rm;
	}
	
	
	private String transFlag(String param){
		String re="停用";
		if("1".equals(param))
			re="启用";
		return re;
	}
}
