package com.sqx.modules.integral.controller;

import com.alibaba.fastjson.JSONObject;
import com.sqx.common.utils.Result;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.integral.dto.IntegralRulesDto;
import com.sqx.modules.integral.service.UserIntegralService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Api(tags={"管理端-积分"})
@RestController
@RequestMapping("/admin/userintegral")
public class AdminUserIntegralController {

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

    @ApiOperation("管理端给用户添加积分")
    @PostMapping(value = "addAdminIntegral")
    public Result addAdminIntegral(Long userId, Integer sum, Integer type){

        return userIntegralService.addAdminIntegral(userId, sum, type);
    }

    @ApiOperation("管理端查询积分规则")
    @PostMapping(value = "queryIntegralRules")
    public Result queryIntegralRules(){
        Map<String,Object> result=new HashMap<>();
        //积分开关
        String flag =commonInfoService.findOne(436).getValue();
        //积分规则
        String rules=commonInfoService.findOne(437).getValue();
        String[] split = rules.split(",",5);
        //积分过期规则 -1永不过期
//        String overdue= commonInfoService.findOne(438).getValue();
        //每笔订单积分上限
        String maxIntegral= commonInfoService.findOne(439).getValue();
        result.put("flag",flag);
        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("overdue",overdue);
//        result.put("overdueFlag","-1".equals(overdue)?"0":"1");
        result.put("maxIntegral",maxIntegral);
        return Result.success().put("data", result);
    }

    @ApiOperation("管理端更新积分规则")
    @PostMapping(value = "updateIntegralRules")
    public Result updateIntegralRules(IntegralRulesDto irDto){
        if(Integer.parseInt(irDto.getRuleAmount1())>Integer.parseInt((irDto.getRuleMaxAmount()))){
            return Result.error("门限金额不能小于规则一金额");
        }
        String rules=irDto.getRuleAmount1()+","+irDto.getRuleValue1()+","+irDto.getRuleMaxAmount()+","+irDto.getRuleAmount2()+
                ","+irDto.getRuleValue2();
        //积分开关
        CommonInfo commonInfo1=commonInfoService.findOne(436);
        commonInfo1.setValue(irDto.getFlag());
        commonInfoService.updateBody(commonInfo1);
        //积分规则
        CommonInfo commonInfo2=commonInfoService.findOne(437);
        commonInfo2.setValue(rules);
        commonInfoService.updateBody(commonInfo2);
        //过期规则
//        CommonInfo commonInfo3=commonInfoService.findOne(438);
//        commonInfo3.setValue(irDto.getOverdue());
//        commonInfoService.updateBody(commonInfo3);
        //积分上限
        CommonInfo commonInfo4=commonInfoService.findOne(439);
        commonInfo4.setValue(irDto.getMaxIntegral());
        commonInfoService.updateBody(commonInfo4);

        return Result.success();
    }

    @ApiOperation("管理端查询积分列表")
    @PostMapping(value = "queryIntegralList")
    public Result queryIntegralList(Integer page, Integer limit,Long phone, String orderNumber, Integer classify){

        return userIntegralService.queryIntegralList(page,limit,phone, orderNumber, classify);
    }
}
