package cn.stylefeng.team.meta.modular.controller;

import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.team.meta.api.entity.MetaField;
import cn.stylefeng.team.meta.api.pojo.MetaFieldShowDTO;
import cn.stylefeng.team.meta.modular.pojo.request.MetaFieldRequest;
import cn.stylefeng.team.meta.modular.pojo.request.SqlImportRequest;
import cn.stylefeng.team.meta.modular.service.MetaFieldService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@ApiResource(name = "元数据字段管理控制器")
public class MetaFieldController {

    @Resource
    private MetaFieldService metaFieldService;

    @GetResource(name = "获取模型定义下的所有字段列表", path = "/metaModelField/getFieldList")
    public ResponseData<List<MetaField>> getFieldList(@Validated(MetaFieldRequest.getFieldList.class) MetaFieldRequest metaFieldRequest) {
        return new SuccessResponseData<>(metaFieldService.getFieldList(metaFieldRequest));
    }

    @GetResource(name = "获取模型定义下所有字段（字段列返回少一些，用于下拉一般）", path = "/metaModelField/simpleGetFieldList")
    public ResponseData<List<MetaField>> simpleGetFieldList(
            @Validated(MetaFieldRequest.getFieldList.class) MetaFieldRequest metaFieldRequest) {
        return new SuccessResponseData<>(metaFieldService.simpleGetFieldList(metaFieldRequest));
    }

    @PostResource(name = "保存模型定义的字段列表", path = "/metaModelField/saveOrUpdateFieldList")
    public ResponseData<?> saveOrUpdateFieldList(
            @RequestBody @Validated(MetaFieldRequest.saveOrUpdateFieldList.class) MetaFieldRequest metaFieldRequest) {
        metaFieldService.saveOrUpdateFieldList(metaFieldRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "解析create table语句，直接生成元数据模型", path = "/metaModelField/sqlImport")
    public ResponseData<?> sqlImport(@RequestBody @Validated SqlImportRequest sqlImportRequest) {

        String createTableSql = sqlImportRequest.getCreateTableSql();
        createTableSql = new String(java.util.Base64.getDecoder().decode(createTableSql));
        sqlImportRequest.setCreateTableSql(createTableSql);

        int successNumber = metaFieldService.sqlImport(sqlImportRequest);
        return new SuccessResponseData<>(successNumber);
    }

    @GetResource(name = "获取默认的字段列表", path = "/metaModelField/getDefaultFieldList")
    public ResponseData<List<MetaField>> getDefaultFieldList(@RequestParam("defaultFieldType") @Validated Integer defaultFieldType) {
        return new SuccessResponseData<>(metaFieldService.getDefaultFieldList(defaultFieldType));
    }

    @PostResource(name = "获取指定模型的字段列表", path = "/metaModelField/getFieldCollectionByModelIdList")
    public ResponseData<List<MetaFieldShowDTO>> getFieldCollectionByModelIdList(
            @RequestBody @Validated(MetaFieldRequest.fieldCollection.class) MetaFieldRequest metaFieldRequest) {
        List<MetaFieldShowDTO> fieldList = metaFieldService.getFieldCollectionByModelIdList(metaFieldRequest);
        return new SuccessResponseData<>(fieldList);
    }

    @PostResource(name = "更新虚拟字段到元数据库中", path = "/metaModelField/updateVirtualField", requiredPermission = true, requirePermissionCode = "META_UPDATE_TABLE")
    public ResponseData<?> updateVirtualField(@RequestBody @Validated(MetaFieldRequest.updateVirtualField.class) MetaFieldRequest metaFieldRequest) {
        metaFieldService.updateVirtualField(metaFieldRequest);
        return new SuccessResponseData<>();
    }

}
