package com.friday.custody.controller;

import com.friday.base.common.response.DubboResult;
import com.friday.custody.service.logicService.LogicExternalService;
import com.friday.custody.service.logicService.impl.DaasBuriedPointRecordInfoServiceImpl;
import com.friday.custody.aop.AccessLimit;
import com.friday.custody.enums.DaasDataTypeEnum;
import com.friday.custody.service.tenantService.CustodyTenantMappingService;
import com.friday.custody.utils.R;
import com.friday.operation.api.custody.service.ICustodyDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * 对外Controller
 */
@RestController
@RequestMapping("/custody/external")
@Api
public class WebExternalController {
    @Autowired
    private LogicExternalService logicExternalService;
    @Autowired
    private DaasBuriedPointRecordInfoServiceImpl daasBuriedPointService;
    @Autowired
    private CustodyTenantMappingService custodyTenantMappingService;
    @Autowired
    private ICustodyDataService custodyDataService;


    @PostMapping("/queryData")
    @AccessLimit
    @ApiOperation(value = "根据type查询当前管理人下的对应的数据",notes = "查询标准表数据",httpMethod = "POST")
    public R queryData(@RequestParam(value = "type", required = false)
                           @ApiParam(name = "type",value = "数据表类型",required = false, defaultValue = "1到24") String type,
                       @RequestHeader(value = "admin", required = false)
                            @ApiParam(name = "admin",value = "租户名(登记编号)",required = false) String tenantName,
                       @RequestHeader(value = "accountKey", required = false)
                           @ApiParam(name = "accountKey",value = "账户Key",required = false) String accountKey,
                       @RequestBody
                           @ApiParam(name = "params",value = "封装查询参数",required = false) String params) {
        //添加埋点记录
        daasBuriedPointService.saveDaasBuriedPointRecord(tenantName, "/queryData", type, params);
        return logicExternalService.queryData(tenantName, accountKey, type, params);
    }

    @PostMapping("/queryDictionary")
//    @MyLimit
    @ApiOperation(value = "数据字典内容查询", notes = "此接口是对某张数据字典表的内容进行查询，例如：枚举键、枚举值等", httpMethod = "POST")
    public R queryDictionary(@RequestParam(value = "type", required = false)
                                 @ApiParam(name = "type", value = "数据字典名称", required = false) String type,
                             @RequestHeader(value = "admin", required = false)
                                 @ApiParam(name = "admin",value = "租户名(登记编号)",required = false) String tenantName,
                             @RequestHeader(value = "accountKey", required = false)
                                 @ApiParam(name = "accountKey",value = "账户Key",required = false) String accountKey) {
        daasBuriedPointService.saveDaasBuriedPointRecord(tenantName, "/queryDictionary", type, null);
        return logicExternalService.queryDictionary(tenantName, type,accountKey);
    }

    @PostMapping("/queryDictionaryDirectory")
//    @MyLimit
    @ApiOperation(value = "数据字典名称查询", notes = "此接口是对当前系统中的所有数据字典名称进行查询，例如：字段名称、数据字典名称等", httpMethod = "POST")
    public R queryDictionaryDirectory(@RequestHeader(value = "admin", required = false)
                                          @ApiParam(name = "admin",value = "租户名(登记编号)",required = false) String tenantName,
                                      @RequestHeader(value = "accountKey", required = false)
                                          @ApiParam(name = "accountKey",value = "账户Key",required = false) String accountKey,
                                      @RequestBody  @ApiParam(name = "params",value = "封装查询参数",required = false) String params) {
        daasBuriedPointService.saveDaasBuriedPointRecord(tenantName, "/queryDictionaryDirectory", DaasDataTypeEnum.DIC_DIR.getCode(), null);
        return logicExternalService.queryDictionaryDirectory(tenantName,accountKey,params);
    }

    @PostMapping("/saveTenants")
    @ApiOperation(value = "租户开立", notes = "此接口用于创建租户，注意：创建租户时，发送该请求的租户账号（请求头headers中的admin+accountKey）的标准表访问权限必须是all：最高级", httpMethod = "POST")
    public R saveTenants(@RequestHeader(value = "admin", required = false)
                             @ApiParam(name = "admin",value = "租户名(登记编号)",required = false) String tenantName,
                         @RequestHeader(value = "accountKey", required = false)
                             @ApiParam(name = "accountKey",value = "账户Key",required = false) String accountKey,
                         @RequestBody @ApiParam(name = "params",value = "封装请求参数",required = false) String params){
        daasBuriedPointService.saveDaasBuriedPointRecord(tenantName, "/saveTenants", DaasDataTypeEnum.TENANT.getCode(), params);
        return logicExternalService.saveTenants(tenantName,accountKey,params);
    }

    @PostMapping("/saveEscrowAuthorization")
    @ApiOperation(value = "托管授权", notes = "此接口用于对发送该请求的租户账号（请求头headers中的admin+accountKey）设置托管数据的访问权限，注意：当数据库中不存在该托管的数据时，授权成功并不代表能成功获取到上游的托管数据，仅代表成功插入授权信息", httpMethod = "POST")
    public R saveEscrowAuthorizations(@RequestHeader(value = "admin", required = false)
                                          @ApiParam(name = "admin",value = "租户名(登记编号)",required = false) String tenantName,
                                     @RequestHeader(value = "accountKey", required = false)
                                          @ApiParam(name = "accountKey",value = "账户Key",required = false)  String accountKey,
                                     @RequestBody @ApiParam(name = "params",value = "封装请求参数",required = false) String params){
        daasBuriedPointService.saveDaasBuriedPointRecord(tenantName, "/saveEscrowAuthorization", DaasDataTypeEnum.AUTH.getCode(), params);
        return logicExternalService.saveEscrowAuthorizations(tenantName,accountKey,params);
    }

