package com.nmgs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nmgs.config.ResultData;
import com.nmgs.service.IntegrationListService;
import com.nmgs.service.impl.IntegralSeriveImpl;
import com.nmgs.util.LogUtil;
import com.nmgs.util.PubTools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value="integral")
@Api(tags = "用户积分")
public class IntegralController {
    public IntegralSeriveImpl integralSeriveImpl;
    @Autowired
    public void setIntegralSeriveImpl(IntegralSeriveImpl integralSeriveImpl) {
        this.integralSeriveImpl = integralSeriveImpl;
    }

    public IntegrationListService integrationListService;
    @Autowired
    public void setIntegrationListService(IntegrationListService integrationListServiceT) {
        this.integrationListService = integrationListServiceT;
    }

    /**
     * 根据用户id 获取当前可用积分
     * @param openId
     * @return
     */
    @RequestMapping(value = "getCurrentIntegral",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="获取人员可用积分")
    public ResultData<JSONArray> getCurrentIntegral(@RequestParam(value = "openId") String openId){
        LogUtil.WriteLog_Info("根据微信标识ID===>"+openId+"查询用户信息开始："+ PubTools.getCurrentDate(),"IntegralController");
        JSONArray js=new JSONArray();
        JSONObject currentIntegral = this.integralSeriveImpl.getCurrentIntegral(openId);
        if(currentIntegral != null  ){
            if("-1".equals(currentIntegral.get("ID").toString())){//说明无数据
                return ResultData.fail("没有匹配的数据");
            }else{
                js.add(currentIntegral);
                LogUtil.WriteLog_Info("根据微信标识ID===>"+openId+"查询用户信息成功："+ PubTools.getCurrentDate(),"IntegralController");

            }
        }else{
            LogUtil.WriteLog_Info("根据微信标识ID===>"+openId+"查询用户信息失败："+ PubTools.getCurrentDate(),"IntegralController");
            return ResultData.fail("查询失败");
        }
        return ResultData.success("查询成功",js,js.size());
    }

    /**
     *
     * @param userId
     * @param acarNo
     * @return
     */
    @RequestMapping(value = "getIntegrationListByUserId",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="根据人员id,查询积分流水")
    public ResultData<List> getListByCarPlate(@RequestParam("userId") String userId,
                                              @RequestParam(value = "acarNo",required = false) String acarNo,
                                              @RequestParam(value = "pageSize",required = false) String pageSize,
                                              @RequestParam(value = "currentPage",required = false) String currentPage,
                                              @RequestParam(value = "payType",required = false) String payType){
        int start=0;
        int end=1000000000;
        List integrationList =null;
        List totalList =null;
        if(!PubTools.isNull(pageSize) && !PubTools.isNull(currentPage)){
            start=(Integer.parseInt(currentPage)-1)* Integer.parseInt(pageSize)+1;
            end=Integer.parseInt(currentPage)*Integer.parseInt(pageSize);
            integrationList = this.integrationListService.getIntegrationList(userId, acarNo,payType,start,end);
            totalList = this.integrationListService.getIntegrationList(userId, acarNo,payType,0,0);
        }else{
            integrationList = this.integrationListService.getIntegrationList(userId, acarNo,payType,start,end);
            totalList=integrationList;
        }

        if(integrationList != null){
            return ResultData.success("查询成功",integrationList,totalList.size());
        }else{
            return ResultData.fail("查询失败");
        }
    }

