
package com.tmerclub.cloud.delivery.controller.supplier;

import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.order.vo.TransportVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.delivery.dto.TransportDTO;
import com.tmerclub.cloud.delivery.service.TransportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 运费模板
 *
 * @author tmerclub
 * @date 2020-12-07 15:10:01
 */
@RestController("supplierTransportController")
@RequestMapping("/s/transport")
@Tag(name = "运费模板")
public class TransportController {

    @Autowired
    private TransportService transportService;


    @GetMapping("/page")
    @Operation(summary = "获取运费模板列表", description = "分页获取运费模板列表")
    public ServerResponseEntity<PageVO<TransportVO>> page(@Valid PageDTO pageDTO, TransportDTO transportDTO) {
        Long tenantId = AuthUserContext.get().getTenantId();
        transportDTO.setShopId(tenantId);
        PageVO<TransportVO> transports = transportService.page(pageDTO, transportDTO);
        return ServerResponseEntity.success(transports);
    }

    @GetMapping
    @Operation(summary = "获取运费模板", description = "根据transportId获取运费模板")
    public ServerResponseEntity<TransportVO> getByTransportId(@RequestParam Long transportId) {
        TransportVO transportVo = transportService.getTransportAndAllItemsById(transportId);
        return ServerResponseEntity.success(transportVo);
    }

    @PostMapping
    @Operation(summary = "保存运费模板", description = "保存运费模板")
    public ServerResponseEntity<Void> save(@Valid @RequestBody TransportDTO transportDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        transportDTO.setShopId(shopId);
        if (Objects.isNull(transportDTO.getTransFees()) && transportDTO.getIsFreeFee() == 0) {
            throw new LuckException("请选择可配送区域");
        }
        transportService.insertTransportAndTransFee(transportDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新运费模板", description = "更新运费模板")
    public ServerResponseEntity<Void> update(@Valid @RequestBody TransportDTO transportDTO) {
        if (Objects.isNull(transportDTO.getTransFees()) && transportDTO.getIsFreeFee() == 0) {
            throw new LuckException("请选择可配送区域");
        }
        UidInfoBO uidInfoBO = AuthUserContext.get();
        transportDTO.setShopId(uidInfoBO.getTenantId());
        transportDTO.setSysType(uidInfoBO.getSysType());
        transportService.updateTransportAndTransFee(transportDTO);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除运费模板", description = "根据运费模板id删除运费模板")
    public ServerResponseEntity<Void> delete(@RequestParam Long transportId) {
        transportService.deleteTransportAndTransFeeAndTransCityById(transportId);
        // 删除运费模板的缓存
        transportService.removeTransportAndAllItemsCache(transportId);
        return ServerResponseEntity.success();
    }

    /**
     * 获取运费模板列表
     */
    @GetMapping("/list")
    public ServerResponseEntity<List<TransportVO>> list(@RequestParam(value = "shopId", required = false) Long shopId) {
        Long tenantId = AuthUserContext.get().getTenantId();
        if (Objects.equals(tenantId, Constant.PLATFORM_SHOP_ID) && Objects.nonNull(shopId)) {
            tenantId = shopId;
        }
        List<TransportVO> transports = transportService.listTransport(tenantId);
        return ServerResponseEntity.success(transports);
    }
}
