package org.jeecg.modules.performance.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.jeecg.modules.performance.entity.PerformanceQuarter;
import org.jeecg.modules.performance.mapper.PerformanceQuarterMapper;
import org.jeecg.modules.performance.service.IPerformanceQuarterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PerformanceQuarterServiceImpl extends ServiceImpl<PerformanceQuarterMapper, PerformanceQuarter> implements IPerformanceQuarterService {

    @Autowired
    PerformanceQuarterMapper performanceQuarterMapper;

    /**
     *  获取绩效表里有的年份季度
     *
     */
    @Override
    public Map<String, Set<String>> getYearQuarter() {
        Map<String, Set<String>> result= new HashMap<>();
        List<JSONObject> jsonObjectList=performanceQuarterMapper.queryYearAndQuarter();
        if(jsonObjectList.isEmpty()){
            return result;
        }
        jsonObjectList.forEach(it->{
            if(result.get(it.getInteger("year").toString())==null){
                Set<String> quarter= new HashSet<>();
                quarter.add(it.getString("quarter"));
                result.put(it.getInteger("year").toString(),quarter);
            }else {
                result.get(it.getInteger("year").toString()).add(it.getString("quarter"));
            }
        });
        return result;
    }

    @Override
    public List<PerformanceQuarter> getPerformanceByConditions(JSONObject requestBody) {
        QueryWrapper<PerformanceQuarter> quarterWrapper=new QueryWrapper<>();
        String[] columns={"user_id","user_name","company_name","role_name","media_type","year","quarter","cost","point","performance","additional_fees"};
        quarterWrapper.select(columns);
        Map<String,Object> map = new HashMap<>(10);
        if(!"".equals(requestBody.getString("company"))) {
            map.put("company_id",requestBody.getString("company"));
        }
        if(!"".equals(requestBody.getString("department"))) {
            map.put("department",requestBody.getString("department"));
        }
        if(requestBody.getInteger("mediaType")!=0) {
            map.put("media_type",requestBody.getInteger("mediaType"));
        }
        if(requestBody.getInteger("year")!=0) {
            map.put("year",requestBody.getInteger("year"));
        }
        if(!"".equals(requestBody.getString("quarter"))) {
            map.put("quarter",requestBody.getString("quarter"));
        }
        if(!"".equals(requestBody.getString("roleCode"))) {
            map.put("role_code",requestBody.getString("roleCode"));
        }
        quarterWrapper.allEq(map);
        quarterWrapper.like("user_name",requestBody.getString("userName"));
        return performanceQuarterMapper.selectList(quarterWrapper);
    }

    @Override
    public PageInfo<PerformanceQuarter> getPerformanceByConditionsPage(int pageNo, int pageSize, String order,JSONObject requestBody) {
        QueryWrapper<PerformanceQuarter> quarterWrapper=new QueryWrapper<>();
        String[] columns={"user_id","user_name","company_name","department","role_name","media_type","year","quarter","cost","point","performance","additional_fees"};
        quarterWrapper.select(columns);
        Map<String,Object> map = new HashMap<>(16);
        if(!"".equals(requestBody.getString("company"))) {
            map.put("company_id",requestBody.getString("company"));
        }
        if(!"".equals(requestBody.getString("department"))) {
            map.put("department",requestBody.getString("department"));
        }
        if(requestBody.getInteger("mediaType")!=0) {
            map.put("media_type",requestBody.getInteger("mediaType"));
        }
        if(requestBody.getInteger("year")!=0) {
            map.put("year",requestBody.getInteger("year"));
        }
        if(!"".equals(requestBody.getString("quarter"))) {
            map.put("quarter",requestBody.getString("quarter"));
        }
        if(!"".equals(requestBody.getString("roleCode"))) {
            map.put("role_code",requestBody.getString("roleCode"));
        }
        quarterWrapper.allEq(map);
        quarterWrapper.like("user_name",requestBody.getString("userName"));
        if("asc".equals(order)){
            quarterWrapper.orderByAsc("cost");
        } else{
            quarterWrapper.orderByDesc("cost");
        }
        PageHelper.startPage(pageNo,pageSize);
        List<PerformanceQuarter> maps = performanceQuarterMapper.selectList(quarterWrapper);
        return (PageInfo<PerformanceQuarter>) new PageInfo(maps);
    }

}
