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.ForceLogoutServNotice;
import com.chinaweal.aiccs.aiccs.forcedCancellation.service.ForceLogoutServNoticeService;
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.List;
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("/forceLogoutServNotice")
public class ForceLogoutServNoticeController extends BaseController {

    @Autowired
    private ForceLogoutServNoticeService forceLogoutServNoticeService;

    /**
     * 分页查询强制注销登记决定书送达公告
     */
    @ApiOperation("分页查询强制注销登记决定书送达公告")
    @PostMapping("/page")
    public RestResult<?> page(HttpServletRequest request,
                                @RequestBody ForceLogoutServNotice forceLogoutServNotice,
                                @RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "10") int rows) {
        IPage<ForceLogoutServNotice> result = forceLogoutServNoticeService.page(new Page<>(page, rows), forceLogoutServNotice);
        return RestResult.ok(result);
    }

    /**
     * 根据ID查询强制注销登记决定书送达公告
     */
    @ApiOperation("根据ID查询强制注销登记决定书送达公告")
    @GetMapping("/getById")
    public RestResult<?> getById(@RequestParam String noticeId) {
        AssertUtils.isNotBlank(noticeId);
        ForceLogoutServNotice result = forceLogoutServNoticeService.getById(noticeId);
        return RestResult.ok(result);
    }

    /**
     * 新增强制注销登记决定书送达公告
     */
    @ApiOperation("新增强制注销登记决定书送达公告")
    @PostMapping("/save")
    public RestResult<?> save(HttpServletRequest request, @RequestBody ForceLogoutServNotice forceLogoutServNotice) {
        // 设置默认值
        if (forceLogoutServNotice.getSExtDatetime() == null) {
            forceLogoutServNotice.setSExtDatetime(LocalDateTime.now());
        }
        boolean result = forceLogoutServNoticeService.save(forceLogoutServNotice);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"系统繁忙，请稍后重试");
    }

    /**
     * 更新强制注销登记决定书送达公告
     */
    @ApiOperation("更新强制注销登记决定书送达公告")
    @PutMapping("/update")
    public RestResult<?> update(HttpServletRequest request, @RequestBody ForceLogoutServNotice forceLogoutServNotice) {
        AssertUtils.isNotBlank(forceLogoutServNotice.getNoticeId());
        forceLogoutServNotice.setSExtDatetime(LocalDateTime.now());
        boolean result = forceLogoutServNoticeService.updateById(forceLogoutServNotice);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"更新失败");
    }

    /**
     * 删除强制注销登记决定书送达公告
     */
    @ApiOperation("删除强制注销登记决定书送达公告")
    @DeleteMapping("/delete")
    public RestResult<?> delete(@RequestParam String noticeId) {
        AssertUtils.isNotBlank(noticeId);
        boolean result = forceLogoutServNoticeService.removeById(noticeId);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"删除失败，请稍后重试");
    }

    /**
     * 根据业务ID查询强制注销登记决定书送达公告
     */
    @ApiOperation("根据业务ID查询强制注销登记决定书送达公告")
    @GetMapping("/getByBizSeqId")
    public RestResult<?> getByBizSeqId(@RequestParam String bizseqId) {
        AssertUtils.isNotBlank(bizseqId);
        ForceLogoutServNotice result = forceLogoutServNoticeService.getByBizSeqId(bizseqId);
        return RestResult.ok(result);
    }

    /**
     * 查询强制注销登记决定书送达公告列表
     */
    @ApiOperation("查询强制注销登记决定书送达公告列表")
    @PostMapping("/list")
    public RestResult<?> list(@RequestBody(required = false) ForceLogoutServNotice forceLogoutServNotice) {
        if (forceLogoutServNotice == null) {
            forceLogoutServNotice = new ForceLogoutServNotice();
        }
        return RestResult.ok();
    }

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

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

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

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

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

    /**
     * 根据下载网址查询
     */
    @ApiOperation("根据下载网址查询")
    @GetMapping("/getByWebsite")
    public RestResult<?> getByWebsite(@RequestParam String website) {
        AssertUtils.isNotBlank(website);
        return RestResult.ok(forceLogoutServNoticeService.getByWebsite(website));
    }

    /**
     * 批量保存
     */
    @ApiOperation("批量保存")
    @PostMapping("/batchSave")
    public RestResult<?> batchSave(HttpServletRequest request, @RequestBody List<ForceLogoutServNotice> notices) {
        if (notices == null || notices.isEmpty()) {
            return RestResult.error(BUSINESS_LOGIC_ERROR,"公告列表不能为空");
        }
        LocalDateTime now = LocalDateTime.now();
        for (ForceLogoutServNotice notice : notices) {
            if (notice.getSExtDatetime() == null) {
                notice.setSExtDatetime(now);
            }
        }
        boolean result = forceLogoutServNoticeService.saveBatch(notices);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"批量保存失败");
    }

    /**
     * 更新公告发布日期
     */
    @ApiOperation("更新公告发布日期")
    @PostMapping("/updateAnnDate")
    public RestResult<?> updateAnnDate(@RequestParam String noticeId,
                                          @RequestParam LocalDate annDate) {
        AssertUtils.isNotBlank(noticeId);
        AssertUtils.isNotNull(annDate);
        boolean result = forceLogoutServNoticeService.updateAnnDate(noticeId, annDate);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"更新公告发布日期失败");
    }

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