package com.docus.server.collect.basic.http;

import com.docus.core.util.json.JSON;
import com.docus.infrastructure.web.api.CommonResult;
import com.docus.log.annotation.TrackGroup;
import com.docus.log.context.TrackHelper;
import com.docus.server.collect.IConverter;
import com.docus.server.collect.IHttpResult;
import com.docus.server.enums.CollectTypeEnum;
import com.docus.server.collect.web.process.VisitorProcessor;
import com.docus.server.collect.web.service.CollectService;
import com.docus.server.record.common.pojo.dto.TBasicDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * @author wen yongbin
 * @date 2023年2月25日21:56:33
 * @see CollectTypeEnum 枚举
 * @see IConverter 通用转化器
 * @see IHttpResult 通用返回结果
 * @see VisitorProcessor 通用处理器
 */

@Api(value = "基础数据管理接口", tags = "基础数据管理接口")
@Slf4j
@RestController
@RequestMapping("/api/rest/basic")
public class BasicRestController {
    @Resource
    private CollectService collectService;

    /**
     * 被动http 新增基础数据接收，进行操作
     *
     * @param message 接收到的新增基础数据参数
     * @return 返回结果
     */
    @ApiOperation("新增基础数据(根据记账号)")
    @PostMapping("/insert")
    @TrackGroup(
        group = "HTTP_PASSIVE_BASIC", retryKey = "basic",
        beanNames = {"httpTBasicConverter", "httpBasicResultImpl"},
        processor = VisitorProcessor.class)
    public CommonResult insertHttpPassiveBasic(@RequestBody String message) {
        log.info("新增基础数据：{}", message);
        collectService.insertOrUpdateBasic(getValue());
        return null;
    }

    /**
     * 被动http 编辑基础数据接收，进行操作
     *
     * @param message 接收到的编辑基础数据参数
     * @return 返回结果
     */
    @ApiOperation("编辑基础数据(根据记账号)")
    @PostMapping("/update")
    @TrackGroup(
        group = "HTTP_PASSIVE_BASIC", retryKey = "basic",
        beanNames = {"httpTBasicConverter", "httpBasicResultImpl"},
        processor = VisitorProcessor.class)
    public CommonResult updateHttpPassiveBasic(@RequestBody String message) {
        log.info("编辑基础数据：{}", message);
        collectService.insertOrUpdateBasic(getValue());
        return null;
    }

    public TBasicDTO getValue() {
        return JSON.fromJSON((String) TrackHelper.getValue("jsonStr"), TBasicDTO.class);
    }
}
