package com.mvc.controller.external;

import com.mvc.constant.BaseConstant;
import com.mvc.entity.TAffiliatedunitExample;
import com.mvc.model.*;
import com.mvc.model.external.UNCRequestModel;
import com.mvc.model.external.UNCResponseModel;
import com.mvc.service.TAffiliatedunitService;
import com.mvc.service.external.UNCRequestService;
import com.mvc.service.taizhang.CustomUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 中北大学请求接口
 */
@Api(tags = "中北大学请求接口")
@RequestMapping("/unc")
@Controller
public class UNCRequestController {
    private static Logger log = LoggerFactory.getLogger(UNCRequestController.class);

    @Resource
    private UNCRequestService uncRequestService;

    @Resource
    private CustomUserService customUserService;

    @Resource
    private TAffiliatedunitService affiliatedunitService;

    @ApiOperation("根据设备ID获取最新数据")
    @RequestMapping(value = "/queryDataByDeviceId", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel queryDataByDeviceId(@RequestBody UNCRequestModel requestModel) {
        ResultGeekerModel resultModel;
        try {
            String errMsg = "";
            if (StringUtils.isEmpty(requestModel.getAccessKey())) {
                errMsg = "accessKey不能为空";
                log.info("中北接口:{}_accessKey不能为空:{}", "queryDataByDeviceId", requestModel.getAccessKey());
            }
            CustomUserModel userModel = customUserService.findCustomUserModelByUserId(requestModel.getAccessKey());
            if (userModel == null || !userModel.getUseFlag().equals(BaseConstant.VALID_FLAG)) {
                errMsg = "accessKey无效";
            }

            if (StringUtils.isEmpty(requestModel.getDeviceId())) {
                errMsg = "deviceId不能为空";
            }

            TAffiliatedunitExample example = new TAffiliatedunitExample();
            example.createCriteria().andUnitAreaEqualTo(requestModel.getDeviceId());
            List<TAffiliatedunitModel> unitList = affiliatedunitService.selectByExample(example);

            if (unitList == null || unitList.size() == 0) {
                errMsg = "deviceId不存在";
            }

            if (!unitList.get(0).getUnitCode().equals(userModel.getUnitCode()) && !unitList.get(0).getCreateUnitCode().equals(userModel.getUnitCode())) {
                errMsg = "无此设备查询权限";
            }

            if (StringUtils.isNotEmpty(errMsg)) {
                resultModel = new ResultGeekerModel(errMsg);
            } else {
                UNCResponseModel res = uncRequestService.queryDataByAffiliatedUnit(unitList.get(0));
                resultModel = new ResultGeekerModel(res);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("分页获取账户下所有表计最新数据")
    @RequestMapping(value = "/queryDataByPage", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel queryDataByPage(@RequestBody UNCRequestModel requestModel) {
        ResultGeekerModel resultModel;
        try {

            String errMsg = "";
            if (StringUtils.isEmpty(requestModel.getAccessKey())) {
                errMsg = "accessKey不能为空";
                log.info("中北接口:{}_accessKey不能为空:{}", "queryDataByPage", requestModel.getAccessKey());
            }
            CustomUserModel userModel = customUserService.findCustomUserModelByUserId(requestModel.getAccessKey());
            if (userModel == null || !userModel.getUseFlag().equals(BaseConstant.VALID_FLAG)) {
                errMsg = "accessKey无效";
            }
            if (requestModel.getPageNum() == null || requestModel.getPageNum() <= 0) {
                errMsg = "pageNum无效";
            }

            if (StringUtils.isNotEmpty(errMsg)) {
                resultModel = new ResultGeekerModel(errMsg);
            } else {
                ResPageModel res = uncRequestService.queryDataByPage(requestModel, userModel);
                resultModel = new ResultGeekerModel(res);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    /*@ApiOperation("分页获取账户下所有表计最新数据")
    @RequestMapping(value = "/queryDataByPage", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel queryDataByPage(@RequestBody UNCRequestModel requestModel) {
        ResultGeekerModel resultModel;
        try {
            String errMsg = "";
            if (StringUtils.isEmpty(requestModel.getAccessKey())) {
                errMsg = "accessKey不能为空";
            }
            CustomUserModel userModel = customUserService.findCustomUserModelByUserId(requestModel.getAccessKey());
            if (userModel == null || !userModel.getUseFlag().equals(BaseConstant.VALID_FLAG)) {
                errMsg = "accessKey无效";
            }
            if (requestModel.getPageNum() == null || requestModel.getPageNum() <= 0) {
                errMsg = "pageNum无效";
            }
            if (StringUtils.isNotEmpty(errMsg)) {
                resultModel = new ResultGeekerModel(errMsg);
            } else {
                ResPageModel res = uncRequestService.queryDataByPage(requestModel, userModel);
                resultModel = new ResultGeekerModel(res);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }*/

    /*@ApiOperation("根据设备ID获取最新数据")
    @RequestMapping(value = "/queryDataByDeviceId", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel queryDataByDeviceId(@RequestBody UNCRequestModel requestModel) {
        ResultGeekerModel resultModel;
        try {
            String errMsg = "";
            if (StringUtils.isEmpty(requestModel.getAccessKey())) {
                errMsg = "accessKey不能为空";
            }
            CustomUserModel userModel = customUserService.findCustomUserModelByUserId(requestModel.getAccessKey());
            if (userModel == null || !userModel.getUseFlag().equals(BaseConstant.VALID_FLAG)) {
                errMsg = "accessKey无效";
            }

            if (StringUtils.isEmpty(requestModel.getDeviceId())) {
                errMsg = "deviceId不能为空";
            }

            StationAccModel accModel = stationAccService.findStationAccByUserDefinedCode(requestModel.getDeviceId());

            if (accModel == null) {
                errMsg = "deviceId不存在";
            }

            if (!accModel.getUnitCode().equals(userModel.getUnitCode()) && !accModel.getVisUnitCodes().contains(userModel.getUnitCode())) {
                errMsg = "无此设备查询权限";
            }

            if (StringUtils.isNotEmpty(errMsg)) {
                resultModel = new ResultGeekerModel(errMsg);
            } else {
                UNCResponseModel res = uncRequestService.queryDataByDeviceId(accModel);
                resultModel = new ResultGeekerModel(res);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }*/
}
