package com.ruoyi.buyerapi.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.buyerapi.domain.BuyerEmpWorkhourByProject;
import com.ruoyi.buyerapi.domain.BuyerProject;
import com.ruoyi.buyerapi.mapper.BuyerProjectMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buyerapi.mapper.BuyerEmpWorkhourMapper;
import com.ruoyi.buyerapi.domain.BuyerEmpWorkhour;
import com.ruoyi.buyerapi.service.IBuyerEmpWorkhourService;

/**
 * 员工工时分摊记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@Service
public class BuyerEmpWorkhourServiceImpl implements IBuyerEmpWorkhourService
{
    @Autowired
    private BuyerEmpWorkhourMapper buyerEmpWorkhourMapper;

    @Autowired
    private BuyerProjectMapper buyerProjectMapper;

    @Override
    public List<BuyerEmpWorkhourByProject> listGrroupProject(BuyerEmpWorkhourByProject buyerEmpWorkhourByProject) {
        return buyerEmpWorkhourMapper.listGrroupProject(buyerEmpWorkhourByProject);
    }

    @Override
    public List<BuyerEmpWorkhourByProject> listByProjectId(String projectId) {
        return buyerEmpWorkhourMapper.listByProjectId(projectId);
    }

    /**
     * 查询员工工时分摊记录
     *
     * @param id 员工工时分摊记录主键
     * @return 员工工时分摊记录
     */
    @Override
    public BuyerEmpWorkhour selectBuyerEmpWorkhourById(String id)
    {
        return buyerEmpWorkhourMapper.selectBuyerEmpWorkhourById(id);
    }

    /**
     * 查询员工工时分摊记录列表
     *
     * @param buyerEmpWorkhour 员工工时分摊记录
     * @return 员工工时分摊记录
     */
    @Override
    public List<BuyerEmpWorkhour> selectBuyerEmpWorkhourList(BuyerEmpWorkhour buyerEmpWorkhour)
    {
        return buyerEmpWorkhourMapper.selectBuyerEmpWorkhourList(buyerEmpWorkhour);
    }

    @Override
    public List<Map<String, Object>> listForYearMonth(BuyerEmpWorkhour buyerEmpWorkhour) {
        List<Map<String, Object>> list = new ArrayList<>();
        // 总计
        Map<String, Object> totalMap = new LinkedHashMap<>();
        // 获取当月的所有人员
        List<BuyerEmpWorkhour> buyerEmpWorkhours = buyerEmpWorkhourMapper.selectBuyerEmpWorkhourList(buyerEmpWorkhour);
        // 根据姓名分组
        Map<String,List<BuyerEmpWorkhour>> mapListOfName = buyerEmpWorkhours.stream().collect(Collectors.groupingBy(BuyerEmpWorkhour::getName));
        Object[] keys = mapListOfName.keySet().toArray();
        for(int i = 0; i < keys.length; i++){
            BuyerEmpWorkhour empWorkhour = mapListOfName.get(keys[i]).get(0);
            if(empWorkhour.getWages() == null)
            {
                empWorkhour.setWages(BigDecimal.valueOf(0));
            }
            if(empWorkhour.getSocialSecurity() == null)
            {
                empWorkhour.setSocialSecurity(BigDecimal.valueOf(0));
            }
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("序号", i + 1);
            totalMap.put("序号", "合计");
            map.put("员工姓名", keys[i]);
            totalMap.put("员工姓名", "");
            map.put("部门", empWorkhour.getDept());
            totalMap.put("部门", "");
            map.put("应发工资金额", empWorkhour.getWages());
            totalMap.put("应发工资金额", totalMap.containsKey("应发工资金额") ?
                    ((BigDecimal)totalMap.get("应发工资金额")).add(empWorkhour.getWages()) :
                    empWorkhour.getWages());
            map.put("五险一金金额", empWorkhour.getSocialSecurity());
            totalMap.put("五险一金金额", totalMap.containsKey("五险一金金额") ?
                    ((BigDecimal)totalMap.get("五险一金金额")).add(empWorkhour.getSocialSecurity()) :
                    empWorkhour.getSocialSecurity());
            map.put("当月人工成本合计", empWorkhour.getWages().add(empWorkhour.getSocialSecurity()));
            totalMap.put("当月人工成本合计", totalMap.containsKey("当月人工成本合计") ?
                    ((BigDecimal)totalMap.get("当月人工成本合计")).add(empWorkhour.getWages().add(empWorkhour.getSocialSecurity())) :
                    empWorkhour.getWages().add(empWorkhour.getSocialSecurity()));
            // 遍历查询项目情况
            // 统计工时
            Map<String,List<BuyerEmpWorkhour>> mapListOfProject = mapListOfName.get(keys[i]).stream().collect(Collectors.groupingBy(BuyerEmpWorkhour::getProjectId));
            Object[] projectKeys = mapListOfProject.keySet().toArray();
            for(int j = 0; j < projectKeys.length; j++){
                BigDecimal totalWorkOfProject = new BigDecimal(0);
                BuyerProject project = buyerProjectMapper.selectBuyerProjectById(projectKeys[j] + "");
                List<BuyerEmpWorkhour> listOfProject = mapListOfProject.get(projectKeys[j]);
                for(BuyerEmpWorkhour workhourOfProject : listOfProject){
                    totalWorkOfProject = totalWorkOfProject.add(workhourOfProject.getCost());
                }
                if(null != project) {
                    map.put("“" + project.getName() + "”工时", totalWorkOfProject);
                    totalMap.put("“" + project.getName() + "”工时", totalMap.containsKey("“" + project.getName() + "”工时") ?
                            ((BigDecimal)totalMap.get("“" + project.getName() + "”工时")).add(totalWorkOfProject) :
                            totalWorkOfProject);
                    map.put("工时合计", map.containsKey("工时合计") ?
                            ((BigDecimal)map.get("工时合计")).add(totalWorkOfProject) :
                            totalWorkOfProject);
                    totalMap.put("工时合计", totalMap.containsKey("工时合计") ?
                            ((BigDecimal)totalMap.get("工时合计")).add(totalWorkOfProject) :
                            totalWorkOfProject);
                }
            }

            // 统计工时
            for(int j = 0; j < projectKeys.length; j++){
                BigDecimal totalCostOfProject = new BigDecimal(0);
                BuyerProject project = buyerProjectMapper.selectBuyerProjectById(projectKeys[j] + "");
                List<BuyerEmpWorkhour> listOfProject = mapListOfProject.get(projectKeys[j]);
                for(BuyerEmpWorkhour workhourOfProject : listOfProject){
                    if(workhourOfProject.getWages() == null) {
                        workhourOfProject.setWages(BigDecimal.valueOf(0));
                    }
                    if(workhourOfProject.getSocialSecurity() == null) {
                        workhourOfProject.setSocialSecurity(BigDecimal.valueOf(0));
                    }
                    totalCostOfProject = totalCostOfProject.add(workhourOfProject.getWages().add(workhourOfProject.getSocialSecurity()));
                }
                if(null != project) {
                    map.put("“" + project.getName() + "”人工成本", totalCostOfProject);
                    totalMap.put("“" + project.getName() + "”人工成本", totalMap.containsKey("“" + project.getName() + "”人工成本") ?
                            ((BigDecimal) totalMap.get("“" + project.getName() + "”人工成本")).add(totalCostOfProject) :
                            totalCostOfProject);
                    map.put("项目成本合计", map.containsKey("项目成本合计") ?
                            ((BigDecimal) map.get("项目成本合计")).add(totalCostOfProject) :
                            totalCostOfProject);
                    totalMap.put("项目成本合计", totalMap.containsKey("项目成本合计") ?
                            ((BigDecimal) totalMap.get("项目成本合计")).add(totalCostOfProject) :
                            totalCostOfProject);
                }
            }
            list.add(map);
        }
        list.add(totalMap);
        return list;
    }

    /**
     * 查询员工工时分摊记录列表
     *
     * @param buyerEmpWorkhour 员工工时分摊记录
     * @return 员工工时分摊记录
     */
    @Override
    public List<String> workDatelist(BuyerEmpWorkhour buyerEmpWorkhour)
    {
        return buyerEmpWorkhourMapper.workDatelist(buyerEmpWorkhour);
    }

    /**
     * 新增员工工时分摊记录
     *
     * @param buyerEmpWorkhour 员工工时分摊记录
     * @return 结果
     */
    @Override
    public int insertBuyerEmpWorkhour(BuyerEmpWorkhour buyerEmpWorkhour)
    {
        buyerEmpWorkhour.setCreateTime(DateUtils.getNowDate());
        return buyerEmpWorkhourMapper.insertBuyerEmpWorkhour(buyerEmpWorkhour);
    }

    /**
     * 修改员工工时分摊记录
     *
     * @param buyerEmpWorkhour 员工工时分摊记录
     * @return 结果
     */
    @Override
    public int updateBuyerEmpWorkhour(BuyerEmpWorkhour buyerEmpWorkhour)
    {
        buyerEmpWorkhour.setUpdateTime(DateUtils.getNowDate());
        return buyerEmpWorkhourMapper.updateBuyerEmpWorkhour(buyerEmpWorkhour);
    }

    /**
     * 批量删除员工工时分摊记录
     *
     * @param ids 需要删除的员工工时分摊记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerEmpWorkhourByIds(String[] ids)
    {
        return buyerEmpWorkhourMapper.deleteBuyerEmpWorkhourByIds(ids);
    }

    /**
     * 删除员工工时分摊记录信息
     *
     * @param id 员工工时分摊记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerEmpWorkhourById(String id)
    {
        return buyerEmpWorkhourMapper.deleteBuyerEmpWorkhourById(id);
    }

    @Override
    public int deleteBuyerEmpWorkhour(BuyerEmpWorkhour buyerEmpWorkhour) {
        return buyerEmpWorkhourMapper.deleteBuyerEmpWorkhour(buyerEmpWorkhour);
    }
}