    /**
     * 公用数据接口名称查询
     */
    @PostMapping("/queryPublicDataDirectory")
    @AccessLimit
    @ApiOperation(value = "公用数据接口名称查询", notes = "此接口是对当前系统中所有公用数据的接口名称进行查询，例如：公用数据名称、接口名称等。注：访问接口需使用公用租户账密", httpMethod = "POST")
    public R queryPublicDataDirectory(@RequestHeader(value = "admin", required = false)
                                          @ApiParam(name = "admin",value = "租户名(登记编号)",required = false) String tenantName,
                                      @RequestHeader(value = "accountKey", required = false)
                                          @ApiParam(name = "accountKey",value = "账户Key",required = false) String accountKey,
                                      @RequestBody @ApiParam(name = "params",value = "封装请求参数",required = false) String params) {
        daasBuriedPointService.saveDaasBuriedPointRecord(tenantName, "/queryPublicDataDirectory", DaasDataTypeEnum.PUBLIC_DATA_DIR.getCode(), params);
        return logicExternalService.queryPublicDataDirectory(tenantName,accountKey,params);
    }

    /**
     * 公用数据内容查询
     */
    @PostMapping("/queryPublicData")
    @AccessLimit
    @ApiOperation(value = "公用数据接口内容查询", notes = "此接口是对某个公用数据的内容进行查询。注：访问接口需使用公用租户账密", httpMethod = "POST")
    public R queryPublicData(@RequestParam(value = "type", required = false)
                                 @ApiParam(name = "type", value = "公共数据接口名称", required = false) String type,
                             @RequestHeader(value = "admin", required = false)
                                @ApiParam(name = "admin",value = "租户名(登记编号)",required = false) String tenantName,
                             @RequestHeader(value = "accountKey", required = false)
                                 @ApiParam(name = "accountKey",value = "账户Key",required = false) String accountKey,
                             @RequestBody @ApiParam(name = "params",value = "封装请求参数",required = false) String params) {
        daasBuriedPointService.saveDaasBuriedPointRecord(tenantName, "/queryPublicData", type, params);
        return logicExternalService.queryPublicData(tenantName, type, accountKey, params);
    }

    /**
     * 查询官网租户信息
     */
    @PostMapping("/queryTenantMapping")
    @ApiOperation(value = "查询官网租户信息", notes = "此接口是对于官网租户信息查询。注：访问接口需使用固定租户账密", httpMethod = "POST")
    public R queryTenantMapping(@RequestHeader(value = "admin", required = false)
                                @ApiParam(name = "admin",value = "租户名(登记编号)",required = false) String admin,
                                @RequestHeader(value = "accountKey", required = false)
                                @ApiParam(name = "accountKey",value = "账户Key",required = false) String accountKey,
                                @RequestBody @ApiParam(name = "params",value = "封装请求参数",required = false) String params) {
        return logicExternalService.queryTenantMapping(admin, accountKey, params);
    }

    /**
     * 保存官网租户信息
     */
    @PostMapping("/saveTenantMapping")
    @ApiOperation(value = "保存官网租户信息", notes = "此接口是对于官网租户信息的保存。注：访问接口需使用固定租户账密", httpMethod = "POST")
    public R saveTenantMapping(@RequestHeader(value = "admin", required = false)
                               @ApiParam(name = "admin",value = "租户名(登记编号)",required = false) String admin,
                               @RequestHeader(value = "accountKey", required = false)
                               @ApiParam(name = "accountKey",value = "账户Key",required = false) String accountKey,
                               @RequestBody @ApiParam(name = "params",value = "封装请求参数",required = false) String params) {
        return logicExternalService.saveTenantMapping(admin, accountKey, params);
    }

    /**
     * 保存官网租户信息
     */
    @PostMapping("/insertOrUpdate")
    @ApiOperation(value = "保存官网租户信息", notes = "此接口是对于官网租户信息的保存。注：访问接口需使用固定租户账密", httpMethod = "POST")
    public R insertOrUpdate(@RequestBody @ApiParam(name = "params",value = "封装请求参数") String params) {
        return custodyTenantMappingService.insertOrUpdate(params);
    }

    /**
     * @author GuoW
     * @date 2023/12/12 18:19
     * @description 手动更新募集余额
     **/
    @GetMapping("/updateMJBalanceData")
    public DubboResult updateMJBalanceData(String admin) {
        return custodyDataService.updateMJBalanceData(admin);
    }

    /**
     * @author GuoW
     * @date 2023/12/12 18:19
     * @description 手动更新募集流水
     **/
    @GetMapping("/updateMJSeqData")
    public DubboResult updateMJSeqData(String admin) {
        return custodyDataService.updateMJSeqData(admin);
    }

    /**
     * @author GuoW
     * @date 2023/12/12 18:19
     * @description 手动更新托管余额
     **/
    @GetMapping("/updateTGBalanceData")
    public DubboResult updateTGBalanceData(String admin) {
        return custodyDataService.updateTGBalanceData(admin);
    }

    /**
     * @author GuoW
     * @date 2023/12/12 18:19
     * @description 手动更新托管流水
     **/
    @GetMapping("/updateTGSeqData")
    public DubboResult updateTGSeqData(String admin) {
        return custodyDataService.updateTGSeqData(admin);
    }

}
