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

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.multishop.vo.ShopRefundAddrVO;
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.multishop.dto.ShopRefundAddrDTO;
import com.tmerclub.cloud.multishop.model.ShopRefundAddr;
import com.tmerclub.cloud.multishop.service.ShopRefundAddrService;
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("adminShopRefundAddrController")
@RequestMapping("/mp/RefundAddr")
@Tag(name = "admin-退货地址")
public class ShopRefundAddrController {

    @Autowired
    private ShopRefundAddrService shopRefundAddrService;


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

    @GetMapping
    @Operation(summary = "获取退货地址", description = "根据shopRefundAddrId获取退货地址")
    public ServerResponseEntity<ShopRefundAddrVO> getByShopRefundAddrId(@RequestParam Long shopRefundAddrId) {
        Long shopId = AuthUserContext.get().getTenantId();
        ShopRefundAddrVO byShopRefund = shopRefundAddrService.getByShopRefundAddrId(shopRefundAddrId, shopId);
        if (StrUtil.isNotBlank(byShopRefund.getMobile())) {
            byShopRefund.setMobile(PhoneUtil.hideBetween(byShopRefund.getMobile()).toString());
        }
        return ServerResponseEntity.success(byShopRefund);
    }

    @PostMapping
    @Operation(summary = "保存退货地址", description = "保存退货地址")
    public ServerResponseEntity<Long> save(@Valid @RequestBody ShopRefundAddrDTO shopRefundAddrDTO) {
        ShopRefundAddr shopRefundAddr = BeanUtil.map(shopRefundAddrDTO, ShopRefundAddr.class);
        Long shopId = AuthUserContext.get().getTenantId();
        shopRefundAddr.setShopId(shopId);
        shopRefundAddrService.save(shopRefundAddr);
        shopRefundAddrService.removeCacheByShopId(shopId);
        return ServerResponseEntity.success(shopRefundAddr.getShopRefundAddrId());
    }

    @PutMapping
    @Operation(summary = "更新退货地址", description = "更新退货地址")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ShopRefundAddrDTO shopRefundAddrDTO) {
        ShopRefundAddr shopRefundAddr = BeanUtil.map(shopRefundAddrDTO, ShopRefundAddr.class);
        Long shopId = AuthUserContext.get().getTenantId();
        shopRefundAddr.setShopId(AuthUserContext.get().getTenantId());
        shopRefundAddrService.update(shopRefundAddr);
        shopRefundAddrService.removeCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }

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

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