package com.sptg.ad.biz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.ad.api.dto.AdPushDeviceDetailsDto;
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.biz.service.AdPushContentServiceInterface;
import com.sptg.ad.biz.service.AdPushDeviceDetailsServiceInterface;
import com.sptg.ad.biz.service.AdPushTaskServiceInterface;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.feigns.DeviceFeign;
import com.sptg.statistics.api.entity.StatisticsAdPush;
import com.sptg.statistics.api.feigns.StatisticsAdPushFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
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.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * title: AdTypeController
 * package: com.sptg.ad.biz.controller
 * description: 广告发布到设备详情--控制器
 *
 *               
 *                 
 */
@RestController
@RequiredArgsConstructor
public class AdPushDeviceDetailsController implements AdPushDeviceDetailsFeign {

    private final AdPushTaskServiceInterface adPushTaskServiceInterface;
    private final AdPushDeviceDetailsServiceInterface adPushDeviceDetailsServiceInterface;
    private final DeviceFeign deviceFeign;
    private final StatisticsAdPushFeign statisticsAdPushFeign;
    private final AdPushContentServiceInterface adPushContentServiceInterface;


    /**
     * 查询详情列表(分页)
     * @return 分页查询结果
     *               
     *                      
     */
    @Override
    @PostMapping(value = "/showAdPushDeviceDetails")
    public ResultBean<Page<AdPushDeviceDetails>> showAdPushDeviceDetails(@RequestBody AdPushDeviceDetailsDto adPushDeviceDetailsDto){
       Page<AdPushDeviceDetails> page = adPushDeviceDetailsServiceInterface.showAdPushDeviceDetails(adPushDeviceDetailsDto);
       return new ResultBean<>(ResultEnum.SUCCESS,page);
    }

    @Override
    @PostMapping(value = "/ad/task/updatePushStatus")
    @Transactional(rollbackFor = Exception.class)
    public ResultBean<Boolean> updatePushStatus(@RequestBody AdPushDeviceDetails dto) {
        Wrapper<AdPushDeviceDetails> wrapper = new EntityWrapper<>();
        wrapper.eq("l_task_id",dto.getTaskId());
        wrapper.eq("s_serial_number",dto.getSerialNumber());
        boolean result = this.adPushDeviceDetailsServiceInterface.update(dto, wrapper);
        Assert.isTrue(result,"请求参数有误");
        //  判断是否推送成功
        if (dto.getPushStatus() == OperationConstant.ONE){
            //  获取推送任务信息
            AdPushTask task = this.adPushTaskServiceInterface.selectById(dto.getTaskId());
            task.setPushSuccess(task.getPushSuccess() + 1);
            result = this.adPushTaskServiceInterface.updateById(task);
        }
        //发布广告内容
        List<AdPushContent> contentList = this.adPushContentServiceInterface.getAdPushContents(dto.getTaskId());
        if(!contentList.isEmpty()){
            Device device = this.deviceFeign.getEntityBySerialNumber(dto.getSerialNumber());
            AdPushTask adPushTask = this.adPushTaskServiceInterface.findAdPushTaskById(dto.getTaskId());
            Date now = new Date();
            List<StatisticsAdPush> statisticsAdPushList = new ArrayList<>();
            contentList.forEach(content -> {
                StatisticsAdPush statisticsAdPush=new StatisticsAdPush();
                statisticsAdPush.setAdPushTaskId(dto.getTaskId());
                statisticsAdPush.setTaskType(adPushTask.getTaskType());
                statisticsAdPush.setAdContentId(content.getAdId());
                statisticsAdPush.setAdBeginDate(adPushTask.getBeginDate());
                statisticsAdPush.setAdEndDate(adPushTask.getEndDate());
                statisticsAdPush.setDeviceSerialNumber(device.getSerialNumber());
                statisticsAdPush.setDeviceInstallPath(device.getInstallPath());
                statisticsAdPush.setDeviceRegionProvince(device.getRegionProvince());
                statisticsAdPush.setDeviceRegionCity(device.getRegionCity());
                statisticsAdPush.setDeviceRegionArea(device.getRegionArea());
                statisticsAdPush.setOrgPosition(adPushTask.getOrgPosition());
                statisticsAdPush.setRootOrgId(adPushTask.getRootOrgId());
                statisticsAdPush.setCreateDate(now);
                statisticsAdPushList.add(statisticsAdPush);
            });
            //统计广告发布投放记录
            this.statisticsAdPushFeign.addAdPushList(statisticsAdPushList);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }
}
