package com.dxhy.erp.controller;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.DateTimeHelper;
import com.dxhy.common.utils.R;
import com.dxhy.erp.entity.RequestRecord;
import com.dxhy.erp.service.RequestRecordService;
import com.dxhy.erp.utils.DateUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@SuppressWarnings("AlibabaMethodTooLong")
@RequestMapping("/recall")
@RestController
@Slf4j
public class RequestRecallController extends AbstractController {

    @Resource
    RequestRecordService requestRecordService;


    @ApiOperation(value = "重调接口入口", notes = "重调接口入口")
    @RequestMapping(path = "/requestRecordRecall", method = {RequestMethod.POST})
    public ResponseEntity<?> recall(@RequestBody RequestRecord requestRecord) {
        log.info("重调用接口调用开始");
        log.info("重调用接口入参:{}", JSONObject.toJSONString(requestRecord));
        ResponseEntity responseEntity = requestRecordService.reCall(requestRecord);
        log.info("重调用接口调用结束，返回参数:{}", JSONObject.toJSONString(responseEntity));
        return responseEntity;
    }

    @ApiOperation(value = "接口记录查询", notes = "接口记录查询")
    @RequestMapping(path = "/requestRecordBatch", method = {RequestMethod.POST})
    public ResponseEntity<?> batchRequestRecord(@RequestBody Map<String, Object> params) {
        String portName = (String) params.get("portName");
        String startTime =  (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        Integer status = (Integer) params.get("status");
        String methodName = (String) params.get("methodName");
        Integer pageNumber = (Integer) params.get("pageNumber");
        Integer pageSize =  (Integer) params.get("pageSize");
        Integer orderBy = (Integer) params.get("orderBy");

        JSONObject resultJson = requestRecordService.getRecordList(portName, DateUtil.strToDate(startTime),
                DateUtil.strToDate(endTime), status, methodName, pageNumber, pageSize, orderBy);
        return ResponseEntity.ok(R.ok().put("data",resultJson));
    }

    @ApiOperation(value = "接口记录详细查询", notes = "接口记录详细查询")
    @RequestMapping(path = "/requestDetailRecordBatch", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<?> batchRequestDetailRecord(@RequestBody Long id) {
        JSONObject resultJson = requestRecordService.getDetailRecord(id);
        return ResponseEntity.ok(R.ok().put("data",resultJson));
    }

}
