package com.cool.store.service.dict.impl;

import cn.hutool.core.collection.CollStreamUtil;
import com.cool.store.dao.EnterpriseUserDAO;
import com.cool.store.dao.dict.SysDictColumnDAO;
import com.cool.store.dao.dict.SysDictTableDAO;
import com.cool.store.entity.dict.SysDictColumnDO;
import com.cool.store.entity.dict.SysDictTableDO;
import com.cool.store.request.dict.DictColumnQueryRequest;
import com.cool.store.service.dict.DictColumnService;
import com.cool.store.utils.BeanUtil;
import com.cool.store.vo.dict.DictColumnSimpleVO;
import com.cool.store.vo.dict.DictColumnVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 字典项 实现类
 * </p>
 *
 * @author wangff
 * @since 2025/2/20
 */
@Service
@RequiredArgsConstructor
public class DictColumnServiceImpl implements DictColumnService {
    private final SysDictColumnDAO columnDAO;
    private final SysDictTableDAO tableDAO;
    private final EnterpriseUserDAO userDAO;

    @Override
    public DictColumnVO getById(Long id) {
        SysDictColumnDO column = columnDAO.getById(id);
        return BeanUtil.toBean(column, DictColumnVO.class);
    }

    @Override
    public PageInfo<DictColumnVO> getPage(DictColumnQueryRequest dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<SysDictColumnDO> list = columnDAO.getList(dto);
        PageInfo<SysDictColumnDO> page = new PageInfo<>(list);
        PageInfo<DictColumnVO> result = BeanUtil.toPage(page, DictColumnVO.class);
        Set<String> userIds = new HashSet<>();
        result.getList().forEach(item -> {
            userIds.add(item.getCreateUserId());
            userIds.add(item.getUpdateUserId());
        });
        if (CollectionUtils.isNotEmpty(userIds)) {
            Map<String, String> userNameMap = userDAO.getUserNameMap(new ArrayList<>(userIds));
            result.getList().forEach(v -> {
                v.setCreateUserName(userNameMap.get(v.getCreateUserId()));
                v.setUpdateUserName(userNameMap.get(v.getUpdateUserId()));
            });
        }
        return result;
    }

    @Override
    public DictColumnSimpleVO getColumnByColumnCode(String columnCode) {
        SysDictColumnDO column = columnDAO.getOpenColumnByCode(columnCode);
        if (Objects.nonNull(column)) {
            SysDictTableDO table = tableDAO.getById(column.getDictTableId());
            DictColumnSimpleVO vo = BeanUtil.toBean(column, DictColumnSimpleVO.class);
            if (Objects.nonNull(table)) {
                vo.setTableCode(table.getTableCode());
            }
            Map<Long, List<DictColumnSimpleVO>> relationColumnMap = columnDAO.getRelationColumnMap(Collections.singletonList(column.getId()));
            vo.setRelationColumns(relationColumnMap.get(column.getId()));
            return vo;
        }
        return null;
    }

    @Override
    public Map<String, List<DictColumnSimpleVO>> getMapListByTableCode(List<String> tableCodes) {
        if (CollectionUtils.isEmpty(tableCodes)) {
            return Collections.emptyMap();
        }
        List<DictColumnSimpleVO> list = columnDAO.getOpenColumnListByTableCode(tableCodes);
        return CollStreamUtil.groupByKey(list, DictColumnSimpleVO::getTableCode);
    }
}
