package com.yc.ship.module.product.api;

import cn.hutool.core.date.DateUtil;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.date.DateUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.tenant.core.util.TenantUtils;
import com.yc.ship.module.ota.api.OtaDistributorApi;
import com.yc.ship.module.ota.api.dto.DistributorProductRespDTO;
import com.yc.ship.module.product.api.dto.*;
import com.yc.ship.module.product.controller.admin.productBase.vo.ProductBaseRespVO;
import com.yc.ship.module.product.controller.admin.productSpu.groupVO.ProductGroupRespVO;
import com.yc.ship.module.product.controller.admin.productSpu.spuVO.ProductSpuRespVO;
import com.yc.ship.module.product.dal.dataobject.productSpu.ProductSpuDO;
import com.yc.ship.module.product.dal.dataobject.voyage.VoyageDO;
import com.yc.ship.module.product.dal.mysql.productGroup.ProductGroupMapper;
import com.yc.ship.module.product.service.productBase.ProductBaseService;
import com.yc.ship.module.product.service.productSpu.ProductSpuService;
import com.yc.ship.module.product.service.voyage.VoyageService;
import com.yc.ship.module.resource.controller.admin.category.vo.CategoryListReqVO;
import com.yc.ship.module.resource.controller.admin.category.vo.CategoryRespVO;
import com.yc.ship.module.resource.dal.dataobject.category.CategoryDO;
import com.yc.ship.module.resource.dal.dataobject.category.CategoryExtraDO;
import com.yc.ship.module.resource.dal.dataobject.checkchannel.CheckChannelDO;
import com.yc.ship.module.resource.service.category.CategoryExtraService;
import com.yc.ship.module.resource.service.category.CategoryService;
import com.yc.ship.module.resource.service.checkchannel.CheckChannelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.yc.ship.framework.common.pojo.CommonResult.success;

/**
 * @author ：qsl
 * @description：TODO
 * @date ：2025/8/12 09:47
 **/
@Service
public class ProductApiImpl implements ProductApi {

    @Resource
    private ProductSpuService productSpuService;
    @Resource
    private CheckChannelService checkChannelService;
    @Resource
    private ProductBaseService productBaseService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private CategoryExtraService categoryExtraService;
    @Resource
    private OtaDistributorApi otaDistributorApi;
    @Resource
    private ProductGroupMapper productGroupMapper;
    @Resource
    private VoyageService voyageService;

    @Override
    public List<ProductSpuRespDTO> getProductListByTenantId(Long tenantId) {
        return productSpuService.getProductListByTenantId(tenantId).getData();
    }

    @Override
    public List<ProductSpuRespDTO> getAllProductList() {
        return productSpuService.listAllSpu();
    }

    @Override
    public List<ProductSpuRespDTO> getProductList(Collection<Long> ids) {
        List<ProductSpuDO> spuList = productSpuService.getProductList(ids);
        return BeanUtils.toBean(spuList, ProductSpuRespDTO.class);

    }

    @Override
    public CommonResult<ProductCheckChannelRespDTO> getProductCheckChannel(Long productId, Long channelId) {
        CheckChannelDO checkChannelDO = checkChannelService.getCheckChannelByChannelAndProduct(productId, channelId);
        return success(BeanUtils.toBean(checkChannelDO, ProductCheckChannelRespDTO.class));
    }

    @Override
    public CommonResult<ProductBaseRespDTO> getProductBaseById(Long baseId) {
        ProductBaseRespVO base = productBaseService.getBase(baseId);
        return success(BeanUtils.toBean(base, ProductBaseRespDTO.class));
    }

    @Override
    public CommonResult<ProductSpuRespDTO> getCalendarProduct(Long id, String useDate) {
        return getCalendarProduct(id, useDate, null);
    }
    @Override
    public CommonResult<ProductSpuRespDTO> getCalendarProduct(Long id, String useDate, Long routePlanId) {
        if(useDate!=null){
            ProductSpuRespVO productSpuResp = productSpuService.getProductSpuResp(id, useDate,routePlanId);
            List<CategoryRespVO> list = categoryService.getCategoryRespListByIds(productSpuResp.getCategoryId());
            String newName = list.stream().map(CategoryRespVO::getCateName).collect(Collectors.joining());
            productSpuResp.setProductName(newName+"-"+productSpuResp.getProductName());
            ProductSpuRespDTO spuRespDTO = BeanUtils.toBean(productSpuResp, ProductSpuRespDTO.class);
            return success(spuRespDTO);
        }
        return getProduct(id);
    }
    @Override
    public CommonResult<ProductSpuRespDTO> getProduct(Long id) {
        String date = DateUtil.format(DateUtil.date(), DateUtils.FORMAT_YEAR_MONTH_DAY);
        ProductSpuRespVO productSpuResp = productSpuService.getProductSpuResp(id,date,null);
        ProductSpuRespDTO spuRespDTO = BeanUtils.toBean(productSpuResp, ProductSpuRespDTO.class);
        // 查询分销产品规格信息
        if (spuRespDTO !=null) {
            spuRespDTO.getPlatProductList().forEach(item -> {
                DistributorProductRespDTO productRespDTO = otaDistributorApi.getDistributorProductById(item.getProductBaseId()).getCheckedData();
                TenantUtils.execute(productRespDTO.getTenantId(),()->{
                    List<ProductGroupRespVO> groupList = productGroupMapper.selectSpuGroupList(productRespDTO.getProductId(),null);
                    item.setProductGroupList(BeanUtils.toBean(groupList, ProductGroupRespDTO.class));
                });
            });
        }
        return success(spuRespDTO);
    }
    @Override
    public CommonResult<CategoryRespDTO> getCategory(Long id) {
        CategoryDO category = categoryService.getCategory(id);
        if(category!=null) {
            CategoryRespDTO respDTO = BeanUtils.toBean(category, CategoryRespDTO.class);
            CategoryExtraDO extraInfo = categoryExtraService.getCategoryExtra(id);
            respDTO.setExtraInfo(BeanUtils.toBean(extraInfo, CategoryExtraRespDTO.class));
            return success(respDTO);
        }
        return null;

    }

    @Override
    public VoyageRespDTO getVoyage(Long id) {
        VoyageDO voyageDO = voyageService.getVoyage(id);
        return BeanUtils.toBean(voyageDO, VoyageRespDTO.class);
    }

    @Override
    public CommonResult<List<CategoryRespDTO>> getCategoryListByIds(Integer type, List<Long> ids) {
        CategoryListReqVO reqVO = new CategoryListReqVO();
        reqVO.setType(type);
        reqVO.setIds(ids);
        List<CategoryRespVO> listByIds = categoryService.getListByIds(reqVO);
        List<CategoryRespDTO> categoryRespDTOS = BeanUtils.toBean(listByIds, CategoryRespDTO.class);
        if (type == 2) {
            // 小程序分类型信息
            for (CategoryRespDTO categoryRespDTO : categoryRespDTOS) {
                CategoryExtraDO extraDO = categoryExtraService.getCategoryExtra(categoryRespDTO.getId());
                categoryRespDTO.setExtraInfo(BeanUtils.toBean(extraDO, CategoryExtraRespDTO.class));
            }
        }
        return CommonResult.success(categoryRespDTOS);
    }

}