package com.trs.digital.module.manager.service.oracle;

import cn.hutool.crypto.digest.MD5;
import com.trs.digital.module.manager.controller.admin.search.service.HyBaseService;
import com.trs.digital.module.manager.dal.dataobject.digftp02.Digftpsource02DO;
import com.trs.hybase.client.params.SearchParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.CompletableFuture;

import com.trs.digital.module.manager.controller.admin.oracle.vo.*;
import com.trs.digital.module.manager.dal.dataobject.oracle.OracleResourceDO;
import com.trs.digital.framework.common.pojo.PageResult;
import com.trs.digital.framework.common.pojo.PageParam;
import com.trs.digital.framework.common.util.object.BeanUtils;

import com.trs.digital.module.manager.dal.mysql.oracle.OracleResourceMapper;

import static com.trs.digital.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.trs.digital.module.system.enums.ErrorCodeConstants.*;

/**
 * oracle资源库管理 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class OracleResourceServiceImpl implements OracleResourceService {

    @Resource
    private OracleResourceMapper oracleResourceMapper;

    @Autowired
    HyBaseService hyBaseService;

    @Override
    public Long createOracleResource(OracleResourceSaveReqVO createReqVO) {
        // 插入
        String uuid = UUID.randomUUID().toString();
        createReqVO.setDocid(uuid);
        OracleResourceDO insertObj = BeanUtils.toBean(createReqVO, OracleResourceDO.class);
        int count = oracleResourceMapper.insert(insertObj);
        if (count > 0) {
            CompletableFuture future = CompletableFuture.runAsync(() -> {
                Map<String, Object> map = new HashMap<>();
                map.put("doc_content",insertObj.getDocContent());
                map.put("doc_abstract",insertObj.getDocAbstract());
                map.put("doc_keywords",insertObj.getDocKeywords());
                map.put("docid",uuid);
                map.put("id",insertObj.getId());
                map.put("doc_title",insertObj.getDocTitle());
                map.put("doc_lable",insertObj.getDocLable());
                map.put("doc_content_translate",insertObj.getDocContentTranslate());
                map.put("file_crtime", Date.from(insertObj.getFileCrtime().atZone(ZoneId.systemDefault()).toInstant()));
                map.put("file_name",insertObj.getFileName());
                map.put("file_path",insertObj.getFilePath());
                map.put("file_type",insertObj.getFileType());
                map.put("file_updatetime",Date.from(insertObj.getFileUpdatetime().atZone(ZoneId.systemDefault()).toInstant()));
                map.put("language",insertObj.getLanguage());
                map.put("file_year",insertObj.getFileYear());
                map.put("sourcename",insertObj.getSourcename());
                hyBaseService.insertHybase("system.oracle", map);
            });
        }
        // 返回
        return insertObj.getId();
    }

    @Override
    public void updateOracleResource(OracleResourceSaveReqVO updateReqVO) {
        // 校验存在
        validateOracleResourceExists(updateReqVO.getId());
        // 更新
        OracleResourceDO updateObj = BeanUtils.toBean(updateReqVO, OracleResourceDO.class);
        Integer count = oracleResourceMapper.updateById(updateObj);
        if (count > 0) {
            CompletableFuture future = CompletableFuture.runAsync(() -> {
                Date utilDate = Date.from(updateObj.getFileCrtime().atZone(ZoneId.systemDefault()).toInstant());
                Map<String, Object> map = new HashMap<>();
                map.put("doc_content",updateObj.getDocContent());
                map.put("doc_abstract",updateObj.getDocAbstract());
                map.put("doc_keywords",updateObj.getDocKeywords());
                map.put("docid",updateObj.getDocid());
                map.put("id",updateObj.getId());
                map.put("doc_title",updateObj.getDocTitle());
                map.put("doc_lable",updateObj.getDocLable());
                map.put("doc_content_translate",updateObj.getDocContentTranslate());
                map.put("file_crtime",Date.from(updateObj.getFileCrtime().atZone(ZoneId.systemDefault()).toInstant()));

                map.put("file_name",updateObj.getFileName());
                map.put("file_path",updateObj.getFilePath());
                map.put("file_type",updateObj.getFileType());
                map.put("file_updatetime",Date.from(updateObj.getFileUpdatetime().atZone(ZoneId.systemDefault()).toInstant()));
                map.put("language",updateObj.getLanguage());
                map.put("file_year",updateObj.getFileYear());
                map.put("sourcename",updateObj.getSourcename());

                SearchParams param = new SearchParams();
                String uid = hyBaseService.selectByDataid("system.oracle", "docid:"+updateObj.getDocid()+"", 0L, 1L, param);
                if(!"".equals(uid)){
                    hyBaseService.updateHybaseById("system.oracle", uid, map);
                }
            });
        }

    }

    @Override
    public void deleteOracleResource(Long id) {
        // 校验存在
        validateOracleResourceExists(id);
        // 删除
        oracleResourceMapper.deleteById(id);
        OracleResourceDO deleteObj = oracleResourceMapper.selectById(id);
        if(deleteObj.getDocid()!=null){
            hyBaseService.deleteHybaseById("system.oracle", deleteObj.getDocid());
        }
    }

    private void validateOracleResourceExists(Long id) {
        if (oracleResourceMapper.selectById(id) == null) {
            throw exception(DIG_ORACLE_NOT_EXISTS);
        }
    }

    @Override
    public OracleResourceDO getOracleResource(Long id) {
        return oracleResourceMapper.selectById(id);
    }

    @Override
    public PageResult<OracleResourceDO> getOracleResourcePage(OracleResourcePageReqVO pageReqVO) {
        return oracleResourceMapper.selectPage(pageReqVO);
    }

}