package com.gxwebsoft.cms.controller;

import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.MpMenuService;
import com.gxwebsoft.cms.entity.MpMenu;
import com.gxwebsoft.cms.param.MpMenuParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 小程序端菜单控制器
 *
 * @author 科技小王子
 * @since 2024-05-02 12:22:25
 */
@Api(tags = "小程序导航图标")
@RestController
@RequestMapping("/api/cms/mp-menu")
public class MpMenuController extends BaseController {
    @Resource
    private MpMenuService mpMenuService;
    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("分页查询小程序端菜单")
    @GetMapping("/page")
    public ApiResult<PageResult<MpMenu>> page(MpMenuParam param) {
        // 使用关联查询
        return success(mpMenuService.pageRel(param));
    }

    @ApiOperation("查询全部小程序端菜单")
    @GetMapping()
    public ApiResult<List<MpMenu>> list(MpMenuParam param) {
      return success(mpMenuService.listRel(param));
    }

    @ApiOperation("根据id查询小程序端菜单")
    @GetMapping("/{id}")
    public ApiResult<MpMenu> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(mpMenuService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('cms:mpMenu:save')")
    @OperationLog
    @ApiOperation("添加小程序端菜单")
    @PostMapping()
    public ApiResult<?> save(@RequestBody MpMenu mpMenu) {
        // 记录当前登录用户id
        // User loginUser = getLoginUser();
        // if (loginUser != null) {
        //  mpMenu.setUserId(loginUser.getUserId());
        // }
        if (mpMenuService.save(mpMenu)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:mpMenu:update')")
    @OperationLog
    @ApiOperation("修改小程序端菜单")
    @PutMapping()
    public ApiResult<?> update(@RequestBody MpMenu mpMenu) {
        if (mpMenuService.updateById(mpMenu)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:mpMenu:remove')")
    @OperationLog
    @ApiOperation("删除小程序端菜单")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (mpMenuService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:mpMenu:save')")
    @OperationLog
    @ApiOperation("批量添加小程序端菜单")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<MpMenu> list) {
        if (mpMenuService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:mpMenu:update')")
    @OperationLog
    @ApiOperation("批量修改小程序端菜单")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<MpMenu> batchParam) {
        if (batchParam.update(mpMenuService, "menu_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:mpMenu:remove')")
    @OperationLog
    @ApiOperation("批量删除小程序端菜单")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (mpMenuService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
