package com.nantian.cloud.jljc.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jljc.entity.UseEngergy;
import com.nantian.cloud.jljc.mapper.UseEngergyMapper;
import com.nantian.cloud.jljc.service.UseEngergyService;
import com.nantian.cloud.jljc.tools.LpExcelUtil;
import com.nantian.sc.cloud.common.data.datascope.DataScope;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

@Service("useEngergyService")
public class UseEngergyServiceImpl extends ServiceImpl<UseEngergyMapper, UseEngergy> implements UseEngergyService {

    @Autowired
    private UseEngergyMapper useEngergyMapper;

    @Override
    public List<UseEngergy> getPage(Page page, UseEngergy useEngergy) {
        return useEngergyMapper.getPage(page, useEngergy,new DataScope());
    }

    @Override
    public List<UseEngergy> excelImport(MultipartFile file, String datel) throws Exception {
        try {
            return LpExcelUtil.useExcelToList(file,datel);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public List<String> getArea() {
        String deptId = SecurityUtils.getUser().getDeptId().toString();
        if("530000".equals(deptId)){ //省级
            deptId = deptId.substring(0,2);
        }else if ("00".equals(deptId.substring(deptId.length() - 2, deptId.length()))){ //州市级
            deptId = deptId.substring(0,4);
        }else {
            deptId = deptId;
        }
        return useEngergyMapper.getArea(deptId);
    }

    @Override
    public List<UseEngergy> getTotal(UseEngergy useEngergy) {
        if (StringUtils.isBlank(useEngergy.getScYear()) || StringUtils.isBlank(useEngergy.getScMonth())) {
            return null; //必须传年份和月度
        }
        List<UseEngergy> totalList = new ArrayList<>();
        String deptId = SecurityUtils.getUser().getDeptId().toString();
        if("530000".equals(deptId)){ //省级
            totalList = useEngergyMapper.getTotal(useEngergy,deptId.substring(0,2));
            List<UseEngergy> tmp = useEngergyMapper.getTotalProv(useEngergy,deptId.substring(0,2));
            totalList.addAll(tmp);
        }else if (deptId.substring(deptId.length() - 2, deptId.length()).equals("00")){ //州市级
            totalList = useEngergyMapper.getTotal(useEngergy,deptId.substring(0,4));
        }else { //区县级
            totalList = useEngergyMapper.getTotal(useEngergy, deptId);
        }
        return totalList;
    }

    @Override
    public List<String> getUnit() {
        return useEngergyMapper.getUnit();
    }
}
