package com.sptg.gateway.controller;

import com.sptg.ad.api.entity.AdPushContent;
import com.sptg.ad.api.entity.AdPushDeviceDetails;
import com.sptg.ad.api.entity.AdPushTask;
import com.sptg.ad.api.feigns.AdPushDeviceDetailsFeign;
import com.sptg.ad.api.feigns.AdPushTaskFeign;
import com.sptg.ad.api.stream.SyncFaceAdvertDataProduceInterface;
import com.sptg.ad.api.stream.SyncLeisureAdvertDataProduceInterface;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.device.api.dto.DeviceDto;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.feigns.DeviceFeign;
import com.sptg.estate.api.dto.BaseUser;
import com.sptg.estate.api.feigns.BaseUserFeign;
import com.sptg.statistics.api.entity.StatisticsAdWatch;
import com.sptg.statistics.api.feigns.StatisticsAdWatchFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * title: AdController
 * package: com.sptg.gateway.controller
 * description: 广告控制器
 *
 *                  
 *               
 */
@RestController
@RequiredArgsConstructor
public class AdController {

    private final StatisticsAdWatchFeign statisticsAdWatchFeign;
    private final BaseUserFeign baseUserFeign;
    private final AdPushTaskFeign adPushTaskFeign;
    private final DeviceFeign deviceFeign;
    private final AdPushDeviceDetailsFeign adPushDeviceDetailsFeign;
    private final SyncFaceAdvertDataProduceInterface syncFaceAdvertDataProduceInterface;
    private final SyncLeisureAdvertDataProduceInterface syncLeisureAdvertDataProduceInterface;

    /**
     * 功能描述: 广告下发任务执行完毕
     * @param dto 请求参数
     * @return ResultBean<Boolean>
     *                  
     *                    
     */
    @PostMapping(value = "/ad/task/end")
    public ResultBean<Boolean> taskEnd(@RequestBody AdPushDeviceDetails dto){
        Assert.notNull(dto.getTaskId(),"任务Id不能为空");
        Assert.hasText(dto.getSerialNumber(),"设备序列号不能为空");
        Assert.notNull(dto.getPushStatus(),"任务执行状态不能为空");
        return this.adPushDeviceDetailsFeign.updatePushStatus(dto);
    }

    /**
     * 功能描述: 广告播放记录
     * @param adWatch 请求参数
     * @return ResultBean<Boolean>
     *                  
     *                     
     */
    @PostMapping(value = "/ad/play/log")
    public ResultBean<Boolean> playLog(@RequestBody StatisticsAdWatch adWatch){
        Assert.notNull(adWatch.getTaskId(),"任务Id不能为空");
        Assert.hasText(adWatch.getSerialNumber(),"设备序列号不能为空");
        Assert.hasText(adWatch.getAdCode(),"广告编号不能为空");
        Assert.notNull(adWatch.getWatchDate(),"播放时间不能为空");
        //  获取广告任务信息
        AdPushContent content = this.adPushTaskFeign.getPushContentByTask(adWatch.getTaskId(), adWatch.getAdCode());
        Assert.notNull(content,"广告任务Id或广告编号有误");
        //  获取设备信息
        Device device = this.deviceFeign.getEntityBySerialNumber(adWatch.getSerialNumber());
        Assert.notNull(device,"设备序列号不存在");
        if (null != adWatch.getUserId()) {
            //  获取用户信息
            BaseUser user = this.baseUserFeign.getUserById(adWatch.getUserId());
            //  填充用户信息
            adWatch.setIsAdmin(user.getIsAdmin());
            if (null == user.getId()) {
                adWatch.setUserId(null);
            } else {
                adWatch.setUserId(user.getId() + "");
            }
            adWatch.setUserName(user.getName());
            adWatch.setUserType(user.getType());
        }else {
            adWatch.setIsAdmin(3);
        }
        //  填充广告信息
        adWatch.setAdId(content.getAdId());
        adWatch.setAdName(content.getAdTitle());
        adWatch.setAdTypeId(content.getAdTypeId());
        adWatch.setAdTypeName(content.getAdTypeName());
        adWatch.setAdCode(content.getAdCode());
        //  填充设备信息
        adWatch.setDeviceId(device.getId());
        adWatch.setResidentialId(device.getXqId());
        adWatch.setResidentialName(device.getXqName());
        adWatch.setInstallPath(device.getInstallPath());
        //获取发布任务
        AdPushTask adPushTask = this.adPushTaskFeign.findAdPushTaskById(adWatch.getTaskId());
        adWatch.setRootOrgId(adPushTask.getRootOrgId());
        adWatch.setOrgPosition(adPushTask.getOrgPosition());
        //  省市区
        adWatch.setRegionProvince(device.getRegionProvince());
        adWatch.setRegionCity(device.getRegionCity());
        adWatch.setRegionArea(device.getRegionArea());
        adWatch.setCreateDate(new Date());
        adWatch.setTaskType(adPushTask.getTaskType());
        return this.statisticsAdWatchFeign.addAdWatch(adWatch);
    }


    /**
     * 同步人脸广告数据
     * @param device 设备信息
     * @return ResultBean
     */
    @PostMapping(value = "/ad/sync/face/data")
    public ResultBean syncFaceAdvert(@RequestBody DeviceDto device){
        Assert.hasText(device.getSerialNumber(),"设备序列号不能为空");
        //  通知队列下发
        Message<String> build = MessageBuilder.withPayload(device.getSerialNumber()).build();
        this.syncFaceAdvertDataProduceInterface.output().send(build);
        return new ResultBean<>(ResultEnum.SUCCESS);
    }

    /**
     * 同步闲时广告数据
     * @param device 设备信息
     * @return ResultBean
     */
    @PostMapping(value = "/ad/sync/leisure/data")
    public ResultBean syncLeisureAdvert(@RequestBody DeviceDto device){
        Assert.hasText(device.getSerialNumber(),"设备序列号不能为空");
        //  通知队列下发
        Message<String> build = MessageBuilder.withPayload(device.getSerialNumber()).build();
        this.syncLeisureAdvertDataProduceInterface.output().send(build);
        return new ResultBean<>(ResultEnum.SUCCESS);
    }
}
