
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.AppParsePromoterRequest;
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.dao.vv.VvPromoterCodeDao;
import com.heyu.api.data.entity.vv.VvActivityEntity;
import com.heyu.api.data.entity.vv.VvPromoterCodeEntity;
import com.heyu.api.data.utils.DateUtils;
import com.heyu.api.data.utils.R;
import com.heyu.api.data.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
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")
public class AppPromoterCodeParseController {

    @Autowired
    private VvPromoterCodeDao vvPromoterCodeDao;


    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private VvActivityDao vvActivityDao;

    /***
     * 列表
     * http://localhost:8888/app/parse/promoter/code
     */
    @Describe("解析推广链接")
    @RequestMapping("/parse/promoter/code")
    public R parsePromoterCode(@RequestBody AppParsePromoterRequest request) {
        VvPromoterCodeEntity promoterCodeEntity =  vvPromoterCodeDao.selectVvPromoterCodeByCode(request.getPromoterCode());
        if(promoterCodeEntity == null){
            return R.error("promoterCode 不存在");
        }
        String content = promoterCodeEntity.getContent();
        VvActivityDTO vvActivityDTO = JSON.parseObject(content, VvActivityDTO.class);
        VvActivityEntity vvActivityEntity = vvActivityDao.selectVvActivityById(vvActivityDTO.getActivityId());
        if(vvActivityEntity ==null){
            log.info("vvActivityEntity is null");
            return R.ok();
        }

        Date time  = DateUtils.addMinutes(new Date(),30);
        vvActivityDTO.setActivityExpiredTime(time.getTime());

        log.info("AppPromoterCodeParseController parse activity token is :{},vvActivityDTO:{}",request.getToken(),JSON.toJSONString(vvActivityDTO));
        redisUtils.set(ApiConstants.token_activity + request.getToken(), JSON.toJSONString(vvActivityDTO), RedisUtils.thirty_minute);
        return R.ok();
    }
}