package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.MerchantCollectService;
import com.gxwebsoft.shop.entity.MerchantCollect;
import com.gxwebsoft.shop.param.MerchantCollectParam;
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 2025-05-23 12:05:53
 */
@Api(tags = "管理")
@RestController
@RequestMapping("/api/shop/merchant-collect")
public class MerchantCollectController extends BaseController {
    @Resource
    private MerchantCollectService merchantCollectService;

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

    @ApiOperation("查询全部")
    @GetMapping()
    public ApiResult<List<MerchantCollect>> list(MerchantCollectParam param) {
        // 使用关联查询
        return success(merchantCollectService.listRel(param));
    }

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

    @ApiOperation("添加")
    @PostMapping()
    public ApiResult<?> save(@RequestBody MerchantCollect merchantCollect) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
            merchantCollect.setUserId(loginUser.getUserId());
            MerchantCollect check = merchantCollectService.check(merchantCollect.getMerchantId(), merchantCollect.getUserId());
            if (check == null) {
                merchantCollectService.save(merchantCollect);
            }else {
                merchantCollectService.removeById(check.getId());
            }
            return success("添加成功");
        }
        return fail("添加失败");
    }

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

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

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

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

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

}
