package com.supplier.service.impl;

import com.supplier.mapper.HomePageMapper;
import com.supplier.mapper.PersonalMapper;
import com.supplier.model.HomePageIndex;
import com.supplier.service.HomePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class HomePageServiceImpl implements HomePageService {

    @Autowired
    private HomePageMapper homePageMapper;
    @Autowired
    private PersonalMapper personalMapper;

    @Override
    public Map<String, Object> getHeadData(HomePageIndex homePageIndex) {
        Map<String, Object> map = new HashMap<>();
        try{
            Map<String, Object> mapList = new HashMap<>();
            //本年报价单每月数据统计
            int[] thisYearOffer = new int[12];//月份数据数组
            for(Map<String, Object> mapVal : homePageMapper.getThisYearOffer(homePageIndex)){
                thisYearOffer[(int)mapVal.get("thisYearMonth") - 1] = (int) mapVal.get("releaseNum");
            }
            //本年成交单每月数据统计
            int[] thisYearClinch = new int[12];//月份数据数组
            for(Map<String, Object> mapVal : homePageMapper.getThisYearClinch(homePageIndex)){
                thisYearClinch[(int)mapVal.get("thisYearMonth") - 1] = (int) mapVal.get("releaseNum");
            }
            mapList.put("thisYearOffer", thisYearOffer);
            mapList.put("thisYearClinch", thisYearClinch);
            map.put("offer", homePageMapper.getHeadData(homePageIndex)); //报价单
            map.put("income", homePageMapper.getHeadIncome(homePageIndex)); //收入
            map.put("user", personalMapper.getPresonalMessage(homePageIndex.getCode())); //用户信息
            map.put("messageRemid", homePageMapper.getMessageRemid(homePageIndex.getCode())); //用户信息
            map.put("map", homePageMapper.getHeadMap());
            map.put("thisYearOffer", mapList); //本年报价单统计
            map.put("msg", "200");
        } catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
            map.put("errMsg", "服务器请求异常，请稍后再试");
        }
        return map;
    }
}
