package com.unis.detectionnum.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.unis.common.utils.LsBeanUtils;
import com.unis.detectionnum.common.vo.RobotCallDetailListVO;
import com.unis.detectionnum.common.vo.RobotCallRecordBaseVo;
import com.unis.detectionnum.common.vo.RobotCallRecordVO;
import com.unis.detectionnum.server.dao.RobotCallDetailListDao;
import com.unis.detectionnum.server.dao.RobotCallRecordDao;
import com.unis.detectionnum.server.entity.RobotCallDetailListDO;
import com.unis.detectionnum.server.entity.RobotCallRecordDO;
import com.unis.detectionnum.server.service.RobotCallRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @ClassName: RobotCallRecordServiceImpl
* @Description: TODO
* @author yangkaifeng
* @date 2019年12月17日 下午5:06:19
*/
@Service
public class RobotCallRecordServiceImpl implements RobotCallRecordService {
    @Autowired
    RobotCallRecordDao robotCallRecordDao;
    @Autowired
    RobotCallDetailListDao robotCallDetailListDao;
    @Override
    public int insert(RobotCallRecordBaseVo robotCallRecordBaseVo) {
        Long recordId;
        int returnRecord=0;
        RobotCallRecordVO robotCallRecordVO = robotCallRecordBaseVo.getData();
        robotCallRecordVO.setDataType(robotCallRecordBaseVo.getDataType());
        if(robotCallRecordVO!=null && robotCallRecordVO.getCallDetailList().size()!=0){
            RobotCallRecordDO robotCallRecordDO=new RobotCallRecordDO();
            LsBeanUtils.copyAllProperties(robotCallRecordVO,robotCallRecordDO);
            robotCallRecordDO.setCustomerConcern(JSON.toJSONString(robotCallRecordVO.getCustomerConcern()));
            robotCallRecordDO.setAttributes(JSON.toJSONString(robotCallRecordVO.getAttributes()));
            robotCallRecordDO.setProperties(JSON.toJSONString(robotCallRecordVO.getProperties()));
            robotCallRecordDO.setCreateTime(new Date());
            returnRecord=robotCallRecordDao.insert(robotCallRecordDO);
            recordId=robotCallRecordDO.getRecordId();
           for(int i=0;i<robotCallRecordVO.getCallDetailList().size();i++){
                RobotCallDetailListVO robotCallDetailListVO=new RobotCallDetailListVO();
                RobotCallDetailListDO robotCallDetailListDO=new RobotCallDetailListDO();
                robotCallDetailListVO.setCallDetailId(robotCallRecordVO.getCallDetailList().get(i).getCallDetailId());
                robotCallDetailListVO.setRecordId(recordId);
                robotCallDetailListVO.setText(robotCallRecordVO.getCallDetailList().get(i).getText());
                robotCallDetailListVO.setType(robotCallRecordVO.getCallDetailList().get(i).getType());
                robotCallDetailListVO.setCreateTime(new Date());
                LsBeanUtils.copyAllProperties(robotCallDetailListVO,robotCallDetailListDO);
                int detailId=robotCallDetailListDao.insert(robotCallDetailListDO);
            }
        }
        return returnRecord;
    }
}
