package com.gxwebsoft.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gxwebsoft.cms.entity.Design;
import com.gxwebsoft.cms.entity.Navigation;
import com.gxwebsoft.cms.param.DesignParam;
import com.gxwebsoft.cms.service.DesignService;
import com.gxwebsoft.cms.service.NavigationService;
import com.gxwebsoft.common.core.web.*;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 页面管理记录表控制器
 *
 * @author 科技小王子
 * @since 2024-03-04 21:20:40
 */
@Api(tags = "网站页面")
@RestController
@RequestMapping("/api/cms/design")
public class DesignController extends BaseController {
    @Resource
    private DesignService designService;
    @Resource
    private NavigationService navigationService;

    @ApiOperation("分页查询页面管理记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<Design>> page(DesignParam param) {
        // 使用关联查询
        return success(designService.pageRel(param));
    }

    @ApiOperation("查询全部页面管理记录表")
    @GetMapping()
    public ApiResult<List<Design>> list(DesignParam param) {
        // 使用关联查询
        return success(designService.listRel(param));
    }

    @ApiOperation("根据id查询页面管理记录表")
    @GetMapping("/{id}")
    public ApiResult<Design> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(designService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('cms:design:save')")
    @ApiOperation("添加页面管理记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Design design) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          design.setUserId(loginUser.getUserId());
        }
//        if (designService.count(new LambdaQueryWrapper<Design>().eq(Design::getPath,design.getPath())) > 0) {
//          return fail("路由地址已存在");
//        }
        if (designService.save(design)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:design:update')")
    @ApiOperation("修改页面管理记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Design design) {
        if (designService.updateById(design)) {
            // 更新导航路径
//            if (design.getNavigationId() > 0) {
//              navigationService.update(new LambdaUpdateWrapper<Navigation>()
//                .eq(Navigation::getPageId,design.getPageId())
//                .set(Navigation::getPath,design.getPath())
//                .set(Navigation::getComponent,design.getComponent()));
//            }
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:design:remove')")
    @ApiOperation("删除页面管理记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (designService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:design:save')")
    @ApiOperation("批量添加页面管理记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Design> list) {
        if (designService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:design:update')")
    @ApiOperation("批量修改页面管理记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Design> batchParam) {
        if (batchParam.update(designService, "page_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:design:remove')")
    @ApiOperation("批量删除页面管理记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (designService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
