package cn.ezhizao.project.business.notification.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.notification.domain.SysNotification;
import cn.ezhizao.project.business.notification.service.ISysNotificationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;




/**
 * 发版通知公告Controller
 *
 * @author ezhizao
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/business/notification")
public class SysNotificationController extends BaseController
{
    @Resource
    private ISysNotificationService sysNotificationService;

    /**
     * 查询发版通知公告列表
     */
//    @PreAuthorize("@ss.hasPermi('business:notification:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysNotification sysNotification) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(sysNotification);
        startPage();
        List<SysNotification> list = sysNotificationService.getList(sysNotification);
        return getDataTable(list);
    }

    /**
     * 导出发版通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('business:notification:export')")
    @Log(title = "发版通知公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysNotification sysNotification) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(sysNotification);
        List<SysNotification> list = sysNotificationService.getList(sysNotification);
        ExcelUtil<SysNotification> util = new ExcelUtil<SysNotification>(SysNotification.class);
        util.exportExcel(response, list, "发版通知公告数据");
    }

    /**
     * 获取发版通知公告详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:notification:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysNotificationService.getById(id));
    }
    @Log(title = "搜索更新中，公告中的发版内容", businessType = BusinessType.SELECT)
    @GetMapping(value = "/getFbNotification")
    public AjaxResult getFbNotification(SysNotification sysNotification) throws NoSuchFieldException, IllegalAccessException
    {
        return success(sysNotificationService.query().eq("type", 2).in("status", 1, 2).list());
    }

    /**
     * 新增发版通知公告
     */
    @PreAuthorize("@ss.hasPermi('business:notification:add')")
    @Log(title = "发版通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysNotification sysNotification) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(sysNotification);
        if(canSave(sysNotification)){
            return toAjax(sysNotificationService.save(sysNotification));
        }
        return error("已经存在发版公告,不能新增");
    }
    //判断如果系统公告类型是发版，且存在其状态为公告中，更新中，不能保存
    public boolean canSave(SysNotification sysNotification) {
        //发版且状态为更新中、公告中
        if(sysNotification.getType() == 2 && (sysNotification.getStatus() == 1 || sysNotification.getStatus() == 2)){
            //新增
            if(sysNotification.getId() == null){
                List<SysNotification> list = sysNotificationService.query().eq("type", 2).in("status", 1, 2).list();
                if (!list.isEmpty()) {
                    return false;
                }
                return true;
            }else{
                List<SysNotification> list = sysNotificationService.query()
                        .eq("type", 2).in("status", 1, 2).ne("id", sysNotification.getId()).list();
                if (!list.isEmpty()) {
                    return false;
                }
                return true;
            }
        }
        return true;
    }

    /**
     * 修改发版通知公告
     */
    @PreAuthorize("@ss.hasPermi('business:notification:edit')")
    @Log(title = "发版通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysNotification sysNotification) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(sysNotification);
        if(canSave(sysNotification)){
            return toAjax(sysNotificationService.updateById(sysNotification));
        }
        return error("已经存在发版公告,不能修改");
    }

    /**
     * 删除发版通知公告
     */
    @PreAuthorize("@ss.hasPermi('business:notification:remove')")
    @Log(title = "发版通知公告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(sysNotificationService.removeBatchByIds(ids));
    }

    @GetMapping("/getUnshowedNotification")
    public AjaxResult getUnshowedNotification() {
        // 获取未结束的公告
        List<SysNotification> list = sysNotificationService.query().eq("status", 2).list();
        // 假设有一个公告为更新中的公告，另一个公告为非更新中则可以接着查询。
        return new AjaxResult(list.stream().anyMatch(t -> t.getStatus() == 2) ? 202 : 200, "", list);
    }
}
