package com.cyksj.web.controller.advertisement;

import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.AdvertisementPopularize;
import com.cyksj.model.entity.AdvertisementProject;
import com.cyksj.model.views.AdvertisementPopularizeView;
import com.cyksj.service.advertisement.AdvertisementPopularizeService;
import com.cyksj.service.advertisement.AdvertisementProjectService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chan
 * @date 2023/5/16 11:49 AM
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/applets/advertisement/popularize")
public class AdvertisementPopularizeController {

    private final AdvertisementPopularizeService advertisementPopularizeService;

    private final AdvertisementProjectService advertisementProjectService;

    @GetMapping("/{id}")
    public Result<AdvertisementPopularizeView> get(@PathVariable Long id){
        log.info("--------广告投放 链接获取详情------ \nid id:{}",id);
        AdvertisementPopularize popularize = advertisementPopularizeService.getById(id);
        if (popularize == null) {
            throw BusinessRuntimeException.getInstance("链接不存在.");
        }
        AdvertisementProject project = advertisementProjectService.getById(popularize.getProjectId());

        AdvertisementPopularizeView view = new AdvertisementPopularizeView();
        view.setId(popularize.getId());
        view.setName(project.getName());
        view.setDetail(project.getDetail());
        view.setSuccessImg(project.getSuccessImg());
        view.setMoney(project.getMoney());
        view.setImg(project.getImg());
        return GatewayResponse.SUCCESS.newBuilder().toResult(view);
    }

}
