package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.service.GoodsServiceService;
import com.gxwebsoft.shop.entity.GoodsService;
import com.gxwebsoft.shop.param.GoodsServiceParam;
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 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 2022-11-23 21:57:50
 */
@Api(tags = "商品服务与承诺记录表管理")
@RestController
@RequestMapping("/api/shop/goods-service")
public class GoodsServiceController extends BaseController {
    @Resource
    private GoodsServiceService goodsServiceService;

    @PreAuthorize("hasAuthority('shop:goodsService:list')")
    @OperationLog
    @ApiOperation("分页查询商品服务与承诺记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<GoodsService>> page(GoodsServiceParam param) {
        PageParam<GoodsService, GoodsServiceParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time asc");
        return success(goodsServiceService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(goodsServiceService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('shop:goodsService:list')")
    @OperationLog
    @ApiOperation("查询全部商品服务与承诺记录表")
    @GetMapping()
    public ApiResult<List<GoodsService>> list(GoodsServiceParam param) {
        PageParam<GoodsService, GoodsServiceParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(goodsServiceService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(goodsServiceService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:goodsService:list')")
    @OperationLog
    @ApiOperation("根据id查询商品服务与承诺记录表")
    @GetMapping("/{id}")
    public ApiResult<GoodsService> get(@PathVariable("id") Integer id) {
        return success(goodsServiceService.getById(id));
        // 使用关联查询
        //return success(goodsServiceService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:goodsService:save')")
    @OperationLog
    @ApiOperation("添加商品服务与承诺记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody GoodsService goodsService) {
        // 记录当前登录用户id、租户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          goodsService.setUserId(loginUser.getUserId());
        }
        if (goodsServiceService.save(goodsService)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:goodsService:update')")
    @OperationLog
    @ApiOperation("修改商品服务与承诺记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody GoodsService goodsService) {
        if (goodsServiceService.updateById(goodsService)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:goodsService:remove')")
    @OperationLog
    @ApiOperation("删除商品服务与承诺记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (goodsServiceService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:goodsService:save')")
    @OperationLog
    @ApiOperation("批量添加商品服务与承诺记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<GoodsService> list) {
        if (goodsServiceService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:goodsService:update')")
    @OperationLog
    @ApiOperation("批量修改商品服务与承诺记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<GoodsService> batchParam) {
        if (batchParam.update(goodsServiceService, "service_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:goodsService:remove')")
    @OperationLog
    @ApiOperation("批量删除商品服务与承诺记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (goodsServiceService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
