/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-07-19
 * 描述            ：客户满意度调查表头基础表
 * 注意事项     ：
 * 遗留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.SlmCustomerSatBaseInfoMapper;
import market.Slm.CustomerInformation.mapper.SlmCustomerSatBaseItemMapper;
import market.Slm.CustomerInformation.mapper.SlmCustomerSatBaseSuggestMapper;
import market.Slm.CustomerInformation.service.SlmCustomerSatBaseInfoService;
import market.Slm.CustomerInformation.vo.SlmCustomerSatBaseInfo;
import market.Slm.CustomerInformation.vo.SlmCustomerSatBaseItem;
import market.Slm.CustomerInformation.vo.SlmCustomerSatBaseSuggest;

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="SlmCustomerSatBaseInfoService")
public class SlmCustomerSatBaseInfoServiceImpl extends BaseServiceImpl<SlmCustomerSatBaseInfo,Object> implements SlmCustomerSatBaseInfoService {
	/**
	 * 多条件查询
	 * @param sum
	 * @return
	 */
	public ResultModel doQuery(CommonPage<SlmCustomerSatBaseInfo> sum){
		ResultModel rm=new ResultModel();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		try{
			PageModel<SlmCustomerSatBaseInfo> pm = new PageModel<SlmCustomerSatBaseInfo>(pageIndex,pageSize);
			SlmCustomerSatBaseInfo sb=sum.getObject()==null?(new SlmCustomerSatBaseInfo()):sum.getObject();
			String sWhere="";
			if(sb!=null&&!"".equals(sb.getModel_name())&&sb.getModel_name()!=null){
				sWhere=" model_name LIKE '%"+sb.getModel_name()+"%' ";
				sb.setModel_name(null);
			}

			pm.setRows(this.getMapper(SlmCustomerSatBaseInfoMapper.class).findWithCondition(sb, sWhere, " model_name ", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(SlmCustomerSatBaseInfoMapper.class).countWithCondition(sb,sWhere));

			rm.setState("200");
			rm.setData(pm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 查询单条明细
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryDetail(CommonPage<SlmCustomerSatBaseInfo> sum){
		ResultModel rm=new ResultModel();
		try{
			SlmCustomerSatBaseInfo satInfo = this.getMapper(SlmCustomerSatBaseInfoMapper.class).findById(sum.getObject().getSat_no());
			
			if(satInfo!=null&&satInfo.getSat_no()!=null){
				SlmCustomerSatBaseItem s=new SlmCustomerSatBaseItem();
				s.setSat_no(satInfo.getSat_no());
				satInfo.setChild(this.getMapper(SlmCustomerSatBaseItemMapper.class).find(s, 0, 0));
			}
			rm.setData(satInfo);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
		}
		return rm;
	}
	
	/**
	 * 添加数据
	 * 
	 * @param bd
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAdd(CommonPage<SlmCustomerSatBaseInfo> sum) throws Exception {
		ResultModel rm=new ResultModel();
		try {
			SlmCustomerSatBaseInfo bd=sum.getObject();
			
			//检索是否存在同编号的数据，版本号+1
			SlmCustomerSatBaseInfo oci=new SlmCustomerSatBaseInfo();
			oci.setModel_no(bd.getModel_no().trim());
			int icount=this.getMapper(SlmCustomerSatBaseInfoMapper.class).count(oci);
			
			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());
			//icount=this.getMapper(SlmCustomerSatBaseInfoMapper.class).countWithCondition(null, " to_char(create_time,'yyyyMMdd')='"+sNo+"' ")+1;
			bd.setModel_edition(String.format("%04d", icount+1));
			bd.setValid_flag("2");
			bd.setEvaluate_result("1");
			bd.setSat_no(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3));
			bd.setCreate_man_no(userId);
			bd.setCreate_man_name(userName);
			bd.setCreate_time(new Date());
			bd.setModel_name(bd.getModel_name().trim());

			this.getMapper(SlmCustomerSatBaseInfoMapper.class).insert(bd);
			rm.setState("200");

		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("服务端异常，新增失败!"+e.getMessage());
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 修改数据
	 * @param bd
	 * @return
	 */
	public ResultModel doUpdate(CommonPage<SlmCustomerSatBaseInfo> sum) {
		ResultModel rm=new ResultModel();
		SlmCustomerSatBaseInfo bd = sum.getObject();
		try {
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);

			SlmCustomerSatBaseInfo sci=this.getMapper(SlmCustomerSatBaseInfoMapper.class).findById(bd.getModel_no());
			if(sci!=null){
				if("2".equals(sci.getValid_flag()) && "1".equals(sci.getEvaluate_result())){
					bd.setUpdate_man_no(userId);
					bd.setUpdate_man_name(userName);
					bd.setUpdate_time(new Date());
					this.getMapper(SlmCustomerSatBaseInfoMapper.class).update(bd);
					rm.setState("200");
				} else {
					rm.setState("500");
					rm.setState("当前状态无法进行该操作!");
				}				
			}else{
				rm.setState("500");
				rm.setMsgInfo("未找到模板数据!");
				return rm;
			}

		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 删除数据
	 * @param bd
	 * @return
	 */
	public ResultModel doDelete(CommonPage<SlmCustomerSatBaseInfo> sum){
		ResultModel rm=new ResultModel();
		try{
			SlmCustomerSatBaseInfo oci = sum.getObject();
			
			SlmCustomerSatBaseInfo ocv=this.getMapper(SlmCustomerSatBaseInfoMapper.class).findById(oci.getSat_no());
			if(ocv.getValid_flag().trim().equals("2")&&ocv.getEvaluate_result().trim().equals("1")){
				SlmCustomerSatBaseItem item=new SlmCustomerSatBaseItem();
				item.setSat_no(oci.getSat_no());
				int icount=this.getMapper(SlmCustomerSatBaseItemMapper.class).count(item);
				if(icount>0){
					rm.setState("存在子项信息，无法删除!");
					return rm;
				}
				SlmCustomerSatBaseSuggest sug=new SlmCustomerSatBaseSuggest();
				sug.setSat_no(oci.getSat_no());
				icount=this.getMapper(SlmCustomerSatBaseSuggestMapper.class).count(sug);
				if(icount>0){
					rm.setState("存在子项信息，无法删除!");
					return rm;
				}
			}else{
				rm.setState("当前状态无法删除!");
				return rm;
			}
			
			this.getMapper(SlmCustomerSatBaseInfoMapper.class).delete(oci.getSat_no().trim());
			rm.setState("200");
		}catch(Exception ex){
			rm.setState(ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 启用、停用数据
	 * @param bd
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdateFlag(CommonPage<SlmCustomerSatBaseInfo> sum) {
		ResultModel rm=new ResultModel();
		List<SlmCustomerSatBaseInfo> obd = sum.getObjectList();
		//String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		//String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		for(SlmCustomerSatBaseInfo sci :obd){
			SlmCustomerSatBaseInfo sc=new SlmCustomerSatBaseInfo();
			sc.setSat_no(sci.getSat_no());
			sc.setValid_flag(sci.getValid_flag());
			this.getMapper(SlmCustomerSatBaseInfoMapper.class).update(sc);
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 模板发布
	 * @param sum
	 * @return
	 */
	public ResultModel doEvaluateChange(CommonPage<SlmCustomerSatBaseInfo> sum) {
		ResultModel rm=new ResultModel();
		try{
			SlmCustomerSatBaseInfo oci=sum.getObject();
			SlmCustomerSatBaseInfo ocv=this.getMapper(SlmCustomerSatBaseInfoMapper.class).findById(oci.getSat_no());
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			if(ocv.getValid_flag().trim().equals("2")){
				if(ocv.getEvaluate_result().trim().equals("1")){
					oci.setEvaluate_result("2");
					oci.setSubmit_man_no(userId);
					oci.setSubmit_man_name(userName);
					//oci.setValid_flag("3");
					this.getMapper(SlmCustomerSatBaseInfoMapper.class).update(oci);
				}
				rm.setState("200");
			}else{
				rm.setState("当前数据无法进行发布!");
			}
			
		}catch(Exception ex){
			rm.setState(ex.getMessage());
		}
		return rm;
	}
}
