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.EarlyWarning;
import com.sptg.ad.api.feigns.EarlyWarningFeign;
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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *            
 *                
 * description: 预警控制器
 */
@Controller
@RequiredArgsConstructor
public class EarlyWarningController {

    private final EarlyWarningFeign earlyWarningFeign;

    /**
     * @return 人脸广告预警记录列表页面
     */
    @GetMapping(value = "/ad/face/early_warning")
    @Security(token = AdSecurityToken.DEVICE_ADVERT_FACE_EARLYWARNING,format = DataFormatEnum.JSON)
    public String faceEarlyWarning(Model model){
        return this.earlyWarning(1,model);
    }

    /**
     * @return 闲时广告预警记录列表页面
     */
    @GetMapping(value = "/ad/leisure/early_warning")
    @Security(token = AdSecurityToken.DEVICE_ADVERT_LEISURE_EARLYWARNING,format = DataFormatEnum.JSON)
    public String leisureEarlyWarning(Model model){
        return this.earlyWarning(2,model);
    }

    private String earlyWarning(Integer taskType,Model model){
        model.addAttribute("taskType",taskType);
        return "ad/early_warning";
    }

    @ResponseBody
    @GetMapping(value = "/ad/face/early_warning/pagingQuery")
    public ResultBean<Page> pagingQuery(
            EarlyWarning warning,
            PageBean<EarlyWarning,EarlyWarning> pageBean
    ){
        Assert.notNull(warning,"参数异常");
        pageBean.setDto(warning);
        return ResultBean.ok(this.earlyWarningFeign.pagingQuery(pageBean));
    }

    @ResponseBody
    @PostMapping(value = "/ad/face/early_warning/add")
    public ResultBean<Boolean> add(@RequestBody EarlyWarning warning){
        Assert.notNull(warning.getTaskType(),"参数异常");
        Assert.notEmpty(warning.getDeviceIdList(),"请选择设备");
        boolean isNull = (null == warning.getMonthExpect()) && (null == warning.getWeekExpect());
        Assert.isTrue(!isNull,"请输入月预警值或月预警值!");
        return this.earlyWarningFeign.addEntity(warning);
    }

}
