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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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("/app/applet/advert")
@Validated
@Slf4j
public class AppAdvertController {

    @Resource
    private AdsenseService adsenseService;

    @Resource
    private AdvertService advertService;

    @Resource
    private AdvertLogService advertLogService;

    @PostMapping("/adsenseList")
    public CommonResult<List<AppAdsenseRespVO>> catalogueList(@RequestBody @Valid AppAdvertReqVO reqVO) {
        List<AdsenseDO> adsenseList = adsenseService.getAdsenseList(reqVO);
        List<AppAdsenseRespVO> respVOS = BeanUtils.toBean(adsenseList, AppAdsenseRespVO.class);
        return success(respVOS);
    }

    @PostMapping("/adsense")
    public CommonResult<AppAdsenseRespVO> catalogue(@RequestBody @Valid AppAdvertReqVO reqVO) {
        AdsenseDO adsenseDO = adsenseService.getAdsenseByCode(reqVO.getAdsenseCode());
        AppAdsenseRespVO respVO = BeanUtils.toBean(adsenseDO, AppAdsenseRespVO.class);
        return success(respVO);
    }

    @PostMapping("/advertList")
    public CommonResult<List<AppAdvertRespVO>> contentList(@RequestBody @Valid AppAdvertReqVO reqVO) {
        List<AppAdvertRespVO> respVOS = new ArrayList<>(3);
        if (reqVO.getAdsenseId() == null) {
            AdsenseDO adsenseDO = adsenseService.getAdsenseByCode(reqVO.getAdsenseCode());
            if (adsenseDO != null) {
                reqVO.setAdsenseId(adsenseDO.getId());
            } else {
                return success(respVOS);
            }
        }
        List<AdvertDO> advertList = advertService.getAdvertList(reqVO.getAdsenseId());
        LocalDateTime now = LocalDateTime.now();
        advertList = advertList.stream()
                .filter(advert -> advert.getIsDisplay() == 1 && (advert.getValidType() == 0 || (advert.getValidType() == 1 && (now.isAfter(advert.getStaDate()) && now.isBefore(advert.getEndDate())))))
                .collect(Collectors.toList());
        respVOS = BeanUtils.toBean(advertList, AppAdvertRespVO.class);
        return success(respVOS);
    }

    @PostMapping("/advert")
    public CommonResult<AppAdvertRespVO> content(@RequestBody @Valid AppAdvertReqVO reqVO) {
        AdvertDO advertDO = advertService.getAdvert(reqVO.getAdvertId());
        AppAdvertRespVO respVO = BeanUtils.toBean(advertDO, AppAdvertRespVO.class);
        return success(respVO);
    }

    @PostMapping("/recordAdvertLog")
    @Operation(summary = "记录广告操作日志")
    public CommonResult<Long> content(@RequestBody @Valid AppAdvertLogReqVO reqVO) {
        AdvertLogSaveReqVO createReqVO = BeanUtils.toBean(reqVO, AdvertLogSaveReqVO.class);
        Long logId = advertLogService.createAdvertLog(createReqVO);
        return success(logId);
    }

}
