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.ForceLogoutNoticeDetail;
import com.chinaweal.aiccs.aiccs.forcedCancellation.mapper.ForceLogoutNoticeDetailMapper;
import com.chinaweal.aiccs.aiccs.forcedCancellation.service.ForceLogoutNoticeDetailService;
import com.chinaweal.aiccs.aiccs.revoke.entity.Tsbizrevlist;
import com.chinaweal.aiccs.aiccs.revoke.entity.Tsrevtasklist;
import com.chinaweal.aiccs.aiccs.revoke.service.TsbizrevlistService;
import com.chinaweal.aiccs.aiccs.revoke.service.TsrevtasklistService;
import com.chinaweal.aiccs.common.util.FlowUtil;
import com.chinaweal.aiccs.common.util.HandleWorkFlowUtil;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 拟强制注销公告明细表（关联公告主表） 服务实现类
 * </p>
 *
 * @author lwd
 * @since 2025-10-22
 */
@Service
public class ForceLogoutNoticeDetailServiceImpl extends BaseServiceImpl<ForceLogoutNoticeDetailMapper, ForceLogoutNoticeDetail> implements ForceLogoutNoticeDetailService {

    @Autowired
    private ForceLogoutNoticeDetailMapper forceLogoutNoticeDetailMapper;
    @Autowired
    private TsrevtasklistService tsrevtasklistService;
    @Autowired
    private TsbizrevlistService tsbizrevlistService;


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

        // 构建查询条件
        if (StringUtils.isNotBlank(entity.getBizseqId())) {
            wrapper.eq(ForceLogoutNoticeDetail::getBizseqId, entity.getBizseqId());
        }
        if (StringUtils.isNotBlank(entity.getForceId())) {
            wrapper.eq(ForceLogoutNoticeDetail::getForceId, entity.getForceId());
        }
        if (StringUtils.isNotBlank(entity.getPripid())) {
            wrapper.eq(ForceLogoutNoticeDetail::getPripid, entity.getPripid());
        }
        if (StringUtils.isNotBlank(entity.getEntName())) {
            wrapper.like(ForceLogoutNoticeDetail::getEntName, entity.getEntName());
        }
        if (StringUtils.isNotBlank(entity.getUniscid())) {
            wrapper.eq(ForceLogoutNoticeDetail::getUniscid, entity.getUniscid());
        }
        if (StringUtils.isNotBlank(entity.getRegOrg())) {
            wrapper.eq(ForceLogoutNoticeDetail::getRegOrg, entity.getRegOrg());
        }
        if (entity.getDecDate() != null) {
            wrapper.ge(ForceLogoutNoticeDetail::getDecDate, entity.getDecDate());
        }
        if (entity.getAnnDate() != null) {
            wrapper.ge(ForceLogoutNoticeDetail::getAnnDate, entity.getAnnDate());
        }
        if (entity.getNoticeFrom() != null) {
            wrapper.ge(ForceLogoutNoticeDetail::getNoticeFrom, entity.getNoticeFrom());
        }
        if (entity.getNoticeTo() != null) {
            wrapper.le(ForceLogoutNoticeDetail::getNoticeTo, entity.getNoticeTo());
        }
        if (StringUtils.isNotBlank(entity.getForceRra())) {
            wrapper.eq(ForceLogoutNoticeDetail::getForceRra, entity.getForceRra());
        }
        if (StringUtils.isNotBlank(entity.getForceRes())) {
            wrapper.eq(ForceLogoutNoticeDetail::getForceRes, entity.getForceRes());
        }
        if (StringUtils.isNotBlank(entity.getEndForceRra())) {
            wrapper.eq(ForceLogoutNoticeDetail::getEndForceRra, entity.getEndForceRra());
        }
        if (StringUtils.isNotBlank(entity.getSExtFromnode())) {
            wrapper.eq(ForceLogoutNoticeDetail::getSExtFromnode, entity.getSExtFromnode());
        }

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

