/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.SlmCustomerSatBaseSuggestMapper;
import market.Slm.CustomerInformation.service.SlmCustomerSatBaseSuggestService;
import market.Slm.CustomerInformation.vo.SlmCustomerSatBaseInfo;
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="SlmCustomerSatBaseSuggestService")
public class SlmCustomerSatBaseSuggestServiceImpl extends BaseServiceImpl<SlmCustomerSatBaseSuggest,Object> implements SlmCustomerSatBaseSuggestService {
	public ResultModel doQuery(CommonPage<SlmCustomerSatBaseSuggest> sum){
		ResultModel rm=new ResultModel();
		try{
			int PageIndex=sum.getPageIndex();
			int PageSize=sum.getPageSize();
			SlmCustomerSatBaseSuggest oci=sum.getObject();
			PageModel<SlmCustomerSatBaseSuggest> pm=new PageModel<SlmCustomerSatBaseSuggest>(PageIndex, PageSize);
			pm.setRows(this.getMapper(SlmCustomerSatBaseSuggestMapper.class).findWithCondition(oci, null, " order_no asc ", (PageIndex-1)*PageSize, PageSize));
			pm.setTotal(this.getMapper(SlmCustomerSatBaseSuggestMapper.class).countWithCondition(oci, null));
			rm.setState("200");
			rm.setData(pm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
		}
		return rm;
	}
	
	public ResultModel doQueryDetail(CommonPage<SlmCustomerSatBaseSuggest> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(SlmCustomerSatBaseSuggestMapper.class).findById(sum.getObject().getSuggest_no().trim()));
			rm.setState("200");
		}catch(Exception ex){
			rm.setMsgInfo("查询失败");
			rm.setState("500");
		}
		return rm;
	}
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAdd(CommonPage<SlmCustomerSatBaseSuggest> sum) throws Exception{
		ResultModel rm=new ResultModel();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		try{
			SlmCustomerSatBaseSuggest oci=sum.getObject();
			SlmCustomerSatBaseInfo ocsbi=this.getMapper(SlmCustomerSatBaseInfoMapper.class).findById(oci.getSat_no());
			if(ocsbi.getValid_flag().trim().equals("2")&&ocsbi.getEvaluate_result().trim().equals("1")){
				SlmCustomerSatBaseSuggest ocsb=new SlmCustomerSatBaseSuggest();
				ocsb.setSat_no(oci.getSat_no());
				List<SlmCustomerSatBaseSuggest> locsb=this.getMapper(SlmCustomerSatBaseSuggestMapper.class).findWithCondition(ocsb, null, " order_no desc ", 0, 1);
				if(locsb!=null&&locsb.size()>0){
					oci.setOrder_no(locsb.get(0).getOrder_no()+1);
				}else{
					oci.setOrder_no(1);
				}
				oci.setCreate_man_no(userId);
				oci.setCreate_man_name(userName);
				oci.setCreate_time(new Date());
				oci.setValid_flag("1");
				
				oci.setSuggest_no(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(3));
				this.getMapper(SlmCustomerSatBaseSuggestMapper.class).insert(oci);
				rm.setState("200");
			}else{
				rm.setState("主数据已发布,无法进行该操作!");
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo(ex.getMessage());
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doUpdate(CommonPage<SlmCustomerSatBaseSuggest> sum) throws Exception{
		ResultModel rm=new ResultModel();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		try{
			SlmCustomerSatBaseSuggest oci=sum.getObject();
			SlmCustomerSatBaseInfo ocsbi=this.getMapper(SlmCustomerSatBaseInfoMapper.class).findById(oci.getSat_no());
			if(ocsbi.getValid_flag().trim().equals("2")&&ocsbi.getEvaluate_result().trim().equals("1")){
				oci.setUpdate_man_no(userId);
				oci.setUpdate_man_name(userName);
				oci.setUpdate_time(new Date());
				
				this.getMapper(SlmCustomerSatBaseSuggestMapper.class).update(oci);
				rm.setState("200");
			}else{
				rm.setState("500");
				rm.setState("主数据已发布,无法进行该操作!");
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setState("操作失败！");
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDelete(CommonPage<SlmCustomerSatBaseSuggest> sum) throws Exception{
		ResultModel rm=new ResultModel();
		try{
			SlmCustomerSatBaseSuggest oci=sum.getObject();
			SlmCustomerSatBaseInfo ocsbi=this.getMapper(SlmCustomerSatBaseInfoMapper.class).findById(oci.getSat_no());
			if(ocsbi.getValid_flag().trim().equals("2")&&ocsbi.getEvaluate_result().trim().equals("1")){
				this.getMapper(SlmCustomerSatBaseSuggestMapper.class).delete(oci.getSuggest_no().trim());
				rm.setState("200");
			}else{
				rm.setState("500");
				rm.setState("主数据已发布,无法进行该操作!");
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setState("操作失败！");
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}
}
