package com.ruixuan.salesLeads.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.core.domain.ResultResponse;
import com.ruixuan.common.utils.PageUtils;
import com.ruixuan.common.utils.StringUtils;
import com.ruixuan.salesLeads.mapper.SaleClueCallRecordMapper;
import com.ruixuan.salesLeads.mapper.SaleClueCallResultMapper;
import com.ruixuan.salesLeads.mapper.SaleClueInfoMapper;
import com.ruixuan.salesLeads.mapper.TestMapper;
import com.ruixuan.salesLeads.pojo.SaleClueCallResultPojo;
import com.ruixuan.salesLeads.pojo.TestPojo;
import com.ruixuan.salesLeads.service.ISaleClueCallRecordService;
import com.ruixuan.salesLeads.service.ITestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * ***************************************************
 *
 * @Auther: zianY
 * @Descipion: demo测试
 * @CreateDate: 2022-05-19
 * ****************************************************
 */
@Service
@Slf4j
public class SaleClueCallRecordServiceImpl implements ISaleClueCallRecordService {


    @Autowired
    private SaleClueCallRecordMapper saleClueCallRecordMapper;

    @Autowired
    private SaleClueCallResultMapper saleClueCallResultMapper;

    @Autowired
    private SaleClueInfoMapper saleClueInfoMapper;

    /**
     *
     * @description: 推送结果记录信息
     * 包括 导入号码记录 和 通话记录
     * @param json
     * @return: com.ruixuan.common.core.domain.ResultResponse
     * @author: zianY
     */
    @Override
    public ResultResponse saveClueCallInfo(JSONObject json) {
        log.info("callBackUrl--------->>>{}",json);
        if (StringUtils.isNull(json)){
            return ResultResponse.error("callBackUrl--->>>没有接收到有效数据");
        }
        //返回参数包含 代表是 导入数据信息
        if (json.containsKey("success") && json.containsKey("error")){
            log.info("callBackUrl-----type----{}----->>>{}","importPhone",json);
            //保存 导入号码的 回调信息
            saleClueCallRecordMapper.addSaleCallRecord(json.toJSONString());

            JSONArray array = json.getJSONArray("error");
            if (!array.isEmpty()){
                log.info("销售线索导入失败------》》》》{}",array);
            }

            JSONArray success = json.getJSONArray("success");
            if (!success.isEmpty()){
                for (Object obj : success) {
                    JSONObject jsonObject = (JSONObject) JSONObject.toJSON(obj);
                    //修改线索的导入记录状态
                    saleClueInfoMapper.updateSaleClueImportPhoneFlag(Long.valueOf(jsonObject.getString("id")),1);
                    log.info("销售线索导入成功，修改线索导入状态------线索id》》》》{}",jsonObject.getString("id"));
                }
            }


            return ResultResponse.successMsg("callBackUrl--->>>导入号码回调信息成功",json);
        }
        log.info("callBackUrl-----type----{}----->>>{}","phoneResult",json);
        //保存通话记录
        SaleClueCallResultPojo callResultPojo = JSON.parseObject(json.toJSONString(), SaleClueCallResultPojo.class);
        callResultPojo.setCustomerPhone(SaleClueCallResultPojo.decryptPhone(callResultPojo.getCustomerPhone()));
        saleClueCallResultMapper.addSaleCallResult(callResultPojo);
        return ResultResponse.successMsg("callBackUrl--->>>保存通话记录信息成功",json);
    }
}