        return page(page, wrapper);
    }

    @Override
    public List<ForceLogoutNoticeDetail> getByForceId(String forceId) {
        LambdaQueryWrapper<ForceLogoutNoticeDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ForceLogoutNoticeDetail::getForceId, forceId);
        wrapper.orderByDesc(ForceLogoutNoticeDetail::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutNoticeDetail> getByBizSeqId(String bizseqId) {
        LambdaQueryWrapper<ForceLogoutNoticeDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ForceLogoutNoticeDetail::getBizseqId, bizseqId);
        wrapper.eq(ForceLogoutNoticeDetail::getIsRemove, "0");
        wrapper.orderByDesc(ForceLogoutNoticeDetail::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutNoticeDetail> getByPripid(String pripid) {
        LambdaQueryWrapper<ForceLogoutNoticeDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ForceLogoutNoticeDetail::getPripid, pripid);
        wrapper.orderByDesc(ForceLogoutNoticeDetail::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutNoticeDetail> getByUniscid(String uniscid) {
        LambdaQueryWrapper<ForceLogoutNoticeDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ForceLogoutNoticeDetail::getUniscid, uniscid);
        wrapper.orderByDesc(ForceLogoutNoticeDetail::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutNoticeDetail> getByEntName(String entName) {
        LambdaQueryWrapper<ForceLogoutNoticeDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.like(ForceLogoutNoticeDetail::getEntName, entName);
        wrapper.orderByDesc(ForceLogoutNoticeDetail::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutNoticeDetail> getByRegOrg(String regOrg) {
        LambdaQueryWrapper<ForceLogoutNoticeDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ForceLogoutNoticeDetail::getRegOrg, regOrg);
        wrapper.orderByDesc(ForceLogoutNoticeDetail::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutNoticeDetail> getByForceRes(String forceRes) {
        LambdaQueryWrapper<ForceLogoutNoticeDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ForceLogoutNoticeDetail::getForceRes, forceRes);
        wrapper.orderByDesc(ForceLogoutNoticeDetail::getSExtDatetime);
        return list(wrapper);
    }

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

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

    @Override
    @Transactional
    public boolean batchUpdateResult(List<String> forceResIds, String forceRes, LocalDate forceResDate) {
        if (forceResIds == null || forceResIds.isEmpty()) {
            return false;
        }

        for (String forceResId : forceResIds) {
            ForceLogoutNoticeDetail entity = new ForceLogoutNoticeDetail();
            entity.setForceResId(forceResId);
            entity.setForceRes(forceRes);
            entity.setForceResDate(forceResDate);
            if (!updateById(entity)) {
                return false;
            }
        }
        return true;
    }

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

    @Override
    public List<ForceLogoutNoticeDetail> statisticsByForceRes(String forceId) {
        LambdaQueryWrapper<ForceLogoutNoticeDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.select(ForceLogoutNoticeDetail::getForceRes);
        wrapper.eq(ForceLogoutNoticeDetail::getForceId, forceId);
        wrapper.groupBy(ForceLogoutNoticeDetail::getForceRes);
        return list(wrapper);
    }

    @Override
    public List<ForceLogoutNoticeDetail> getByForceIdAndRegOrg(String forceId, String regOrg) {
        LambdaQueryWrapper<ForceLogoutNoticeDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ForceLogoutNoticeDetail::getForceId, forceId);
        wrapper.eq(ForceLogoutNoticeDetail::getRegOrg, regOrg);
        wrapper.orderByDesc(ForceLogoutNoticeDetail::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public Tsrevtasklist saveRevListZXAccept(AICUser aicUser, Tsrevtasklist revtasklistModel, ArrayList<Tsbizrevlist> tsbizrevlists) {
        if (StringUtils.isBlank(revtasklistModel.getTasklistid())) {
            for (Tsbizrevlist bizRevListModel : tsbizrevlists) {
                bizRevListModel.setIntosponsor(aicUser.getName());//设置主办人
                bizRevListModel.setIntosponsorId(aicUser.getPrimaryKey());//主办人id
                bizRevListModel.setStatus("31"); // 流程启动把状态设置为 "立案经办中"
                tsbizrevlistService.saveOrUpdate(bizRevListModel);
            }
            String definedname = FlowUtil.BATCHDEACTIVATIONLWORKFLOW;//注销销登记批量注销流程模板

            //新待办 设置待办信息
            revtasklistModel = HandleWorkFlowUtil.createRevTaskList(revtasklistModel, aicUser);
            //开始工作流
            tsrevtasklistService.startWorkFlow(revtasklistModel, aicUser, definedname);
        }
        return revtasklistModel;
    }



}
