package com.czg.controller.admin;

import com.czg.account.dto.PageDTO;
import com.czg.account.dto.shopinfo.ShopBranchSelectDTO;
import com.czg.account.dto.shopinfo.ShopDetailDTO;
import com.czg.account.dto.shopinfo.ShopInfoAddDTO;
import com.czg.account.dto.shopinfo.ShopInfoEditDTO;
import com.czg.account.entity.ShopRegister;
import com.czg.account.entity.ShopInfo;
import com.czg.account.service.AuthorizationService;
import com.czg.account.service.ShopRegisterService;
import com.czg.account.service.ShopInfoService;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.annotation.SaAdminCheckRole;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺管理
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/shopInfo")
public class ShopInfoController {
    @Resource
    private ShopInfoService shopInfoService;
    @Resource
    private AuthorizationService authorizationService;
    @Resource
    private ShopRegisterService shopRegisterService;

    /**
     * 店铺列表, 只允许管理员调用
     * 权限标识: shopInfo:list
     */
    @SaAdminCheckRole("管理员")
    @SaAdminCheckPermission(parentName = "店铺管理接口", value = "shopInfo:list", name = "店铺列表")
    @GetMapping
    public CzgResult<Page<ShopDetailDTO>> get(PageDTO pageDTO, String profiles, String phone, String shopName, Integer status, Integer isHeadShop) {
        return CzgResult.success(shopInfoService.get(pageDTO, profiles, phone, shopName, status, isHeadShop));
    }

    /**
     * 店铺的 店铺列表
     *
     */
    @GetMapping("/otherShop")
    public CzgResult<Page<ShopDetailDTO>> getShopByMainId(PageDTO pageDTO, String shopName, Integer status) {
        return CzgResult.success(shopInfoService.getShopByMainId(pageDTO, shopName, status));
    }

    /**
     * 店铺详情
     * 权限标识: shopInfo:detail
     */
//    @SaAdminCheckPermission("shopInfo:detail")
    @GetMapping("/detail")
    public CzgResult<ShopDetailDTO> detail(Long id) {
        if (StpKit.USER.isAdmin() || StpKit.USER.getShopId().equals(id)) {
            return CzgResult.success(shopInfoService.detail(id));
        }
        return CzgResult.success(shopInfoService.detail(StpKit.USER.getShopId()));
    }

    /**
     * 店铺添加, 只允许管理员调用
     * 权限标识: shopInfo:add
     */
    @SaAdminCheckRole("管理员")
    @SaAdminCheckPermission(parentName = "店铺管理接口", value = "shopInfo:add", name = "店铺添加")
    @PostMapping
    public CzgResult<?> add(@RequestBody @Validated ShopInfoAddDTO shopInfoAddDTO) {
        return CzgResult.success(shopInfoService.add(shopInfoAddDTO));
    }

    /**
     * 店铺编辑
     * 权限标识: shopInfo:edit
     */
    @SaAdminCheckPermission(parentName = "店铺管理接口", value = "shopInfo:edit", name = "店铺编辑")
    @PutMapping
    public CzgResult<?> edit(@RequestBody @Validated ShopInfoEditDTO shopInfoEditDTO) {
        return CzgResult.success(shopInfoService.edit(shopInfoEditDTO));
    }

    /**
     * 店铺删除
     * 权限标识: shopInfo:del
     */
    @SaAdminCheckRole("管理员")
    @SaAdminCheckPermission(parentName = "店铺管理接口", value = "shopInfo:del", name = "店铺删除")
    @DeleteMapping
    public CzgResult<?> delete(@RequestParam Integer id) {
        return CzgResult.success(shopInfoService.remove(new QueryWrapper().eq(ShopInfo::getId, id)));
    }

    /**
     * 店铺分店列表(下拉展示主店和分店使用，默认第一个是主店，其余是分店）
     */
    @GetMapping("branchList")
    public CzgResult<List<ShopBranchSelectDTO>> findShopBranch() {
        List<ShopBranchSelectDTO> data = shopInfoService.findShopBranch(StpKit.USER.getShopId());
        return CzgResult.success(data);
    }

    /**
     * 切换店铺
     *
     * @param shopId 店铺ID
     */
    @PostMapping("/change/{shopId}")
    public CzgResult<Void> change(@PathVariable Long shopId) {
        authorizationService.switchTo(shopId);
        return CzgResult.success();
    }

    /**
     * 续期记录
     *
     * @param pageDTO 分页参数
     * @return 续期记录列表
     */
    @SaAdminCheckRole("管理员")
    @GetMapping("/registerRecord")
    public CzgResult<Page<ShopRegister>> get(PageDTO pageDTO, Long shopId) {
        Page<ShopRegister> page = shopRegisterService.page(new Page<>(pageDTO.page(), pageDTO.size()),
                new QueryWrapper().eq(ShopRegister::getShopId, shopId).orderBy(ShopRegister::getCreateTime, false));
        return CzgResult.success(page);
    }
}
