package com.fs.qw.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.qw.domain.FsAppContactWay;
import com.fs.qw.domain.QwAppContactWay;
import com.fs.qw.mapper.QwAppContactWayMapper;
import com.fs.qw.service.IFsAppContactWayService;
import com.fs.qw.service.IQwAppContactWayService;
import com.fs.qwApi.Result.QwAddContactWayResult;
import com.fs.qwApi.domain.QwResult;
import com.fs.qwApi.param.QwAddContactWayParam;
import com.fs.qwApi.param.QwGetContactWayParam;
import com.fs.qwApi.service.QwApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * app活码Service业务层处理
 *
 * @author fs
 * @date 2024-12-11
 */
@Service
public class QwAppContactWayServiceImpl implements IQwAppContactWayService
{
    @Autowired
    private QwAppContactWayMapper qwAppContactWayMapper;
    @Autowired
    private IFsAppContactWayService appContactWayService;

    @Autowired
    private QwApiService qwApiService;
    /**
     * 查询app活码
     *
     * @param id app活码主键
     * @return app活码
     */
    @Override
    public QwAppContactWay selectQwAppContactWayById(Long id)
    {
        return qwAppContactWayMapper.selectQwAppContactWayById(id);
    }

    /**
     * 查询app活码列表
     *
     * @param qwAppContactWay app活码
     * @return app活码
     */
    @Override
    public List<QwAppContactWay> selectQwAppContactWayList(QwAppContactWay qwAppContactWay)
    {
        return qwAppContactWayMapper.selectQwAppContactWayList(qwAppContactWay);
    }

    /**
     * 新增app活码
     *
     * @param qwAppContactWay app活码
     * @return 结果
     */
    @Override
    public int insertQwAppContactWay(QwAppContactWay qwAppContactWay)
    {
        qwAppContactWay.setCreateTime(DateUtils.getNowDate());
        return qwAppContactWayMapper.insertQwAppContactWay(qwAppContactWay);
    }

    /**
     * 修改app活码
     *
     * @param qwAppContactWay app活码
     * @return 结果
     */
    @Override
    public int updateQwAppContactWay(QwAppContactWay qwAppContactWay)
    {
        return qwAppContactWayMapper.updateQwAppContactWay(qwAppContactWay);
    }

    /**
     * 批量删除app活码
     *
     * @param ids 需要删除的app活码主键
     * @return 结果
     */
    @Override
    public int deleteQwAppContactWayByIds(Long[] ids)
    {
        return qwAppContactWayMapper.deleteQwAppContactWayByIds(ids);
    }

    /**
     * 删除app活码信息
     *
     * @param id app活码主键
     * @return 结果
     */
    @Override
    public int deleteQwAppContactWayById(Long id)
    {
        return qwAppContactWayMapper.deleteQwAppContactWayById(id);
    }

    @Override
    public String getAppContactWayImgCode(Long userId) {

        QwAppContactWay qw = qwAppContactWayMapper.selectQwAppContactWayByUserId(userId);
        if (qw!=null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.HOUR_OF_DAY, 2);
            qw.setExpirationTime(calendar.getTime());
            qwAppContactWayMapper.updateQwAppContactWay(qw);
            return qw.getQrCode();
        }else {
            QwAddContactWayParam qwAddContactWayParam = new QwAddContactWayParam();
            FsAppContactWay appContactWay = appContactWayService.selectContactWayRandom();
            if (appContactWay!=null){
                List<String> users = Arrays.asList(appContactWay.getQwUserIds().split(","));;
                String corpId = appContactWay.getCorpId();
                QwAppContactWay qwAppContactWay = new QwAppContactWay();
                qwAppContactWay.setStatus(1);
                qwAppContactWay.setWayStatus(0);
                qwAppContactWay.setCorpId(corpId);
                qwAppContactWay.setQwUserIds(appContactWay.getQwUserIds());
                qwAppContactWay.setUserId(userId);
                qwAppContactWay.setCreateTime(DateUtils.getNowDate());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.HOUR_OF_DAY, 2);
                qwAppContactWay.setExpirationTime(calendar.getTime());
                qwAppContactWayMapper.insertQwAppContactWay(qwAppContactWay);
                Long id = qwAppContactWay.getId();
                qwAddContactWayParam.setType(2);
                qwAddContactWayParam.setScene(2);
                qwAddContactWayParam.setState("appCont:"+corpId+":"+id);
                qwAddContactWayParam.setUser(users);
                System.out.println("users:"+users);
                qwAddContactWayParam.setSkip_verify(true);
                QwAddContactWayResult qwAddContactWayResult = qwApiService.addContactWay(qwAddContactWayParam, corpId);
                System.out.println(qwAddContactWayResult);
                if (qwAddContactWayResult.getErrcode()==0){
                    qwAppContactWay.setState("appCont:"+corpId+":"+id);
                    qwAppContactWay.setConfigId(qwAddContactWayResult.getConfig_id());
                    qwAppContactWay.setQrCode(qwAddContactWayResult.getQr_code());
                    qwAppContactWay.setWayStatus(1);
                    qwAppContactWayMapper.updateQwAppContactWay(qwAppContactWay);
                }else {
                    qwAppContactWayMapper.deleteQwAppContactWayById(qwAppContactWay.getId());
                    return "错误请联系客服";
                }
                return qwAddContactWayResult.getQr_code();
            }
        }

        return  "未配置";

    }

    @Override
    public void expirationQwAppCountWay() {
        List<QwAppContactWay> fsAppContactWays = qwAppContactWayMapper.selectContactWayExpiration();
        for (QwAppContactWay qwAppContactWay : fsAppContactWays) {
            if (qwAppContactWay!=null){
                QwGetContactWayParam qwGetContactWayParam = new QwGetContactWayParam();
                qwGetContactWayParam.setConfig_id(qwAppContactWay.getConfigId());
                QwResult qwResult = qwApiService.delContactWay(qwGetContactWayParam, qwAppContactWay.getCorpId());
                if (qwResult.getErrcode()==0||qwResult.getErrcode()==41044){
                    QwAppContactWay w = new QwAppContactWay();
                    w.setId(qwAppContactWay.getId());
                    w.setWayStatus(2);
                    qwAppContactWayMapper.updateQwAppContactWay(w);
                }
            }
        }
    }
}