    @RequestMapping(value = "getIntegraAndMileage",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="根据车牌号获取当月积分与里程详情")
    public ResultData<List> getIntegraAndMileage(@RequestParam("userId") String userId,
                                                 @RequestParam(value = "carPlate") String carPlate,
                                                 @RequestParam(value = "currentDate") String currentDate){
        List integrationList = this.integralSeriveImpl.getIntegraAndMileage(userId, carPlate,currentDate,"","MONTH");
        if(integrationList != null){
            return ResultData.success("查询成功",integrationList,integrationList.size());
        }else{
            return ResultData.fail("查询失败");
        }
    }
    @RequestMapping(value = "getIntegraAndMileageQuarter",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="根据车牌号获取当前季度积分与里程详情")
    public ResultData<List> getIntegraAndMileageQuarter(@RequestParam("userId") String userId,
                                                 @RequestParam(value = "carPlate") String carPlate,
                                                 @RequestParam(value = "quarter") String quarter,
                                                 @RequestParam(value = "currentDate") String currentDate){
        List integrationList = this.integralSeriveImpl.getIntegraAndMileage(userId, carPlate,currentDate,quarter,"QUARTER");
        if(integrationList != null){
            return ResultData.success("查询成功",integrationList,integrationList.size());
        }else{
            return ResultData.fail("查询失败");
        }
    }
    @RequestMapping(value = "getIntegraAndMileageYear",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="根据车牌号获取当年积分与里程详情")
    public ResultData<List> getIntegraAndMileageYear(@RequestParam("userId") String userId,
                                                 @RequestParam(value = "carPlate") String carPlate,
                                                 @RequestParam(value = "currentDate") String currentDate){
        List integrationList = this.integralSeriveImpl.getIntegraAndMileage(userId, carPlate,currentDate,"","YEAR");
        if(integrationList != null){
            return ResultData.success("查询成功",integrationList,integrationList.size());
        }else{
            return ResultData.fail("查询失败");
        }
    }

    @RequestMapping(value = "getIntegraRank",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="根据日期查询月度车辆里程排名")
    public ResultData<List> getIntegraRank(@RequestParam("currentDate") String currentDate){
        List integrationList = this.integralSeriveImpl.getIntegraRank(currentDate,"","MONTH");
        if(integrationList != null){
            return ResultData.success("查询成功",integrationList,integrationList.size());
        }else{
            return ResultData.fail("查询失败");
        }
    }

    @RequestMapping(value = "getIntegraRankQuarter",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="根据日期查询季度车辆里程排名")
    public ResultData<List> getIntegraRankQuarter(@RequestParam("currentDate") String currentDate,@RequestParam("quarter") String quarter){
        List integrationList = this.integralSeriveImpl.getIntegraRank(currentDate,quarter,"QUARTER");
        if(integrationList != null){
            return ResultData.success("查询成功",integrationList,integrationList.size());
        }else{
            return ResultData.fail("查询失败");
        }
    }
    @RequestMapping(value = "getIntegraRankYear",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="根据日期查询年度车辆里程排名")
    public ResultData<List> getIntegraRankYear(@RequestParam("currentDate") String currentDate){
        List integrationList = this.integralSeriveImpl.getIntegraRank(currentDate,"","YEAR");
        if(integrationList != null){
            return ResultData.success("查询成功",integrationList,integrationList.size());
        }else{
            return ResultData.fail("查询失败");
        }
    }
    @RequestMapping(value = "getUserRank",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="根据日期查询用户里程排名")
    public ResultData<List> getUserRank(@RequestParam("currentDate") String currentDate){
        List integrationList = this.integralSeriveImpl.getUserRank(currentDate);
        if(integrationList != null){
            return ResultData.success("查询成功",integrationList,integrationList.size());
        }else{
            return ResultData.fail("查询失败");
        }
    }
    @RequestMapping(value = "getIntegraByMile",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="根据里程计算积分")
    public ResultData<List> getIntegraByMile(@RequestParam("mileAge") String mileAge,
                                             @RequestParam("vehicleType") String vehicleType){
        Double integrationList = this.integralSeriveImpl.getIntegraByMile(mileAge,vehicleType);
        JSONArray js=new JSONArray();
        JSONObject jo=new JSONObject();
        jo.put("INTEGRALVALUE",integrationList);
        js.add(jo);
        return ResultData.success("查询成功",js,js.size());
    }

    @RequestMapping(value = "getIntegraRule",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="查询积分规则")
    public ResultData<List> getIntegraRule(){
        String integrationList = this.integralSeriveImpl.getIntegraRule();
        JSONArray js=new JSONArray();
        JSONObject jo=new JSONObject();
        jo.put("INTEGRALRULE",integrationList);
        js.add(jo);
        return ResultData.success("查询成功",js,js.size());
    }
}
