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.LicenseCancellationDeclare;
import com.chinaweal.aiccs.aiccs.forcedCancellation.mapper.LicenseCancellationDeclareMapper;
import com.chinaweal.aiccs.aiccs.forcedCancellation.service.LicenseCancellationDeclareService;
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 LicenseCancellationDeclareServiceImpl extends BaseServiceImpl<LicenseCancellationDeclareMapper, LicenseCancellationDeclare> implements LicenseCancellationDeclareService {

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

        // 构建查询条件
        if (StringUtils.isNotBlank(entity.getBizseqId())) {
            wrapper.eq(LicenseCancellationDeclare::getBizseqId, entity.getBizseqId());
        }
        if (StringUtils.isNotBlank(entity.getPripid())) {
            wrapper.eq(LicenseCancellationDeclare::getPripid, entity.getPripid());
        }
        if (StringUtils.isNotBlank(entity.getEntName())) {
            wrapper.like(LicenseCancellationDeclare::getEntName, entity.getEntName());
        }
        if (StringUtils.isNotBlank(entity.getUniscid())) {
            wrapper.eq(LicenseCancellationDeclare::getUniscid, entity.getUniscid());
        }
        if (StringUtils.isNotBlank(entity.getRegOrg())) {
            wrapper.eq(LicenseCancellationDeclare::getRegOrg, entity.getRegOrg());
        }
        if (StringUtils.isNotBlank(entity.getRegType())) {
            wrapper.eq(LicenseCancellationDeclare::getRegType, entity.getRegType());
        }
        if (StringUtils.isNotBlank(entity.getLicCanReason())) {
            wrapper.eq(LicenseCancellationDeclare::getLicCanReason, entity.getLicCanReason());
        }
        if (StringUtils.isNotBlank(entity.getIsoriCop())) {
            wrapper.eq(LicenseCancellationDeclare::getIsoriCop, entity.getIsoriCop());
        }
        if (entity.getLicCanDate() != null) {
            wrapper.ge(LicenseCancellationDeclare::getLicCanDate, entity.getLicCanDate());
        }
        if (StringUtils.isNotBlank(entity.getLicCopyNo())) {
            wrapper.like(LicenseCancellationDeclare::getLicCopyNo, entity.getLicCopyNo());
        }
        if (StringUtils.isNotBlank(entity.getSExtFromnode())) {
            wrapper.eq(LicenseCancellationDeclare::getSExtFromnode, entity.getSExtFromnode());
        }

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

        return page(page, wrapper);
    }

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

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

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

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

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

    @Override
    public List<LicenseCancellationDeclare> getByLicCanReason(String licCanReason) {
        LambdaQueryWrapper<LicenseCancellationDeclare> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(LicenseCancellationDeclare::getLicCanReason, licCanReason);
        wrapper.orderByDesc(LicenseCancellationDeclare::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<LicenseCancellationDeclare> getByIsoriCop(String isoriCop) {
        LambdaQueryWrapper<LicenseCancellationDeclare> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(LicenseCancellationDeclare::getIsoriCop, isoriCop);
        wrapper.orderByDesc(LicenseCancellationDeclare::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<LicenseCancellationDeclare> getByLicCanDate(LocalDate licCanDate) {
        LambdaQueryWrapper<LicenseCancellationDeclare> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(LicenseCancellationDeclare::getLicCanDate, licCanDate);
        wrapper.orderByDesc(LicenseCancellationDeclare::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<LicenseCancellationDeclare> getByLicCopyNo(String licCopyNo) {
        LambdaQueryWrapper<LicenseCancellationDeclare> wrapper = Wrappers.lambdaQuery();
        wrapper.like(LicenseCancellationDeclare::getLicCopyNo, licCopyNo);
        wrapper.orderByDesc(LicenseCancellationDeclare::getSExtDatetime);
        return list(wrapper);
    }

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

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

    @Override
    public int countByRegTypeAndReason(String regType, String licCanReason) {
        LambdaQueryWrapper<LicenseCancellationDeclare> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(LicenseCancellationDeclare::getRegType, regType);
        wrapper.eq(LicenseCancellationDeclare::getLicCanReason, licCanReason);
        return count(wrapper);
    }

    @Override
    public List<LicenseCancellationDeclare> getByLicCanDateRange(LocalDate startDate, LocalDate endDate) {
        LambdaQueryWrapper<LicenseCancellationDeclare> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(LicenseCancellationDeclare::getLicCanDate, startDate);
        wrapper.le(LicenseCancellationDeclare::getLicCanDate, endDate);
        wrapper.orderByDesc(LicenseCancellationDeclare::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    public List<LicenseCancellationDeclare> getByRegOrgAndDateRange(String regOrg, LocalDate startDate, LocalDate endDate) {
        LambdaQueryWrapper<LicenseCancellationDeclare> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(LicenseCancellationDeclare::getRegOrg, regOrg);
        wrapper.ge(LicenseCancellationDeclare::getLicCanDate, startDate);
        wrapper.le(LicenseCancellationDeclare::getLicCanDate, endDate);
        wrapper.orderByDesc(LicenseCancellationDeclare::getSExtDatetime);
        return list(wrapper);
    }

    @Override
    @Transactional
    public boolean batchUpdateLicCanDate(List<String> recIds, LocalDate licCanDate) {
        if (recIds == null || recIds.isEmpty()) {
            return false;
        }

        for (String recId : recIds) {
            LicenseCancellationDeclare entity = new LicenseCancellationDeclare();
            entity.setRecId(recId);
            entity.setLicCanDate(licCanDate);
            if (!updateById(entity)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public List<LicenseCancellationDeclare> getByNodeAndDateRange(String sExtFromnode, LocalDate startDate, LocalDate endDate) {
        LambdaQueryWrapper<LicenseCancellationDeclare> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(LicenseCancellationDeclare::getSExtFromnode, sExtFromnode);
        wrapper.ge(LicenseCancellationDeclare::getLicCanDate, startDate);
        wrapper.le(LicenseCancellationDeclare::getLicCanDate, endDate);
        wrapper.orderByDesc(LicenseCancellationDeclare::getSExtDatetime);
        return list(wrapper);
    }
}
