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

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.supplier.vo.SupplierApiRefundAddrVO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
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.supplier.dto.SupplierRefundAddrDTO;
import com.tmerclub.cloud.supplier.model.SupplierRefundAddr;
import com.tmerclub.cloud.supplier.service.SupplierRefundAddrService;
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;

/**
 * 退货地址
 *
 * @author YXF
 * @date 2021-01-27 09:10:00
 */
@RestController("adminSupplierRefundAddrController")
@RequestMapping("/s/supplier_refund_addr")
@Tag(name = "admin-供应商供应商退货地址")
public class SupplierRefundAddrController {

    @Autowired
    private SupplierRefundAddrService supplierRefundAddrService;


    @GetMapping("/page")
    @Operation(summary = "分页获取退货地址列表", description = "分页获取退货地址列表")
    public ServerResponseEntity<PageVO<SupplierApiRefundAddrVO>> page(@Valid PageDTO pageDTO, SupplierRefundAddrDTO supplierRefundAddrDTO) {
        supplierRefundAddrDTO.setSupplierId(AuthUserContext.get().getTenantId());
        PageVO<SupplierApiRefundAddrVO> refundAddrPage = supplierRefundAddrService.page(pageDTO, supplierRefundAddrDTO);
        for (SupplierApiRefundAddrVO supplierRefundAddrVO : refundAddrPage.getList()) {
            if (StrUtil.isNotBlank(supplierRefundAddrVO.getMobile())) {
                supplierRefundAddrVO.setMobile(PhoneUtil.hideBetween(supplierRefundAddrVO.getMobile()).toString());
            }
        }
        return ServerResponseEntity.success(refundAddrPage);
    }

    @GetMapping
    @Operation(summary = "获取退货地址", description = "根据supplierRefundAddrId获取退货地址")
    public ServerResponseEntity<SupplierApiRefundAddrVO> getBySupplierRefundAddrId(@RequestParam Long supplierRefundAddrId) {
        Long supplierId = AuthUserContext.get().getTenantId();
        SupplierApiRefundAddrVO bySupplierRefund = supplierRefundAddrService.getBySupplierRefundAddrId(supplierRefundAddrId, supplierId);
        if (StrUtil.isNotBlank(bySupplierRefund.getMobile())) {
            bySupplierRefund.setMobile(PhoneUtil.hideBetween(bySupplierRefund.getMobile()).toString());
        }
        return ServerResponseEntity.success(bySupplierRefund);
    }

    @PostMapping
    @Operation(summary = "保存退货地址", description = "保存退货地址")
    public ServerResponseEntity<Long> save(@Valid @RequestBody SupplierRefundAddrDTO supplierRefundAddrDTO) {
        SupplierRefundAddr supplierRefundAddr = BeanUtil.map(supplierRefundAddrDTO, SupplierRefundAddr.class);
        Long supplierId = AuthUserContext.get().getTenantId();
        supplierRefundAddr.setSupplierId(supplierId);
        supplierRefundAddrService.save(supplierRefundAddr);
        supplierRefundAddrService.removeCacheBySupplierId(supplierId);
        return ServerResponseEntity.success(supplierRefundAddr.getSupplierRefundAddrId());
    }

    @PutMapping
    @Operation(summary = "更新退货地址", description = "更新退货地址")
    public ServerResponseEntity<Void> update(@Valid @RequestBody SupplierRefundAddrDTO supplierRefundAddrDTO) {
        SupplierRefundAddr supplierRefundAddr = BeanUtil.map(supplierRefundAddrDTO, SupplierRefundAddr.class);
        Long supplierId = AuthUserContext.get().getTenantId();
        supplierRefundAddr.setSupplierId(AuthUserContext.get().getTenantId());
        supplierRefundAddrService.update(supplierRefundAddr);
        supplierRefundAddrService.removeCacheBySupplierId(supplierId);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除退货地址", description = "根据退货地址id删除退货地址")
    public ServerResponseEntity<Void> delete(@RequestParam Long supplierRefundAddrId) {
        Long supplierId = AuthUserContext.get().getTenantId();
        supplierRefundAddrService.deleteById(supplierRefundAddrId, supplierId);
        supplierRefundAddrService.removeCacheBySupplierId(supplierId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/list")
    @Operation(summary = "获取退货地址列表", description = "获取退货地址列表")
    public ServerResponseEntity<List<SupplierApiRefundAddrVO>> list() {
        return ServerResponseEntity.success(supplierRefundAddrService.listBySupplierId(AuthUserContext.get().getTenantId()));
    }
}
