package org.dromara.main.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.main.domain.MainBackOrder;
import org.dromara.main.domain.MainBackRecord;
import org.dromara.main.domain.bo.MainBackOrderBo;
import org.dromara.main.domain.vo.MainBackOrderVo;
import org.dromara.main.mapper.MainBackOrderMapper;
import org.dromara.main.mapper.MainBackRecordMapper;
import org.dromara.main.service.IMainBackOrderService;
import org.dromara.system.domain.bo.SysTenantBo;
import org.dromara.system.domain.vo.SysTenantVo;
import org.dromara.system.service.ISysTenantService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
public class MainBackOrderServiceImpl implements IMainBackOrderService {

    private final MainBackOrderMapper baseMapper;
    private final MainBackRecordMapper recordMapper;
    private final ISysTenantService tenantService;

    @Override
    public TableDataInfo<MainBackOrderVo> queryPageList(MainBackOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MainBackOrder> lqw = new LambdaQueryWrapper<>();

        // 1. 左侧分类过滤
        lqw.eq(bo.getCategoryId() != null, MainBackOrder::getCategoryId, bo.getCategoryId());

        // 2. 状态过滤
        lqw.eq(StrUtil.isNotBlank(bo.getStatus()), MainBackOrder::getStatus, bo.getStatus());

        // 3. 右侧单位名称搜索逻辑
        if (StrUtil.isNotBlank(bo.getKeyword())) {
            // 通过单位名称模糊查询租户ID列表
            SysTenantBo tenantBo = new SysTenantBo();
            tenantBo.setCompanyName(bo.getKeyword());
            List<String> tenantIds = tenantService.queryList(tenantBo)
                .stream()
                .map(SysTenantVo::getTenantId)
                .toList();

            if (tenantIds.isEmpty()) {
                return TableDataInfo.build(new ArrayList<>());
            }
            lqw.in(MainBackOrder::getTenantId, tenantIds);
        }

        // 4. 分页查询订单主表
        Page<MainBackOrderVo> pageResult = baseMapper.selectVoPage(pageQuery.build(), lqw);
        TableDataInfo<MainBackOrderVo> result = TableDataInfo.build(pageResult);

        // 5. 动态填充页面所需字段
        for (MainBackOrderVo vo : result.getRows()) {
            // A. 统计候选人人数
            Long count = recordMapper.selectCount(
                new LambdaQueryWrapper<MainBackRecord>()
                    .eq(MainBackRecord::getOrderId, vo.getId())
            );
            vo.setCandidateCount(count);

            // B. 填充企业名称
            SysTenantVo tenant = tenantService.queryByTenantId(vo.getTenantId());
            if (tenant != null) {
                vo.setCompanyName(tenant.getCompanyName());
            }
        }

        return result;
    }
}


