package com.cool.store.controller.webb;

import com.cool.store.context.CurrentUserHolder;
import com.cool.store.dto.wallet.TradeRecordDTO;
import com.cool.store.job.XxlJobHandler;
import com.cool.store.request.SplitOrderPageRequest;
import com.cool.store.request.SplitOrderRequest;
import com.cool.store.response.ResponseResult;
import com.cool.store.response.SplitOrderResponse;
import com.cool.store.service.SplitOrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 分账主表 Controller
 */
@RequestMapping("/pc/splitOrder")
@RestController
@Api(tags = "分账单管理")
@Slf4j
public class SplitOrderController {

    @Resource
    private SplitOrderService splitOrderService;

    @Resource
    private XxlJobHandler xxlJobHandler;

    @PostMapping("/add")
    @ApiOperation("新增分账单")
    public ResponseResult<Boolean> add(@Valid @RequestBody SplitOrderRequest request) {
        return ResponseResult.success(splitOrderService.add(request));
    }

    @PostMapping("/update")
    @ApiOperation("编辑分账单")
    public ResponseResult<Boolean> update(@Valid @RequestBody SplitOrderRequest request) {
        return ResponseResult.success(splitOrderService.update(request));
    }

    @PostMapping("/delete")
    @ApiOperation("删除分账单")
    public ResponseResult<Boolean> delete(@ApiParam("分账单ID") @RequestParam Long id) {
        return ResponseResult.success(splitOrderService.delete(id));
    }

    @GetMapping("/getById")
    @ApiOperation("根据ID查询分账单详情")
    public ResponseResult<SplitOrderResponse> getById(@ApiParam("分账单ID") @RequestParam Long id) {
        return ResponseResult.success(splitOrderService.getById(id));
    }

    @PostMapping("/page")
    @ApiOperation("分页查询分账单")
    public ResponseResult<PageInfo<SplitOrderResponse>> page(@RequestBody SplitOrderPageRequest request) {
        return ResponseResult.success(splitOrderService.page(request));
    }

    @PostMapping("/batchUpdateStatus")
    @ApiOperation("批量更新状态")
    public ResponseResult<Boolean> batchUpdateStatus(
            @ApiParam("分账单ID列表") @RequestParam List<Long> ids,
            @ApiParam("目标状态") @RequestParam String status) {
        return ResponseResult.success(splitOrderService.batchUpdateStatus(ids, status));
    }

    @PostMapping("/confirmSplitOrder")
    @ApiOperation("确认分账")
    public ResponseResult<Boolean> confirmSplitOrder(@ApiParam("分账单ID") @RequestParam Long id) {
        return ResponseResult.success(splitOrderService.confirmSplitOrder(id, CurrentUserHolder.getUser()));
    }

    @ApiOperation("交易付款详情")
    @GetMapping("/billDetail")
    public ResponseResult<TradeRecordDTO> getBillDetail( @RequestParam Long id) {
        return ResponseResult.success(splitOrderService.getBillDetail(id));
    }

    @ApiOperation("刷新分账单状态")
    @GetMapping("/refreshStatus")
    public ResponseResult<Boolean> refreshStatus() {
        xxlJobHandler.transfer();
        return ResponseResult.success(Boolean.TRUE);
    }
}
