package com.fs.app.controller;


import com.fs.app.annotation.Login;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.store.domain.*;
import com.fs.store.param.*;
import com.fs.store.service.*;
import com.fs.store.vo.*;
import com.fs.system.service.ISysConfigService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api("积分接口")
@RestController
@RequestMapping(value="/app/integral")
public class IntegralController extends  AppBaseController {

    @Autowired
    private IFsIntegralGoodsService goodsService;
    @Autowired
    private IFsUserIntegralLogsService userIntegralLogsService;
    @Autowired
    private IFsIntegralOrderService integralOrderService;
    @Autowired
    private IFsUserService userService;
    @Autowired
    private IFsUserSignService userSignService;

    @Autowired
    private ISysConfigService configService;
    @ApiOperation("获取积分商品列表")
    @GetMapping("/getIntegralGoodsList")
    public R getIntegralGoodsList(FsIntegralGoodsListUParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsIntegralGoodsListUVO> list=goodsService.selectFsIntegralGoodsListUVO(param);
        PageInfo<FsIntegralGoodsListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @ApiOperation("获取积分商品详情")
    @GetMapping("/getIntegralGoodsById")
    public R getIntegralGoodsById(@RequestParam("goodsId")Long goodsId, HttpServletRequest request){
        FsIntegralGoods goods=goodsService.selectFsIntegralGoodsByGoodsId(goodsId);
        return R.ok().put("data",goods);
    }

    @Login
    @ApiOperation("获取积分记录")
    @GetMapping("/getUserIntegralLogsList")
    public R getUserIntegralLogsList(FsUserIntegralLogsListUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsUserIntegralLogsListUVO> list=userIntegralLogsService.selectFsUserIntegralLogsListUVO(param);
        PageInfo<FsUserIntegralLogsListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @Login
    @ApiOperation("获取积分订单列表")
    @GetMapping("/getIntegralOrderList")
    public R getIntegralOrderList(FsIntegralOrderListUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsIntegralOrderListUVO> list=integralOrderService.selectFsIntegralOrderListUVO(param);
        PageInfo<FsIntegralOrderListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @Login
    @ApiOperation("获取积分订单详情")
    @GetMapping("/getIntegralOrderById")
    public R getIntegralOrderById(@RequestParam("orderId")Long orderId, HttpServletRequest request){
        FsIntegralOrder order=integralOrderService.selectFsIntegralOrderByOrderId(orderId);
        return R.ok().put("data",order);
    }


    @Login
    @ApiOperation("创建订单")
    @PostMapping("/createOrder")
    @RepeatSubmit
    public R createOrder(@Validated @RequestBody FsIntegralOrderCreateParam param, HttpServletRequest request){
        param.setUserId(Long.parseLong(getUserId()));
        return integralOrderService.createOrder(param);
    }

    @Login
    @ApiOperation("获取用户签到记录")
    @GetMapping("/getUserSign")
    public R getUserSign(HttpServletRequest request){
        FsUser user=userService.selectFsUserByUserId(Long.parseLong(getUserId()));
        //获取签到配置
        String json=configService.selectConfigByKey("store.sign");
        //判断用户昨天是否签到过
        Long signNum=userSignService.getSign(user);
        Boolean isDaySign=userSignService.isDaySign(user);
        return R.ok().put("isDaySign", isDaySign).put("signNum", signNum).put("integral",user.getIntegral().intValue()).put("sign", json);
    }
    @Login
    @ApiOperation("签到")
    @PostMapping("/sign")
    public R sign(HttpServletRequest request){
        FsUser user=userService.selectFsUserByUserId(Long.parseLong(getUserId()));
        Long integral=userSignService.sign(user);
        return R.ok("签到获得" + integral + "积分");
    }
}
