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

import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.CustomerInformation.mapper.SlmCustomerSatInfoMapper;
import market.Slm.CustomerInformation.mapper.SlmCustomerSatItemMapper;
import market.Slm.CustomerInformation.mapper.SlmCustomerSatSuggestMapper;
import market.Slm.CustomerInformation.service.SlmCustomerSatItemService;
import market.Slm.CustomerInformation.vo.SlmCustomerSatInfo;
import market.Slm.CustomerInformation.vo.SlmCustomerSatItem;
import market.Slm.CustomerInformation.vo.SlmCustomerSatSuggest;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="SlmCustomerSatItemService")
public class SlmCustomerSatItemServiceImpl extends BaseServiceImpl<SlmCustomerSatItem,Object> implements SlmCustomerSatItemService {
	public ResultModel doQuery(CommonPage<SlmCustomerSatItem> sum){
		ResultModel rm=new ResultModel();
		try{
			SlmCustomerSatItem oci=sum.getObject();
			if(oci==null||oci.getActual_no()==null){
				rm.setState("500");
				rm.setMsgInfo("请传入正确参数!");;
				return rm;
			}
			rm.setState("200");
			rm.setData(this.getMapper(SlmCustomerSatItemMapper.class).findWithCondition(oci, null," item_name asc", 0,  0));
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
		}
		return rm;
	}
	
	//问卷 提交
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAdd(CommonPage<SlmCustomerSatInfo> sum){
		ResultModel rm=new ResultModel();
		SlmCustomerSatInfo ocsi=sum.getObject();
		List<SlmCustomerSatItem> loci = sum.getObject().getItem();
		List<SlmCustomerSatSuggest> locs = sum.getObject().getSuggest();
		
		SlmCustomerSatInfo ocsc=new SlmCustomerSatInfo();
		ocsc.setValid_flag("1");
		ocsc.setActual_no(ocsi.getActual_no());
		int icount=this.getMapper(SlmCustomerSatInfoMapper.class).count(ocsc);
		if(icount==0){
			rm.setState("已被评分!");
			return rm;
		}
		
		ocsi.setValid_flag("2");
		ocsi.setFill_time(new Date());
		ocsi.setFill_man_name((String)this.getSession().getAttribute(HomeService.SESSION_USER_NAME));//mca.getAccount_name());
		ocsi.setFill_man_no((String)this.getSession().getAttribute(HomeService.SESSION_USER_ID));
		this.getMapper(SlmCustomerSatInfoMapper.class).update(ocsi);
		

		int i=0;
		for(SlmCustomerSatItem oci:loci){
			oci.setSat_item_no(oci.getActual_no().substring(0,17)+String.format("%03d", i+1));
			this.getMapper(SlmCustomerSatItemMapper.class).insert(oci);
			i++;
		}
		
		i=0;
		
		for(SlmCustomerSatSuggest ocs:locs){
			ocs.setSat_suggest_no(ocs.getActual_no().substring(0,17)+String.format("%03d", i+1));
			this.getMapper(SlmCustomerSatSuggestMapper.class).insert(ocs);
			i++;
		}
		rm.setState("200");
		return rm;
	}
}
