package com.shining.cloud.controller.AdminStatistic;

import com.njcn.enums.app.ReturnCode;
import com.njcn.pojo.commons.DataIntegrMsgData;
import com.njcn.pojo.commons.ResponseMsgData;
import com.njcn.utils.PubUtils;
import com.njcn.utils.UserUtil;
import com.shining.cloud.enums.AdminStatistic.DataIntegrityEnums;
import com.shining.cloud.enums.information.DevMsgCodeEnum;
import com.shining.cloud.pojo.AdminStatistic.DataIntegrity;
import com.shining.cloud.pojo.AdminStatistic.Integrity;
import com.shining.cloud.pojo.information.DevMsgDetail;
import com.shining.cloud.service.AdminStatistic.DataIntegrityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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 javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static sun.security.krb5.Confounder.longValue;

/**
 * @description: 统计终端在线率
 * @author: denghuajun
 * @time: 2020-01-02 09:53:02
 **/
@RestController
@RequestMapping("/dataIntegrity")
@Api(tags = "统计数据完整性率接口")
public class DataIntegrityController {
    @Autowired
    private DataIntegrityService dataIntegrityService;
    @Autowired
    UserUtil userUtil;

    /**
     * 统计数据完整性率
     */
    @PostMapping("getDataIntegrity")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query"),
    })
    @ApiOperation(value = "统计数据完整性率入口", notes = "统计数据完整性率列表", response = ResponseMsgData.class)
    public DataIntegrMsgData deviceMsgList(String userId, HttpServletRequest request) {
        DataIntegrMsgData responseData = null;
        if (PubUtils.isBlank(userId)) {
            responseData = PubUtils.assignmentAppDataIntgrResponse(ReturnCode.RETURN_FAIL, DataIntegrityEnums.USERID_WRONG.getCode(), DataIntegrityEnums.USERID_WRONG.getMsg(), null, null, null,null);
            return responseData;
        }

        try {
        //获取相应的值
            Integrity integrity = new Integrity();
            List<Integer> lineIndex = new ArrayList<>();
            lineIndex = userUtil.ApplistLine(userId);
            if(!lineIndex.isEmpty()){
                integrity=dataIntegrityService.getIngrity(lineIndex);
                List<DataIntegrity> dataIntegrities = dataIntegrityService.getDataIntegrity(lineIndex);
                responseData = PubUtils.assignmentAppDataIntgrResponse(ReturnCode.RETURN_SUCCESS,DataIntegrityEnums.GETDATAINTEGRITY_SUCCESS.getCode(), DataIntegrityEnums.GETDATAINTEGRITY_SUCCESS.getMsg(), integrity.getTimeStart(), integrity.getTimeEnd(),integrity.getIntegrityz(),dataIntegrities);
            }else {
                responseData = PubUtils.assignmentAppDataIntgrResponse(ReturnCode.RETURN_FAIL,DataIntegrityEnums.GETDATAINTEGRITY_FALL.getCode(), DataIntegrityEnums.GETDATAINTEGRITY_FALL.getMsg(), null, null, null,null);
            }
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppDataIntgrResponse(ReturnCode.RETURN_FAIL,DataIntegrityEnums.GETDATAINTEGRITY_FALL.getCode(), DataIntegrityEnums.GETDATAINTEGRITY_FALL.getMsg(), null, null, null,null);
        }
        return responseData;
    }
}