package com.yiqi.admin.controller.core;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiqi.app.domain.AppUser;
import com.yiqi.app.service.IAppUserService;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.CommentSourceType;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.enums.SysBoolType;
import com.yiqi.core.domain.GoodsCommentReply;
import com.yiqi.core.domain.dto.GoodsCommentReplyAddDTO;
import com.yiqi.core.domain.dto.GoodsCommentReplyQueryDTO;
import com.yiqi.core.domain.vo.GoodsCommentReplyQueryVO;
import com.yiqi.core.service.IGoodsCommentReplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 商品评论回复Controller
 *
 * @author ruoyi
 * @date 2023-12-25
 */
@Api(tags = "管理后台 - 商家评论回复")
@RestController
@RequestMapping(UrlConstants.managerApi + "/goods/reply")
public class GoodsCommentReplyController extends BaseController {
    @Autowired
    private IGoodsCommentReplyService goodsCommentReplyService;

    @Autowired
    private IAppUserService appUserService;

    /**
     * 新增商家评论回复
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/addCommentReply")
    @ApiOperation(value = "新增商家评论回复")
    public R<Boolean> addCommentReply(@RequestBody @Valid GoodsCommentReplyAddDTO goodsCommentReplyAddDTO) {
        GoodsCommentReply goodsCommentReply = new GoodsCommentReply();
        goodsCommentReply.setCommentSrcType(CommentSourceType.HQ.getCode());
        goodsCommentReply.setGoodsCommentId(goodsCommentReplyAddDTO.getGoodsCommentId());
        goodsCommentReply.setCommentContent(goodsCommentReplyAddDTO.getCommentContent());
        goodsCommentReply.setDelFlag(DeleteStatus.OK.getCode());
        goodsCommentReply.buildCreateData();
        return R.ok(goodsCommentReplyService.save(goodsCommentReply));
    }

    /**
     * 查询商品评论回复
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/queryCommentReplyByCommentId")
    @ApiOperation(value = "查询商品评论回复")
    public R<Page<GoodsCommentReplyQueryVO>> queryCommentReplyByCommentId(@RequestBody @Valid GoodsCommentReplyQueryDTO goodsCommentReplyQueryDTO) {
        Page<GoodsCommentReplyQueryVO> voPage = new Page<>();
        Page<GoodsCommentReply> page = goodsCommentReplyService.page(new Page<>(goodsCommentReplyQueryDTO.getPageNum(), goodsCommentReplyQueryDTO.getPageSize())
                , new QueryWrapper<GoodsCommentReply>().lambda()
                        .eq(GoodsCommentReply::getGoodsCommentId, goodsCommentReplyQueryDTO.getGoodsCommentId())
                        .eq(GoodsCommentReply::getDelFlag, DeleteStatus.OK.getCode())
                        .eq(GoodsCommentReply::getIsShow, SysBoolType.YES.getCode())
                        .orderByDesc(GoodsCommentReply::getCreateTime));
        if (CollUtil.isEmpty(page.getRecords())){
            return R.ok( new Page<>());
        }
        BeanUtils.copyProperties(page, voPage);

        List<GoodsCommentReplyQueryVO> queryVOList = JSON.parseArray(JSON.toJSONString(page.getRecords()), GoodsCommentReplyQueryVO.class);
        List<AppUser> appUsers = appUserService.list(new QueryWrapper<AppUser>().lambda()
                .in(AppUser::getId, page.getRecords().stream().map(GoodsCommentReply::getUserId).collect(Collectors.toSet())));
        if (CollUtil.isNotEmpty(appUsers)){
            Map<Long, AppUser> userMap = appUsers.stream().collect(Collectors.toMap(AppUser::getId, AppUser -> AppUser));
            for (GoodsCommentReplyQueryVO vo : queryVOList) {
                if (vo.getUserId() != null){
                    AppUser appUser = userMap.get(vo.getUserId());
                    vo.setNickName(appUser.getNickName());
                    vo.setRealName(appUser.getRealName());
                    vo.setPhoneNumber(appUser.getPhoneNumber());
                }
            }
        }
        voPage.setRecords(queryVOList);
        return R.page(voPage);
    }
}