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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.jixiaoPersonPrice.dto.JixiaoPersonPriceExportDTO;
import org.jeecg.modules.jixiaoPersonPrice.entity.JixiaoPersonPrice;
import org.jeecg.modules.jixiaoPersonPrice.mapper.JixiaoPersonPriceMapper;
import org.jeecg.modules.jixiaoPersonPrice.service.IJixiaoPersonPriceService;
import org.jeecgframework.poi.excel.def.MapExcelConstants;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.view.JeecgMapExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 人员绩效提成
 * @Author: jeecg-boot
 * @Date:   2023-09-04
 * @Version: V1.0
 */
@Service
public class JixiaoPersonPriceServiceImpl extends ServiceImpl<JixiaoPersonPriceMapper, JixiaoPersonPrice> implements IJixiaoPersonPriceService {

    @Autowired
    @SuppressWarnings("all")
    private JixiaoPersonPriceMapper jixiaoPersonPriceMapper;

    @Value("${jeecg.path.upload}")
    private String upLoadPath;

    /**查询规定时间内完结的委托对应的绩效导出的人员*/
    public List<String> selectPersonByDate(JixiaoPersonPriceExportDTO dto){
        return jixiaoPersonPriceMapper.selectPersonByDate(dto);
    }

    /**查询导出的人员绩效*/
    public List<Map<String,String>> exportPersonJX(JixiaoPersonPriceExportDTO dto){
        List<List<Map>> list = jixiaoPersonPriceMapper.exportPersonJX(dto);
        List<Map> mapList = list.get(2);
        List<Map<String,String>> mapList1 = mapList.stream().map(i->{
                    //todo 遍历map转化成Map<String,String>优化？
                    Map<String,String> map = new HashMap<>();
                    Set<Map.Entry> set=i.entrySet();
                    set.stream().forEach(x -> map.put(x.getKey().toString(), x.getValue().toString()));
                    Map<String, String> map1 = new HashMap<>();
                    for (Map.Entry str : set) {
                        map1.put(str.getKey().toString(), str.getValue().toString());
                    }
                    return map1;
                }
        ).collect(Collectors.toList());
        return mapList1;
    }

    /**
     * 人员绩效——按时间导出excel
     */
    public ModelAndView exportXlsByTime(HttpServletResponse response,List<Map<String,String>> exportList, String title) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        String exportPerson = "未知";
//        if(sysUser!=null)  exportPerson=sysUser.getRealname();
        String exportPerson=sysUser.getRealname();

        // 设置表头样式
        List<ExcelExportEntity> filedsList = new ArrayList<>();
        if(exportList != null && !exportList.isEmpty()){
            Map map = exportList.get(0);
            filedsList.add(new ExcelExportEntity("委托编号", "weituo_no"));
            // 打印键集合
            for (Object key : map.keySet()) {
                System.out.println((String) key);
                if(key.equals("weituo_no") || key.equals("TOTAL") || key.equals("create_time")) continue;
                filedsList.add(new ExcelExportEntity((String) key, (String)key));
            }
            filedsList.add(new ExcelExportEntity("总计", "TOTAL"));
            filedsList.add(new ExcelExportEntity("完结时间", "create_time",20));
        }

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgMapExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, title);//此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(MapExcelConstants.ENTITY_LIST, filedsList);
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams=new ExportParams(title + "报表", "导出人:" + exportPerson, title);
        exportParams.setImageBasePath(upLoadPath);
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS,exportParams);
        mv.addObject(NormalExcelConstants.MAP_LIST, exportList);
        return mv;
    }

}
