package com.gxwebsoft.apps.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.apps.service.BcCookbookService;
import com.gxwebsoft.apps.entity.BcCookbook;
import com.gxwebsoft.apps.param.BcCookbookParam;
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 2023-05-05 14:56:54
 */
@Api(tags = "常用菜谱管理")
@RestController
@RequestMapping("/api/apps/bc-cookbook")
public class BcCookbookController extends BaseController {
    @Resource
    private BcCookbookService bcCookbookService;

    @PreAuthorize("hasAuthority('apps:bcCookbook:list')")
    @OperationLog
    @ApiOperation("分页查询常用菜谱")
    @GetMapping("/page")
    public ApiResult<PageResult<BcCookbook>> page(BcCookbookParam param) {
        PageParam<BcCookbook, BcCookbookParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(bcCookbookService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(bcCookbookService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('apps:bcCookbook:list')")
    @OperationLog
    @ApiOperation("查询全部常用菜谱")
    @GetMapping()
    public ApiResult<List<BcCookbook>> list(BcCookbookParam param) {
        PageParam<BcCookbook, BcCookbookParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(bcCookbookService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(bcCookbookService.listRel(param));
    }

    @PreAuthorize("hasAuthority('apps:bcCookbook:list')")
    @OperationLog
    @ApiOperation("根据id查询常用菜谱")
    @GetMapping("/{id}")
    public ApiResult<BcCookbook> get(@PathVariable("id") Integer id) {
        return success(bcCookbookService.getById(id));
        // 使用关联查询
        //return success(bcCookbookService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('apps:bcCookbook:save')")
    @OperationLog
    @ApiOperation("添加常用菜谱")
    @PostMapping()
    public ApiResult<?> save(@RequestBody BcCookbook bcCookbook) {
        // 记录当前登录用户id、租户id、商户编号
        User loginUser = getLoginUser();
        if (loginUser != null) {
          bcCookbook.setUserId(loginUser.getUserId());
        }
        if (bcCookbookService.save(bcCookbook)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:bcCookbook:update')")
    @OperationLog
    @ApiOperation("修改常用菜谱")
    @PutMapping()
    public ApiResult<?> update(@RequestBody BcCookbook bcCookbook) {
        if (bcCookbookService.updateById(bcCookbook)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:bcCookbook:remove')")
    @OperationLog
    @ApiOperation("删除常用菜谱")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (bcCookbookService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('apps:bcCookbook:save')")
    @OperationLog
    @ApiOperation("批量添加常用菜谱")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<BcCookbook> list) {
        if (bcCookbookService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:bcCookbook:update')")
    @OperationLog
    @ApiOperation("批量修改常用菜谱")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<BcCookbook> batchParam) {
        if (batchParam.update(bcCookbookService, "cookbook_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:bcCookbook:remove')")
    @OperationLog
    @ApiOperation("批量删除常用菜谱")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (bcCookbookService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
