package cn.ibizlab.core.data.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.math.BigInteger;

import cn.ibizlab.core.data.lite.EntityModel;
import cn.ibizlab.core.data.lite.LiteStorage;
import cn.ibizlab.core.data.model.PojoSchema;
import cn.ibizlab.core.data.model.TransUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import cn.ibizlab.util.errors.BadRequestAlertException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Lazy;
import cn.ibizlab.core.data.domain.DOModel;
import cn.ibizlab.core.data.filter.DOModelSearchContext;
import cn.ibizlab.core.data.service.IDOModelService;

import cn.ibizlab.util.helper.CachedBeanCopier;
import cn.ibizlab.util.helper.DEFieldCacheMap;


/**
 * 实体[业务实体] 无存储服务对象接口实现
 */
@Slf4j
@Service
public class DOModelServiceImpl implements IDOModelService {

    @Value("${ibiz.model.path:/app/file/model/}")
    private String modelPath;

    @Override
    public boolean create(DOModel et) {
        //代码实现
        return true;
    }

    public void createBatch(List<DOModel> list){
    }

    @Override
    public boolean update(DOModel et) {
        //代码实现
        return true;
    }

    public void updateBatch(List<DOModel> list){
    }

    @Override
    public boolean remove(String key) {
        return true;
    }

    public void removeBatch(Collection<String> idList){
    }

    @Override
    public DOModel get(String key) {
        DOModel doModel=new DOModel();
        PojoSchema schema= null;
        String[] args=key.split("[.]");
        String system=args[0];
        String entity=args[2];
        File domainDir = new File(modelPath+system+File.separator+"domain");
        if(!domainDir.exists())
            domainDir.mkdirs();

        File schemaJson=new File(domainDir,entity+".json");
        if(!schemaJson.exists())
        {
            EntityModel entityModel= LiteStorage.getLiteModelService().getProxyEntityModel(system,entity);
            if(entityModel!=null) {
                schema=TransUtils.LiteEntityModelModel2Schema(entityModel);
            }
        }
        else
        {
            try {
                String schemaJSON=new String(Files.readAllBytes(Paths.get(domainDir.getAbsolutePath(),entity+".json")),StandardCharsets.UTF_8);
                schema=JSON.parseObject(schemaJSON,PojoSchema.class);
            } catch (IOException e) {

            }
        }

        if(schema!=null)
        {
            doModel.setSchema(schema);
            try {
                Files.write(Paths.get(domainDir.getAbsolutePath(),entity+".json"), JSON.toJSONString(schema).getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
            } catch (IOException e) {

            }
        }
        else
        {
            throw new BadRequestAlertException("未找到对应的模型","DOModel",key);
        }


        return doModel;
    }

    @Override
    public DOModel getDraft(DOModel et) {
        return et;
    }

    @Override
    public boolean checkKey(DOModel et) {
        return false;
    }
    @Override
    @Transactional
    public boolean save(DOModel et) {
        //代码实现
        return true;
    }

    @Override
    public void saveBatch(List<DOModel> list) {
    }





    /**
     * 查询集合 数据集
     */
    @Override
    public Page<DOModel> searchDefault(DOModelSearchContext context) {
        return new PageImpl<DOModel>(new ArrayList(),context.getPageable(),0);
    }

}


