package com.ruoyi.wx.web.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.framework.recovery.domain.RecContent;
import com.ruoyi.framework.recovery.domain.RecContentUser;
import com.ruoyi.wx.web.domain.dto.WxContentDto;
import com.ruoyi.wx.web.domain.vo.SaleInfoVo;
import com.ruoyi.wx.web.service.IWxHomeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 首页服务
 */
@RestController
@RequestMapping("/wx/home")
@Slf4j
public class WxHomeController {
    @Autowired
    private IWxHomeService wxHomeService;

//    @ApiOperation(value = "查询首页所有信息")
    @GetMapping("/index")
    public AjaxResult getIndex() {
        try {
            return wxHomeService.getIndex();
        } catch (InterruptedException | ExecutionException e) {
            return AjaxResult.error(e.getMessage(), e);
        }
    }

//    @ApiOperation(value = "发售信息详情", notes = "根据id查询详情内容")
    @GetMapping("/sale/info/{id}/{storeIds}")
    public AjaxResult getSaleInfo(@PathVariable("id") Long id, @PathVariable("storeIds") String storeIds) {
        try {
            List<SaleInfoVo> infoList = wxHomeService.getSaleInfo(id, storeIds);
            return AjaxResult.success(infoList);
        } catch (Exception e) {
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }

    /**
     * 销售日历
     *
     */
    @GetMapping("/sale/calendar")
    public AjaxResult saleCalendar() {
        try {
            List<SaleInfoVo> infoList = wxHomeService.getSaleCalendar();
            return AjaxResult.success(infoList);
        } catch (Exception e) {
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }

    /**
     * 点赞
     *
     * @param id 评论Id
     * @return 结果集
     */
    @GetMapping("/thumbs/{id}")
    public AjaxResult thumbs(@PathVariable("id") Long id) {
        try {
            Long thumbs = wxHomeService.thumbs(id);
            return AjaxResult.success(thumbs);
        } catch (Exception e) {
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }

    /**
     * 当前用户点过的赞
     *
     */
    @GetMapping("/thumbs")
    public AjaxResult userThumbs() {
        try {
            List<RecContentUser> thumbs = wxHomeService.userThumbs();
            return AjaxResult.success(thumbs);
        } catch (Exception e) {
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }

    @PostMapping("/add/content")
    public AjaxResult thumbs(@RequestBody WxContentDto dto) {
        try {
            int thumbs = wxHomeService.addRecContent(dto);
            return AjaxResult.success(thumbs);
        } catch (Exception e) {
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }
}
