package org.dromara.zhishu.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.zhishu.domain.ShopGoodsPublishedLog;
import org.dromara.zhishu.domain.ShopGoodsRejection;
import org.dromara.zhishu.domain.vo.ShopGoodsPublishedVo;
import org.dromara.zhishu.service.ShopGoodsPublishedLogService;
import org.dromara.zhishu.service.ShopGoodsRejectionService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 记录已发布书籍
 *
 * @author yxy
 * @date 2025-04-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhishu/shopgoodsrejection")
public class ShopGoodsRejectionController {

    private final ShopGoodsRejectionService shopGoodsRejectionService;

    /**
     * 分页查询列表
     */
    @GetMapping("/list")
    public TableDataInfo<ShopGoodsRejection> list(ShopGoodsRejection bo, PageQuery pageQuery) {
        TableDataInfo<ShopGoodsRejection> list = shopGoodsRejectionService.searchWithPage(bo,pageQuery);

          return list;
    }

    /**
     * 根据ID查询详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getById(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            ShopGoodsRejection rejection = shopGoodsRejectionService.findById(id);
            if (rejection != null) {
                result.put("success", true);
                result.put("data", rejection);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "记录不存在");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 新增记录
     */
    @PostMapping("/add")
    public ResponseEntity<Map<String, Object>> create(@RequestBody ShopGoodsRejection shopGoodsRejection) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = shopGoodsRejectionService.save(shopGoodsRejection);
            if (success) {
                result.put("success", true);
                result.put("message", "新增成功");
                result.put("data", shopGoodsRejection);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "新增失败");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "新增失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 更新记录
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> update(@PathVariable Long id, @RequestBody ShopGoodsRejection shopGoodsRejection) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 设置ID
            shopGoodsRejection.setId(id);
            boolean success = shopGoodsRejectionService.update(shopGoodsRejection);
            if (success) {
                result.put("success", true);
                result.put("message", "更新成功");
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "更新失败，记录可能不存在");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 删除记录
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = shopGoodsRejectionService.delete(id);
            if (success) {
                result.put("success", true);
                result.put("message", "删除成功");
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "删除失败，记录可能不存在");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/batch")
    public ResponseEntity<Map<String, Object>> batchDelete(@RequestBody List<Long> ids) {
        Map<String, Object> result = new HashMap<>();
        try {
            int successCount = 0;
            for (Long id : ids) {
                if (shopGoodsRejectionService.delete(id)) {
                    successCount++;
                }
            }
            result.put("success", true);
            result.put("message", "成功删除 " + successCount + " 条记录");
            result.put("successCount", successCount);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "批量删除失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 根据店铺ID查询
     */
    @GetMapping("/shop/{shopId}")
    public ResponseEntity<Map<String, Object>> getByShopId(@PathVariable String shopId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ShopGoodsRejection> list = shopGoodsRejectionService.findByShopId(shopId);
            result.put("success", true);
            result.put("data", list);
            result.put("total", list.size());
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 根据平台商品ID查询
     */
    @GetMapping("/platform/{platformId}")
    public ResponseEntity<Map<String, Object>> getByPlatformId(@PathVariable String platformId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ShopGoodsRejection> list = shopGoodsRejectionService.findByPlatformId(platformId);
            result.put("success", true);
            result.put("data", list);
            result.put("total", list.size());
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(result);
        }
    }

}
