package com.hs.api.service.Impl;

import com.hs.api.common.enums.DimType;
import com.hs.api.mapper.DicDimMapper;
import com.hs.api.mapper.SerDimValueMapper;
import com.hs.api.model.DicDim;
import com.hs.api.model.SerDimValue;
import com.hs.api.model.respmodel.DimValue;
import com.hs.api.service.DicDimService;
import org.springframework.stereotype.Service;

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

@Service
public class DicDimServiceImpl implements DicDimService {
    @Resource
    private DicDimMapper dicDimMapper;
    @Resource
    private SerDimValueMapper serDimValueMapper;

    @Override
    public int add(DicDim dicDim) {
        return dicDimMapper.insert(dicDim);
    }

    @Override
    public List<DicDim> findAll() {
        return dicDimMapper.selectAll();
    }

    @Override
    public DicDim find(Long id) {
        return dicDimMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Long id) {
        return dicDimMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int logicDelete(Long id) {
        return 0;
    }

    @Override
    public int update(DicDim dicDim) {
        return dicDimMapper.updateByPrimaryKey(dicDim);
    }

    @Override
    public List<DimValue> getByPageCode(String pageCode,Long orgId,String date) {
        List<DicDim> dicDimList = dicDimMapper.selectByPageCode(pageCode);
        if(dicDimList != null && dicDimList.size() > 0){
            List<DimValue> dimValueList = new ArrayList<>();
            for(DicDim dic : dicDimList){
                DimValue dimValue = new DimValue();
                dimValue.setDicDim(dic);
                if(dic.getDimType() == DimType.DATABASE.getValue()){
                    List<SerDimValue> serDimValueList = serDimValueMapper.selectByDimId(pageCode,orgId,dic.getId(),date);
                    dimValue.setDimValues(serDimValueList);
                }
                dimValueList.add(dimValue);
            }
            return dimValueList;
        }

        return null;
    }


}
