package com.chinaweal.aiccs.aiccs.forcedCancellation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.forcedCancellation.entity.ForceLogoutServNotice;
import com.chinaweal.aiccs.aiccs.forcedCancellation.mapper.ForceLogoutServNoticeMapper;
import com.chinaweal.aiccs.aiccs.forcedCancellation.service.ForceLogoutServNoticeService;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 强制注销登记决定书送达公告表 服务实现类
 * </p>
 *
 * @author lwd
 * @since 2025-10-22
 */
@Service
public class ForceLogoutServNoticeServiceImpl extends BaseServiceImpl<ForceLogoutServNoticeMapper, ForceLogoutServNotice> implements ForceLogoutServNoticeService {

    @Override
    public IPage<ForceLogoutServNotice> page(IPage<ForceLogoutServNotice> page, ForceLogoutServNotice entity) {
        LambdaQueryWrapper<ForceLogoutServNotice> wrapper = Wrappers.lambdaQuery();

        // 构建查询条件
        if (StringUtils.isNotBlank(entity.getBizseqId())) {
            wrapper.eq(ForceLogoutServNotice::getBizseqId, entity.getBizseqId());
        }
        if (StringUtils.isNotBlank(entity.getNoticeTitle())) {
            wrapper.like(ForceLogoutServNotice::getNoticeTitle, entity.getNoticeTitle());
        }
        if (StringUtils.isNotBlank(entity.getJudAuth())) {
            wrapper.eq(ForceLogoutServNotice::getJudAuth, entity.getJudAuth());
        }
        if (entity.getAnnDate() != null) {
            wrapper.ge(ForceLogoutServNotice::getAnnDate, entity.getAnnDate());
        }
        if (entity.getNoticeFrom() != null) {
            wrapper.ge(ForceLogoutServNotice::getNoticeFrom, entity.getNoticeFrom());
        }
        if (entity.getNoticeTo() != null) {
            wrapper.le(ForceLogoutServNotice::getNoticeTo, entity.getNoticeTo());
        }
        if (StringUtils.isNotBlank(entity.getSExtFromnode())) {
            wrapper.eq(ForceLogoutServNotice::getSExtFromnode, entity.getSExtFromnode());
        }
        if (StringUtils.isNotBlank(entity.getWebsite())) {
            wrapper.like(ForceLogoutServNotice::getWebsite, entity.getWebsite());
        }

        // 按时间降序排序
        wrapper.orderByDesc(ForceLogoutServNotice::getSExtDatetime);

        return page(page, wrapper);
    }

    @Override
    public ForceLogoutServNotice getByBizSeqId(String bizseqId) {
        LambdaQueryWrapper<ForceLogoutServNotice> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ForceLogoutServNotice::getBizseqId, bizseqId);
        wrapper.orderByDesc(ForceLogoutServNotice::getSExtDatetime);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    @Override
    public List<ForceLogoutServNotice> getByNoticeTitle(String noticeTitle) {
        LambdaQueryWrapper<ForceLogoutServNotice> wrapper = Wrappers.lambdaQuery();
        wrapper.like(ForceLogoutServNotice::getNoticeTitle, noticeTitle);
        wrapper.orderByDesc(ForceLogoutServNotice::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutServNotice> getByJudAuth(String judAuth) {
        LambdaQueryWrapper<ForceLogoutServNotice> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ForceLogoutServNotice::getJudAuth, judAuth);
        wrapper.orderByDesc(ForceLogoutServNotice::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutServNotice> getByAnnDateRange(LocalDate annDateFrom, LocalDate annDateTo) {
        LambdaQueryWrapper<ForceLogoutServNotice> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(ForceLogoutServNotice::getAnnDate, annDateFrom);
        wrapper.le(ForceLogoutServNotice::getAnnDate, annDateTo);
        wrapper.orderByDesc(ForceLogoutServNotice::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutServNotice> getByNoticeDateRange(LocalDate noticeFrom, LocalDate noticeTo) {
        LambdaQueryWrapper<ForceLogoutServNotice> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(ForceLogoutServNotice::getNoticeFrom, noticeFrom);
        wrapper.le(ForceLogoutServNotice::getNoticeTo, noticeTo);
        wrapper.orderByDesc(ForceLogoutServNotice::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutServNotice> getBySExtFromnode(String sExtFromnode) {
        LambdaQueryWrapper<ForceLogoutServNotice> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ForceLogoutServNotice::getSExtFromnode, sExtFromnode);
        wrapper.orderByDesc(ForceLogoutServNotice::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutServNotice> getByWebsite(String website) {
        LambdaQueryWrapper<ForceLogoutServNotice> wrapper = Wrappers.lambdaQuery();
        wrapper.like(ForceLogoutServNotice::getWebsite, website);
        wrapper.orderByDesc(ForceLogoutServNotice::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    @Transactional
    public boolean updateAnnDate(String noticeId, LocalDate annDate) {
        ForceLogoutServNotice entity = new ForceLogoutServNotice();
        entity.setNoticeId(noticeId);
        entity.setAnnDate(annDate);
        return updateById(entity);
    }

    @Override
    @Transactional
    public boolean updateNoticePeriod(String noticeId, LocalDate noticeFrom, LocalDate noticeTo) {
        ForceLogoutServNotice entity = new ForceLogoutServNotice();
        entity.setNoticeId(noticeId);
        entity.setNoticeFrom(noticeFrom);
        entity.setNoticeTo(noticeTo);
        return updateById(entity);
    }

    @Override
    public List<ForceLogoutServNotice> getValidNotices(LocalDate currentDate) {
        LambdaQueryWrapper<ForceLogoutServNotice> wrapper = Wrappers.lambdaQuery();
        wrapper.le(ForceLogoutServNotice::getAnnDate, currentDate);
        wrapper.ge(ForceLogoutServNotice::getNoticeTo, currentDate);
        wrapper.orderByDesc(ForceLogoutServNotice::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutServNotice> getByJudAuthAndDateRange(String judAuth, LocalDate startDate, LocalDate endDate) {
        LambdaQueryWrapper<ForceLogoutServNotice> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ForceLogoutServNotice::getJudAuth, judAuth);
        wrapper.ge(ForceLogoutServNotice::getAnnDate, startDate);
        wrapper.le(ForceLogoutServNotice::getAnnDate, endDate);
        wrapper.orderByDesc(ForceLogoutServNotice::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutServNotice> getByNoticeDateRangeAndNode(LocalDate noticeFrom, LocalDate noticeTo, String sExtFromnode) {
        LambdaQueryWrapper<ForceLogoutServNotice> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(ForceLogoutServNotice::getNoticeFrom, noticeFrom);
        wrapper.le(ForceLogoutServNotice::getNoticeTo, noticeTo);
        wrapper.eq(ForceLogoutServNotice::getSExtFromnode, sExtFromnode);
        wrapper.orderByDesc(ForceLogoutServNotice::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    @Transactional
    public boolean batchUpdateNoticeContent(List<String> noticeIds, String noticeTitle, String noticeContent) {
        if (noticeIds == null || noticeIds.isEmpty()) {
            return false;
        }

        for (String noticeId : noticeIds) {
            ForceLogoutServNotice entity = new ForceLogoutServNotice();
            entity.setNoticeId(noticeId);
            entity.setNoticeTitle(noticeTitle);
            entity.setNoticeContent(noticeContent);
            if (!updateById(entity)) {
                return false;
            }
        }
        return true;
    }
}
