package com.cf.imes.module.system.service.parser;


import cn.hutool.core.collection.CollUtil;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.system.controller.admin.parser.vo.ParserReqVO;
import com.cf.imes.module.system.controller.admin.parser.vo.ParserRespVO;
import com.cf.imes.module.system.controller.admin.parser.vo.ParserSimpleRespVO;
import com.cf.imes.module.system.dal.dataobject.parser.ParserDO;
import com.cf.imes.module.system.dal.mysql.parser.ParserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getUserOrganId;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.PARSER_NO_EXIST;

/**
 * @author 解析器管理 Service 实现类
 */
@Service
@Validated
@Slf4j
public class ParserServiceImpl implements ParserService{



    @Resource
    private ParserMapper parserMapper;



    @Override
    public void update(ParserReqVO reqVO) {

        ParserDO parserDO = validateParserExists(reqVO.getId());

        parserDO.setName(reqVO.getName());
        parserDO.setSetting(reqVO.getSetting());

        parserMapper.updateById(parserDO);


    }





    @Override
    public Map<Integer, List<ParserRespVO>> getParserTree(Long type) {


        List<ParserDO> parserDOS = parserMapper.selectList(new LambdaQueryWrapperX<ParserDO>()
                .eqIfPresent(ParserDO::getOrganId, getUserOrganId())
                .eq(ParserDO::getDeleted,false)
                .eqIfPresent(ParserDO::getParserType, type));


        if(CollUtil.isEmpty(parserDOS)){
            return new HashMap<>();
        }


        List<ParserRespVO> respVOS = BeanUtils.toBean(parserDOS, ParserRespVO.class);

        return respVOS.stream().collect(Collectors.groupingBy(ParserRespVO::getParserType));

    }





    @Override
    public Map<Integer, List<ParserSimpleRespVO>> getParserSimple(Long type) {

        List<ParserDO> parserDOS = parserMapper.selectList(new LambdaQueryWrapperX<ParserDO>()
                .eqIfPresent(ParserDO::getOrganId, getUserOrganId())
                .eq(ParserDO::getDeleted,false)
                .eqIfPresent(ParserDO::getParserType, type)
                .select(ParserDO::getId,ParserDO::getName,ParserDO::getParserType));

        if(CollUtil.isEmpty(parserDOS)){
            return new HashMap<>();
        }

        List<ParserSimpleRespVO> respVOS = BeanUtils.toBean(parserDOS, ParserSimpleRespVO.class);

        return respVOS.stream().collect(Collectors.groupingBy(ParserSimpleRespVO::getParserType));

    }






    @Override
    public ParserRespVO getParser(Long id) {

        ParserDO parserDO = validateParserExists(id);

        return BeanUtils.toBean(parserDO,ParserRespVO.class);

    }







    private ParserDO validateParserExists(Long id) {

        ParserDO parserDO = parserMapper.selectById(id);
        if (parserDO == null) {

            throw new ServiceException(PARSER_NO_EXIST);
        }

        return parserDO;

    }


}
