package cn.ezhizao.project.business.socialSecurity.service.impl;

import cn.ezhizao.common.exception.ServiceException;
import cn.ezhizao.common.utils.StringUtils;
import cn.ezhizao.project.business.salary.domain.BizFinancialSalary;
import cn.ezhizao.project.business.salary.domain.BizFinancialSalaryDetail;
import cn.ezhizao.project.business.salary.domain.BizFinancialSalaryDetailEmployee;
import cn.ezhizao.project.business.socialSecurity.domain.BizSocialSecurityConfirm;
import cn.ezhizao.project.business.socialSecurity.domain.BizSocialSecurityConfirmDetail;
import cn.ezhizao.project.business.socialSecurity.domain.BizSocialSecurityConfirmDetailEmployee;
import cn.ezhizao.project.business.socialSecurity.mapper.BizSocialSecurityConfirmDetailEmployeeMapper;
import cn.ezhizao.project.business.socialSecurity.mapper.BizSocialSecurityConfirmDetailMapper;
import cn.ezhizao.project.business.socialSecurity.mapper.BizSocialSecurityConfirmMapper;
import cn.ezhizao.project.business.socialSecurity.service.IBizSocialSecurityConfirmService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 周晓
 */

@Service
public class BizSocialSecurityConfirmServiceImpl extends ServiceImpl<BizSocialSecurityConfirmMapper, BizSocialSecurityConfirm> implements IBizSocialSecurityConfirmService {

    @Resource
    BizSocialSecurityConfirmMapper financialSocialSecurityConfirmDao;

    @Resource
    BizSocialSecurityConfirmDetailMapper bizSocialSecurityConfirmDetailMapper;

    @Resource
    BizSocialSecurityConfirmDetailEmployeeMapper bizSocialSecurityConfirmDetailEmployeeMapper;

    @Override
    public List<BizSocialSecurityConfirm> getList(Map<String,Object> conditions) {
        return financialSocialSecurityConfirmDao.getList(conditions);
    }

    @Override
    public List<BizSocialSecurityConfirm> getListByBean(BizSocialSecurityConfirm financialSocialSecurityConfirm) {
        return financialSocialSecurityConfirmDao.getList(financialSocialSecurityConfirm);
    }

    @Override
    public int getTotal(Map<String,Object> conditions) {
        return financialSocialSecurityConfirmDao.getTotal(conditions);
    }

    @Override
    public String importSecurityConfirm(List<BizSocialSecurityConfirmDetailEmployee> detailList, String year, String month, Long tenantId) {
        if (StringUtils.isNull(detailList) || detailList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        // 根据 客户名称 字段将元素分组
        Map<String, List<BizSocialSecurityConfirmDetailEmployee>> groupedByName = detailList.stream()
                .collect(Collectors.groupingBy(BizSocialSecurityConfirmDetailEmployee::getCompanyName));
        int number = 0;
        // 循环遍历每个分组
        for (Map.Entry<String, List<BizSocialSecurityConfirmDetailEmployee>> entry : groupedByName.entrySet()) {
            String name = entry.getKey();

            List<BizSocialSecurityConfirmDetailEmployee> employees = entry.getValue();
            BizSocialSecurityConfirmDetailEmployee detai = employees.get(number++);

            BizSocialSecurityConfirm securityConfirm=new BizSocialSecurityConfirm();
            securityConfirm.setCompanyName(detai.getCompanyName());
            securityConfirm.setTenantId(tenantId);
            List <BizSocialSecurityConfirm> list= getListByBean(securityConfirm);

            //是否存在该客户
            if(list.size() == 0){
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、客户 ").append(detai.getCompanyName()).append(" 不存在");
                continue;
            }
            if(list.size() > 1){
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、客户 ").append(detai.getCompanyName()).append("请输入详细客户名称");
                continue;
            }
            securityConfirm=list.get(0);
            //获取工资明细
            BizSocialSecurityConfirmDetail detail=new BizSocialSecurityConfirmDetail();
            detail.setSocialSecurityConfirmId(securityConfirm.getId());
            detail.setMonth(month);
            detail.setYear(year);
            List<BizSocialSecurityConfirmDetail> salaryDetailList=bizSocialSecurityConfirmDetailMapper.getList(detail);
            if(salaryDetailList.size()>0){
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、客户 ").append(detai.getCompanyName()).append(year+"年"+month+"月").append("社保已有数据");
                continue;
            }

            //设置状态为待确认
            detail.setStatus(1);
            detail.setCompanyId(securityConfirm.getCompanyId());
            //新增社保明细
            bizSocialSecurityConfirmDetailMapper.insert(detail);

            for (BizSocialSecurityConfirmDetailEmployee l : employees) {
                //设置外键
                l.setSocialSecurityConfirmDetailId(detail.getId());
                l.setSocialSecurityConfirmId(securityConfirm.getId());
                //新增人员明细
                bizSocialSecurityConfirmDetailEmployeeMapper.insert(l);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、社保 ").append(l.getCompanyName()).append(" 导入成功");
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return successMsg.toString();
    }
}
