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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.Result;
import co.elastic.clients.elasticsearch._types.query_dsl.*;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.es.core.service.ESDocumentService;
import com.cf.imes.framework.security.core.LoginUser;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import com.cf.imes.module.system.controller.admin.machine.vo.SettingPageReqVO;
import com.cf.imes.module.system.dal.dataobject.machine.MachineDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author there
 */
@Service
public class MachineSettingServiceImpl implements MachineSettingService {

    @Resource
    private ElasticsearchClient elasticsearchClient;

    @Resource
    private ESDocumentService esDocumentService;

    public final static String MACHINE_SETTING = "user_machine";

    @Override
    public PageResult<MachineDO> page(SettingPageReqVO pageReqVO) throws IOException {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        List<MachineDO> userMachineDOS = new ArrayList<>();
        List<Query> queryList = new ArrayList<>();
        if(StrUtil.isNotEmpty(pageReqVO.getName())) {
            MatchQuery query = MatchQuery.of(e -> e.field("name").query(pageReqVO.getName()));
            queryList.add(query._toQuery());
        }
        queryList.add(TermQuery.of(e->e.field("organId").value( loginUser.getOrganId()))._toQuery());
        SearchResponse<MachineDO> response = elasticsearchClient.search(s-> s.index(MACHINE_SETTING)
                        .from((pageReqVO.getPageNo() - 1) * pageReqVO.getPageSize())
                        .size(pageReqVO.getPageSize())
                        .query(b -> b.bool( builder -> builder.must(queryList))),
                MachineDO.class);
        long value = response.hits().total().value();
        for (Hit<MachineDO> hit : response.hits().hits()) {
            userMachineDOS.add(hit.source());
        }
        return new PageResult<MachineDO>(userMachineDOS, value);

    }

    @Override
    public String create(MachineDO machineDO) throws Exception {
        return null;
        /*IndexResponse response = esDocumentService.createByFluentDSL(MACHINE_SETTING, machineDO.getId(), machineDO);
        return response.id();*/
    }

    @Override
    public String update(MachineDO machineDO) throws IOException {
        return null;
      /*  Result result = esDocumentService.updateById(MACHINE_SETTING, machineDO.getId(), MachineDO.class, BeanUtil.beanToMap(machineDO));
        return result.jsonValue();*/
    }

    @Override
    public Boolean delete(String id) throws IOException {
        return esDocumentService.deleteById(MACHINE_SETTING, id).booleanValue();
    }

    @Override
    public Boolean batchDelete(List<String> ids) throws Exception {
        return !esDocumentService.bulkDeleteByIds(MACHINE_SETTING, ids).errors();
    }

    @Override
    public MachineDO get(String id) throws IOException {
        return esDocumentService.getById(MACHINE_SETTING, id, MachineDO.class);
    }
}
