package com.yami.shop.user.api.controller;
/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.PointsRecord;
import com.yami.shop.common.bean.SysConfig;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.SysConfigService;
import com.yami.shop.bean.dto.ScoreDataDto;
import com.yami.shop.user.comment.service.UserScoreDetailService;
import com.yami.shop.user.comment.service.UserScoreLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 用户积分信息管理实现
 *
 * @author LHD
 * @version 1.0.0
 * @since 2019-03-18
 */
@RestController
@RequestMapping("/p/score")
@Api(tags = "积分中心接口")
@AllArgsConstructor
public class UserScoreController {

    private final UserScoreLogService scoreLogService;
    private final UserScoreDetailService userScoreDetailService;
    private final SysConfigService sysConfigService;


    @GetMapping("/scoreInfo")
    @ApiOperation(value = "查看积分中心信息", notes = "查看积分中心信息")
    public ResponseEntity<ScoreDataDto> scoreInfo(@RequestParam Long channelId) {
        String userId = SecurityUtils.getUser().getUserId();
        return ResponseEntity.ok(userScoreDetailService.doGetUserScore(userId, channelId));
    }


    /**
     * 分页查询积分明细
     *
     * @param page 分页对象
     * @return 分页数据
     */
    @GetMapping("/page")
    @ApiOperation(value = "查询积分明细", notes = "查询积分明细")
    public ResponseEntity<IPage<PointsRecord>> getScoreLogPage(@ModelAttribute PageParam<PointsRecord> page, @RequestParam Long channelId) {
        String userId = SecurityUtils.getUser().getUserId();
        return ResponseEntity.ok(userScoreDetailService.doGetScorePage(page, userId,channelId));
    }


    /**
     * 积分说明
     *
     * @return 积分说明
     */
    @GetMapping("/getScoreExplain")
    @ApiOperation(value = "积分说明", notes = "积分说明")
    public ResponseEntity<SysConfig> getScoreExplain() {
        SysConfig config = sysConfigService.getOne(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getParamKey, Constant.SCORE_EXPLAIN));
        if (Objects.isNull(config)) {
            return ResponseEntity.ok(new SysConfig());
        }
        return ResponseEntity.ok(config);
    }

    /**
     * 等级页展示
     *
     * @return 等级页展示
     */
    @GetMapping("/getLevelShow")
    @ApiOperation(value = "等级页展示", notes = "等级页展示")
    public ResponseEntity<String> getLevelShow() {
        String config = sysConfigService.getSysConfigObject(Constant.LEVEL_SHOW, String.class);
        if (Objects.isNull(config)) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.ok(config);
    }

    /**
     * 积分常见问题
     *
     * @return 积分常见问题
     */
    @GetMapping("/getScoreQuestion")
    @ApiOperation(value = "积分常见问题", notes = "积分常见问题")
    public ResponseEntity<SysConfig> getScoreQuestion() {
        SysConfig config = sysConfigService.getOne(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getParamKey, Constant.SCORE_QUESTION));
        if (Objects.isNull(config)) {
            return ResponseEntity.ok(new SysConfig());
        }
        return ResponseEntity.ok(config);
    }

}
