package top.continew.admin.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.continew.admin.business.mapper.DeptModelMapper;
import top.continew.admin.business.model.entity.DeptModelDO;
import top.continew.admin.business.model.query.ModelQuery;
import top.continew.admin.business.model.req.DeptModelReq;
import top.continew.admin.business.model.resp.ModelResp;
import top.continew.admin.business.service.DeptModelService;
import top.continew.admin.business.service.ModelService;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;

import java.util.List;

/**
 * 部门与模型绑定业务实现
 *
 * @author 张波
 * @since 2024/10/20 14:43
 */
@Service
@RequiredArgsConstructor
public class DeptModeServiceImpl extends ServiceImpl<DeptModelMapper, DeptModelDO> implements DeptModelService {

    private final ModelService modelService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bind(DeptModelReq req) {
        // 查询数据库中已绑定的数据
        List<DeptModelDO> dbList = lambdaQuery().eq(DeptModelDO::getDeptId, req.getDeptId()).list();

        // 筛选出数据库中存在 新绑定不存在的 进行删除
        List<Long> removeIds = dbList.stream()
            .filter(d -> !req.getModelIds().contains(d.getModelId()))
            .map(DeptModelDO::getId)
            .toList();
        if (CollUtil.isNotEmpty(removeIds)) {
            removeBatchByIds(removeIds);
        }

        if (CollUtil.isNotEmpty(req.getModelIds())) {
            // 筛选出新列表存在 数据库中不存在的 进行新增
            List<DeptModelDO> saveList = req.getModelIds()
                .stream()
                .filter(p -> dbList.stream().noneMatch(d -> p.equals(d.getModelId())))
                .map(i -> {
                    DeptModelDO temp = new DeptModelDO();
                    temp.setDeptId(req.getDeptId());
                    temp.setModelId(i);
                    return temp;
                })
                .toList();
            saveBatch(saveList);
        }
    }

    @Override
    public PageResp<ModelResp> getModels(ModelQuery query, PageQuery pageQuery) {
        // 获取当前登录用户信息
        //LoginUser loginUser = LoginHelper.getLoginUser();
        //List<Long> deptIds = loginUser.getModelDeptIds();

        // 部门为空不限制 查询所有模型
        /*if (CollUtil.isNotEmpty(deptIds)) {
            List<Integer> modelIds = lambdaQuery().in(DeptModelDO::getDeptId, deptIds)
                .list()
                .stream()
                .map(DeptModelDO::getModelId)
                .toList();
        
            // 不是管理员并且没有绑定的模型， 返回空
            if (!loginUser.isAdmin() && CollUtil.isEmpty(modelIds)) {
                return PageResp.build(null);
            }
        
            query.setIds(modelIds);
        }*/

        // 根据登录用户所在部门查询可见模型
        return modelService.page(query, pageQuery);
    }

    @Override
    public List<Integer> getModelByDept(Long deptId) {
        return lambdaQuery().eq(DeptModelDO::getDeptId, deptId).list().stream().map(DeptModelDO::getModelId).toList();
    }
}
