package com.fs.app.controller.store;


import com.fs.app.annotation.Login;
import com.fs.app.controller.AppBaseController;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.hisStore.domain.FsIntegralGoodsScrm;
import com.fs.hisStore.domain.FsIntegralOrderScrm;
import com.fs.hisStore.domain.FsUserScrm;
import com.fs.hisStore.param.FsIntegralGoodsListUParam;
import com.fs.hisStore.param.FsIntegralOrderCreateParam;
import com.fs.hisStore.param.FsIntegralOrderListUParam;
import com.fs.hisStore.param.FsUserIntegralLogsListUParam;
import com.fs.hisStore.service.*;
import com.fs.hisStore.vo.FsIntegralGoodsListUVO;
import com.fs.hisStore.vo.FsIntegralOrderListUVO;
import com.fs.hisStore.vo.FsUserIntegralLogsListUVO;
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="/store/app/integral")
public class IntegralScrmController extends AppBaseController {

    @Autowired
    private IFsIntegralGoodsScrmService goodsService;
    @Autowired
    private IFsUserIntegralLogsScrmService userIntegralLogsService;
    @Autowired
    private IFsIntegralOrderScrmService integralOrderService;
    @Autowired
    private IFsUserScrmService userService;
    @Autowired
    private IFsUserSignScrmService 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){
        FsIntegralGoodsScrm 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){
        FsIntegralOrderScrm 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){
        FsUserScrm 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){
        FsUserScrm user=userService.selectFsUserByUserId(Long.parseLong(getUserId()));
        Long integral=userSignService.sign(user);
        return R.ok("签到获得" + integral + "积分");
    }
}
