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

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
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.delivery.dto.OutletConfigDTO;
import com.tmerclub.cloud.delivery.model.OutletConfig;
import com.tmerclub.cloud.delivery.service.OutletConfigService;
import com.tmerclub.cloud.delivery.vo.OutletConfigInfoVO;
import com.tmerclub.cloud.delivery.vo.OutletConfigVO;
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 TRACK
 * @date 2023-08-23 14:48:23
 */
@RestController("adminOutletConfigController")
@RequestMapping("/mp/outletConfig")
@Tag(name = "网点配置")
public class OutletConfigController {

    @Autowired
    private OutletConfigService outletConfigService;

    @GetMapping("/page")
    @Operation(summary = "获取网点配置列表", description = "分页获取网点配置列表")
    public ServerResponseEntity<PageVO<OutletConfigVO>> page(@Valid PageDTO pageDTO, OutletConfigDTO outletConfigDTO) {
        outletConfigDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<OutletConfigVO> outletConfigPage = outletConfigService.page(pageDTO, outletConfigDTO);
        return ServerResponseEntity.success(outletConfigPage);
    }

    @GetMapping
    @Operation(summary = "获取网点配置", description = "根据outletConfig获取网点配置")
    public ServerResponseEntity<OutletConfig> getByOutletConfig(@RequestParam Long outletConfigId) {
        OutletConfig outletConfig = outletConfigService.getByOutletConfig(outletConfigId, AuthUserContext.get().getSysType());
        if (StrUtil.isNotBlank(outletConfig.getMobile())) {
            outletConfig.setMobile(PhoneUtil.hideBetween(outletConfig.getMobile()).toString());
        }
        return ServerResponseEntity.success(outletConfig);
    }

    @PostMapping
    @Operation(summary = "保存网点配置", description = "保存网点配置")
    public ServerResponseEntity<Void> save(@Valid @RequestBody OutletConfigDTO outletConfigDTO) {
        OutletConfig outletConfig = BeanUtil.map(outletConfigDTO, OutletConfig.class);
        outletConfig.setOutletConfigId(null);
        outletConfig.setSupplierId(null);
        outletConfig.setShopId(AuthUserContext.get().getTenantId());
        outletConfigService.save(outletConfig);
        outletConfigService.removeCache(AuthUserContext.get().getTenantId(), AuthUserContext.get().getSysType());
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新网点配置", description = "更新网点配置")
    public ServerResponseEntity<Void> update(@Valid @RequestBody OutletConfigDTO outletConfigDTO) {
        OutletConfig outletConfig = BeanUtil.map(outletConfigDTO, OutletConfig.class);
        Long shopId = AuthUserContext.get().getTenantId();
        outletConfig.setSupplierId(null);
        outletConfig.setShopId(shopId);
        outletConfigService.update(outletConfig);
        outletConfigService.removeCache(shopId, AuthUserContext.get().getSysType());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除网点配置", description = "根据网点配置id删除网点配置")
    public ServerResponseEntity<Void> delete(@RequestParam Long outletConfigId) {
        outletConfigService.deleteById(outletConfigId);
        outletConfigService.removeCache(AuthUserContext.get().getTenantId(), AuthUserContext.get().getSysType());
        return ServerResponseEntity.success();
    }

    @PutMapping("/setDefault")
    @Operation(summary = "设网点信息为默认/取消默认")
    public ServerResponseEntity<Void> setDefault(@RequestParam Long outletConfigId) {
        outletConfigService.setDefault(outletConfigId);
        outletConfigService.removeCache(AuthUserContext.get().getTenantId(), AuthUserContext.get().getSysType());
        return ServerResponseEntity.success();
    }

    @GetMapping("/outletConfigList")
    @Operation(summary = "获取网点信息列表")
    public ServerResponseEntity<List<OutletConfigInfoVO>> outletConfigList() {
        List<OutletConfigInfoVO> list = outletConfigService.listByShopId(AuthUserContext.get().getTenantId(), AuthUserContext.get().getSysType());
        return ServerResponseEntity.success(list);
    }
}
