package com.sqx.modules.integral.controller.app;

import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.integral.service.UserIntegralService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(tags={"用户端-积分"})
@RestController
@RequestMapping("/app/userintegral")
public class UserIntegralController {

    @Autowired
    private UserIntegralService userIntegralService;
    @Autowired
    private CommonInfoService commonInfoService;

    @Login
    @ApiOperation("判断用户是否已签到")
    @GetMapping(value = "/isSignIn")
    public Result isSignIn(@RequestAttribute Long userId){

        return userIntegralService.isSignIn(userId);
    }

    @Login
    @ApiOperation("计算今日签到应得积分数")
    @GetMapping(value = "/todayIntegral")
    public Result todayIntegral(@RequestAttribute Long userId){

        return userIntegralService.todayIntegral(userId);
    }

    @Login
    @ApiOperation("查看连续签到天数")
    @GetMapping(value = "continuousDay")
    public Result continuousDay(@RequestAttribute Long userId){

        return userIntegralService.continuousDay(userId);
    }


    @Login
    @ApiOperation("每日签到")
    @GetMapping(value = "/signIn")
    public Result signIn(@RequestAttribute Long userId){

        return userIntegralService.signIn(userId);
    }

    @Login
    @ApiOperation("查看积分")
    @GetMapping(value = "/selectUserIntegral")
    public Result selectUserIntegral(@RequestAttribute Long userId){

        return userIntegralService.selectUserIntegral(userId);
    }

    @Login
    @ApiOperation("用户积分、优惠券、钱包余额")
    @GetMapping(value = "/findUserMessage")
    public Result findUserMessage(@RequestAttribute Long userId){

        return userIntegralService.findUserMessage(userId);
    }

    @Login
    @ApiOperation("根据计算可获得积分数")
    @GetMapping(value = "/getIntegralByMoney")
    public Result getIntegralByMoney(String money){
        //积分规则
        Map<String,Object> result=new HashMap<>();
        String flag=commonInfoService.findOne(436).getValue();
        String rules=commonInfoService.findOne(437).getValue();
        String maxIntegral= commonInfoService.findOne(439).getValue();
        String[] split = rules.split(",",5);
        int a= Integer.parseInt(split[0]);
        int b = Integer.parseInt(split[1]);
        int c= Integer.parseInt(split[2]);
        int d = Integer.parseInt(split[3]);
        int e= Integer.parseInt(split[4]);
        double amount= Double.parseDouble(money);
        int intergral=0;
        if (amount>a){
            if(amount<c){
                intergral=intergral+(int)(amount/a)*b;
            } else if (amount>=c+d){
                int count= (int) ((amount-c)/d);
                intergral=intergral+(c/a)*b+count*e;
            }else{
                intergral=intergral+(c/a)*b;
            }
        }
        int max =Integer.parseInt(maxIntegral);
        result.put("ruleAmount1",split[0]);
        result.put("ruleValue1",split[1]);
        result.put("ruleMaxAmount",split[2]);
        result.put("ruleAmount2",split[3]);
        result.put("ruleValue2",split[4]);
        result.put("maxIntegral",maxIntegral);
        result.put("flag",flag);
        result.put("intergral", Math.min(max, intergral));
        return Result.success().put("data", result);
    }

}
