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

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="SlmCustomerSatBaseItemService")
public class SlmCustomerSatBaseItemServiceImpl extends BaseServiceImpl<SlmCustomerSatBaseItem,Object> implements SlmCustomerSatBaseItemService {
	public ResultModel doQuery(CommonPage<SlmCustomerSatBaseItem> sum){
		ResultModel rm=new ResultModel();
		try{
			SlmCustomerSatBaseItem oci=sum.getObject();
			int PageIndex = sum.getPageIndex();
			int PageSize = sum.getPageSize();
			PageModel<SlmCustomerSatBaseItem> pm=new PageModel<SlmCustomerSatBaseItem>(PageIndex, PageSize);
			pm.setRows(this.getMapper(SlmCustomerSatBaseItemMapper.class).findWithCondition(oci, null, " item_no,order_no asc ", (PageIndex-1)*PageSize, PageSize));
			pm.setTotal(this.getMapper(SlmCustomerSatBaseItemMapper.class).countWithCondition(oci, null));
			rm.setState("200");
			rm.setData(pm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
		}
		return rm;
	}
	
	public ResultModel doQueryDetail(CommonPage<SlmCustomerSatBaseItem> sum){
		ResultModel rm=new ResultModel();
		try{
			SlmCustomerSatBaseItem oci=sum.getObject();
			rm.setState("200");
			rm.setData(this.getMapper(SlmCustomerSatBaseItemMapper.class).findById(oci.getItem_no().trim()));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
		}
		return rm;
	}
	
	public ResultModel doAdd(CommonPage<SlmCustomerSatBaseItem> sum){
		ResultModel rm=new ResultModel();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		try{
			SlmCustomerSatBaseItem 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")){
				SlmCustomerSatBaseItem ocsb=new SlmCustomerSatBaseItem();
				ocsb.setSat_no(oci.getSat_no());
				String sWhere="";
				if(oci.getP_item_no()!=null&&oci.getP_item_no().trim()!=""){
					sWhere=" p_item_no='"+oci.getP_item_no()+"' ";
				}else{
					sWhere=" p_item_no='' ";
				}
				List<SlmCustomerSatBaseItem> locsb=this.getMapper(SlmCustomerSatBaseItemMapper.class).findWithCondition(ocsb, sWhere, " order_no desc ", 0, 1);
				if(locsb!=null&&locsb.size()>0){
					oci.setOrder_no(locsb.get(0).getOrder_no()+1);
				}else{
					oci.setOrder_no(1l);
				}
				oci.setCreate_man_no(userId);
				oci.setCreate_man_name(userName);
				oci.setCreate_time(new Date());
				oci.setValue_standard("1");
			
				if(oci.getP_item_no()!=null&&!oci.getP_item_no().trim().equals("")){
					oci.setItem_no(oci.getP_item_no()+String.format("%03d", oci.getOrder_no()));
					if(oci.getItem_no().length()>20){
						rm.setState("当前数据不能超出2级!");
						return rm;
					}
				}else{
					oci.setItem_no(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
				}
				this.getMapper(SlmCustomerSatBaseItemMapper.class).insert(oci);
				rm.setState("200");
			}else{
				rm.setState("主数据为发布状态,无法进行该操作!");
			}
		}catch(Exception ex){
			rm.setState(ex.getMessage());
		}
		return rm;
	}
	
	public ResultModel doUpdate(CommonPage<SlmCustomerSatBaseItem> sum){
		ResultModel rm=new ResultModel();
		
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		try{
			SlmCustomerSatBaseItem 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(SlmCustomerSatBaseItemMapper.class).update(oci);
				rm.setState("200");
			}else{
				rm.setState("主数据已发布,无法进行该操作!");
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setState(ex.getMessage());
		}
		return rm;
	}
	
	public ResultModel doDelete(CommonPage<SlmCustomerSatBaseItem> sum){
		ResultModel rm=new ResultModel();
		try{
			SlmCustomerSatBaseItem 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")){
				SlmCustomerSatBaseItem ocii=this.getMapper(SlmCustomerSatBaseItemMapper.class).findById(oci.getItem_no());
				if(ocii!=null&&ocii.getP_item_no()!=null){
					//删除当前项及子项信息
					this.getMapper(SlmCustomerSatBaseItemMapper.class).doDelete(ocii);
				}else{
					this.getMapper(SlmCustomerSatBaseItemMapper.class).delete(oci.getItem_no().trim());
				}
				rm.setState("200");
			}else{
				rm.setState("主数据已发布,无法进行该操作!");
			}
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo(ex.getMessage());
		}
		return rm;
	}
}
