package com.sptg.gateway.controller.ad;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.ad.api.constant.AdSecurityToken;
import com.sptg.ad.api.entity.DeviceAdvert;
import com.sptg.ad.api.entity.DeviceAdvertStatistics;
import com.sptg.ad.api.feigns.DeviceAdvertFeign;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.feigns.DeviceAuthorizeFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 *            
 *               
 * description: 设备广告控制器
 */
@RestController
@RequiredArgsConstructor
public class DeviceAdvertController {

    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final DeviceAdvertFeign deviceAdvertFeign;
    private final DeviceAuthorizeFeign deviceAuthorizeFeign;

    /**
     * @return 设备人脸广告统计页面
     */
    @Security(token = AdSecurityToken.DEVICE_ADVERT_FACE,format = DataFormatEnum.VIEW)
    @GetMapping(value = "/ad/device/face/statistics")
    public ModelAndView faceAdvert(){
        ModelAndView modelAndView = new ModelAndView("ad/device-advert-statistics");
        modelAndView.addObject("taskType",1);
        return modelAndView;
    }

    /**
     * @return 设备闲时广告统计页面
     */
    @Security(token = AdSecurityToken.DEVICE_ADVERT_LEISURE,format = DataFormatEnum.VIEW)
    @GetMapping(value = "/ad/device/leisure/statistics")
    public ModelAndView leisureAdvert(){
        ModelAndView modelAndView = new ModelAndView("ad/device-advert-statistics");
        modelAndView.addObject("taskType",2);
        return modelAndView;
    }

    /**
     * 设备人脸广告记录页面
     * @param serialNumber 设备序列号
     * @return ModelAndView
     */
    @Security(token = AdSecurityToken.DEVICE_ADVERT_FACE,format = DataFormatEnum.VIEW)
    @GetMapping(value = "/ad/device/face/list")
    public ModelAndView faceAdvert(String serialNumber){
        Assert.hasText(serialNumber,"设备序列号不能为空");
        ModelAndView modelAndView = new ModelAndView("ad/device-advert-list");
        modelAndView.addObject("serialNumber",serialNumber);
        modelAndView.addObject("taskType",1);
        return modelAndView;
    }

    /**
     * 设备闲时广告记录页面
     * @param serialNumber 设备序列号
     * @return ModelAndView
     */
    @Security(token = AdSecurityToken.DEVICE_ADVERT_LEISURE,format = DataFormatEnum.VIEW)
    @GetMapping(value = "/ad/device/leisure/list")
    public ModelAndView leisureAdvert(String serialNumber){
        Assert.hasText(serialNumber,"设备序列号不能为空");
        ModelAndView modelAndView = new ModelAndView("ad/device-advert-list");
        modelAndView.addObject("serialNumber",serialNumber);
        modelAndView.addObject("taskType",2);
        return modelAndView;
    }

    /**
     * 分页查询设备中的广告列表
     * @param advert 查询条件
     * @param pageBean 分页条件
     * @return ResultBean<Page>
     */
    @GetMapping(value = "/ad/device/pagingQuery")
    public ResultBean<Page> pagingQuery(
            DeviceAdvert advert,
            PageBean<DeviceAdvert,DeviceAdvert> pageBean
    ){
        Assert.notNull(advert,"参数异常");
        Assert.hasText(advert.getSerialNumber(),"设备序列号不能为空");
        pageBean.setDto(advert);
        return ResultBean.ok(this.deviceAdvertFeign.pagingQuery(pageBean));
    }

    /**
     * 分页查询设备广告统计
     * @param statistics 查询条件
     * @param pageBean 分页条件
     * @return ResultBean<Page>
     */
    @Security(format = DataFormatEnum.JSON)
    @GetMapping(value = "/ad/device/statistics/pagingQuery")
    public ResultBean<Page> pagingQuery(
            DeviceAdvertStatistics statistics,
            PageBean<DeviceAdvertStatistics,DeviceAdvertStatistics> pageBean){
        //  获取当前用户可见的所有设备Id列表
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        List<Long> deviceIdList = this.deviceAuthorizeFeign.getDeviceIdListByOrg(onLineUser.getOrgPosition());
        statistics.setDeviceIdList(deviceIdList);
        pageBean.setDto(statistics);
        return ResultBean.ok(this.deviceAdvertFeign.pagingQueryStatistics(pageBean));
    }

    /**
     * 删除设备中的广告
     * @param advert { id:必填 }
     * @return ResultBean<Boolean>
     */
    @Security(format = DataFormatEnum.JSON)
    @GetMapping(value = "/ad/device/delete")
    public ResultBean<Boolean> deleteAdvert(DeviceAdvert advert){
        Assert.notNull(advert,"参数异常");
        Assert.notNull(advert.getId(),"Id不能为空");
        return this.deviceAdvertFeign.deleteAdvert(advert);
    }

}
