package com.tuoheng.system.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuoheng.common.common.BaseQuery;
import com.tuoheng.common.common.BaseServiceImpl;
import com.tuoheng.common.config.CommonConfig;
import com.tuoheng.common.utils.CommonUtils;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.common.utils.StringUtils;
import com.tuoheng.system.entity.Ad;
import com.tuoheng.system.mapper.AdMapper;
import com.tuoheng.system.query.AdQuery;
import com.tuoheng.system.service.IAdService;
import com.tuoheng.system.utils.ShiroUtils;
import com.tuoheng.system.vo.ad.AdListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 广告管理表 服务实现类
 * </p>
 *
 * @author 拓恒
 * @since 2020-11-07
 */
@Service
public class AdServiceImpl extends BaseServiceImpl<AdMapper, Ad> implements IAdService {

    @Autowired
    private AdMapper adMapper;

    /**
     * 获取广告列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    public JsonResult getList(BaseQuery query) {
        AdQuery adQuery = (AdQuery) query;
        // 查询条件
        QueryWrapper<Ad> queryWrapper = new QueryWrapper<>();
        // 租户ID
        queryWrapper.eq("tenant_id", ShiroUtils.getTenantId());
        // 广告标题
        if (!StringUtils.isEmpty(adQuery.getTitle())) {
            queryWrapper.like("title", adQuery.getTitle());
        }
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByAsc("sort");

        // 查询分页数据
        IPage<Ad> page = new Page<>(adQuery.getPage(), adQuery.getLimit());
        IPage<Ad> pageData = adMapper.selectPage(page, queryWrapper);
        pageData.convert(x -> {
            AdListVo adListVo = Convert.convert(AdListVo.class, x);
            // 封面
            if (!StringUtils.isEmpty(x.getCover())) {
                adListVo.setCover(CommonUtils.getImageURL(x.getCover()));
            }
            // 广告内容
            List<String> stringList = CommonUtils.getImgStr(x.getContent());
            if (stringList.size() > 0) {
                stringList.forEach(item -> {
                    // 公告内容
                    adListVo.setContent(x.getContent().replaceAll(item, CommonUtils.getImageURL(item)));
                });
            }
            return adListVo;
        });
        return JsonResult.success(pageData);
    }

    /**
     * 获取图片信息
     *
     * @param id 记录ID
     * @return
     */
    @Override
    public Object getInfo(Serializable id) {
        Ad info = (Ad) super.getInfo(id);
        List<String> stringList = CommonUtils.getImgStr(info.getContent());
        if (stringList.size() > 0) {
            stringList.forEach(item -> {
                info.setContent(info.getContent().replaceAll(item, CommonUtils.getImageURL(item)));
            });
        }
        return info;
    }

    /**
     * 添加或编辑
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult edit(Ad entity) {
        // 图片处理
        if (!StringUtils.isEmpty(entity.getCover()) && entity.getCover().contains(CommonConfig.imageURL)) {
            entity.setCover(entity.getCover().replaceAll(CommonConfig.imageURL, ""));
        }
        // 处理富文本信息
        entity.setContent(entity.getContent().replaceAll(CommonConfig.ossURL, ""));
        // 添加是添加租户ID
        if (StringUtils.isNotNull(entity.getId()) && entity.getId() > 0) {
            // 更新
        } else {
            // 添加
            entity.setTenantId(ShiroUtils.getTenantId());
        }
        return super.edit(entity);
    }
}
