package co.yixiang.yshop.module.member.controller.app.signinrecord;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.util.date.LocalDateTimeUtils;
import co.yixiang.yshop.framework.security.core.annotations.PreAuthenticated;
import co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils;
import co.yixiang.yshop.module.member.controller.admin.integralrule.vo.IntegralRuleExportReqVO;
import co.yixiang.yshop.module.member.controller.admin.integralrule.vo.IntegralRuleRespVO;
import co.yixiang.yshop.module.member.controller.app.signinrecord.vo.SignInInfoVO;
import co.yixiang.yshop.module.member.convert.integralrule.IntegralRuleConvert;
import co.yixiang.yshop.module.member.dal.dataobject.integralrule.IntegralRuleDO;
import co.yixiang.yshop.module.member.service.integralrule.IntegralRuleService;
import co.yixiang.yshop.module.member.service.signinrecord.SignInRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author pepis
 * @apiNote app签到
 **/
@RestController
@RequestMapping("/member/sign-in-record")
@Tag(name = "app-签到")

public class AppSignInRecordController {
    @Resource
    private SignInRecordService signInRecordService;
    @Resource
    private IntegralRuleService integralRuleService;

    @GetMapping("signInInfo")
    @Operation(summary = "签到信息")
    @PreAuthenticated
    public CommonResult<SignInInfoVO> signInInfo() {
        LocalDateTime startTime = LocalDateTimeUtils.weekStartTime();
        LocalDateTime endTime = LocalDateTimeUtils.weekEndTime();
        SignInInfoVO vo = signInRecordService.signInInfo(SecurityFrameworkUtils.getLoginUserId(), startTime, endTime);
        return CommonResult.success(vo);
    }

    @GetMapping("signIn")
    @Operation(summary = "签到")
    @PreAuthenticated
    public CommonResult<BigDecimal> signIn() {
        BigDecimal integral = signInRecordService.signIn(SecurityFrameworkUtils.getLoginUserId());
        return CommonResult.success(integral);
    }

    @GetMapping("integralRule")
    @Operation(summary = "每日任务")
    @PreAuthenticated
    public CommonResult<List<IntegralRuleRespVO>> integralRule() {
        IntegralRuleExportReqVO reqVO = new IntegralRuleExportReqVO();
        reqVO.setEnable(Boolean.TRUE);
        List<IntegralRuleDO> list = integralRuleService.getIntegralRuleList(reqVO);
        return CommonResult.success(IntegralRuleConvert.INSTANCE.convertList(list));
    }
}
