package com.ruoyi.system.service.impl;

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

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ClewMapper;
import com.ruoyi.system.domain.Clew;
import com.ruoyi.system.service.IClewService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.ObjectUtils;

/**
 * 线索Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-15
 */
@Slf4j
@Service
public class ClewServiceImpl implements IClewService
{
    @Autowired
    private ClewMapper clewMapper;

    /**
     * 查询线索
     *
     * @param id 线索主键
     * @return 线索
     */
    @Override
    public Clew selectClewById(Long id)
    {
        return clewMapper.selectClewById(id);
    }

    /**
     * 查询线索列表
     *
     * @param clew 线索
     * @return 线索
     */
    @Override
    public List<Clew> selectClewList(Clew clew)
    {
        return clewMapper.selectClewList(clew);
    }

    @Override
    public List<Clew> selectClewPublicList(Clew clew)
    {
        return clewMapper.selectClewPublicList(clew);
    }

    @Override
    public List<Clew> selectClewQualityList(Clew clew)
    {
        return clewMapper.selectClewQualityList(clew);
    }

    /**
     * 新增线索
     *
     * @param clew 线索
     * @return 结果
     */
    @Override
    public int insertClew(Clew clew)
    {
        // 线索短信通知
        try {
            JSONObject smsContent = new JSONObject();
            smsContent.put("uid", "12347");
            smsContent.put("pwd", "wJgzaC0u");
            smsContent.put("mobile", "18580868339");
            if(clew.getSourceApp() != null && clew.getSourceApp() == 2L){
                smsContent.put("content", "你有一条来自大象，新线索生成，客户电话：" + clew.getPhone() + "（请注意及时跟进）");
            }else {
                smsContent.put("content", "你有一条来自黑猫，新线索生成，客户电话：" + clew.getPhone() + "（请注意及时跟进）");
            }
            if(!ObjectUtil.equals(clew.getSourceApp(), 8L)){
                log.info("请求傲众短信入参:{}", smsContent.toJSONString());
                String s = HttpUtils.sendPost("http://www.aozhongyun.com/Admin/index.php/Message/send", smsContent.toJSONString());
                log.info("请求傲众短信url:{}, 响应:{}", "http://www.aozhongyun.com/Admin/index.php/Message/send", s);
            }
        }catch (Exception e){
            log.error("请求傲众短信url:{}, 报错:{}","http://www.aozhongyun.com/Admin/index.php/Message/send", e.getMessage());
        }
        clew.setCreateTime(DateUtils.getNowDate());
        return clewMapper.insertClew(clew);
    }

    /**
     * 修改线索
     *
     * @param clew 线索
     * @return 结果
     */
    @Override
    public int updateClew(Clew clew)
    {
        return clewMapper.updateClew(clew);
    }

    @Override
    public int updateBatchClew(Clew clew)
    {
        return clewMapper.updateBatchClew(clew);
    }

    @Override
    public int catchClewToQuality(Clew clew)
    {
        return clewMapper.updateClew(clew);
    }

    /**
     * 批量删除线索
     *
     * @param ids 需要删除的线索主键
     * @return 结果
     */
    @Override
    public int deleteClewByIds(String ids)
    {
        return clewMapper.deleteClewByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除线索信息
     *
     * @param id 线索主键
     * @return 结果
     */
    @Override
    public int deleteClewById(Long id)
    {
        return clewMapper.deleteClewById(id);
    }

    @Override
    public void updateSuitAbleNotAddWx(Date beginTime) {
        clewMapper.updateSuitAbleNotAddWx(beginTime);
    }

    @Override
    public void updateSuitAbleNotDeal(Date beginTime) {
        clewMapper.updateSuitAbleNotDeal(beginTime);
    }
}
