package com.chinaweal.aiccs.aiccs.forcedCancellation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.forcedCancellation.entity.ForceLogoutNotice;
import com.chinaweal.aiccs.aiccs.forcedCancellation.service.ForceLogoutNoticeService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

import static com.chinaweal.youfool.framework.springboot.rest.ResultCode.BUSINESS_LOGIC_ERROR;

/**
 * <p>
 * 拟强制注销公告信息表 前端控制器
 * </p>
 *
 * @author lwd
 * @since 2025-10-22
 */
@RestController
@RequestMapping("/forceLogoutNotice")
public class ForceLogoutNoticeController extends BaseController {

    @Autowired
    private ForceLogoutNoticeService forceLogoutNoticeService;

    /**
     * 分页查询拟强制注销公告信息
     */
    @ApiOperation("分页查询拟强制注销公告信息")
    @PostMapping("/page")
    public RestResult<?> page(HttpServletRequest request,
                                @RequestBody ForceLogoutNotice forceLogoutNotice,
                                @RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "10") int rows) {
        IPage<ForceLogoutNotice> result = forceLogoutNoticeService.page(new Page<>(page, rows), forceLogoutNotice);
        return RestResult.ok(result);
    }

    /**
     * 根据ID查询拟强制注销公告信息
     */
    @ApiOperation("根据ID查询拟强制注销公告信息")
    @GetMapping("/getById")
    public RestResult<?> getById(@RequestParam String forceId) {
        AssertUtils.isNotBlank(forceId);
        ForceLogoutNotice result = forceLogoutNoticeService.getById(forceId);
        return RestResult.ok(result);
    }

    /**
     * 新增拟强制注销公告信息
     */
    @ApiOperation("新增拟强制注销公告信息")
    @PostMapping("/save")
    public RestResult<?> save(HttpServletRequest request, @RequestBody ForceLogoutNotice forceLogoutNotice) {
        // 设置默认值
        if (forceLogoutNotice.getSExtDatetime() == null) {
            forceLogoutNotice.setSExtDatetime(LocalDateTime.now());
        }
        boolean result = forceLogoutNoticeService.save(forceLogoutNotice);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"保存失败");
    }

    /**
     * 更新拟强制注销公告信息
     */
    @ApiOperation("更新拟强制注销公告信息")
    @PutMapping("/update")
    public RestResult<?> update(HttpServletRequest request, @RequestBody ForceLogoutNotice forceLogoutNotice) {
        AssertUtils.isNotBlank(forceLogoutNotice.getForceId());
        forceLogoutNotice.setSExtDatetime(LocalDateTime.now());
        boolean result = forceLogoutNoticeService.updateById(forceLogoutNotice);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"更新失败");
    }

    /**
     * 删除拟强制注销公告信息
     */
    @ApiOperation("删除拟强制注销公告信息")
    @DeleteMapping("/delete")
    public RestResult<?> delete(@RequestParam String forceId) {
        AssertUtils.isNotBlank(forceId);
        boolean result = forceLogoutNoticeService.removeById(forceId);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"删除失败");
    }

    /**
     * 根据业务ID查询拟强制注销公告信息
     */
    @ApiOperation("根据业务ID查询拟强制注销公告信息")
    @GetMapping("/getByBizSeqId")
    public RestResult<?> getByBizSeqId(@RequestParam String bizseqId) {
        AssertUtils.isNotBlank(bizseqId);
        ForceLogoutNotice result = forceLogoutNoticeService.getByBizSeqId(bizseqId);
        return RestResult.ok(result);
    }

    /**
     * 查询拟强制注销公告列表
     */
    @ApiOperation("查询拟强制注销公告列表")
    @PostMapping("/list")
    public RestResult<?> list(@RequestBody(required = false) ForceLogoutNotice forceLogoutNotice) {
        if (forceLogoutNotice == null) {
            forceLogoutNotice = new ForceLogoutNotice();
        }
        return RestResult.ok();
    }

    /**
     * 根据公告标题查询
     */
    @ApiOperation("根据公告标题查询")
    @GetMapping("/getByNoticeTitle")
    public RestResult<?> getByNoticeTitle(@RequestParam String noticeTitle) {
        AssertUtils.isNotBlank(noticeTitle);
        return RestResult.ok(forceLogoutNoticeService.getByNoticeTitle(noticeTitle));
    }

    /**
     * 根据作出决定机关查询
     */
    @ApiOperation("根据作出决定机关查询")
    @GetMapping("/getByJudAuth")
    public RestResult<?> getByJudAuth(@RequestParam String judAuth) {
        AssertUtils.isNotBlank(judAuth);
        return RestResult.ok(forceLogoutNoticeService.getByJudAuth(judAuth));
    }

    /**
     * 根据公告日期范围查询
     */
    @ApiOperation("根据公告日期范围查询")
    @PostMapping("/getByNoticeDateRange")
    public RestResult<?> getByNoticeDateRange(@RequestParam LocalDate noticeFrom,
                                             @RequestParam LocalDate noticeTo) {
        AssertUtils.isNotNull(noticeFrom);
        AssertUtils.isNotNull(noticeTo);
        return RestResult.ok(forceLogoutNoticeService.getByNoticeDateRange(noticeFrom, noticeTo));
    }

    /**
     * 根据二级部署标识查询
     */
    @ApiOperation("根据二级部署标识查询")
    @GetMapping("/getBySExtFromnode")
    public RestResult<?> getBySExtFromnode(@RequestParam String sExtFromnode) {
        AssertUtils.isNotBlank(sExtFromnode);
        return RestResult.ok(forceLogoutNoticeService.getBySExtFromnode(sExtFromnode));
    }

    /**
     * 更新公告有效期
     */
    @ApiOperation("更新公告有效期")
    @PostMapping("/updateNoticePeriod")
    public RestResult<?> updateNoticePeriod(@RequestParam String forceId,
                                            @RequestParam LocalDate noticeFrom,
                                            @RequestParam LocalDate noticeTo) {
        AssertUtils.isNotBlank(forceId);
        AssertUtils.isNotNull(noticeFrom);
        AssertUtils.isNotNull(noticeTo);
        boolean result = forceLogoutNoticeService.updateNoticePeriod(forceId, noticeFrom, noticeTo);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"更新有效期失败");
    }
}
