/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2017.10.24
 * 描述               :        			客户运输方案信息
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Slm.CustomerInformation.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.CustomerInformation.mapper.SlmCustomerTransSolutionMapper;
import market.Slm.CustomerInformation.service.SlmCustomerTransSolutionService;
import market.Slm.CustomerInformation.vo.SlmCustomerTransSolution;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.service.support.PageModel;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="SlmCustomerTransSolutionService")
public class SlmCustomerTransSolutionServiceImpl extends BaseServiceImpl<SlmCustomerTransSolution,Object> implements SlmCustomerTransSolutionService {
	/**
	 * 查询数据
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmCustomerTransSolution> sum){
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();

		PageModel<SlmCustomerTransSolution> pm = new PageModel<SlmCustomerTransSolution>(pageIndex,pageSize);
		
		try {
			SlmCustomerTransSolution sb=sum.getObject();
			String sWhere=" 1=1 ";
			if(sb!=null){
				if(!"".equals(sb.getSolutions_name())&&sb.getSolutions_name()!=null){
					sWhere+=" and (solutions_name like '%"+sb.getSolutions_name()+"%') ";
					sb.setSolutions_name(null);
				}
			}
			

			pm.setRows(this.getMapper(SlmCustomerTransSolutionMapper.class).findWithCondition(sb, sWhere, " valid_flag desc,solutions_no desc ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmCustomerTransSolutionMapper.class).countWithCondition(sb,sWhere));
			rm.setState("200");
			rm.setData(pm);
	        
		} catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 查看详情
	 * */
	public ResultModel doQueryDetailWeb(CommonPage<SlmCustomerTransSolution> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setState("200");
			rm.setData(this.getMapper(SlmCustomerTransSolutionMapper.class).findById(sum.getObject().getSolutions_no()));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 添加数据
	 * @throws Exception 
	 * 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddWeb(CommonPage<SlmCustomerTransSolution> sum) throws Exception {
		ResultModel rm=new ResultModel();
		try {
			SlmCustomerTransSolution bd=sum.getObject();
			
			SlmCustomerTransSolution obd = new SlmCustomerTransSolution();
			obd.setCustomer_no(bd.getCustomer_no());
			obd.setSolutions_name(bd.getSolutions_name());
			
			List<SlmCustomerTransSolution> list2 = this.getMapper(SlmCustomerTransSolutionMapper.class).find(obd, 0, 0);
			if (list2 != null && list2.size() > 0) {
				rm.setState("500");
				rm.setMsgInfo("该公司已存在相同名称的运输方案");
			}else{
				String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
				String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
				String sNo=new SimpleDateFormat("yyyyMMdd").format(new Date());
				int icount=this.getMapper(SlmCustomerTransSolutionMapper.class).countWithCondition(null, " to_char(create_time,'yyyyMMdd')='"+sNo+"' ")+1;
				bd.setSolutions_no(sNo+String.format("%04d", icount));
				bd.setCreate_man_no(userId);
				bd.setCreate_man_name(userName);
				bd.setValid_flag("1");
				bd.setCreate_time(new Date());
				this.getMapper(SlmCustomerTransSolutionMapper.class).insert(bd);
				rm.setState("200");
			}
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!");
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}
	
	/**
	 * 修改数据
	 * @param br
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doUpdateWeb(CommonPage<SlmCustomerTransSolution> sum) throws Exception {
		ResultModel rm=new ResultModel();
		SlmCustomerTransSolution bd = sum.getObject();
		try {
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			
			SlmCustomerTransSolution sca=this.getMapper(SlmCustomerTransSolutionMapper.class).findById(bd.getSolutions_no());
			if(sca==null){
				rm.setState("500");
				rm.setMsgInfo("当前数据不存在!");
				return rm;
			}
				SlmCustomerTransSolution obd = new SlmCustomerTransSolution();
				obd.setCustomer_no(bd.getCustomer_no());
				obd.setSolutions_name(bd.getSolutions_name());
				List<SlmCustomerTransSolution> list2 = this.getMapper(SlmCustomerTransSolutionMapper.class).find(obd, 0, 0);
				if (list2 != null && list2.size() > 0 &&!list2.get(0).getSolutions_no().equals(sca.getSolutions_no())) {
					rm.setState("500");
					rm.setMsgInfo("该公司已存在相同名称的运输方案!");
					return rm;
				}else{
					bd.setUpdate_man_no(userId);
					bd.setUpdate_man_name(userName);
					bd.setUpdate_time(new Date());
					this.getMapper(SlmCustomerTransSolutionMapper.class).update(bd);
				}
			
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;			
	}
	
	/**
	 * 启用、停用数据
	 * @param bd
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdateFlagWeb(CommonPage<SlmCustomerTransSolution> sum) {
		ResultModel rm=new ResultModel();
		List<SlmCustomerTransSolution> obd = sum.getObjectList();
		for(SlmCustomerTransSolution sbr:obd){
			this.getMapper(SlmCustomerTransSolutionMapper.class).update(sbr);
		}
		rm.setState("200");
		return rm;
	}

	//详情查询
	public SlmCustomerTransSolution doQueryOne(String solutionsNo) {
		return this.getMapper(SlmCustomerTransSolutionMapper.class).findById(solutionsNo);
	}
	
}
