package com.gxwebsoft.open.controller;

import cn.hutool.core.date.DateUtil;
import com.gxwebsoft.apps.entity.BcTemporary;
import com.gxwebsoft.apps.param.BcTemporaryParam;
import com.gxwebsoft.apps.service.BcTemporaryService;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
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 2023-04-24 21:47:57
 */
@Api(tags = "临时报餐管理管理")
@RestController
@RequestMapping("/api/open/bc-temporary")
public class OpenBcTemporaryController extends BaseController {
    @Resource
    private BcTemporaryService bcTemporaryService;

    @ApiOperation("分页查询临时报餐管理")
    @GetMapping("/page")
    public ApiResult<PageResult<BcTemporary>> page(BcTemporaryParam param) {
        // 验证签名
        isCheckSign();
        // 使用关联查询
        param.setUserId(getLoginUserId());
        param.setDayTime(DateUtil.date());
      return success(bcTemporaryService.pageRel(param));
    }

    @ApiOperation("查询全部临时报餐管理")
    @GetMapping()
    public ApiResult<List<BcTemporary>> list(BcTemporaryParam param) {
        // 验证签名
        isCheckSign();
        // 使用关联查询
        return success(bcTemporaryService.listRel(param));
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:list')")
    @OperationLog
    @ApiOperation("根据id查询临时报餐管理")
    @GetMapping("/{id}")
    public ApiResult<BcTemporary> get(@PathVariable("id") Integer id) {
        return success(bcTemporaryService.getById(id));
        // 使用关联查询
        //return success(bcTemporaryService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:save')")
    @OperationLog
    @ApiOperation("添加临时报餐管理")
    @PostMapping()
    public ApiResult<?> save(@RequestBody BcTemporary bcTemporary) {
        if (bcTemporaryService.save(bcTemporary)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:update')")
    @OperationLog
    @ApiOperation("修改临时报餐管理")
    @PutMapping()
    public ApiResult<?> update(@RequestBody BcTemporary bcTemporary) {
        if (bcTemporaryService.updateById(bcTemporary)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:remove')")
    @OperationLog
    @ApiOperation("删除临时报餐管理")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (bcTemporaryService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:save')")
    @OperationLog
    @ApiOperation("批量添加临时报餐管理")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<BcTemporary> list) {
        if (bcTemporaryService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:update')")
    @OperationLog
    @ApiOperation("批量修改临时报餐管理")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<BcTemporary> batchParam) {
        if (batchParam.update(bcTemporaryService, "temporary_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:bcTemporary:remove')")
    @OperationLog
    @ApiOperation("批量删除临时报餐管理")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (bcTemporaryService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
