/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-07-19
 * 描述            ：客户满意度调查表头实绩表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Slm.CustomerInformation.impl;

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

import market.Com.pb.PbMethod;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.CustomerInformation.mapper.SlmCustomerPersonInfoMapper;
import market.Slm.CustomerInformation.mapper.SlmCustomerSatInfoMapper;
import market.Slm.CustomerInformation.mapper.SlmCustomerSatItemMapper;
import market.Slm.CustomerInformation.service.SlmCustomerSatInfoService;
import market.Slm.CustomerInformation.vo.SlmCustomerPersonInfo;
import market.Slm.CustomerInformation.vo.SlmCustomerSatInfo;
import market.Slm.CustomerInformation.vo.SlmCustomerSatItem;

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="SlmCustomerSatInfoService")
public class SlmCustomerSatInfoServiceImpl extends BaseServiceImpl<SlmCustomerSatInfo,Object> implements SlmCustomerSatInfoService {
	public ResultModel doQuery(CommonPage<SlmCustomerSatInfo> sum){
		ResultModel rm=new ResultModel();
		try{
			int PageIndex=sum.getPageIndex();
			int PageSize=sum.getPageSize();
			SlmCustomerSatInfo oci=sum.getObject();
			//============如果是客户，那么其用户名跟客户编号应该在session里有，跟客户用户表匹配下即可=========
			try{
				SlmCustomerPersonInfo sci=new SlmCustomerPersonInfo();
				sci.setUser_id((String)this.getSession().getAttribute(HomeService.SESSION_USER_ID));
				sci.setCustomer_no((String)this.getSession().getAttribute(HomeService.SESSION_DEPT_ID));
				int icount=this.getMapper(SlmCustomerPersonInfoMapper.class).count(sci);
				if(icount>0) {
					oci.setCustomer_no(sci.getCustomer_no());
				}
			}catch(Exception ex){}
			//===========================================================================================
			
			String sWhere="";
			if(oci!=null&&oci.getModel_name()!=null){
				sWhere=" model_name like concat('%','"+oci.getModel_name().trim()+"','%')";
			}
			PageModel<SlmCustomerSatInfo> pm=new PageModel<SlmCustomerSatInfo>(PageIndex, PageSize);
			pm.setRows(this.getMapper(SlmCustomerSatInfoMapper.class).findWithCondition(oci, sWhere, " create_time desc ", (PageIndex-1)*PageSize, PageSize));
			pm.setTotal(this.getMapper(SlmCustomerSatInfoMapper.class).countWithCondition(oci, sWhere));
			
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
		}
		return rm;
	}
	
	public ResultModel doQueryDetail(SlmCustomerSatInfo oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(SlmCustomerSatInfoMapper.class).findById(oci.getActual_no().trim()));
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
		}
		return rm;
	}
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAdd(CommonPage<SlmCustomerSatInfo> sum) throws Exception{
		ResultModel rm=new ResultModel();
		try{
			SlmCustomerSatInfo oci=sum.getObject();
			oci.setActual_no(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3));
			oci.setValid_flag("1");
			oci.setCreate_time(new Date());
			oci.setCreate_man_no((String)this.getSession().getAttribute(HomeService.SESSION_USER_ID));
			oci.setCreate_man_no((String)this.getSession().getAttribute(HomeService.SESSION_USER_NAME));
			this.getMapper(SlmCustomerSatInfoMapper.class).insert(oci);
			rm.setState("200");
		}catch(Exception ex){
			rm.setData("操作失败！");
			rm.setState("500");
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doUpdate(CommonPage<SlmCustomerSatInfo> sum) throws Exception{
		ResultModel rm=new ResultModel();
		try{
			SlmCustomerSatInfo oci=sum.getObject();
			this.getMapper(SlmCustomerSatInfoMapper.class).update(oci);
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setData("操作失败！");
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDelete(CommonPage<SlmCustomerSatInfo> sum) throws Exception{
		ResultModel rm=new ResultModel();
		try{
			SlmCustomerSatInfo oci=sum.getObject();
			SlmCustomerSatItem ocsi=new SlmCustomerSatItem();
			ocsi.setActual_no(oci.getActual_no());
			int icount=this.getMapper(SlmCustomerSatItemMapper.class).count(ocsi);
			if(icount>0){
				rm.setState("存在子项信息,无法删除!");
				rm.setState("200");
			}else{
				this.getMapper(SlmCustomerSatInfoMapper.class).delete(oci.getActual_no().trim());
				rm.setState("200");
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setData("操作失败！");
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}
}
