package cn.fateverse.query.service.impl;

import cn.fateverse.admin.dubbo.DubboMenuService;
import cn.fateverse.admin.vo.MenuVo;
import cn.fateverse.common.core.result.Result;
import cn.fateverse.common.core.result.page.TableDataInfo;
import cn.fateverse.common.decrypt.service.EncryptService;
import cn.fateverse.common.mybatis.utils.PageUtils;
import cn.fateverse.query.entity.UniQuery;
import cn.fateverse.query.entity.vo.UniQueryVo;
import cn.fateverse.query.entity.dto.UniQueryDto;
import cn.fateverse.query.entity.query.UniQueryQuery;
import cn.fateverse.query.mapper.UniQueryMapper;
import cn.fateverse.query.service.UniQueryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 抽象自定义查询
 *
 * @author Clay
 * @date 2023-08-09
 */
public abstract class AbstractUniQueryService implements UniQueryService {

    protected final UniQueryMapper uniQueryMapper;
    //加密服务
    private final EncryptService encryptService;

    //dubbo 新增菜单
    @DubboReference
    protected DubboMenuService menuService;

    protected AbstractUniQueryService(UniQueryMapper uniQueryMapper, EncryptService encryptService) {
        this.uniQueryMapper = uniQueryMapper;
        this.encryptService = encryptService;
    }

    @Override
    public UniQueryVo searchById(Long id) {
        UniQuery uniQuery = uniQueryMapper.selectById(id);
        return UniQueryVo.toUniQueryVo(uniQuery);
    }

    @Override
    public List<UniQueryVo> exportList(UniQueryQuery query) {
        setQueryType(query);
        List<UniQuery> list = uniQueryMapper.selectList(query);
        return list.stream().map(UniQueryVo::toUniQueryVo)
                .collect(Collectors.toList());
    }

    @Override
    public String view(Long id) {
        UniQuery uniQuery = uniQueryMapper.selectById(id);
        if (!uniQuery.getPublish()){
            throw new RuntimeException("该查询未发布");
        }
        Result<MenuVo> result = menuService.selectMenuByMenuId(uniQuery.getMenuId());
        MenuVo menuVo = result.getData();
        return "page/" + menuVo.getParentId() + "/" +
                encryptService.encrypt(menuVo.getMenuId().toString());
    }

    @Override
    public TableDataInfo<UniQueryVo> searchList(UniQueryQuery query) {
        setQueryType(query);
        PageUtils.startPage();
        List<UniQuery> list = uniQueryMapper.selectList(query);
        Long total = PageUtils.getTotal(list);
        List<UniQueryVo> convertList = list.stream().map(UniQueryVo::toUniQueryVo)
                .collect(Collectors.toList());
        return PageUtils.convertDataTable(convertList, total);
    }


    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int edit(UniQueryDto dto) {
        UniQuery info = UniQueryDto.toUniQuery(dto);
        return uniQueryMapper.update(info);
    }


    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int removeById(Long id) {
        return uniQueryMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int removeBatch(List<Long> idList) {
        return uniQueryMapper.deleteBatchByIdList(idList);
    }


    private void setQueryType(UniQueryQuery query) {
        if (this instanceof TopoUniQueryServiceImpl) {
            query.setType(2);
        } else if (this instanceof SqlUniQueryServiceImpl) {
            query.setType(1);
        } else {
            query.setType(0);
        }
    }

}
