package com.gxwebsoft.shop.controller;

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 com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.entity.ShopDealerRecord;
import com.gxwebsoft.shop.param.ShopDealerRecordParam;
import com.gxwebsoft.shop.service.ShopDealerRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 客户跟进情况控制器
 *
 * @author 科技小王子
 * @since 2025-10-02 12:21:50
 */
@Tag(name = "客户跟进情况管理")
@RestController
@RequestMapping("/api/shop/shop-dealer-record")
public class ShopDealerRecordController extends BaseController {
    @Resource
    private ShopDealerRecordService shopDealerRecordService;

    @Operation(summary = "分页查询客户跟进情况")
    @GetMapping("/page")
    public ApiResult<PageResult<ShopDealerRecord>> page(ShopDealerRecordParam param) {
        // 使用关联查询
        return success(shopDealerRecordService.pageRel(param));
    }

    @Operation(summary = "查询全部客户跟进情况")
    @GetMapping()
    public ApiResult<List<ShopDealerRecord>> list(ShopDealerRecordParam param) {
        // 使用关联查询
        return success(shopDealerRecordService.listRel(param));
    }

    @Operation(summary = "根据id查询客户跟进情况")
    @GetMapping("/{id}")
    public ApiResult<ShopDealerRecord> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(shopDealerRecordService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:shopDealerApply:save')")
    @OperationLog
    @Operation(summary = "添加客户跟进情况")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ShopDealerRecord shopDealerRecord) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         shopDealerRecord.setUserId(loginUser.getUserId());
        }
        if (shopDealerRecordService.save(shopDealerRecord)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopDealerApply:update')")
    @OperationLog
    @Operation(summary = "修改客户跟进情况")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ShopDealerRecord shopDealerRecord) {
        if (shopDealerRecordService.updateById(shopDealerRecord)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:shopDealerApply:remove')")
    @OperationLog
    @Operation(summary = "删除客户跟进情况")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (shopDealerRecordService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:shopDealerApply:save')")
    @OperationLog
    @Operation(summary = "批量添加客户跟进情况")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ShopDealerRecord> list) {
        if (shopDealerRecordService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopDealerApply:update')")
    @OperationLog
    @Operation(summary = "批量修改客户跟进情况")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ShopDealerRecord> batchParam) {
        if (batchParam.update(shopDealerRecordService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:shopDealerApply:remove')")
    @OperationLog
    @Operation(summary = "批量删除客户跟进情况")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (shopDealerRecordService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
