package com.ho.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ho.business.constant.DeviceTypeConstant;
import com.ho.business.entity.PlanningIssueComm;
import com.ho.business.entity.TabSort;
import com.ho.business.mapper.TabSortMapper;
import com.ho.business.service.TabSortService;
import com.ho.business.vo.DeviceTransfer;
import com.ho.business.vo.req.BusinessModelIssue;
import com.ho.business.vo.resp.tabSort.TabSortExcel;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TabSortServiceImpl implements TabSortService {

    @Autowired
    private TabSortMapper tabSortMapper;

    @Autowired
    RedisService redisService;

    @Override
    public List<TabSort> queryIssueDatas(TabSort tabSort) {
        return tabSortMapper.queryIssueDatas(tabSort);
    }

    @Override
    public List<PlanningIssueComm> queryPlanControlIssueData(String deviceType) {
        return tabSortMapper.queryPlanControlIssueData(deviceType);
    }

    @Override
    public String getStationSn(Integer stationId) {
        return tabSortMapper.getStationSn(stationId);
    }

    @Override
    public JSONObject getTabModel(BusinessModelIssue issue) {

        if( issue == null || issue.getStationId() == null ){
            throw new BusinessException(BaseResponseCode.NON_DATA);
        }
        String deviceType = DeviceTypeConstant.PLAN_CURVE.PALN_CONTROL_DEVICE_TYPE+issue.getStationId();
        List<PlanningIssueComm> planningIssueComm = queryPlanControlIssueData(deviceType);
        JSONObject jsonObject = new JSONObject();
        for (PlanningIssueComm p:planningIssueComm) {
            DeviceTransfer deviceTransfer = (DeviceTransfer)redisService.hget(p.getDeviceType() + ":" + p.getStationId() + ":" + p.getSrcId(), p.getTypeCol());
            if(deviceTransfer!=null){
                jsonObject.put(p.getModelCol(),deviceTransfer.getValue());
            }
        }
        return jsonObject;
    }

    @Override
    public TabSort queryTabSortById(Integer id) {
        return tabSortMapper.queryTabSortById(id);
    }

    @Override
    public List<TabSortExcel> queryExcelDatas(TabSort tabSort) {
        return tabSortMapper.queryExcelDatas(tabSort);
    }

    @Override
    public String getColNameByCol(String col,String deviceType) {
        return tabSortMapper.getColNameByCol(col,deviceType);
    }
}
