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.AppCreatePromoterCodeRequest;
import com.heyu.api.alibaba.request.vv.AppCreatePromoterLinkRequest;
import com.heyu.api.alibaba.request.vv.resp.AppCreatePromoterCodeResp;
import com.heyu.api.alibaba.request.vv.resp.AppCreatePromoterLinkResp;
import com.heyu.api.common.annotation.AppRealyLogin;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.*;
import com.heyu.api.data.entity.vv.VvPromoterCodeEntity;
import com.heyu.api.data.utils.MD5Utils;
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.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@Slf4j
@RestController
@RequestMapping("/app")
public class AppPromoterCodeController {

    @Autowired
    private VvPromoterCodeDao vvPromoterCodeDao;

    @Value("${eb.config.weixin.pay.appid}")
    private String appid;


    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private VvActivityDao vvActivityDao;


    @Autowired
    private VvPromoterDao vvPromoterDao;


    @Autowired
    private VvPromoterBuyerDao vvPromoterBuyerDao;


    @Autowired
    private VvBuyerDao vvBuyerDao;

    /***
     * 列表
     * http://localhost:8888/app/create/promoter/code
     */
    @Describe("创建推广码")
    @RequestMapping("/create/promoter/code")
    @AppRealyLogin
    public R promoterCode(@RequestBody AppCreatePromoterCodeRequest request) {
        VvActivityDTO vvActivityDTO = getPromoterCode(request.getBuyerId());
        insertPromoterCode(vvActivityDTO);

        AppCreatePromoterCodeResp promoterCodeResp = new AppCreatePromoterCodeResp();
        promoterCodeResp.setPromoterCode(vvActivityDTO.getPromoterCode());
        return R.ok().setData(promoterCodeResp);
    }


    /***
     * 列表
     * http://localhost:8888/app/create/promoter/link
     *
     * weixin://dl/business/?appid=wx078a627fc7c5f8e6&path=pages/index/index
     */
    @Describe("创建推广链接")
    @RequestMapping("/create/promoter/link")
    @AppRealyLogin
    public R promoterlink(@RequestBody AppCreatePromoterLinkRequest request) {
        AppCreatePromoterLinkResp promoterLinkResp = new AppCreatePromoterLinkResp();
        VvActivityDTO vvActivityDTO = getPromoterCode(request.getBuyerId());
        String url = "weixin://dl/business/?appid=" + appid + "&path=pages/index/index?promoterCode=" + vvActivityDTO.getPromoterCode();
        promoterLinkResp.setPromoterLink(url);
        insertPromoterCode(vvActivityDTO);
        return R.ok().setData(promoterLinkResp);
    }


    public VvActivityDTO getPromoterCode(Long buyerId) {
        Random random = new Random(100000000L);
        VvActivityDTO vvActivityDTO = new VvActivityDTO();

        vvActivityDTO.setBuyerId(buyerId);
        vvActivityDTO.setActivityId(2L);
        String promoteCode = MD5Utils.encode(System.nanoTime() + random.nextInt() + "" + buyerId);

        vvActivityDTO.setPromoterCode(promoteCode);
        return vvActivityDTO;
    }


    public void insertPromoterCode(VvActivityDTO vvActivityDTO) {
        VvPromoterCodeEntity vvPromoterCode = new VvPromoterCodeEntity();
        vvPromoterCode.setPromoterCode(vvActivityDTO.getPromoterCode());
        vvPromoterCode.setContent(JSON.toJSONString(vvActivityDTO));
        vvPromoterCode.setBuyerId(vvActivityDTO.getBuyerId());
        vvPromoterCode.setCreateTimestamp(System.currentTimeMillis());
        vvPromoterCodeDao.insert(vvPromoterCode);
    }

}