package com.cool.store.service.impl.xinfa;

import com.cool.store.dto.huoma.*;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.SpecialTagEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.xinfa.XinFaBusinessService;
import com.cool.store.service.xinfa.XinFaDeviceService;
import com.cool.store.utils.poi.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author suzhuhong
 * @Date 2025/11/5 16:17
 * @Version 1.0
 */
@Service
public class XinFaBusinessServiceImpl implements XinFaBusinessService {
    @Resource
    XinFaDeviceService xinFaDeviceService;

    @Override
    public List<StoreXinFaDeviceDetail> getStoreXinFaDeviceDetail(String storeNum) {
        if (StringUtils.isEmpty(storeNum)){
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        return xinFaDeviceService.getStoreXinFaDeviceDetail(storeNum);
    }

    @Override
    public List<TagDetailDTO> getAccountAllTags(String storeNum,String deviceName) {
        //如果是广告机,不需要展示标签
        if (deviceName.contains("广告机")){
            return new ArrayList<>();
        }
        List<TagDetailDTO> accountAllTags = xinFaDeviceService.getAccountAllTags(storeNum);
        if (accountAllTags != null){
            return accountAllTags.stream()
                    .filter(tag -> tag.getName() != null &&
                            SpecialTagEnum.getAllTagNames().contains(tag.getName()))
                    .sorted(Comparator.comparing(tag -> SpecialTagEnum.getAllTagNames().indexOf(tag.getName())))
                    .collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public List<ProgramResponseDTO> getProgramList(ProgramReqDTO programReqDTO) {
        //如果没传tag 查所有 根据更新时间倒序排
        programReqDTO.setSort("desc");
        programReqDTO.setDate("updateTime");
        if (CollectionUtils.isEmpty(programReqDTO.getTagIds())){
            //设备名称包含 广告机 只查询电子价目表
            programReqDTO.setTagIds(xinFaDeviceService.getAccountSpecialTagIds(programReqDTO.getStoreCode(), SpecialTagEnum.getElectronicPriceTagName()));
        }
        List<ProgramResponseDTO> programList = xinFaDeviceService.getProgramList(programReqDTO);
        return programList;
    }

    @Override
    public Boolean publishProgram(PublishDTO publishDTO) {
        return xinFaDeviceService.publish(publishDTO);
    }
}
