package com.heyu.api.controller.vv;


import com.alibaba.fastjson.JSON;
import com.heyu.api.alibaba.request.mm.order.VvActivityDTO;
import com.heyu.api.alibaba.request.vv.AppActivityRequest;
import com.heyu.api.common.annotation.AppLogin;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.dao.vv.VvActivityDao;
import com.heyu.api.data.entity.vv.VvActivityEntity;
import com.heyu.api.data.utils.DateUtils;
import com.heyu.api.data.utils.R;
import com.heyu.api.data.utils.RedisUtils;
import com.heyu.api.data.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


@Slf4j
@RestController
@RequestMapping("/app/activity")
@AppLogin
public class AppActivityController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private VvActivityDao vvActivityDao;

    /***
     * 列表
     * /app/buyer/info
     */
    @Describe("访问推广链接")
    @RequestMapping("/info")
    public R list(@RequestBody AppActivityRequest request) {
        Long activityId = request.getActivityId();
        if (request.getActivityId() == null) {
            activityId = 1L;
        }
        VvActivityEntity vvActivityEntity = vvActivityDao.selectVvActivityById(activityId);
        if (StringUtils.isNotEmpty(request.getToken())) {
            VvActivityDTO vvActivityDTO = new VvActivityDTO();
            BeanUtils.copyProperties(vvActivityEntity, vvActivityDTO);
            Date endTime = DateUtils.addMinutes(new Date(), 30);
            vvActivityDTO.setActivityExpiredTime(endTime.getTime());
            vvActivityDTO.setPromoterId(request.getPromoterId());
            redisUtils.set(ApiConstants.token_activity + request.getToken(), JSON.toJSONString(vvActivityDTO), RedisUtils.thirty_minute);
            return R.ok().setData(vvActivityEntity);
        }
        return R.ok();
    }


}
