package com.heyu.api.controller.vv;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.heyu.api.alibaba.request.mm.ActivityUtils;
import com.heyu.api.alibaba.request.mm.order.PromoterBuyerInfoDTO;
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.dao.vv.VvBuyerDao;
import com.heyu.api.data.dao.vv.VvPromoterBuyerDao;
import com.heyu.api.data.dao.vv.VvPromoterDao;
import com.heyu.api.data.entity.vv.VvActivityEntity;
import com.heyu.api.data.entity.vv.VvBuyerEntity;
import com.heyu.api.data.entity.vv.VvPromoterBuyerEntity;
import com.heyu.api.data.entity.vv.VvPromoterEntity;
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;
import java.util.List;


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

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private VvActivityDao vvActivityDao;


    @Autowired
    private VvPromoterDao vvPromoterDao;


    @Autowired
    private VvPromoterBuyerDao vvPromoterBuyerDao;


    @Autowired
    private VvBuyerDao vvBuyerDao;

    /***
     * 列表
     * /app/buyer/info
     */
    @Describe("访问推广链接")
    @RequestMapping("/register")
    public R register(@RequestBody AppActivityRequest request) {
        VvPromoterEntity vvPromoterEntity = vvPromoterDao.selectVvPromoterById(Long.parseLong(request.getPromoterId()));
        if (vvPromoterEntity == null) {
            return R.error("推广信息不存在");
        }
        Long activityId = vvPromoterEntity.getActivityId();
        if (vvPromoterEntity.getActivityId() == null) {
            activityId = 2L;
        }

        VvActivityEntity vvActivityEntity = vvActivityDao.selectVvActivityById(activityId);
        String activityInfo = redisUtils.get(ApiConstants.token_activity + request.getToken());
        VvActivityDTO vvActivityOldDTO = null;
        if (StringUtils.isNotEmpty(activityInfo)) {
            vvActivityOldDTO = JSONObject.parseObject(activityInfo, VvActivityDTO.class);
        }
        PromoterBuyerInfoDTO promoterBuyerInfoDTO = JSONObject.parseObject(JSON.toJSONString(vvActivityOldDTO), PromoterBuyerInfoDTO.class);

        VvBuyerEntity wasBuyerEntity = vvBuyerDao.selectVvBuyerById(promoterBuyerInfoDTO.getWasBuyerId());

        List<VvBuyerEntity> promoterBuyerList = vvBuyerDao.selectVvBuyerByIdList(promoterBuyerInfoDTO.getPromoterBuyerIdList());
        for (VvBuyerEntity promoterBuyerEntity : promoterBuyerList) {
            VvPromoterBuyerEntity vvPromoterBuyerEntity = new VvPromoterBuyerEntity();
            vvPromoterBuyerEntity.setPromoterId(request.getPromoterId());
            vvPromoterBuyerEntity.setPromoterBuyerId(promoterBuyerEntity.getId());
            vvPromoterBuyerEntity.setPromoterBuyerName(promoterBuyerEntity.getBuyerName());
            vvPromoterBuyerEntity.setPromoterBuyerPhone(promoterBuyerEntity.getBuyerPhone());
            vvPromoterBuyerEntity.setPromoterBuyerWeixin(promoterBuyerEntity.getBuyerWeixin());

            vvPromoterBuyerEntity.setWasBuyerId(wasBuyerEntity.getId());
            vvPromoterBuyerEntity.setWasBuyerName(wasBuyerEntity.getBuyerName());
            vvPromoterBuyerEntity.setWasBuyerPhone(wasBuyerEntity.getBuyerPhone());
            vvPromoterBuyerEntity.setWasBuyerWeixin(wasBuyerEntity.getBuyerWeixin());
            vvPromoterBuyerDao.insertOrUpdateVvPromoterBuyer(vvPromoterBuyerEntity);
        }

        if (StringUtils.isNotEmpty(request.getToken())) {
            VvActivityDTO vvActivityDTO = new VvActivityDTO();
            BeanUtils.copyProperties(vvActivityEntity, vvActivityDTO);

            vvActivityDTO = ActivityUtils.bestActivityDTO(vvActivityDTO, vvActivityOldDTO);
            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();
    }


}
