package com.czg.controller.admin;

import com.czg.account.dto.ShopBranchDTO;
import com.czg.account.entity.ShopConfig;
import com.czg.account.enums.BranchDataSyncMethodEnum;
import com.czg.account.param.ShopBranchParam;
import com.czg.account.service.ShopBranchService;
import com.czg.account.service.ShopConfigService;
import com.czg.log.annotation.OperationLog;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * 分店管理
 *
 * @author tankaikai
 * @since 2025-04-07 14:05
 */
@RestController
@RequestMapping("/admin/shop/branch")
public class ShopBranchController {

    @Resource
    private ShopBranchService shopBranchService;
    @Resource
    private ShopConfigService shopConfigService;

    /**
     * 分店列表
     */
    @GetMapping("page")
    @OperationLog("分店管理-分页")
    public CzgResult<Page<ShopBranchDTO>> getBranchPage(ShopBranchParam param) {
        Long shopId = StpKit.USER.getShopId(0L);
        param.setShopId(shopId);
        Page<ShopBranchDTO> data = shopBranchService.findPage(param);
        return CzgResult.success(data);
    }


    /**
     * 设置数据同步方式
     *
     * @param dataSyncMethod 数据同步方式 auto-实时自动同步 manual-手动同步
     */
    @PostMapping("/setting/dataSyncMethod")
    @OperationLog("分店管理-设置数据同步方式")
    public CzgResult<Void> settingDataSyncMethod(@RequestParam String dataSyncMethod) {
        Long shopId = StpKit.USER.getShopId(0L);
        shopBranchService.settingDataSyncMethod(shopId, dataSyncMethod);
        return CzgResult.success();
    }

    /**
     * 同步启用
     *
     * @param branchShopId 分店id
     */
    @PostMapping("/data/sync/enable")
    @OperationLog("分店管理-同步启用")
    public CzgResult<Void> dataSyncEnable(@RequestParam Long branchShopId) {
        shopBranchService.dataSyncEnable(branchShopId);
        CzgResult<Void> ret = CzgResult.success();
        ret.setMsg("启用成功，数据正在后台同步中...");
        return ret;
    }

    /**
     * 账号启用
     *
     * @param branchShopId 分店id
     */
    @PostMapping("/account/enable")
    @OperationLog("分店管理-账号启用")
    public CzgResult<Void> accountEnable(@RequestParam Long branchShopId) {
        shopBranchService.accountEnable(branchShopId);
        return CzgResult.success();
    }

    /**
     * 账号禁用
     *
     * @param branchShopId 分店id
     */
    @PostMapping("/account/disable")
    @OperationLog("分店管理-账号禁用")
    public CzgResult<Void> accountDisable(@RequestParam Long branchShopId) {
        shopBranchService.accountDisable(branchShopId);
        return CzgResult.success();
    }

    /**
     * 获取数据同步方式
     * @return 数据同步方式 auto-实时自动同步 manual-手动同步
     */
    @GetMapping("/get/dataSyncMethod")
    @OperationLog("分店管理-获取数据同步方式")
    public CzgResult<String> getDataSyncMethod() {
        Long shopId = StpKit.USER.getShopId(0L);
        ShopConfig shopConfig = shopConfigService.getById(shopId);
        return CzgResult.success(shopConfig == null? BranchDataSyncMethodEnum.AUTO.getValue() : shopConfig.getBranchDataSyncMethod());
    }
}
