package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.MessageService;
import com.gxwebsoft.shop.entity.Message;
import com.gxwebsoft.shop.param.MessageParam;
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-11-18 22:03:11
 */
@Api(tags = "管理")
@RestController
@RequestMapping("/api/shop/message")
public class MessageController extends BaseController {
    @Resource
    private MessageService messageService;

    @PostMapping("/unread-num")
    public ApiResult<Integer> unreadNum() {
        // 使用关联查询
        return success(messageService.unreadNum(getLoginUserId()));
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ApiResult<PageResult<Message>> page(MessageParam param) {
        // 使用关联查询
        return success(messageService.pageRel(param));
    }

    @ApiOperation("查询全部")
    @GetMapping()
    public ApiResult<List<Message>> list(MessageParam param) {
        // 使用关联查询
        param.setUserId(getLoginUserId());
        return success(messageService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:message:list')")
    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public ApiResult<Message> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(messageService.getByIdRel(id));
    }

    @ApiOperation("添加")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Message message) {
        if (messageService.save(message)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Message message) {
        if (messageService.updateById(message)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

    @ApiOperation("批量添加")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Message> list) {
        if (messageService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

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

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

}
