package shop.alien.second.controller;


import com.alipay.api.kms.aliyun.utils.BackoffUtils;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.BusinessException;
import shop.alien.entity.result.R;
import shop.alien.entity.second.SecondTradeRecord;
import shop.alien.entity.second.vo.SecondTradeRecordVo;
import shop.alien.second.service.SecondTradeRecordService;

import java.util.List;

/**
 * <p>
 * 二手交易记录表 前端控制器
 * </p>
 *
 * @author qrs
 * @since 2025-07-07
 */
@Slf4j
@Api(tags = {"二手平台-商品交易记录"})
@ApiSort(1)
@CrossOrigin
@RequiredArgsConstructor
@RestController
@RequestMapping("/secondTradeRecord")
public class SecondTradeRecordController {

    private final SecondTradeRecordService secondTradeRecordService;

    @ApiOperation("创建交易")
    @ApiOperationSupport(order = 1)
    @PostMapping("/createTrade")
    public R<Boolean> createTrade(@RequestBody SecondTradeRecord entity) throws Exception {
        log.info("SecondTradeRecordController.createTrade?entity={}", entity.toString());
        if (secondTradeRecordService.createTrade(entity)) return R.success("创建成功");
        return R.fail("创建失败");
    }

    @ApiOperation("确认商品是否可以交易")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "goodsId", value = "商品id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/goodsTradeConfirm")
    public R<Boolean> goodsTradeConfirm(@RequestParam int goodsId) throws Exception {
        log.info("SecondTradeRecordController.goodsTradeConfirm?goodsId={}", goodsId);
        return R.data(secondTradeRecordService.goodsTradeConfirm(goodsId));
    }

    @ApiOperation("确认/拒绝交易")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeId", value = "交易id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "messageId", value = "消息id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "0-拒绝  1-确认", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/tradeConfirm")
    public R<Boolean> tradeConfirm(@RequestParam int tradeId, @RequestParam int messageId, @RequestParam int type) {
        log.info("SecondTradeRecordController.tradeConfirm?tradeId={}, messageId={}, type={}", tradeId, messageId, type);
        try {
            secondTradeRecordService.tradeConfirm(tradeId, messageId, type);
            return R.success("操作成功");
        } catch (BusinessException e) {
            return R.fail(e.getCause().getMessage());
        }  catch (Exception e) {
            return R.fail("系统繁忙,请稍后再试");
        }
    }

    @ApiOperation("取消交易")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeId", value = "交易id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "cancelReason", value = "取消原因", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "cancelReasonSupplement", value = "取消原因补充", dataType = "String", paramType = "query")})
    @GetMapping("/cancelTrade")
    public R<Boolean> cancelTrade(@RequestParam int tradeId, @RequestParam String cancelReason, String cancelReasonSupplement) throws Exception {
        log.info("SecondTradeRecordController.cancelTrade?tradeId={}, cancelReason={}, cancelReasonSupplement={}", tradeId, cancelReason, cancelReasonSupplement);
        if (secondTradeRecordService.cancelTrade(tradeId, cancelReason, cancelReasonSupplement)) return R.success("取消交易成功");
        return R.fail("取消交易失败");
    }

    @ApiOperation("交易是否可以签到")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({@ApiImplicitParam(name = "tradeId", value = "交易id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/whetherCanSignIn")
    public R<Boolean> whetherCanSignIn(@RequestParam int tradeId) throws Exception {
        log.info("SecondTradeRecordController.whetherCanSignIn?tradeId={}", tradeId);
        return R.data(secondTradeRecordService.whetherCanSignIn(tradeId));
    }

    @ApiOperation("交易签到")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeId", value = "交易id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "messageId", value = "消息id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "signInLatitudeLongitude", value = "签到地点(经纬度)", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "signInLatitudeLongitudeAddress", value = "签到地点(经纬度地址)", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/tradeSignIn")
    public R<Boolean> tradeSignIn(@RequestParam int tradeId, int messageId, String signInLatitudeLongitude, String signInLatitudeLongitudeAddress) {
        log.info("SecondTradeRecordController.tradeSignIn?tradeId={}, messageId={}", tradeId, messageId);
        try {
            secondTradeRecordService.tradeSignIn(tradeId, messageId, signInLatitudeLongitude, signInLatitudeLongitudeAddress);
            return R.success("签到成功");
        } catch (BusinessException e) {
            return R.fail(e.getCause().getMessage());
        }  catch (Exception e) {
            return R.fail("系统繁忙,请稍后再试");
        }
    }

    @ApiOperation("交易完成确认")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeId", value = "交易id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "1-交易成功  2-交易失败", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "evaluate", value = "评价", dataType = "String", paramType = "query")})
    @GetMapping("/tradeCompleteConfirm")
    public R<Boolean> tradeCompleteConfirm(@RequestParam int tradeId, @RequestParam int type, String evaluate) throws Exception {
        log.info("SecondTradeRecordController.tradeCompleteConfirm?tradeId={}, type={}, evaluate={}", tradeId, type, evaluate);
        return R.data(secondTradeRecordService.tradeCompleteConfirm(tradeId, type, evaluate));
    }

    @ApiOperation("获取用户交易确认信息")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({@ApiImplicitParam(name = "tradeId", value = "交易id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getUserTradeStatus")
    public R<SecondTradeRecordVo> getUserTradeStatus(@RequestParam int tradeId) throws Exception {
        log.info("SecondTradeRecordController.getUserTradeStatus?tradeId={}", tradeId);
        return R.data(secondTradeRecordService.getUserTradeStatus(tradeId));
    }

    @ApiOperation("交易窗")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({@ApiImplicitParam(name = "sideId", value = "对方的id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getTradeRecord")
    public R<List<SecondTradeRecordVo>> getTradeRecord(@RequestParam int sideId) throws Exception {
        log.info("SecondTradeRecordController.getTradeRecord?sideId={}", sideId);
        return R.data(secondTradeRecordService.getTradeRecord(sideId));
    }
}
