package com.ho.business.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ho.business.entity.ElecMeterValue;
import com.ho.business.entity.HisCurveRelate;
import com.ho.business.entity.TranslateName;
import com.ho.business.mapper.HisCurveRelateMapper;
import com.ho.business.service.DynamicConfigService;
import com.ho.business.service.HisCurveRelateService;
import com.ho.business.vo.req.modelType.ModelTypeQueryReq;
import com.ho.business.vo.req.point.HisPointReqVo;
import com.ho.common.tools.entity.HisPointCurveReqVo;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.util.PageResult;
import com.ho.common.tools.util.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author gyan
 * @desc: 实现类
 * @DateTime: 2023/6/20 11:20
 */
@Service
@Slf4j
public class HisCurveRelateServiceImpl implements HisCurveRelateService {

    @Autowired
    Snowflake snowflake;

    @Autowired
    private HisCurveRelateMapper hisCurveRelateMapper;

    @Autowired
    private DynamicConfigService dynamicConfigService;


    @Override
    public void add(HisPointReqVo hisPointReqVo, SimpleUser user) {
        List<HisCurveRelate> list =new ArrayList<>();
        long modelId = snowflake.nextId();
        if (!hisPointReqVo.getList().isEmpty()) {
            for (HisPointCurveReqVo hisPointCurveReqVo : hisPointReqVo.getList()) {
                HisCurveRelate hisCurveRelate = new HisCurveRelate();
                hisCurveRelate.setCol(hisPointCurveReqVo.getCol());
                hisCurveRelate.setDeviceType(hisPointCurveReqVo.getDeviceType());
                hisCurveRelate.setModelId(String.valueOf(modelId));
                hisCurveRelate.setColName(hisPointCurveReqVo.getColName());
                TranslateName colTrans = dynamicConfigService.getNameLanguage(hisPointCurveReqVo.getColName());
                hisCurveRelate.setColNameEn(colTrans.getNameEn());
                hisCurveRelate.setColNameFra(colTrans.getNameFra());
                hisCurveRelate.setColNameSpa(colTrans.getNameSpa());
                hisCurveRelate.setColNameDe(colTrans.getNameDe());
                hisCurveRelate.setColNamePl(colTrans.getNamePl());
                hisCurveRelate.setColNameAra(colTrans.getNameAra());
                hisCurveRelate.setStationId(hisPointCurveReqVo.getStationId());
                //modelname 不能重复
                //hisCurveRelateMapper.selectName(hisPointReqVo.getModelName());
                hisCurveRelate.setModelName(hisPointReqVo.getModelName());
                TranslateName modelTrans = dynamicConfigService.getNameLanguage(hisPointReqVo.getModelName());
                hisCurveRelate.setModelNameEn(modelTrans.getNameEn());
                hisCurveRelate.setModelNameFra(modelTrans.getNameFra());
                hisCurveRelate.setModelNameSpa(modelTrans.getNameSpa());
                hisCurveRelate.setModelNameDe(modelTrans.getNameDe());
                hisCurveRelate.setModelNamePl(modelTrans.getNamePl());
                hisCurveRelate.setModelNameAra(modelTrans.getNameAra());
                hisCurveRelate.setSrcId(hisPointCurveReqVo.getSrcId());
                hisCurveRelate.setUserId(user.getUserId());
                list.add(hisCurveRelate);
            }
        }
        hisCurveRelateMapper.insertBatch(list);
    }


    @Override
    public PageResult<HisCurveRelate> pageSelectAll(ModelTypeQueryReq vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        //加一个用户的条件
        HisCurveRelate queryVo = new HisCurveRelate();
        queryVo.setStationId(vo.getStationId());
        queryVo.setUserId(vo.getUserId());
        queryVo.setLang(vo.getLang());
        List<HisCurveRelate> hisCurveRelateList = hisCurveRelateMapper.selectAll(queryVo);
        Map<String, List<HisCurveRelate>> chargeMap = hisCurveRelateList.stream().collect(Collectors.groupingBy(HisCurveRelate::getModelId));
        List<HisCurveRelate> resultList = new ArrayList<>();
        for(Map.Entry<String, List<HisCurveRelate>> m:chargeMap.entrySet()){
            HisCurveRelate hisCurveRelate = new HisCurveRelate();
            hisCurveRelate.setModelId(m.getKey());
            hisCurveRelate.setModelName(m.getValue().get(0).getModelName());
            hisCurveRelate.setDeviceType(m.getValue().get(0).getDeviceType());
            hisCurveRelate.setList(m.getValue());
            resultList.add(hisCurveRelate);
        }
        return PageUtils.getPageResult(new PageInfo<>(resultList));
    }

    @Override
    public int delete(String modelId) {
        return hisCurveRelateMapper.delete(modelId);
    }

    @Override
    public boolean hasModelName(String modelName, Integer stationId,String userId) {
        HisCurveRelate queryVo = new HisCurveRelate();
        queryVo.setStationId(stationId);
        queryVo.setModelName(modelName);
        queryVo.setUserId(userId);
        List<HisCurveRelate> hisCurveRelateList = hisCurveRelateMapper.selectAll(queryVo);
        if (CollectionUtils.isNotEmpty(hisCurveRelateList)){
            return true;
        }else {
            return false;
        }
    }
}
