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

import com.trs.digital.module.manager.controller.admin.search.service.HyBaseService;
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.sql.Array;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.CompletableFuture;

import com.trs.digital.module.manager.controller.admin.digftp.vo.*;
import com.trs.digital.module.manager.dal.dataobject.digftp.DigFtpSourceDO;
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.digftp.DigFtpSourceMapper;

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

/**
 * STF资源管理 Service 实现类
 *
 * @author Wakaka
 */
@Service
@Validated
public class DigFtpSourceServiceImpl implements DigFtpSourceService {

    @Resource
    private DigFtpSourceMapper digFtpSourceMapper;

    @Autowired
    HyBaseService hyBaseService;

    @Override
    public Long createDigFtpSource(DigFtpSourceSaveReqVO createReqVO) {
        // 插入
        String uuid = UUID.randomUUID().toString();
        createReqVO.setDocid(uuid);
        DigFtpSourceDO insertObj = BeanUtils.toBean(createReqVO, DigFtpSourceDO.class);
        int count = digFtpSourceMapper.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("area_lable",insertObj.getAreaLable());
                map.put("org_lable",insertObj.getOrgLable());
                map.put("people_lable",insertObj.getPeopleLable());
                map.put("batch_no",insertObj.getBatchNo());
                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_ftp", map);
            });
        }
        // 返回
        return insertObj.getId();
    }

    @Override
    public void updateDigFtpSource(DigFtpSourceSaveReqVO updateReqVO) {
        // 校验存在
        validateDigFtpSourceExists(updateReqVO.getId());
        // 更新
        DigFtpSourceDO updateObj = BeanUtils.toBean(updateReqVO, DigFtpSourceDO.class);
        int count = digFtpSourceMapper.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("area_lable",updateObj.getAreaLable());
                map.put("org_lable",updateObj.getOrgLable());
                map.put("people_lable",updateObj.getPeopleLable());
                map.put("batch_no",updateObj.getBatchNo());

                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_ftp", "docid:"+updateObj.getDocid()+"", 0L, 1L, param);
                if(!"".equals(uid)){
                    hyBaseService.updateHybaseById("system.dig_ftp", uid, map);
                }
            });
        }

    }

    @Override
    public void deleteDigFtpSource(Long id) {
        // 校验存在
        validateDigFtpSourceExists(id);
        // 删除
        digFtpSourceMapper.deleteById(id);
        DigFtpSourceDO deleteObj = digFtpSourceMapper.selectById(id);
        if(deleteObj.getDocid()!=null){
            hyBaseService.deleteHybaseById("system.dig_ftp", deleteObj.getDocid());
        }
    }

    private void validateDigFtpSourceExists(Long id) {
        if (digFtpSourceMapper.selectById(id) == null) {
            throw exception(DIG_FTP_SOURCE_NOT_EXISTS);
        }
    }

    @Override
    public DigFtpSourceDO getDigFtpSource(Long id) {
        return digFtpSourceMapper.selectById(id);
    }

    @Override
    public PageResult<DigFtpSourceDO> getDigFtpSourcePage(DigFtpSourcePageReqVO pageReqVO) {
        return digFtpSourceMapper.selectPage(pageReqVO);
    }

}