package org.jeecg.modules.adweb.site.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.FastJsonUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.adweb.common.constant.NumConstant;
import org.jeecg.modules.adweb.common.constant.WordPressConstants;
import org.jeecg.modules.adweb.site.dto.WordPressConfig;
import org.jeecg.modules.adweb.site.entity.AdwebSite;
import org.jeecg.modules.adweb.site.entity.AdwebUserWpsite;
import org.jeecg.modules.adweb.site.mapper.AdwebSiteMapper;
import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
import org.jeecg.modules.system.mapper.SysUserRoleMapper;
import org.jeecg.modules.adweb.site.service.IAdwebUserWpsiteService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import java.util.List;
import java.util.Map;

/**
 * @Description: adweb站点配置表单
 * @Author: jeecg-boot
 * @Date:   2024-09-29
 * @Version: V1.0
 */
@Service
public class AdwebSiteServiceImpl extends ServiceImpl<AdwebSiteMapper, AdwebSite> implements IAdwebSiteService {


    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private IAdwebUserWpsiteService adwebUserWpsiteService;

    private final RedisUtil redisUtil;

    public AdwebSiteServiceImpl(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    /**
     * 查询wordpress站点配置
     *
     * @param siteCode
     */
    @Override
    public WordPressConfig queryWordPressConfig(String siteCode) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        WordPressConfig config = new WordPressConfig();
        if (StringUtils.isBlank(siteCode)) {
            return config;
        }
        try {
            List<AdwebSite> list = this.list(new LambdaQueryWrapper<AdwebSite>()
                    .eq(AdwebSite::getCode, siteCode));
            if (!list.isEmpty()) {
                AdwebSite adwebSite = list.get(0);
                BeanUtils.copyProperties(adwebSite, config);
                // 开启WP&未上线站点
                if (adwebSite.getStatus() == NumConstant.TWO.intValue() && adwebSite.getWordpressSwitch() == NumConstant.ONE.intValue()) {
                    String domainDev = adwebSite.getDomainDev();
                    domainDev = domainDev.replaceAll("https://", "").replaceAll("http://", "");
                    domainDev = "https://" + domainDev;
                    config.setDomain(domainDev);
                }
                config.setSiteCode(adwebSite.getCode());
                String ssoResKeyName = WordPressConstants.SSO_RES_KEY_NAME + siteCode;
                if (redisUtil.hasKey(ssoResKeyName)) {
                    String ssoResKey = String.valueOf(redisUtil.get(ssoResKeyName));
                    config.setSsoResKey(ssoResKey);
                }

                config.setWordpressName("adweb-user");
                //处理WordPress配置数据
                if (StringUtils.isNotBlank(adwebSite.getWordpressSetting())) {
                    Map<String, Object> wordpressSettingMap = FastJsonUtil.parseObject(adwebSite.getWordpressSetting(), Map.class);
                    int userAccount = 2;

                    if (wordpressSettingMap.get("userAccount") != null) {
                        userAccount = Integer.parseInt(wordpressSettingMap.get("userAccount").toString());
                        if (userAccount == 1) {
                            if (loginUser != null) {
                                List<String> role = sysUserRoleMapper.getRoleByUserName(loginUser.getUsername());
                                if (role.toString().contains("adweb_sub_vip") || role.toString().contains("adweb_vip")) {
                                    List<AdwebUserWpsite> adwebUserWpsites = adwebUserWpsiteService.getSitesByUid(loginUser.getId());
                                    config.setWordpressName(adwebUserWpsites.get(0).getWpUsername());
                                } else if (role.toString().contains("admin") || role.toString().contains("adweb_seo_manager") || role.toString().contains("adweb_site_manager") || role.toString().contains("adweb_oem")) {
                                    List<AdwebUserWpsite> sites = adwebUserWpsiteService.getSitesByUid(adwebSite.getUid());
                                    config.setWordpressName(sites.get(0).getWpUsername());
                                }
                            }
                        }
                    }

                    if (wordpressSettingMap.size() == 1) {
                        config.setProductType(wordpressSettingMap.get("productType").toString());
                    } else {
                        config.setWordpressSetting(wordpressSettingMap.get("setting").toString());
                        List<String> open = FastJsonUtil.parseList(wordpressSettingMap.get("open").toString(), String.class);
                        if (!open.isEmpty()) {
                            config.setOpenSetting(open);
                        }
                        if (wordpressSettingMap.size() > 2) {
                            config.setProductType(wordpressSettingMap.get("productType").toString());
                        }
                    }

                }

            }
        } catch (Exception e) {
            log.error("查询wordpress站点配置失败", e);
        }
        return config;
    }
}
