package com.org.hktshop.controller.order;

import com.org.hktshop.base.BaseResult;
import com.org.hktshop.dto.HktInvoiceSearchDto;
import com.org.hktshop.dto.HktOrderInvoiceBatchDto;
import com.org.hktshop.dto.PageQueryDto;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.order.HktOrderInvoice;
import com.org.hktshop.service.order.HktOrderInvoiceService;
import com.org.hktshop.utils.HttpCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/async/orderInvoice")
@Api(tags = "管理端订单发票接口")
public class HktOrderInvoiceAdminController {
    @Autowired
    private HktOrderInvoiceService orderInvoiceService;

    @PostMapping("/getOrderInvoiceBySearch")
    @ApiOperation(value = "开票订单搜索", notes = "开票订单搜索")
    public BaseResult<Page<HktOrderInvoice>> getOrderInvoiceBySearch(@RequestBody PageQueryDto<HktInvoiceSearchDto> pageQueryDto) {
        try {
            Page<HktOrderInvoice> orderInvoiceBySearch = orderInvoiceService.getOrderInvoiceBySearch(pageQueryDto);
            return BaseResult.successWithData(orderInvoiceBySearch);
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @PostMapping("/selectOrderInvoice")
    @ApiOperation(value = "开票订单列表", notes = "开票订单列表")
    public BaseResult<Page<HktOrderInvoice>> selectOrderInvoice(@RequestBody HktOrderInvoice hktOrderInvoice) {
        try {
            Page<HktOrderInvoice> page = orderInvoiceService.selectOrderInvoice(hktOrderInvoice);
            return BaseResult.successWithData(page);
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @PostMapping("/batchUpdate")
    @ApiOperation(value = "订单发票批量修改", notes = "订单发票批量修改")
    public BaseResult<Page<HktOrderInvoice>> batchUpdate(@RequestBody HktOrderInvoiceBatchDto orderInvoiceBatchDto) {
        try {
            HktOrderInvoice orderInvoice = new HktOrderInvoice();
            BeanUtils.copyProperties(orderInvoiceBatchDto, orderInvoice);
            orderInvoiceService.batchUpdate(orderInvoiceBatchDto.getIds(), orderInvoice);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("/getOrderInvoiceByOrderId")
    @ApiOperation(value = "orderId查询开票订单", notes = "orderId查询开票订单")
    public BaseResult getOrderInvoiceById(@RequestParam("id")String id) {
        try {
            HktOrderInvoice hktOrderInvoice = orderInvoiceService.getHktOrderInvoice(id);
            return BaseResult.successWithData(hktOrderInvoice);
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }
}
