package cn.com.ctop.crawler.modules.pangolin.service.impl;

import cn.com.ctop.common.module.vo.PangolinReportAppMonthVO;
import cn.com.ctop.crawler.modules.pangolin.entity.PangolinReportBillMonth;
import cn.com.ctop.crawler.modules.pangolin.mapper.PangolinReportBillMonthMapper;
import cn.com.ctop.crawler.modules.pangolin.service.PangolinReportBillMonthService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class PangolinReportBillMonthServiceImpl extends ServiceImpl<PangolinReportBillMonthMapper, PangolinReportBillMonth> implements PangolinReportBillMonthService {
    @Autowired
    private PangolinReportBillMonthMapper pangolinReportBillMonthMapper;

    @Override
    public PageInfo<PangolinReportAppMonthVO> checkAppList(String userId, JSONObject data) {
        Integer pageNo = data.getInteger("pageNo");
        if(null == pageNo||pageNo == 0){
            pageNo = 1;
        }
        Integer pageSize = data.getInteger("pageSize");
        if(null == pageSize||pageSize == 0){
            pageSize = 10;
        }
        Integer appId = data.getInteger("appId");
        if(null == appId||appId == 0){
            appId = null;
        }
        List<Long>projectIds = null;
        List<String>accountNames = null;
        JSONArray accountNameArray = data.getJSONArray("accountName");
        JSONArray projectIdArray = data.getJSONArray("projectId");
        if(null!=accountNameArray&&!accountNameArray.isEmpty()){
            accountNames = new ArrayList<>();
            for(int i=0;i<accountNameArray.size();i++){
                accountNames.add(accountNameArray.getString(i));
            }
        }
        if(null!=projectIdArray&&!projectIdArray.isEmpty()){
            projectIds = new ArrayList<>();
            for(int i=0;i<projectIdArray.size();i++){
                projectIds.add(projectIdArray.getLong(i));
            }
        }
        String startDate = data.getString("startDate");
        String endDate = data.getString("endDate");
        String operateSystem = data.getString("operateSystem");
        if(null!=operateSystem&& "all".equals(operateSystem.trim())){
            operateSystem = null;
        }
        PageHelper.startPage(pageNo,pageSize);
        List<PangolinReportAppMonthVO> list = pangolinReportBillMonthMapper.checkAppList(appId,userId,startDate,endDate,operateSystem,projectIds,accountNames);
        PageInfo<PangolinReportAppMonthVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
