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 org.springframework.web.multipart.MultipartRequest;
import shop.alien.entity.result.R;
import shop.alien.entity.store.vo.*;
import shop.alien.store.service.StoreCommentService;

import java.util.Map;
import java.util.Set;

/**
 * 评论表 前端控制器
 *
 * @author ssk
 * @since 2025-01-02
 */
@Slf4j
@Api(tags = {"二期-评论/评价"})
@ApiSort(11)
@CrossOrigin
@RestController
@RequestMapping("/storeComment")
@RequiredArgsConstructor
public class StoreCommentController {

    private final StoreCommentService storeCommentService;

    @ApiOperation("评论/评价列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "businessId", value = "业务id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "businessType", value = "业务类型(1:订单评论, 2:动态社区评论, 3:活动评论, 4:店铺打卡评论, 5:订单评价, 6:订单评论的评论)", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "replyStatus", value = "回复状态(0:全部, 1:已回复, 2:未回复)", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "commentLevel", value = "评论等级(0:全部, 1:好评, 2:中评, 3:差评)", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "days", value = "查询时间, 多少天前", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "phoneId", value = "消息标识", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userType", value = "评论的用户类型(0:商家, 其他:用户)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "tagId", value = "标签id)", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "hasImage", value = "是否有图片(0否/1是)", dataType = "Boolean", paramType = "query")
    })
    @GetMapping("/getList")
    public R<IPage<StoreCommentVo>> getList(Integer pageNum, Integer pageSize, Integer businessId, Integer businessType, Integer storeId, Integer replyStatus, Integer commentLevel, Integer days, String phoneId, Integer userType, Integer tagId,@RequestParam(required = false, defaultValue = "false") Boolean hasImage) {
        log.info("StoreCommentController.getList?pageNum={}&pageSize={}&businessId={}&businessType={}&storeId={}&replyStatus={}&commentLevel={}&days={}&phoneId={}&userType={}&tagId={}&hasImage={}", pageNum, pageSize, businessId, businessType, storeId, replyStatus, commentLevel, days, phoneId, userType, tagId, hasImage);
        return R.data(storeCommentService.getList(pageNum, pageSize, businessId, businessType, storeId, replyStatus, commentLevel, days, phoneId, userType, tagId, hasImage));
    }

    @ApiOperation("获取最新一条评论/评价")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId", value = "业务id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "businessType", value = "业务类型(1:订单评论, 2:动态社区评论, 3:活动评论, 4:店铺打卡评论, 5:订单评价)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userType", value = "用户类型(0:商家, 其他:用户)", dataType = "String", paramType = "query")
    })
    @GetMapping("/getOne")
    public R<StoreCommentVo> getOne(Integer businessId, Integer businessType, Integer storeId) {
        log.info("StoreCommentController.getOne?businessId={}&businessType={}&storeId={}", businessId, businessType, storeId);
        return R.data(storeCommentService.getOne(businessId, businessType, storeId));
    }

    @ApiOperation("评论/评价数量和评分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId", value = "业务id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "businessType", value = "业务类型(1:订单评论, 2:动态社区评论, 3:活动评论, 4:店铺打卡评论, 5:订单评价)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "String", paramType = "query")
    })
    @GetMapping("/getCommitCountAndScore")
    public R<Map<String, Object>> getCommitCountAndScore(Integer businessId, Integer businessType, Integer storeId, String phoneId, Integer days) {
        log.info("StoreCommentController.getCommitCountAndScore?businessId={}&businessType={}&storeId={}&phoneId={}&days={}", businessId, businessType, storeId, phoneId, days);
        return R.data(storeCommentService.getCommitCountAndScore(businessId, businessType, storeId, phoneId, days));
    }

    @ApiOperation(value = "新增评论(旧)", notes = "0:成功, 1:失败, 2:文本内容异常, 3:图片内容异常")
    @ApiImplicitParams({@ApiImplicitParam(name = "multipartRequest", value = "文件", dataType = "File", paramType = "query"),
            @ApiImplicitParam(name = "businessId", value = "业务id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "businessType", value = "业务类型(1:订单评论, 2:动态社区评论, 3:活动评论)", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "replyId", value = "回复id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "commentContent", value = "评论内容", dataType = "String", paramType = "query")
    })
    @PostMapping("/userComment")
    public R<Integer> userComment(MultipartRequest multipartRequest,
                                  Integer businessId,
                                  Integer businessType,
                                  Integer storeId,
                                  Integer userId,
                                  Integer replyId,
                                  String commentContent) {
        log.info("StoreCommentController.userComment?businessId={}&businessType={}&storeId={}&userId={}&replyId={}&commentContent={}", businessId, businessType, storeId, userId, replyId, commentContent);
        Set<String> fileNameSet = multipartRequest.getMultiFileMap().keySet();
        log.info(String.valueOf(fileNameSet.size()));
        return R.data(storeCommentService.userComment(multipartRequest, businessId, businessType, storeId, userId, replyId, commentContent));
    }

    @ApiOperation(value = "新增或修改评论/评价", notes = "0:成功, 1:失败, 2:文本内容异常, 3:图片内容异常")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "multipartRequest", value = "文件", dataType = "File", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "businessId", value = "业务id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "businessType", value = "业务类型(1:订单评论, 2:动态社区评论, 3:活动评论, 4:店铺打卡评论, 5:订单评价)", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "replyId", value = "回复id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "commentContent", value = "评论内容", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "score", value = "评分", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "otherScore", value = "其他评分", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "isAnonymous", value = "是否匿名(0:否(默认), 1:是)", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "evaluationTags", value = "评价标签", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phoneId", value = "用户id", dataType = "String", paramType = "query")
    })
    @PostMapping("/saveComment")
    @Deprecated
    public R<Integer> saveComment(MultipartRequest multipartRequest, Integer id, Integer businessId, Integer businessType, Integer storeId, Integer userId, Integer replyId, String commentContent, Double score, String otherScore, Integer isAnonymous, String evaluationTags, String phoneId) {
        log.info("StoreCommentController.saveComment?id={}&businessId={}&businessType={}&storeId={}&userId={}&replyId={}&commentContent={}&score={}&otherScore={}&isAnonymous={}&evaluationTags={}&phoneId={}", id, businessId, businessType, storeId, userId, replyId, commentContent, score, otherScore, isAnonymous, evaluationTags, phoneId);
        Set<String> fileNameSet = multipartRequest.getMultiFileMap().keySet();
        log.info(String.valueOf(fileNameSet.size()));
        return R.data(storeCommentService.addComment(multipartRequest, id, businessId, businessType, storeId, userId, replyId, commentContent, score, otherScore, isAnonymous, evaluationTags, phoneId));
    }

    @ApiOperation(value = "新增或修改评论/评价(单独对商家)", notes = "0:成功, 1:失败, 2:文本内容异常, 3:图片内容异常")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "multipartRequest", value = "文件", dataType = "File", paramType = "query"),
            @ApiImplicitParam(name = "imageUrls", value = "图片url", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "businessType", value = "业务类型(1:订单评论, 2:动态社区评论, 3:活动评论, 4:店铺打卡评论, 5:订单评价,6:评论店铺)", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "replyId", value = "回复id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "commentContent", value = "评论内容", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "score", value = "评分", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "otherScore", value = "其他评分", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "isAnonymous", value = "是否匿名(0:否(默认), 1:是)", dataType = "Integer", paramType = "query")
    })
    @PostMapping("/saveCommentOnlyStore")
    public R<Integer> saveCommentOnlyStore(@RequestBody Map<String,String> map) {
//        log.info("StoreCommentController.saveComment?id={}&businessType={}&storeId={}&userId={}&replyId={}&commentContent={}&score={}&otherScore={}&isAnonymous={}", id, businessType, storeId, userId, replyId, commentContent, score, otherScore, isAnonymous);
//        log.info(String.valueOf(imageUrls));
        String imageUrls = map.get("imageUrls");
        String s = map.get("id");
        Integer id = null;
        if(null != s) {
            id =  Integer.parseInt(s);
        }
        Integer businessType = Integer.parseInt(map.get("businessType"));
        Integer storeId = Integer.parseInt(map.get("storeId"));
        Integer userId = Integer.parseInt(map.get("userId"));
        String b = map.get("replyId");
        Integer replyId = null;
        if(null != b) {
            replyId =  Integer.parseInt(b);
        }

        String commentContent = map.get("commentContent");
        Double score = Double.parseDouble(map.get("score"));
        String otherScore = map.get("otherScore");
        Integer isAnonymous = Integer.parseInt(map.get("isAnonymous"));
        return R.data(storeCommentService.saveCommentOnlyStore(imageUrls, id, businessType, storeId, userId, replyId, commentContent, score, otherScore, isAnonymous));
    }

    @ApiOperation(value = "回复率, 评价比例")
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getCommitPercent")
    public R<StoreCommitPercentVo> getCommitPercent(@RequestParam("storeId") Integer storeId) {
        return R.data(storeCommentService.getCommitPercent(storeId));
    }

    @ApiOperation(value = "删除评论/评价")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "评论主键", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/delete")
    public R<Boolean> delete(Integer id) {
        if (storeCommentService.removeById(id)) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @ApiOperation("未评价/已评价列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "1:未评价, 2:已评价", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String", paramType = "query", required = true),
    })
    @GetMapping("/getCommentOrderPage")
    public R<IPage<LifeUserOrderCommentVo>> getCommentOrderPage(Integer pageNum, Integer pageSize, Integer type, String userId) {
        log.info("StoreCommentController.getCommentOrderPage?pageNum={}&pageSize={}&type={}&userId={}", pageNum, pageSize, type, userId);
        return R.data(storeCommentService.getCommentOrderPage(pageNum, pageSize, type, userId));
    }

    @ApiOperation("获取店铺评价计数统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getAppraiseCount")
    public R<StoreCommentCountVo> getAppraiseCount(@RequestParam("storeId") Integer storeId) {
        log.info("StoreCommentController.getAppraiseCount?storeId={}", storeId);
        return R.data(storeCommentService.getAppraiseCount(storeId));
    }

    @ApiOperation("分页查询当前用户所有订单（包含商户订单和律师订单）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "订单类型：1-未评价，2-已评价", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getUserAllOrders")
    public R<IPage<UserOrderVo>> getUserAllOrders(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam("userId") Integer userId,
            @RequestParam(required = false) Integer type) {
        log.info("StoreCommentController.getUserAllOrders?pageNum={}&pageSize={}&userId={}&type={}", pageNum, pageSize, userId, type);
        return R.data(storeCommentService.getUserAllOrders(pageNum, pageSize, userId, type));
    }



}
