package com.fs.live.service.impl;


import com.fs.live.domain.LiveAfterSalesItem;
import com.fs.live.mapper.LiveAfterSalesItemMapper;
import com.fs.live.service.ILiveAfterSalesItemService;
import com.fs.hisStore.domain.FsStoreAfterSalesItemScrm;
import com.fs.hisStore.service.IFsStoreAfterSalesItemScrmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 售后子Service业务层处理
 *
 * @author fs
 * @date 2025-07-08
 */
@Service
public class LiveAfterSalesItemServiceImpl  implements ILiveAfterSalesItemService {

    @Autowired
    private LiveAfterSalesItemMapper baseMapper;
    
    @Autowired
    private IFsStoreAfterSalesItemScrmService fsStoreAfterSalesItemScrmService;

    /**
     * 查询售后子
     *
     * @param id 售后子主键
     * @return 售后子
     */
    @Override
    public LiveAfterSalesItem selectLiveAfterSalesItemById(Long id)
    {
        return baseMapper.selectLiveAfterSalesItemById(id);
    }

    /**
     * 查询售后子列表
     *
     * @param liveAfterSalesItem 售后子
     * @return 售后子
     */
    @Override
    public List<LiveAfterSalesItem> selectLiveAfterSalesItemList(LiveAfterSalesItem liveAfterSalesItem)
    {
        return baseMapper.selectLiveAfterSalesItemList(liveAfterSalesItem);
    }

    /**
     * 新增售后子
     *
     * @param liveAfterSalesItem 售后子
     * @return 结果
     */
    @Override
    public int insertLiveAfterSalesItem(LiveAfterSalesItem liveAfterSalesItem)
    {
        return baseMapper.insertLiveAfterSalesItem(liveAfterSalesItem);
    }

    /**
     * 修改售后子
     *
     * @param liveAfterSalesItem 售后子
     * @return 结果
     */
    @Override
    public int updateLiveAfterSalesItem(LiveAfterSalesItem liveAfterSalesItem)
    {
        return baseMapper.updateLiveAfterSalesItem(liveAfterSalesItem);
    }

    /**
     * 批量删除售后子
     *
     * @param ids 需要删除的售后子主键
     * @return 结果
     */
    @Override
    public int deleteLiveAfterSalesItemByIds(Long[] ids)
    {
        return baseMapper.deleteLiveAfterSalesItemByIds(ids);
    }

    /**
     * 删除售后子信息
     *
     * @param id 售后子主键
     * @return 结果
     */
    @Override
    public int deleteLiveAfterSalesItemById(Long id)
    {
        return baseMapper.deleteLiveAfterSalesItemById(id);
    }

    @Override
    public List<LiveAfterSalesItem> selectLiveAfterSalesItemByAfterId(Long afterId) {
        // 查询商城售后商品信息
        FsStoreAfterSalesItemScrm queryItem = new FsStoreAfterSalesItemScrm();
        queryItem.setStoreAfterSalesId(afterId);
        List<FsStoreAfterSalesItemScrm> storeItems = fsStoreAfterSalesItemScrmService.selectFsStoreAfterSalesItemList(queryItem);
        
        if (storeItems == null || storeItems.isEmpty()) {
            return Collections.emptyList();
        }
        
        // 转换为 LiveAfterSalesItem（为了兼容性）
        List<LiveAfterSalesItem> liveItems = new ArrayList<>();
        for (FsStoreAfterSalesItemScrm storeItem : storeItems) {
            LiveAfterSalesItem liveItem = new LiveAfterSalesItem();
            liveItem.setId(storeItem.getId());
            liveItem.setAfterSalesId(storeItem.getStoreAfterSalesId());
            liveItem.setProductId(storeItem.getProductId());
            liveItem.setJsonInfo(storeItem.getJsonInfo());
            liveItem.setIsDel(storeItem.getIsDel());
            liveItems.add(liveItem);
        }
        return liveItems;
    }
}
