package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreEvaluation;
import shop.alien.entity.store.dto.GroupRes;
import shop.alien.entity.store.dto.StoreEvaluationDto;
import shop.alien.store.service.StoreEvaluationService;

import java.util.List;
import java.util.Map;

/**
 * @Author: fcw
 * @CreateTime: 2025-04-24
 * @Description: 用户评价
 */
@Slf4j
@Api(tags = {"用户评价"})
@CrossOrigin
@RestController
@RequestMapping("/storeEvaluation")
@RequiredArgsConstructor
public class StoreEvaluationController {

    private final StoreEvaluationService storeEvaluationService;

    @ApiOperation("新增或修改评价")
    @ApiOperationSupport(order = 1)
    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody StoreEvaluation storeEvaluation) {
        log.info("StoreEvaluationController.saveOrUpdate?storeEvaluation={}", storeEvaluation);
        Integer id = storeEvaluation.getId();
        if (storeEvaluationService.saveStoreEvaluation(storeEvaluation)) {
            if (null != id) {
                return R.success("修改成功");
            }
            return R.success("新增成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("评价保存 max 1:成功, 2:失败, 3:文本内容异常")
    @ApiOperationSupport(order = 2)
    @PostMapping("/save")
    public R<Integer> userComment(@RequestBody StoreEvaluationDto dto) {
        if (dto.getType().equals("max")) {
            return R.data(storeEvaluationService.saveEvaluation(dto));
        } else {
            return R.data(storeEvaluationService.saveLev(dto.getId(), dto.getEvaluationContent(), dto.getUserId()));
        }
    }

    @ApiOperation("评论保存 min 1:成功, 2:失败, 3:文本内容异常")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评价id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "评论内容", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String", paramType = "query"),
    })
    @GetMapping("/inr")
    public R<Integer> inr(Integer id, String content, String userId) {
        return R.data(storeEvaluationService.saveLev(id, content, userId));
    }

    @ApiOperation("查询当前数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评价id", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/byId")
    public R<StoreEvaluation> inr(Integer id) {
        return R.data(storeEvaluationService.getById(id));
    }

    @ApiOperation("点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评价id", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/like")
    public R<Integer> like(Integer id) {
        return R.data(storeEvaluationService.like(id));
    }

    @ApiOperation("标题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商店id", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/title")
    public R<Map<String, String>> tit(String storeId) {
        return R.data(storeEvaluationService.title(storeId));
    }

    @ApiOperation("标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商店id", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/label")
    public R<List<GroupRes>> label(String storeId) {
        return R.data(storeEvaluationService.groupCount(storeId));
    }

    @ApiOperation("获取分页列表")
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "商店id (必传)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "evaluationImage", value = "查询传exist。不查询传空", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "score", value = "差传2", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id(必传)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "评论主键（查询详情传）", dataType = "String", paramType = "query"),
    })
    public R getAppeals(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(required = false) String storeId,
            @RequestParam(required = false) String evaluationImage,
            @RequestParam(required = false) String score,
            @RequestParam(required = false) List<String> lable,
            @RequestParam(required = false) String userId,
            @RequestParam(required = false) String id) {
        log.info("StoreEvaluationController.getAppeals?page={}&size={}&storeId={}&evaluationImage={}&score={}&userId={}&id={}", page, size, storeId, evaluationImage, score, userId, id);
        IPage<StoreEvaluationDto> stores = storeEvaluationService.page(page, size, storeId, evaluationImage, score, lable, userId, id);
        return R.data(stores);
    }
}
