package com.czg.controller.admin;

import cn.hutool.core.collection.CollUtil;
import com.czg.account.entity.QuickMenu;
import com.czg.account.service.QuickMenuService;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 悬浮窗/快捷菜单
 *
 * @author ww
 * @description
 */
@RestController
@RequestMapping("/admin/quick")
@Slf4j
public class QuickMenuController {

    @Resource
    private QuickMenuService quickMenuService;

//    @SaAdminCheckPermission(parentName = "悬浮窗", value = "quick:list", name = "悬浮窗-列表")
    @GetMapping
    public CzgResult<List<QuickMenu>> getQuickList(@RequestParam(required = false) Integer status,
                                                   @RequestParam(required = false, defaultValue = "0") Integer isEdit) {
        List<QuickMenu> list = quickMenuService.list(QueryWrapper.create()
                .eq(QuickMenu::getShopId, StpKit.USER.getShopId())
                .eq(QuickMenu::getStatus, status)
                .orderBy(QuickMenu::getSort, true));
        if (isEdit.equals(0)) {
            if (CollUtil.isEmpty(list)) {
                list = quickMenuService.list(QueryWrapper.create()
                        .eq(QuickMenu::getShopId, 1)
                        .eq(QuickMenu::getStatus, status)
                        .orderBy(QuickMenu::getSort, true));
            }
        }
        return CzgResult.success(list);
    }

    @SaAdminCheckPermission(parentName = "悬浮窗", value = "quick:list", name = "悬浮窗-新增")
    @PostMapping
    public CzgResult<Boolean> saveQuick(@RequestBody @Validated QuickMenu quickMenu) {
        quickMenu.setShopId(StpKit.USER.getShopId());
        return CzgResult.success(quickMenuService.save(quickMenu));
    }

    @SaAdminCheckPermission(parentName = "悬浮窗", value = "quick:list", name = "悬浮窗-修改")
    @PutMapping
    public CzgResult<Boolean> updateQuick(@RequestBody @Validated QuickMenu quickMenu) {
        return CzgResult.success(quickMenuService.update(quickMenu, QueryWrapper.create()
                .eq(QuickMenu::getId, quickMenu.getId()).eq(QuickMenu::getShopId, StpKit.USER.getShopId())));
    }

    @SaAdminCheckPermission(parentName = "悬浮窗", value = "quick:list", name = "悬浮窗-删除")
    @DeleteMapping
    public CzgResult<Boolean> deleteQuick(@RequestBody Set<Long> ids) {
        return CzgResult.success(quickMenuService.remove(QueryWrapper.create().in(QuickMenu::getId, ids).eq(QuickMenu::getShopId, StpKit.USER.getShopId())));
    }
}
