package com.friday.custody.service.basicService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.friday.custody.service.trustService.zx.utils.SearchReturnClass;
import com.friday.custody.dao.DataDictionaryZxFndBasicInfoCyDao;
import com.friday.custody.entity.DataDictionaryZxFndBasicInfoCyEntity;
import com.friday.custody.service.basicService.DataDictionaryZxFndBasicInfoCyService;
import com.friday.custody.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


@Service("dataDictionaryZxFndbasicinfocyService")
@Slf4j
public class DataDictionaryZxFndBasicInfoCyServiceImpl extends ServiceImpl<DataDictionaryZxFndBasicInfoCyDao, DataDictionaryZxFndBasicInfoCyEntity>
        implements DataDictionaryZxFndBasicInfoCyService {

    @Autowired
    private SearchReturnClass searchReturnClass;


    @Override
    public String getFundCodeByFundname(String fundName) {
        QueryWrapper<DataDictionaryZxFndBasicInfoCyEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("value", fundName);

        DataDictionaryZxFndBasicInfoCyEntity entity = this.baseMapper.selectOne(queryWrapper);
        if(entity!=null){
            String keyStr = entity.getKeyStr();
            log.info("DataDictionaryZxFndBasicInfoCyServiceImpl_getFundCodeByFundname入参fundName={}，出参fundcode={}",fundName,keyStr);
            return keyStr;
        }else{
            log.info("DataDictionaryZxFndBasicInfoCyServiceImpl_getFundCodeByFundname入参fundName={}，出参为null",fundName);
            return null;
        }
    }

    @Override
    public List<DataDictionaryZxFndBasicInfoCyEntity> listAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("key_str","value");
        List list = this.list(queryWrapper);
        return list;
    }



    @Transactional(rollbackFor = Exception.class)
    public R saveData(String type, List<DataDictionaryZxFndBasicInfoCyEntity> dataDictionaryZxFndBasicInfoCyEntities) {
        if (CollectionUtils.isEmpty(dataDictionaryZxFndBasicInfoCyEntities)){
            return R.ok();
        }
        List<String> valueList = dataDictionaryZxFndBasicInfoCyEntities
                .stream()
                .map(DataDictionaryZxFndBasicInfoCyEntity::getValue)
                .collect(Collectors.toList());
        Set<String> uniqueKeySet = baseMapper.queryUniqueKeyList(valueList);
        if (CollectionUtils.isNotEmpty(uniqueKeySet)) {
            baseMapper.deleteByUniqueKeys(uniqueKeySet);
        }
        String ukList = String.join("--", valueList);
        boolean saveStatus = this.saveBatch(dataDictionaryZxFndBasicInfoCyEntities);
        if (!saveStatus) {
            log.error("保存DataDictionaryZxFndBasicInfoCyEntity失败:{}", ukList);
        }
        return R.ok();
    }

    /**
     * 模糊查询
     */
    public String queryKeyByValue(String value){
        String key = baseMapper.queryKeyByValue(value);
        return key;
    }

}