package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.system.domain.ZsProduct;
import com.ruoyi.system.domain.ZsProductCategory;
import com.ruoyi.system.domain.ZsPurpose;
import com.ruoyi.system.domain.ZsWebsite;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.mapper.ZsProductCategoryMapper;
import com.ruoyi.system.mapper.ZsProductMapper;
import com.ruoyi.system.mapper.ZsPurposeMapper;
import com.ruoyi.system.mapper.ZsWebsiteMapper;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.system.service.ZsStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class ZsStatisticsServiceImpl implements ZsStatisticsService {

    @Autowired
    private ZsWebsiteMapper zsWebsiteMapper;

    @Autowired
    private ZsProductMapper zsProductMapper;
    @Autowired
    private ZsProductCategoryMapper zsProductCategoryMapper;
    @Autowired
    private ZsPurposeMapper zsPurposeMapper;
    @Autowired
    private ISysOssService iSysOssService;

    @Override
    public List<Map> getProductJson(String code,String languageCode) {
        List<Map> reList = new ArrayList();
        Long webSiteId = getWebSiteId(code,languageCode);
        if (webSiteId!=null) {
            List<Map> productList = zsProductMapper.getProductAndCategoryInfo(webSiteId,languageCode);
            if (productList!=null&&productList.size()>0) {
                for (int i=0;i<productList.size();i++) {
                    Map productMap = productList.get(i);
                    String purposeIds = (String)productMap.get("purpose_ids");
                    if (StringUtils.isNotBlank(purposeIds)) {
                        String[] purposeIdsArr =  purposeIds.split(",");
                        QueryWrapper<ZsPurpose> zsPurposeQueryWrapper = new QueryWrapper<>();
                        zsPurposeQueryWrapper.select("name");
                        zsPurposeQueryWrapper.in("id",purposeIdsArr);
                        List<ZsPurpose> zsPurposes = zsPurposeMapper.selectList(zsPurposeQueryWrapper);
                        List<String> purposeNames = new ArrayList();
                        if (zsPurposes!=null&&zsPurposes.size()>0) {
                            for (ZsPurpose zsPurpose:zsPurposes) {
                                purposeNames.add(zsPurpose.getName());
                            }
                        }

                        SysOssVo sysOssVo = iSysOssService.getById(Long.valueOf((String)productMap.get("cover")));
                        Map reMap = new HashMap();
                        reMap.put("id",i);
                        reMap.put("name",productMap.get("product_name"));
                        reMap.put("category",productMap.get("category_name"));
                        reMap.put("usage",purposeNames);
                        reMap.put("capacities",productMap.get("tag"));
                        reMap.put("image",sysOssVo.getUrl());
                        reMap.put("description",productMap.get("descripetion1"));
                        reList.add(reMap);
                    }
                }
            }
        }
        return reList;
    }
    //产品分类
    @Override
    public List<Map> getProductCategoryJson(String code, String languageCode) {
        List<Map> reList = new ArrayList();
        Long webSiteId = getWebSiteId(code,languageCode);
        if (webSiteId!=null) {
            QueryWrapper<ZsProductCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("language_code",languageCode);
            queryWrapper.eq("website_id",webSiteId);
            List<ZsProductCategory> lists = zsProductCategoryMapper.selectList(queryWrapper);
            if (lists!=null&&lists.size()>0) {
                for (ZsProductCategory zsProductCategory:lists) {
                    Map map = new HashMap();
                    map.put("id",zsProductCategory.getId());
                    map.put("title",zsProductCategory.getName());
                    map.put("description",zsProductCategory.getDescripetion());
                    map.put("features",zsProductCategory.getFeatures());
                    String imageUrl = "";
                    if (zsProductCategory.getCover()!=null) {
                        SysOssVo sysOssVo = iSysOssService.getById(Long.valueOf(zsProductCategory.getCover()));
                        imageUrl = sysOssVo.getUrl();
                    }
                    map.put("image",imageUrl);
                    map.put("link",zsProductCategory.getBlankUrl());
                    reList.add(map);
                }
            }
        }
        return reList;
    }
    //产品用途
    @Override
    public List<Map> getProductPurposeJson(String code, String languageCode) {
        List<Map> reList = new ArrayList();
        Long webSiteId = getWebSiteId(code,languageCode);
        if (webSiteId!=null) {
            QueryWrapper<ZsPurpose> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("language_code",languageCode);
            queryWrapper.eq("website_id",webSiteId);
            List<ZsPurpose> lists = zsPurposeMapper.selectList(queryWrapper);

            if (lists!=null&&lists.size()>0) {
                for (ZsPurpose zsPurpose:lists) {
                    Map map = new HashMap();
                    map.put("id",zsPurpose.getId());
                    map.put("title",zsPurpose.getName());
                    map.put("description",zsPurpose.getDescripetion());
                    reList.add(map);
                }
            }
        }
        return reList;
    }
    //产品详情
    @Override
    public List<Map> getProductDetailJson(String code, String languageCode) {
        List<Map> reList = new ArrayList();
        Long webSiteId = getWebSiteId(code,languageCode);
        if (webSiteId!=null) {
            QueryWrapper<ZsProduct> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("language_code",languageCode);
            queryWrapper.eq("website_id",webSiteId);
            List<ZsProduct> lists = zsProductMapper.selectList(queryWrapper);
            if (lists!=null&&lists.size()>0) {
                for (ZsProduct zsProduct:lists) {
                    Map map = new HashMap();
                    map.put("id",zsProduct.getId());
                    map.put("name",zsProduct.getName());

                    List<String> purposeNames = new ArrayList();
                    ZsProductCategory zsProductCategory= zsProductCategoryMapper.selectById(zsProduct.getCategoryId());
                    map.put("category",zsProductCategory.getName());
                    String purposeIds = zsProduct.getPurposeIds();
                    if (StringUtils.isNotBlank(purposeIds)) {
                        String[] purposeIdsArr = purposeIds.split(",");
                        QueryWrapper<ZsPurpose> zsPurposeQueryWrapper = new QueryWrapper<>();
                        zsPurposeQueryWrapper.select("name");
                        zsPurposeQueryWrapper.in("id", purposeIdsArr);
                        List<ZsPurpose> zsPurposes = zsPurposeMapper.selectList(zsPurposeQueryWrapper);

                        if (zsPurposes != null && zsPurposes.size() > 0) {
                            for (ZsPurpose zsPurpose : zsPurposes) {
                                purposeNames.add(zsPurpose.getName());
                            }
                        }
                    }
                    map.put("usage",purposeNames);
                    map.put("capacities",zsProduct.getTag());
                    //封面
                    String imageUrl = "";
                    if (zsProduct.getCover()!=null) {
                        SysOssVo sysOssVo = iSysOssService.getById(Long.valueOf(zsProduct.getCover()));
                        imageUrl = sysOssVo.getUrl();
                    }
                    map.put("image",imageUrl);
                    //图片集合
                    List<Long> ids = new ArrayList();
                    if (zsProduct.getGalleryList()!=null&&zsProduct.getGalleryList().length()>0) {
                        String[] gallerys = zsProduct.getGalleryList().split(",");
                        for (String galleryId:gallerys) {
                            ids.add(Long.parseLong(galleryId));
                        }
                    }
                    List<SysOssVo> sysOssVos = iSysOssService.listByIds(ids);
                    List<String> galleryList = new ArrayList();
                    if (sysOssVos!=null&&sysOssVos.size()>0) {
                        for (SysOssVo sysOssVo1:sysOssVos) {
                            galleryList.add(sysOssVo1.getUrl());
                        }
                    }
                    map.put("gallery",galleryList);
                    //   map.put("summary",);
                    map.put("description1",zsProduct.getDescripetion1());
                    map.put("description2",zsProduct.getDescripetion2());
                    reList.add(map);
                }
            }
        }

        return reList;
    }
    //FAQ留言信息
    @Override
    public List<Map> getFAQJson(String code, String languageCode) {
        return null;
    }

    public Long getWebSiteId(String code,String languageCode) {
        Long webSiteId = null;
        //根据网站编码和语言查询
        QueryWrapper<ZsWebsite> zsWebsiteQueryWrapper = new QueryWrapper<>();
        zsWebsiteQueryWrapper.eq("language_code",languageCode);
        zsWebsiteQueryWrapper.eq("code",code);
        List<ZsWebsite> websites = zsWebsiteMapper.selectList(zsWebsiteQueryWrapper);

        if (websites!=null&&websites.size()>0) {
            ZsWebsite zsWebsite = websites.get(0);
            webSiteId = zsWebsite.getId();
        }
        return webSiteId;
    }
}
