package com.yc.ship.module.miniapplet.controller.app.advert;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.miniapplet.controller.admin.advertlog.vo.AdvertLogSaveReqVO;
import com.yc.ship.module.miniapplet.controller.app.advert.vo.AppAdsenseRespVO;
import com.yc.ship.module.miniapplet.controller.app.advert.vo.AppAdvertLogReqVO;
import com.yc.ship.module.miniapplet.controller.app.advert.vo.AppAdvertReqVO;
import com.yc.ship.module.miniapplet.controller.app.advert.vo.AppAdvertRespVO;
import com.yc.ship.module.miniapplet.dal.dataobject.adsense.AdsenseDO;
import com.yc.ship.module.miniapplet.dal.dataobject.advert.AdvertDO;
import com.yc.ship.module.miniapplet.service.adsense.AdsenseService;
import com.yc.ship.module.miniapplet.service.advert.AdvertService;
import com.yc.ship.module.miniapplet.service.advertlog.AdvertLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

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

/**
 *
 * @description ：TODO
 * @date ：2024/8/19 17:13
 */
@Tag(name = "用户 APP - 广告")
@RestController
@RequestMapping("/applet/advert")
@Validated
@Slf4j
public class AppAdvertController {

    @Resource
    private AdsenseService adsenseService;

    @Resource
    private AdvertService advertService;

    // 根据广告位编码获取广告列表
    @GetMapping("/advertListByCode")
    @Operation(summary = "根据广告位编码获取广告列表")
    public CommonResult<List<AppAdvertRespVO>> advertListByCode(@RequestParam("adsenseCode") String adsenseCode) {
        AdsenseDO adsenseDO = adsenseService.getAdsenseByCode(adsenseCode);
        Long adsenseId = adsenseDO.getId();
        List<AdvertDO> advertList = advertService.getCanUseAdvertList(adsenseId);
        List<AppAdvertRespVO> respVOS = BeanUtils.toBean(advertList, AppAdvertRespVO.class);
        respVOS.stream().forEach(item -> {
            item.setAdsenseName(adsenseDO.getAdsenseName());
            item.setAdsenseType(adsenseDO.getAdsenseType());
        });
        return success(respVOS);
    }

}
