package com.bc.exam.modules.dict.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bc.exam.core.api.dto.PagingReqDTO;
import com.bc.exam.core.exception.ServiceException;
import com.bc.exam.modules.dict.entity.SysDictData;
import com.bc.exam.modules.dict.entity.SysDictType;
import com.bc.exam.modules.dict.mapper.SysDictDataMapper;
import com.bc.exam.modules.dict.service.SysDictDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bc.exam.modules.dict.service.SysDictTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 描述:码值数据表 服务实现类
 * @Author A贾宇婷034244310
 * @Date 20260615
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {
    @Resource
    private SysDictTypeService dictTypeService;

    @Override
    public List<SysDictData> getListByCode(String code) {
        if (StrUtil.isBlank(code)) {
            throw new ServiceException("码值类型不能为空");
        }
        QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
        wrapper.eq("CODE", code) // 类型
                .eq("STATE", "0").orderByAsc("priority"); // 状态
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public List<SysDictData> getAllListByCode(String code) {
        if (StrUtil.isBlank(code)) {
            throw new ServiceException("码值类型不能为空");
        }
        QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
        wrapper.eq("CODE", code).orderByAsc("priority");
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public List<SysDictData> getListBySourceSql(String sql) {
        return this.baseMapper.dynamicSql(sql);
    }

    @Override
    public IPage<SysDictData> pagingBySql(Page<SysDictData> query, String sql) {
        return this.baseMapper.pagingDynamicSql(query, sql);
    }

    @Override
    public IPage<SysDictData> pagingByCode(Page<SysDictData> query, String code) {
        QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
        wrapper.eq("CODE", code).orderByAsc("priority");
        return this.page(query, wrapper);
    }

    @Override
    public IPage<SysDictData> paging(PagingReqDTO<SysDictType> reqDTO) {
        //创建分页对象
        Page<SysDictData> query = new Page<>(reqDTO.getCurrent(), reqDTO.getSize());
        String code = reqDTO.getParams().getCode();
        if (StrUtil.isBlank(code)) {
            return query;
        }
        SysDictType one = dictTypeService.getOne(new QueryWrapper<SysDictType>().eq("CODE", code));
        if (one.getSourceType().equals("1")) {
            return this.pagingByCode(query, code);
        }
        return this.pagingBySql(query, one.getSourceSql());
    }

    @Override
    public void saveData(SysDictData reqDTO) {
        if (StrUtil.isBlank(reqDTO.getCode())) {
            throw new ServiceException("请先选择码值大类!");
        }
        QueryWrapper<SysDictType> typeQueryWrapper = new QueryWrapper<>();
        typeQueryWrapper.eq("CODE", reqDTO.getCode());
        SysDictType one = dictTypeService.getOne(typeQueryWrapper);
        if (one.getSourceType().equals("2")) {
            throw new ServiceException("码值来源sql 无法添加!");
        }
        QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
        wrapper.eq("CODE", reqDTO.getCode())
                .eq("VALUE", reqDTO.getValue());
        SysDictData item = this.baseMapper.selectOne(wrapper);
        if (StrUtil.isBlank(reqDTO.getId())) {
            if (item != null && StrUtil.isNotBlank(item.getId())) {
                throw new ServiceException("码值重复");
            }
            reqDTO.setId(IdWorker.getIdStr());
        } else {
            if (item != null && !item.getId().equals(reqDTO.getId())) {
                throw new ServiceException("码值重复");
            }
        }
        this.saveOrUpdate(reqDTO);
    }

    @Override
    public Map<String, List<SysDictData>> getAllEffectDicData() {
        QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
        wrapper.eq("state", "0").orderByAsc("priority");
        // 获取所有sys_dic_data中的码值
        List<SysDictData> sysDictData = this.baseMapper.selectList(wrapper);

        QueryWrapper<SysDictType> typeWrapper = new QueryWrapper<>();
        typeWrapper.eq("state", "0").eq("source_type", "2");
        List<SysDictType> list = dictTypeService.list(typeWrapper);
        list.forEach(i -> sysDictData.addAll(this.getListBySourceSql(i.getSourceSql())));
        return sysDictData.stream().collect(Collectors.groupingBy(SysDictData::getCode));
    }
}
