
package com.tmerclub.cloud.order.controller.multishop;

import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.order.dto.OrderInvoiceDTO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.order.constant.InvoiceHeaderType;
import com.tmerclub.cloud.order.constant.OrderInvoiceState;
import com.tmerclub.cloud.order.model.Order;
import com.tmerclub.cloud.order.model.OrderInvoice;
import com.tmerclub.cloud.order.service.OrderInvoiceService;
import com.tmerclub.cloud.order.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Objects;

/**
 * @author Pineapple
 * @date 2021/8/2 8:59
 */
@RestController("multishopOrderInvoiceController")
@RequestMapping("/m/order_invoice")
@Tag(name = "商家端订单发票")
public class OrderInvoiceController {

    private final Logger LOGGER = LoggerFactory.getLogger(OrderInvoiceController.class);
    @Autowired
    private OrderInvoiceService orderInvoiceService;
    @Autowired
    private OrderService orderService;


    @GetMapping
    @Operation(summary = "获取", description = "根据orderInvoiceId获取")
    public ServerResponseEntity<OrderInvoice> getByOrderInvoiceId(@RequestParam Long orderInvoiceId) {
        return ServerResponseEntity.success(orderInvoiceService.getByOrderInvoiceId(orderInvoiceId));
    }

    @PutMapping
    @Operation(summary = "更新", description = "更新")
    public ServerResponseEntity<Void> update(@Valid @RequestBody OrderInvoiceDTO orderInvoiceDTO) {
        OrderInvoice orderInvoice = BeanUtil.map(orderInvoiceDTO, OrderInvoice.class);
        if (Objects.isNull(orderInvoiceDTO.getFileId())) {
            //商家提交，不上传文件不给保存
            LOGGER.error("请上传文件!");
            throw new LuckException("请上传文件!");
        }
        orderInvoice.setInvoiceState(OrderInvoiceState.ISSUED.value());
        orderInvoice.setUploadTime(new Date());
        orderInvoiceService.update(orderInvoice);
        return ServerResponseEntity.success();
    }

    @Operation(summary = "该订单是否已经上传发票")
    @GetMapping("/is_upload")
    public ServerResponseEntity<Boolean> isUpload(@RequestParam Long orderId) {
        Long shopId = AuthUserContext.get().getTenantId();
        return ServerResponseEntity.success(orderInvoiceService.isUpload(orderId, shopId, null));
    }

    @PostMapping("/applyInvoice")
    @Operation(summary = "申请开票(向供应商)", description = "保存申请信息")
    public ServerResponseEntity<Void> applyInvoice(@Valid @RequestBody OrderInvoiceDTO orderInvoiceDTO) {
        Order order = orderService.getByOrderId(orderInvoiceDTO.getOrderId());
        if (Objects.isNull(order)) {
            throw new LuckException("订单不存在");
        }
        Long supplierId = order.getSupplierId();
        if (Objects.isNull(supplierId)
                || Objects.equals(supplierId, 0L)) {
            throw new LuckException("非供应商订单，不能向供应商申请开票");
        }
        Long orderId = orderInvoiceService.getByOrderId(orderInvoiceDTO.getOrderId(), null, supplierId);
        if (Objects.nonNull(orderId)) {
            LOGGER.error("该订单已经申请发票，请勿重复申请！");
            throw new LuckException("该订单已经申请发票，请勿重复申请！");
        }
        OrderInvoice orderInvoice = BeanUtil.map(orderInvoiceDTO, OrderInvoice.class);
        orderInvoice.setShopId(0L);
        orderInvoice.setSupplierId(supplierId);
        if (Objects.equals(InvoiceHeaderType.PERSONAL.value(), orderInvoice.getHeaderType())) {
            LOGGER.error("个人发票不需要填写税号");
            orderInvoice.setInvoiceTaxNumber(null);
        }
        orderInvoice.setInvoiceState(OrderInvoiceState.APPLICATION.value());
        orderInvoice.setApplicationTime(new Date());
        orderInvoiceService.save(order.getUserId(), orderInvoice);
        return ServerResponseEntity.success();
    }

    @PutMapping("/changeInvoice")
    @Operation(summary = "申请换开(向供应商)", description = "更新发票信息")
    public ServerResponseEntity<Void> changeInvoice(@Valid @RequestBody OrderInvoiceDTO orderInvoiceDTO) {
        OrderInvoice orderInvoice = BeanUtil.map(orderInvoiceDTO, OrderInvoice.class);
        orderInvoice.setInvoiceState(OrderInvoiceState.APPLICATION.value());
        orderInvoice.setApplicationTime(new Date());
        if (Objects.equals(InvoiceHeaderType.PERSONAL.value(), orderInvoice.getHeaderType())) {
            LOGGER.error("个人发票不需要填写税号");
            orderInvoice.setInvoiceTaxNumber(null);
        }
        orderInvoiceService.update(orderInvoice);
        return ServerResponseEntity.success();
    }
}
