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.entity.ZstbInfo;
import com.nantian.cloud.jljc.mapper.ZstbInfoMapper;
import com.nantian.cloud.jljc.service.ZstbInfoService;
import com.nantian.cloud.jljc.tools.LpExcelUtil;
import com.nantian.sc.cloud.common.data.datascope.DataScope;
import com.nantian.sc.cloud.common.security.service.ScUser;
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("zstbInfoService")
public class ZstbInfoServiceImpl extends ServiceImpl<ZstbInfoMapper, ZstbInfo> implements ZstbInfoService {

    @Autowired
    private ZstbInfoMapper zstbInfoMapper;

    @Override
    public Page<ZstbInfo> getPage(Page page,ZstbInfo zstbInfo) {
        return zstbInfoMapper.getPage(page, zstbInfo,new DataScope());
    }

    @Override
    public List<ZstbInfo> excelImport(MultipartFile file,String date) throws Exception {
//        try {
//            return LpExcelUtil.infoToList(file,date);
//        } catch (Exception e) {
//            throw new Exception(e.getMessage());
//        }
        return LpExcelUtil.infoToList(file,date);
    }

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