package com.yiqi.admin.controller.core;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.validation.Valid;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.domain.AuthHolder;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.*;
import com.yiqi.core.domain.GoodsCommentRelation;
import com.yiqi.core.domain.dto.GoodsCommentAddDTO;
import com.yiqi.core.domain.dto.GoodsCommentQueryDTO;
import com.yiqi.core.domain.dto.GoodsCommentUpdateDTO;
import com.yiqi.core.domain.vo.GoodsCommentQueryVO;
import com.yiqi.core.service.IGoodsCommentRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.core.domain.GoodsComment;
import com.yiqi.core.service.IGoodsCommentService;

/**
 * 商品评论Controller
 *
 * @author ruoyi
 * @date 2023-12-19
 */
@Api(tags = "管理后台 - 商品评论")
@RestController
@RequestMapping(UrlConstants.managerApi + "/goods/goodsComment")
public class GoodsCommentController extends BaseController {
    @Autowired
    private IGoodsCommentService goodsCommentService;

    @Autowired
    private IGoodsCommentRelationService goodsCommentRelationService;

    //<editor-folder desc="基础函数">

    /**
     * 查询商品评论列表
     */
    @PreAuthorize("@ss.hasPermi('goods:goodsComment:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询商品评论列表")
    public R<Page<GoodsCommentQueryVO>> list(GoodsCommentQueryDTO goodsCommentQueryDTO) {
        Page<GoodsCommentQueryVO> page = new Page<>();
        List<GoodsCommentQueryVO> goodsCommentList = goodsCommentService.selectGoodsCommentList(goodsCommentQueryDTO);
        if (CollUtil.isEmpty(goodsCommentList)) {
            return R.page(page);
        }
        Integer count = goodsCommentService.countNumberByGoodsCommentList(goodsCommentQueryDTO);

        Set<Long> commentIdSet = goodsCommentList.stream().map(GoodsCommentQueryVO::getId).collect(Collectors.toSet());
        Map<Long, List<GoodsCommentRelation>> map = goodsCommentRelationService.list(new QueryWrapper<GoodsCommentRelation>().lambda().in(GoodsCommentRelation::getGoodsCommentId, commentIdSet))
                .stream().collect(Collectors.groupingBy(GoodsCommentRelation::getGoodsCommentId));
        if (CollUtil.isEmpty(map)) {
            page.setRecords(goodsCommentList);
            page.setTotal(count);
            return R.page(page);
        }
        for (GoodsCommentQueryVO vo : goodsCommentList) {
            vo.setSkuList(map.get(vo.getId()));
        }
        page.setRecords(goodsCommentList);
        page.setTotal(count);
        return R.page(page);
    }

    /**
     * 获取商品评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:goodsComment:query')")
    @GetMapping(value = "/getGoodsCommentInfoById")
    @ApiOperation(value = "获取商品评论详细信息")
    public R<GoodsComment> getInfo(@RequestParam("id") Long id) {
        return R.ok(goodsCommentService.selectGoodsCommentById(id));
    }

    /**
     * 新增虚拟商品评论
     */
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("@ss.hasPermi('goods:goodsComment:add')")
    @PostMapping
    @ApiOperation(value = "新增虚拟商品评论")
    public R<Integer> add(@RequestBody @Valid GoodsCommentAddDTO goodsCommentAddDTO) {
        GoodsComment goodsComment = new GoodsComment();
        BeanUtils.copyProperties(goodsCommentAddDTO, goodsComment);
        goodsComment.setCommentSrcType(CommentSourceType.HQ.getCode());
        goodsComment.setDelFlag(DeleteStatus.OK.getCode());
        goodsComment.setUserId(AuthHolder.userId());
        if (goodsComment.getCommentScore() >= 3) {
            goodsComment.setCommentType("0");
        } else {
            goodsComment.setCommentType("1");
        }
        goodsComment.setIsShow(SysBoolType.YES.getCode());
        goodsComment.setIsVirtual(SysBoolType.YES.getCode());
        goodsComment.buildCreateData();
        return R.ok(goodsCommentService.insertGoodsComment(goodsComment, goodsCommentAddDTO.getGoodsId(), goodsCommentAddDTO.getGoodsSkuId()));
    }

    /**
     * 修改虚拟商品评论
     */
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("@ss.hasPermi('goods:goodsComment:edit')")
    @PostMapping("/updateComment")
    @ApiOperation(value = "修改虚拟商品评论")
    public R<Boolean> updateComment(@RequestBody @Valid GoodsCommentUpdateDTO goodsCommentUpdateDTO) {
        GoodsComment comment = goodsCommentService.getOne(new QueryWrapper<GoodsComment>().lambda()
                .eq(GoodsComment::getId, goodsCommentUpdateDTO.getGoodsCommentId())
                .eq(GoodsComment::getDelFlag, DeleteStatus.OK.getCode())
                .eq(GoodsComment::getIsVirtual, SysBoolType.YES.getCode())
                .eq(GoodsComment::getCommentSrcType, CommentSourceType.HQ.getCode()));
        if (comment == null) {
            return R.fail("未查询到该评论的信息");
        }
        comment.buildUpdateData();
        comment.setIsShow(goodsCommentUpdateDTO.getIsShow());
        comment.setCommentContent(goodsCommentUpdateDTO.getCommentContent());
        comment.setCommentImgs(goodsCommentUpdateDTO.getCommentImgs());
        comment.setCommentType(goodsCommentUpdateDTO.getCommentType());
        comment.setCommentScore(goodsCommentUpdateDTO.getCommentScore());
        return R.ok(goodsCommentService.updateById(comment));
    }

    /**
     * 批量处理商品评论-删除评论
     */
    @PreAuthorize("@ss.hasPermi('goods:goodsComment:remove')")
    @PostMapping("/deleteComment")
    @ApiOperation(value = "批量处理商品评论-删除评论")
    public R<Boolean> deleteComment(@RequestBody List<Long> ids) {
        return R.ok(goodsCommentService.update(new UpdateWrapper<GoodsComment>().lambda()
                .in(GoodsComment::getId, ids)
                .set(GoodsComment::getDelFlag, DeleteStatus.DELETED.getCode())));
    }

    /**
     * 批量处理商品评论-隐藏
     */
    @PreAuthorize("@ss.hasPermi('goods:goodsComment:edit')")
    @PostMapping("/noShowComment")
    @ApiOperation(value = "批量处理商品评论-是否显示")
    public R<Boolean> noShowComment(@RequestBody List<Long> ids) {
        return R.ok(goodsCommentService.update(new UpdateWrapper<GoodsComment>().lambda()
                .in(GoodsComment::getId, ids)
                .set(GoodsComment::getIsShow, SysBoolType.NO.getCode())));
    }

    /**
     * 批量处理商品评论-显示
     */
    @PreAuthorize("@ss.hasPermi('goods:goodsComment:edit')")
    @PostMapping("/showComment")
    @ApiOperation(value = "批量处理商品评论-是否显示")
    public R<Boolean> showComment(@RequestBody List<Long> ids) {
        return R.ok(goodsCommentService.update(new UpdateWrapper<GoodsComment>().lambda()
                .in(GoodsComment::getId, ids)
                .set(GoodsComment::getIsShow, SysBoolType.YES.getCode())));
    }


    //</editor-folder>
}
