package com.cpop.jambox.business.service.impl;

import com.cpop.core.base.R;
import com.cpop.jambox.business.entity.website.*;
import com.cpop.jambox.business.mapper.DevelopmentMapper;
import com.cpop.jambox.business.service.*;
import com.cpop.jambox.business.vo.CompanyIntroVo;
import com.cpop.jambox.business.vo.CooperationPlatformVo;
import com.cpop.jambox.business.vo.DevelopmentVo;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 果酱官网接口 服务实现层
 *
 * @author: Yxz
 * @date: 2024/1/21 11:45
 **/
@Service("websiteService")
public class WebsiteServiceImpl implements WebsiteService {
    @Autowired
    private CooperationTypeService cooperationTypeService;
    @Autowired
    private CooperationService cooperationService;
    @Autowired
    private NavigationService navigationService;
    @Autowired
    private CompanyIntroService companyIntroService;
    @Autowired
    private BusinessIntroService businessIntroService;
    @Autowired
    private WebProductService productService;
    @Autowired
    private DevelopmentMapper developmentMapper;

    /**
     * @description: 获取官网合作平台VO
     * @author: Yxz
     * @date: 2024/1/19 17:37
     * @param: []
     * @return: java.util.List<com.cpop.jambox.business.vo.CooperationPlatformVo>
     **/
    @Override
    public List<CooperationPlatformVo> getCooperationPlatform() {
        // 根据sort倒序查找合作平台种类
        List<CooperationType> cooperationTypeList = cooperationTypeService.list(QueryWrapper.create().orderBy("sort", false));
        List<CooperationPlatformVo> voList = cooperationTypeList.stream().map(entity -> {
            // 根据type_id 及 sort倒序查找合作平台
            CooperationPlatformVo vo = new CooperationPlatformVo();
            vo.setTypeName(entity.getTypeName());

            List<Cooperation> cooperationList = cooperationService.list(QueryWrapper.create().eq("type_id", entity.getId())
                    .orderBy("sort", false));
            vo.setCooperationList(cooperationList);
            return vo;
        }).collect(Collectors.toList());

        return voList;
    }

    /**
     *  获取公司介绍
     * @author: Yxz
     * @date: 2024/1/19 18:32
     * @param: []
     * @return: com.cpop.jambox.business.vo.CompanyIntroVo
     **/
    @Override
    public CompanyIntroVo getCompanyIntro() {
        Navigation navigation = navigationService.getOne(QueryWrapper.create().eq("path", "profile"));
        CompanyIntro companyIntro = companyIntroService.getOne(QueryWrapper.create());

        CompanyIntroVo vo = new CompanyIntroVo();
        vo.setHeadline(navigation.getNavigationName());
        vo.setCompanyIntro(companyIntro);
        return vo;
    }

    /**
     * 获取公司业务介绍
     *
     * @author: Yxz
     * @date: 2024/1/21 11:28
     * @param: []
     * @return: com.cpop.jambox.business.entity.website.BusinessIntro
     **/
    @Override
    public BusinessIntro getBusinessIntro()  {
        BusinessIntro businessIntro = businessIntroService.getOne(QueryWrapper.create());
        /*// 提取图片json数组中的url
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(businessIntro.getImgUrl());
            JsonNode firstElement = jsonNode.get(0);

            String url = firstElement.get("url").asText();
            businessIntro.setImgUrl(url);
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        return businessIntro;
    }



    /**
     * 获取发展历程
     *
     * @author: Yxz
     * @date: 2024/1/21 11:58
     * @param: []
     * @return: com.cpop.core.base.R
     **/
    @Override
    public R getDevelopmentCourse() {
        // 去重查找发布时间点
        List<String> publishTimeList = developmentMapper.selectDistinctPublishTime();
        List<DevelopmentVo> developmentVoList = publishTimeList.stream().map(entity ->{
            DevelopmentVo vo = new DevelopmentVo();
            vo.setMilestone(entity);
            List<Development> developmentList = developmentMapper.selectDevelopmentByPublishTime(entity);
            vo.setDevelopmentList(developmentList);
            return vo;
        }).collect(Collectors.toList());
        return R.ok(developmentVoList);
    }

}
