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

import com.trs.digital.module.manager.controller.admin.search.service.HyBaseService;
import com.trs.digital.module.manager.dal.dataobject.digftp.DigFtpSourceDO;
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 com.trs.digital.module.manager.controller.admin.digftp02.vo.*;
import com.trs.digital.module.manager.dal.dataobject.digftp02.Digftpsource02DO;
import com.trs.digital.framework.common.pojo.PageResult;
import com.trs.digital.framework.common.util.object.BeanUtils;

import com.trs.digital.module.manager.dal.mysql.digftp02.digftpsource02Mapper;

import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

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

/**
 * STF02资源管理 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class Digftpsource02ServiceImpl implements Digftpsource02Service {

    @Resource
    private digftpsource02Mapper digftpsource02Mapper;

    @Autowired
    HyBaseService hyBaseService;

    @Override
    public Long createdigftpsource02(Digftpsource02SaveReqVO createReqVO) {
        // 插入
        String uuid = UUID.randomUUID().toString();
        createReqVO.setDocid(uuid);
        Digftpsource02DO insertObj = BeanUtils.toBean(createReqVO, Digftpsource02DO.class);
        int count = digftpsource02Mapper.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.dig_ftp02", map);
            });
        }
        // 返回
        return insertObj.getId();
    }

    @Override
    public void updatedigftpsource02(Digftpsource02SaveReqVO updateReqVO) {
        // 校验存在
        validatedigftpsource02Exists(updateReqVO.getId());
        // 更新
        Digftpsource02DO updateObj = BeanUtils.toBean(updateReqVO, Digftpsource02DO.class);
        int count = digftpsource02Mapper.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.dig_ftp02", "docid:"+updateObj.getDocid()+"", 0L, 1L, param);
                if(!"".equals(uid)){
                    hyBaseService.updateHybaseById("system.dig_ftp02", uid, map);
                }
            });
        }
    }

    @Override
    public void deletedigftpsource02(Long id) {
        // 校验存在
        validatedigftpsource02Exists(id);
        // 删除
        digftpsource02Mapper.deleteById(id);
        Digftpsource02DO deleteObj = digftpsource02Mapper.selectById(id);
        if(deleteObj.getDocid()!=null){
            hyBaseService.deleteHybaseById("system.dig_ftp02", deleteObj.getDocid());
        }
    }

    private void validatedigftpsource02Exists(Long id) {
        if (digftpsource02Mapper.selectById(id) == null) {
            throw exception(DIG_FTP02_SOURCE_NOT_EXISTS);
        }
    }

    @Override
    public Digftpsource02DO getdigftpsource02(Long id) {
        return digftpsource02Mapper.selectById(id);
    }

    @Override
    public PageResult<Digftpsource02DO> getdigftpsource02Page(Digftpsource02PageReqVO pageReqVO) {
        return digftpsource02Mapper.selectPage(pageReqVO);
    }

}