package com.gxwebsoft.cms.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.DesignRecordService;
import com.gxwebsoft.cms.entity.DesignRecord;
import com.gxwebsoft.cms.param.DesignRecordParam;
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.web.bind.annotation.*;

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

/**
 * 页面组件表控制器
 *
 * @author 科技小王子
 * @since 2024-08-25 20:34:14
 */
@Api(tags = "页面组件表管理")
@RestController
@RequestMapping("/api/cms/design-record")
public class DesignRecordController extends BaseController {
    @Resource
    private DesignRecordService designRecordService;

    @ApiOperation("分页查询页面组件表")
    @GetMapping("/page")
    public ApiResult<PageResult<DesignRecord>> page(DesignRecordParam param) {
        // 使用关联查询
        return success(designRecordService.pageRel(param));
    }

    @ApiOperation("查询全部页面组件表")
    @GetMapping()
    public ApiResult<List<DesignRecord>> list(DesignRecordParam param) {
        // 使用关联查询
        return success(designRecordService.listRel(param));
    }

    @PreAuthorize("hasAuthority('cms:designRecord:list')")
    @ApiOperation("根据id查询页面组件表")
    @GetMapping("/{id}")
    public ApiResult<DesignRecord> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(designRecordService.getByIdRel(id));
    }

    @ApiOperation("添加页面组件表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody DesignRecord designRecord) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         designRecord.setUserId(loginUser.getUserId());
        }
        if (designRecordService.save(designRecord)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改页面组件表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody DesignRecord designRecord) {
        if (designRecordService.updateById(designRecord)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除页面组件表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (designRecordService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加页面组件表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<DesignRecord> list) {
        if (designRecordService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改页面组件表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<DesignRecord> batchParam) {
        if (batchParam.update(designRecordService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除页面组件表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (designRecordService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
