/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2020-03-17
 * 描述            ：销售用户月协议量
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Slm.PlanManagement.impl;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.PlanManagement.mapper.SlmMonthUserCompleteMapper;
import market.Slm.PlanManagement.service.SlmMonthUserCompleteService;
import market.Slm.PlanManagement.vo.SlmMonthUserComplete;

import org.apache.commons.collections.CollectionUtils;
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;

@RemoteService(path="SlmMonthUserCompleteService")
public class SlmMonthUserCompleteServiceImpl extends BaseServiceImpl<SlmMonthUserComplete,Object> implements SlmMonthUserCompleteService {

	/**
	 * 
	 * @param cp
	 * @return
	 */
	public ResultModel doQuerySlmMonthUserComplete(CommonPage<SlmMonthUserComplete> cp){
		ResultModel rm=new ResultModel();
		SlmMonthUserComplete smu=cp.getObject();
		try{
			List<SlmMonthUserComplete> list=this.getMapper(SlmMonthUserCompleteMapper.class).findWithCondition(smu, null, "plan_year desc, plan_month", 0,0);
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 新增
	 * @param cp
	 * @return
	 */
	public ResultModel doInsertSlmMonthUserComplete(CommonPage<SlmMonthUserComplete> cp){
		ResultModel rm=new ResultModel();
		SlmMonthUserComplete smu=cp.getObject();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try{
			smu.setCreate_man_name(userName);
			smu.setCreate_time(new Date());
			this.getMapper(SlmMonthUserCompleteMapper.class).insert(smu);
			rm.setMsgInfo("新增成功");
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 删除
	 * @param cp
	 * @return
	 * @throws Exception 
	 */	
	public ResultModel doDeleteSlmMonthUserComplete(CommonPage<SlmMonthUserComplete> cp) throws Exception{
		ResultModel rm=new ResultModel();
		List<SlmMonthUserComplete> smuList = cp.getObjectList();
		if (smuList==null || smuList.size()<=0) {
			throw new Exception("请传入参数");
		}
		try{
			for (SlmMonthUserComplete smu : smuList) {
				this.getMapper(SlmMonthUserCompleteMapper.class).doDeleteSlmMonthUserComplete(smu.getCustomer_no(),smu.getPlan_month());			
			}
			rm.setMsgInfo("删除成功");
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("删除失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 修改
	 * @param cp
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doUpdateSlmMonthUserComplete(CommonPage<SlmMonthUserComplete> cp){
		ResultModel rm=new ResultModel();
		List<SlmMonthUserComplete> smuList = cp.getObjectList();
		try{
			if (smuList!=null && smuList.size()==2) {				
				this.getMapper(SlmMonthUserCompleteMapper.class).doDeleteSlmMonthUserComplete(smuList.get(0).getCustomer_no(),smuList.get(0).getPlan_month());
				this.getMapper(SlmMonthUserCompleteMapper.class).insert(smuList.get(1));
			}
			rm.setMsgInfo("修改成功");
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("修改失败"+e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 复制
	 * @param cp
	 * @return
	 */
	public ResultModel doCopySlmMonthUserComplete(CommonPage<SlmMonthUserComplete> cp){
		ResultModel rm=new ResultModel();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date now = new Date();
		try{
			//1.1 判断复制到月份memo2是否存在数据
			String condition = "plan_month = '"+cp.getMemo2()+"'";
			int count = this.getMapper(SlmMonthUserCompleteMapper.class).countWithCondition(null, condition);
			if (count > 0) {
				throw new Exception("月份【"+cp.getMemo2()+"】已经存在月度协议数据，无法复制");
			}
			//1.2 判断复制来源月份memo1是否存在数据
			condition = "plan_month = '"+cp.getMemo1()+"'";
			count = this.getMapper(SlmMonthUserCompleteMapper.class).countWithCondition(null, condition);
			if (count <= 0) {
				throw new Exception("未找到月份【"+cp.getMemo1()+"】存在月度协议数据，无法复制");
			}
			SlmMonthUserComplete smuc = new SlmMonthUserComplete();
			smuc.setPlan_month(cp.getMemo1());
			List<SlmMonthUserComplete> smucList = this.getMapper(SlmMonthUserCompleteMapper.class).find(smuc,0,0);
			if (CollectionUtils.isNotEmpty(smucList)) {
				for (SlmMonthUserComplete sc : smucList) {
					sc.setPlan_month(cp.getMemo2());
					sc.setPlan_year(cp.getMemo2().substring(0, 4));
					sc.setCreate_time(now);
					sc.setCreate_man_name(userName);
					sc.setUpdate_man_name(null);
					sc.setUpdate_time(null);
					this.getMapper(SlmMonthUserCompleteMapper.class).insert(sc);
				}
			}
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("修改失败"+e.getMessage());
			e.printStackTrace();
		}		
		rm.setState("200");
		rm.setMsgInfo("复制成功");
		return rm;		 
	}
	
}
