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.AdPushTaskDto;
import com.sptg.ad.api.entity.AdPushContent;
import com.sptg.ad.api.entity.AdPushTask;
import com.sptg.ad.api.entity.AdPushTaskDetails;
import com.sptg.ad.api.entity.AdPushThresholdValue;
import com.sptg.ad.api.feigns.AdPushTaskFeign;
import com.sptg.ad.biz.service.AdPushContentServiceInterface;
import com.sptg.ad.biz.service.AdPushTaskDetailsServiceInterface;
import com.sptg.ad.biz.service.AdPushTaskServiceInterface;
import com.sptg.ad.biz.service.AdPushThresholdValueServiceInterface;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * title: AdPushTaskController
 * package: com.sptg.ad.biz.controller
 * description: 广告任务发布-控制器
 *
 *               
 *                 
 */
@RestController
@RequiredArgsConstructor
public class AdPushTaskController implements AdPushTaskFeign {

    private final AdPushContentServiceInterface adPushContentServiceInterface;
    private final AdPushTaskServiceInterface adPushTaskServiceInterface;
    private final AdPushTaskDetailsServiceInterface adPushTaskDetailsServiceInterface;
    private final AdPushThresholdValueServiceInterface adPushThresholdValueServiceInterface;

    /**
     * 功能描述:显示广告类容列表
     * @param
     * @return
     *               
     *                       
     */
    @Override
    @PostMapping(value = "/showAdPushTaskList")
    public ResultBean<Page<AdPushTask>> showAdPushTaskList(@RequestBody AdPushTaskDto adPushTaskDto) {
        Page<AdPushTask> adPushTaskPage = adPushTaskServiceInterface.showAdPushTaskList(adPushTaskDto);
        return new ResultBean<>(ResultEnum.SUCCESS,adPushTaskPage);
    }

    @Override
    @PostMapping(value = "/addAdPushTask")
    public ResultBean<Boolean> addAdPushTask(@RequestBody AdPushTask adPushTask){
        return this.adPushTaskServiceInterface.addAdPushTask(adPushTask);
    }

    @Override
    @PostMapping(value = "/cancelAdPushTask")
    public ResultBean<Boolean> cancelAdPushTask(@RequestBody Long id) {
        return this.adPushTaskServiceInterface.cancelAdPushTask(id);
    }

    @Override
    @PostMapping(value = "/findAdPushTaskById")
    public AdPushTask findAdPushTaskById(@RequestBody Long id) {
        return this.adPushTaskServiceInterface.findAdPushTaskById(id);
    }

    @Override
    @PostMapping(value = "/manuallyIssued")
    public ResultBean<Boolean> manuallyIssued(@RequestBody Long id) {
        return this.adPushTaskServiceInterface.manuallyIssued(id);
    }

    @Override
    @GetMapping(value = "/getPushContentByTask/{taskId}/{code}")
    public AdPushContent getPushContentByTask(
            @PathVariable(value = "taskId")Long taskId,
            @PathVariable(value = "code")String code
    ) {
        Wrapper<AdPushContent> wrapper = new EntityWrapper<>();
        wrapper.eq("l_task_id",taskId);
        wrapper.eq("s_ad_code",code);
        return this.adPushContentServiceInterface.selectOne(wrapper);
    }

    @Override
    @PostMapping(value = "/getAdPushTaskDetailsByTaskId")
    public List<AdPushTaskDetails> getAdPushTaskDetailsByTaskId(@RequestBody Long id) {
        return this.adPushTaskDetailsServiceInterface.getAdPushTaskDetails(id);
    }

    @Override
    @PostMapping(value = "/getAdPushContentsByTaskId")
    public List<AdPushContent> getAdPushContentsByTaskId(@RequestBody Long id) {
        return this.adPushContentServiceInterface.getAdPushContents(id);
    }

    @Override
    @PostMapping(value = "/adIssued")
    public ResultBean<Boolean> adIssued(@RequestBody Long deviceId) {
        return this.adPushTaskServiceInterface.adIssued(deviceId);
    }

    @Override
    @PostMapping(value = "/getAdPushThresholdValue")
    public AdPushThresholdValue getAdPushThresholdValue(@RequestBody AdPushThresholdValue adPushThresholdValue) {
        return this.adPushThresholdValueServiceInterface.getAdPushThresholdValue(adPushThresholdValue);
    }

    @Override
    @PostMapping(value = "/savePushThresholdValue")
    public ResultBean<Boolean> savePushThresholdValue(@RequestBody AdPushThresholdValue adPushThresholdValue) {
        boolean result=this.adPushThresholdValueServiceInterface.saveOrUpdate(adPushThresholdValue);
        return  new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/getValidAdPushCountDevice")
    public int getValidAdPushCountDevice(@RequestBody Long deviceId) {
        int count=this.adPushTaskServiceInterface.getValidAdPushCountDevice(deviceId);
        return  count;
    }
}
