package com.fs.app.controller;


import com.fs.app.annotation.Login;
import com.fs.common.core.domain.R;
import com.fs.store.domain.FsUser;
import com.fs.store.domain.FsUserBill;
import com.fs.store.enums.BillDetailEnum;
import com.fs.store.param.FsUserBillQueryParam;
import com.fs.store.service.IFsUserBillService;
import com.fs.store.service.IFsUserService;
import com.fs.store.service.IFsUserSignService;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Api("签到接口")
@RestController
@RequestMapping(value="/app/sign")
public class UserSignController extends  AppBaseController {

    @Autowired
    private IFsUserService userService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IFsUserSignService signService;
    @Autowired
    private IFsUserBillService billService;

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

    @ApiOperation("获取积分记录")
    @GetMapping("/getIntegral")
    public R getIntegral(FsUserBillQueryParam param){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        param.setUserId(param.getUserId());
        param.setCategory(BillDetailEnum.CATEGORY_2.getValue());
        List<FsUserBill> list=billService.selectFsUserBillListQuery(param);
        PageInfo<FsUserBill> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @Login
    @ApiOperation("签到")
    @PostMapping("/sign")
    public R sign(){
        FsUser user=userService.selectFsUserById(Long.parseLong(getUserId()));
        Long integral=signService.sign(user);
        return R.ok("签到获得" + integral + "积分");
    }

}
