package com.dgtly.web.controller.system;


import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.dgtly.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.dgtly.common.annotation.Log;
import com.dgtly.common.enums.BusinessType;
import com.dgtly.system.domain.AlertConfiguration;
import com.dgtly.system.service.IAlertConfigurationService;
import com.dgtly.common.core.controller.BaseController;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.utils.poi.ExcelUtil;
import com.dgtly.common.core.page.TableDataInfo;

/**
 * 弹框配置Controller
 * 
 * @author chenyn
 * @date 2023-06-13
 */
@Slf4j
@Controller
@RequestMapping("/system/configuration")
public class AlertConfigurationController extends BaseController
{
    //前端代码路径
    private String prefix = "system/alertConfiguration";

    @Autowired
    private IAlertConfigurationService alertConfigurationService;

    @RequiresPermissions("system:configuration:view")
    @GetMapping()
    public String configuration()
    {
        return prefix + "/configuration";
    }

    /**
     * 查询弹框配置列表
     */
    @RequiresPermissions("system:configuration:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AlertConfiguration alertConfiguration)
    {
        startPage();
        List<AlertConfiguration> list = alertConfigurationService.selectAlertConfigurationList(alertConfiguration);
        return getDataTable(list);
    }

    /**
     * 导出弹框配置列表
     */
    @RequiresPermissions("system:configuration:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AlertConfiguration alertConfiguration)
    {
        List<AlertConfiguration> list = alertConfigurationService.selectAlertConfigurationList(alertConfiguration);
        ExcelUtil<AlertConfiguration> util = new ExcelUtil<AlertConfiguration>(AlertConfiguration.class);
        return util.exportExcel(list, "configuration");
    }

    /**
     * 新增弹框配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存弹框配置
     */
    @RequiresPermissions("system:configuration:add")
    @Log(title = "弹框配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AlertConfiguration alertConfiguration)
    {
        //如果userType为空,则添加所有人通知
        if (StringUtils.isEmpty(alertConfiguration.getUserType())){
            alertConfiguration.setUserType("2");
        }
        return toAjax(alertConfigurationService.insertAlertConfiguration(alertConfiguration));
    }



    /**
     * 修改弹框配置
     */
    @GetMapping("/edit/{alertId}")
    public String edit(@PathVariable("alertId") Long alertId, ModelMap mmap)
    {
        AlertConfiguration alertConfiguration = alertConfigurationService.selectAlertConfigurationById(alertId);
        mmap.put("alertConfiguration", alertConfiguration);
        return prefix + "/edit";
    }
    /**
     * 修改保存弹框配置
     */
    @RequiresPermissions("system:configuration:edit")
    @Log(title = "弹框配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AlertConfiguration alertConfiguration)
    {

        log.info("新增弹框配置");
        //如果userType为空,则添加所有人通知
        if (StringUtils.isEmpty(alertConfiguration.getUserType())){
            alertConfiguration.setUserType("2");
        }
        if(("0").equals(alertConfiguration.getAlertStatus())){
            return toAjax(alertConfigurationService.updateAlertConfiguration(alertConfiguration));
        }
        return error("请取消发布后再做修改");
    }
    /**
     * 是否发布/取消发布弹框配置
     */
    @RequiresPermissions("system:configuration:edit")
    @Log(title = "发布弹框", businessType = BusinessType.UPDATE)
    @PostMapping("/release")
    @ResponseBody
    public AjaxResult release(AlertConfiguration alertConfiguration)
    {
       if(alertConfiguration.getAlertStatus().equals("0")){
           return toAjax(alertConfigurationService.updateAlertConfiguration(alertConfiguration));
       }else{
           AlertConfiguration alert = alertConfigurationService.selectAlertConfigurationById(alertConfiguration.getAlertId());
           int num = alertConfigurationService.selectAlertConfigurationListFilter(alert);
           if(num==1){
               return AjaxResult.error("已发布该组织在该时间内的弹框,请核查关闭已启动的后可开启本弹框");
           }else {
               return toAjax(alertConfigurationService.updateAlertConfiguration(alertConfiguration));
           }

       }
    }

    /**
     * 删除弹框配置
     */
    @RequiresPermissions("system:configuration:remove")
    @Log(title = "弹框配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(alertConfigurationService.deleteAlertConfigurationByIds(ids));
    }


}
