package cn.com.countrygarden.bi.main.service.performance.impl;


import cn.com.countrygarden.bi.main.dao.performance.PerformanceElementMapper;
import cn.com.countrygarden.bi.main.model.performance.PerformanceElement;
import cn.com.countrygarden.bi.main.service.performance.PerformanceElementService;
import cn.com.countrygarden.bi.main.util.erp.StringUtil;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 指标项
 *
 * @author leo 严明斌
 * @version 2018-4-3 16:58:32
 */

@Service
public class PerformanceElementServiceImpl implements PerformanceElementService {

    @Autowired
    private PerformanceElementMapper performanceElementMapper;



    @Override
    public List<PerformanceElement> getElementId(PerformanceElement element) {
        return this.performanceElementMapper.selectFirstElement(element);
    }

    @Override
    public List<PerformanceElement> selectElementByPreElementId(long preElementId) {
        return this.performanceElementMapper.selectElementByPreElementId(preElementId);
    }

    @Override
    public void save(PerformanceElement model) {

    }

    @Override
    public void save(List<PerformanceElement> models) {

    }

    @Override
    public void deleteById(Integer id) {

    }

    @Override
    public void deleteByIds(String ids) {

    }

    @Override
    public void update(PerformanceElement model) {

    }

    @Override
    public PerformanceElement findById(Integer id) {
        return null;
    }

    @Override
    public PerformanceElement findBy(String fieldName, Object value) throws TooManyResultsException {
        return null;
    }

    @Override
    public List<PerformanceElement> findByIds(String ids) {
        return null;
    }

    @Override
    public List<PerformanceElement> findByCondition(Condition condition) {
        return null;
    }

    @Override
    public List<PerformanceElement> findAll() {
        return null;
    }

    @Override
    public List<PerformanceElement> selectBigElement() {
        return performanceElementMapper.selectBigElement();
    }

    @Override
    public List<PerformanceElement> selectUserCodePower(int sign, String userCode) {
        return performanceElementMapper.selectUserCodePower(sign, userCode);
    }

    @Override
    public PerformanceElement getElementNameById(long elementId) {
        return performanceElementMapper.selectElementNameById(elementId);
    }

    @Override
    public List<Map<String, Object>> getElementByType(String type) {

        if (StringUtil.isBlankOrEmpty(type)) {
            throw new RuntimeException("类型为空!");
        }
        if (!type.equals("1") && !type.equals("2")) {
            throw new RuntimeException("当前不支持该类型");
        }

        List<Map<String, Object>> returnList = new ArrayList<>();

        List<PerformanceElement> list = performanceElementMapper.selectElementByType(type);

        for (PerformanceElement pe : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("elementId", pe.getElementId());
            //跟前端指标名称保持一致性
            map.put("elementName", pe.getName().contains("【城】") ? pe.getName().replace("【城】", "") : pe.getName());
            returnList.add(map);
        }

        return returnList;
    }
}
