package com.gic.haoban.manage.service.service.out.impl;

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

import com.gic.haoban.manage.service.dao.mapper.TabHaobanWelcomeMediaMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gic.api.base.commons.JSONResponse;
import com.gic.commons.util.ToolUtil;
import com.gic.haoban.manage.api.dto.WxEnterpriseDTO;
import com.gic.haoban.manage.api.dto.WxEnterpriseQwDTO;
import com.gic.haoban.manage.service.config.Config;
import com.gic.haoban.manage.service.dao.mapper.TabMiniprogramSettingMapper;
import com.gic.haoban.manage.service.entity.TabMiniprogramSetting;
import com.gic.haoban.manage.service.service.MiniprogramSettingService;
import com.gic.haoban.manage.service.service.WxEnterpriseService;
import com.gic.haoban.manage.service.util.CommonUtil;
import com.gic.wechat.api.enums.QywxMediaTypeEnum;
import com.gic.wechat.api.service.qywx.QywxSuiteApiService;

/**
 * Created 2021/4/16.
 *
 * @author hua
 */
@Service
public class MiniprogramSettingServiceImpl implements MiniprogramSettingService {

    @Autowired
    private TabMiniprogramSettingMapper tabMiniprogramSettingMapper;

    @Autowired
    private QywxSuiteApiService qywxSuiteApiService;

    @Autowired
    private Config config;

    @Autowired
    private WxEnterpriseService wxEnterpriseService;

    @Autowired
    private TabHaobanWelcomeMediaMapper tabHaobanWelcomeMediaMapper;

    @Override
    public TabMiniprogramSetting getById(String miniprogramSettingId) {
        TabMiniprogramSetting miniprogramSetting = tabMiniprogramSettingMapper.selectByPrimaryKey(miniprogramSettingId);
        if (Objects.isNull(miniprogramSetting)) {
            return null;
        }
        if (StringUtils.isBlank(miniprogramSetting.getImageUrl())) {
            return miniprogramSetting;
        }
        Date date = new Date();
        Date compareDate = DateUtils.addDays(date, -3);
        if (miniprogramSetting.getCreateTime() != null && miniprogramSetting.getCreateTime().after(compareDate) && StringUtils.isNotBlank(miniprogramSetting.getMediaId())) {
            return miniprogramSetting;
        }
        String[] arr = miniprogramSetting.getImageUrl().split("/");
        WxEnterpriseQwDTO qwDTO = this.wxEnterpriseService.getQwInfo(miniprogramSetting.getWxEnterpriseId()) ;
        boolean selfFlag = qwDTO.isSelf() ;
        JSONResponse jp = qywxSuiteApiService.uploadMedia(qwDTO.getThirdCorpid(), qwDTO.getSelf3thSecret(), miniprogramSetting.getImageUrl(), arr[arr.length - 1], QywxMediaTypeEnum.IMAGE.getCode(),selfFlag,qwDTO.getUrlHost());
        if (jp.getErrorCode() == 0) {
            miniprogramSetting.setMediaTime(date);
            miniprogramSetting.setMediaId(jp.getResult().toString());
            miniprogramSetting.setUpdateTime(new Date());
            tabMiniprogramSettingMapper.updateByPrimaryKeySelective(miniprogramSetting);
        } else {
            throw new RuntimeException("处理素材异常");
        }
        return miniprogramSetting;
    }

    @Override
    public void saveMiniprogramSetting(TabMiniprogramSetting miniprogramSetting) {
        if (null == miniprogramSetting.getMiniprogramSettingId()) {
            miniprogramSetting.setMiniprogramSettingId(ToolUtil.randomUUID());
            miniprogramSetting.setUpdateTime(new Date());
            miniprogramSetting.setCreateTime(new Date());
            miniprogramSetting.setStatusFlag(1);
            tabMiniprogramSettingMapper.insertSelective(miniprogramSetting);
        } else {
            //减掉时间重新配置
            Date date = DateUtils.addDays(new Date(), -5);
            miniprogramSetting.setMediaTime(date);
            miniprogramSetting.setUpdateTime(new Date());
            tabMiniprogramSettingMapper.updateByPrimaryKeySelective(miniprogramSetting);
            //判断是否需要更新欢迎语里面冗余小程序的封面
            if(null!=miniprogramSetting.getRenewWelcome() && miniprogramSetting.getRenewWelcome()==1){
                // tabHaobanWelcomeMediaMapper.updateByMiniProgramId(miniprogramSetting.getImageUrl(),miniprogramSetting.getMiniprogramSettingId());
            }
        }
    }

    @Override
    public List<TabMiniprogramSetting> listMiniprogram(String wxEnterpriseId) {
        return tabMiniprogramSettingMapper.listMiniprogramSetting(wxEnterpriseId);
    }

    @Override
    public TabMiniprogramSetting getMiniprogramSetting(String wxEnterpriseId, String enterpriseId) {
        TabMiniprogramSetting miniprogramSetting = tabMiniprogramSettingMapper.getMiniprogramSetting(wxEnterpriseId, enterpriseId);
        if (miniprogramSetting == null) {
            return null;
        }
        if (StringUtils.isBlank(miniprogramSetting.getImageUrl())) {
            return miniprogramSetting;
        }
        Date date = new Date();
        Date compareDate = DateUtils.addDays(date, -3);
        if (miniprogramSetting.getCreateTime() != null && miniprogramSetting.getCreateTime().after(compareDate) && StringUtils.isNotBlank(miniprogramSetting.getMediaId())) {
            return miniprogramSetting;
        }
        String[] arr = miniprogramSetting.getImageUrl().split("/");
        WxEnterpriseQwDTO qwDTO = this.wxEnterpriseService.getQwInfo(wxEnterpriseId) ;
        boolean selfFlag = qwDTO.isSelf() ;
        JSONResponse jp = qywxSuiteApiService.uploadMedia(qwDTO.getThirdCorpid(), qwDTO.getSelf3thSecret(), miniprogramSetting.getImageUrl(), arr[arr.length - 1], QywxMediaTypeEnum.IMAGE.getCode(),selfFlag,qwDTO.getUrlHost());
        if (jp.getErrorCode() == 0) {
            miniprogramSetting.setMediaTime(date);
            miniprogramSetting.setMediaId(jp.getResult().toString());
            miniprogramSetting.setUpdateTime(new Date());
            tabMiniprogramSettingMapper.updateByPrimaryKeySelective(miniprogramSetting);
        } else {
            throw new RuntimeException("处理素材异常");
        }
        return miniprogramSetting;
    }

    @Override
    public TabMiniprogramSetting getMiniprogramSettingNoMedia(String wxEnterpriseId, String enterpriseId) {
        TabMiniprogramSetting miniprogramSetting = tabMiniprogramSettingMapper.getMiniprogramSetting(wxEnterpriseId, enterpriseId);
        return miniprogramSetting;
    }

    @Override
    public boolean delMiniprogramSetting(String id) {
        tabMiniprogramSettingMapper.deleteByPrimaryKey(id);
        return true;
    }

    @Override
    public TabMiniprogramSetting getMiniprogramSettingByAppId(String wxEnterpriseId, String appId) {
        return tabMiniprogramSettingMapper.getMiniprogramSettingByAppId(wxEnterpriseId, appId);
    }
}
