package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.MerchantTypeService;
import com.gxwebsoft.shop.entity.MerchantType;
import com.gxwebsoft.shop.param.MerchantTypeParam;
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-04-05 00:08:51
 */
@Api(tags = "商户类型管理")
@RestController
@RequestMapping("/api/shop/merchant-type")
public class MerchantTypeController extends BaseController {
    @Resource
    private MerchantTypeService merchantTypeService;

    @ApiOperation("分页查询商户类型")
    @GetMapping("/page")
    public ApiResult<PageResult<MerchantType>> page(MerchantTypeParam param) {
        // 使用关联查询
        return success(merchantTypeService.pageRel(param));
    }

    @ApiOperation("查询全部商户类型")
    @GetMapping()
    public ApiResult<List<MerchantType>> list(MerchantTypeParam param) {
        // 使用关联查询
        return success(merchantTypeService.listRel(param));
    }

    @ApiOperation("根据id查询商户类型")
    @GetMapping("/{id}")
    public ApiResult<MerchantType> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(merchantTypeService.getByIdRel(id));
    }

    @ApiOperation("添加商户类型")
    @PostMapping()
    public ApiResult<?> save(@RequestBody MerchantType merchantType) {
        // 记录当前登录用户id
        // User loginUser = getLoginUser();
        // if (loginUser != null) {
        //  merchantType.setUserId(loginUser.getUserId());
        // }
        if (merchantTypeService.save(merchantType)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改商户类型")
    @PutMapping()
    public ApiResult<?> update(@RequestBody MerchantType merchantType) {
        if (merchantTypeService.updateById(merchantType)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
