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

import java.text.SimpleDateFormat;
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.CustomerInformation.mapper.SlmCustomerObruInfoMapper;
import market.Slm.CustomerInformation.service.SlmCustomerObruInfoService;
import market.Slm.CustomerInformation.vo.SlmCustomerInfo;
import market.Slm.CustomerInformation.vo.SlmCustomerObruInfo;

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="SlmCustomerObruInfoService")
public class SlmCustomerObruInfoServiceImpl extends BaseServiceImpl<SlmCustomerObruInfo,Object> implements SlmCustomerObruInfoService {
	/**
	 * 查询
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<SlmCustomerObruInfo> sum) {
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();

		PageModel<SlmCustomerObruInfo> pm = new PageModel<SlmCustomerObruInfo>(pageIndex,pageSize);

		try {
			SlmCustomerObruInfo sb=sum.getObject();
			pm.setRows(this.getMapper(SlmCustomerObruInfoMapper.class).find(sb, (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmCustomerObruInfoMapper.class).count(sb));
			rm.setState("200");
			rm.setData(pm);

		} catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 详情查询
	 * */
	public ResultModel doQueryDetailWeb(CommonPage<SlmCustomerObruInfo> sum){
		ResultModel rm=new ResultModel();
		try{
			SlmCustomerObruInfo sco=sum.getObject();
			rm.setData(this.getMapper(SlmCustomerObruInfoMapper.class).findById(sco.getObru_no()));
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo(ex.getMessage());
		}
		return rm;
	}

	/**
	 * 添加数据
	 * @throws Exception 
	 * 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddWeb(CommonPage<SlmCustomerObruInfo> sum) throws Exception {
		ResultModel rm=new ResultModel();
		SlmCustomerObruInfo bd=sum.getObject();
		try {
			SlmCustomerObruInfo obru=new SlmCustomerObruInfo();
			obru.setCustomer_no(bd.getCustomer_no());
			obru.setUnit_no(bd.getUnit_no());
			int count=this.getMapper(SlmCustomerObruInfoMapper.class).count(obru);
			if(count>0){
				rm.setState("500");
				rm.setMsgInfo("已存在相同单位");
				return rm;
			}
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			bd.setObru_no(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3));
			bd.setCreate_man_no(userId);
			bd.setCreate_man_name(userName);
			bd.setValid_flag("1");
			bd.setCreate_time(new Date());
			this.getMapper(SlmCustomerObruInfoMapper.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<SlmCustomerObruInfo> sum) throws Exception {
		ResultModel rm=new ResultModel();
		SlmCustomerObruInfo bd = sum.getObject();
		try {
			SlmCustomerObruInfo obru=new SlmCustomerObruInfo();
			obru.setCustomer_no(bd.getCustomer_no());
			obru.setUnit_no(bd.getUnit_no());
			List<SlmCustomerObruInfo> obruList=this.getMapper(SlmCustomerObruInfoMapper.class).find(obru,0,0);
			if(obruList.size()>0&&!obruList.get(0).getObru_no().equals(bd.getObru_no())){
				rm.setState("500");
				rm.setMsgInfo("已存在相同单位");
				return rm;
			}
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);

			bd.setUpdate_man_no(userId);
			bd.setUpdate_man_name(userName);
			bd.setUpdate_time(new Date());
			this.getMapper(SlmCustomerObruInfoMapper.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<SlmCustomerObruInfo> sum) {
		ResultModel rm=new ResultModel();
		List<SlmCustomerObruInfo> obd = sum.getObjectList();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		for(SlmCustomerObruInfo sbr:obd){
			if("0".equals(sbr.getValid_flag())){
				sbr.setDelete_man_no(userId);
				sbr.setDelete_man_name(userName);
				sbr.setDelete_time(new Date());
			}
			this.getMapper(SlmCustomerObruInfoMapper.class).update(sbr);
		}
		rm.setState("200");
		return rm;
	}

	//新增客户信息或修改客户信息时 为其添加or修改订结收用信息
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddorUpdate(SlmCustomerInfo sci) throws Exception{
		ResultModel rm=new ResultModel();
		//新增一条订结收用
		SlmCustomerObruInfo obru=new SlmCustomerObruInfo();
		obru.setCustomer_no(sci.getCustomer_no());
		obru.setUnit_no(sci.getCustomer_no());
		List<SlmCustomerObruInfo> obruList=this.getMapper(SlmCustomerObruInfoMapper.class).find(obru,0,0);
		if(obruList.size()>0){		//已有订结收用信息
			obru=obruList.get(0);
		}else{
			obru=new SlmCustomerObruInfo();
		}
		obru.setOrder_unit_flag(sci.getOrder_unit_flag());
		obru.setReciver_unit_flag(sci.getReciver_unit_flag());
		obru.setBalance_unit_flag(sci.getBalance_unit_flag());
		obru.setUser_unit_flag(sci.getUser_unit_flag());	
		obru.setCustomer_name(sci.getCustomer_name());
		obru.setUnit_name(sci.getCustomer_name());
		CommonPage<SlmCustomerObruInfo> obruSum=new CommonPage<SlmCustomerObruInfo>();
		if(obruList.size()>0){		//已有订结收用信息
			obruSum.setObject(obru);
			doUpdateWeb(obruSum);
		}else{
			obru.setCustomer_no(sci.getCustomer_no());
			obru.setUnit_no(sci.getCustomer_no());
			obruSum.setObject(obru);
			doAddWeb(obruSum);
		}
		return rm;
	}
}
