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

import cn.ezhizao.common.exception.ServiceException;
import cn.ezhizao.common.utils.StringUtils;
import cn.ezhizao.project.business.housingFund.domain.BizHousingFundConfirm;
import cn.ezhizao.project.business.housingFund.domain.BizHousingFundConfirmDetail;
import cn.ezhizao.project.business.housingFund.domain.BizHousingFundConfirmDetailEmployee;
import cn.ezhizao.project.business.housingFund.mapper.BizHousingFundConfirmDetailEmployeeMapper;
import cn.ezhizao.project.business.housingFund.mapper.BizHousingFundConfirmDetailMapper;
import cn.ezhizao.project.business.housingFund.mapper.BizHousingFundConfirmMapper;
import cn.ezhizao.project.business.housingFund.service.IBizHousingFundConfirmService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * @author 周晓
 */

@Service
public class BizHousingFundConfirmServiceImpl extends ServiceImpl<BizHousingFundConfirmMapper, BizHousingFundConfirm> implements IBizHousingFundConfirmService {

    @Resource
    BizHousingFundConfirmMapper financialHousingFundConfirmDao;

    @Resource
    BizHousingFundConfirmDetailMapper housingFundConfirmDetailDao;

    @Resource
    BizHousingFundConfirmDetailEmployeeMapper housingFundConfirmDetailEmployeeDao;

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

    @Override
    public List<BizHousingFundConfirm> getListByBean(BizHousingFundConfirm financialHousingFundConfirm) {
        return financialHousingFundConfirmDao.getList(financialHousingFundConfirm);
    }

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

    public String importHousingFundCon(List<BizHousingFundConfirmDetailEmployee> 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<BizHousingFundConfirmDetailEmployee>> groupedByName = detailList.stream()
                .collect(Collectors.groupingBy(BizHousingFundConfirmDetailEmployee::getCompanyName));
        int number = 0;

        // 循环遍历每个分组
        for (Map.Entry<String, List<BizHousingFundConfirmDetailEmployee>> entry : groupedByName.entrySet()) {
            String name = entry.getKey();

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

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

            //是否存在该客户
            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;
            }

            housingFundConfirm = list.get(0);
            //获取明细
            BizHousingFundConfirmDetail detail = new BizHousingFundConfirmDetail();
            detail.setHousingFundConfirmId(housingFundConfirm.getId());
            detail.setMonth(month);
            detail.setYear(year);
            List<BizHousingFundConfirmDetail> confirmDetailList = housingFundConfirmDetailDao.getList(detail);
            if (confirmDetailList.size() > 0) {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、客户 ").append(detai.getCompanyName()).append(year + "年" + month + "月").append("公积金已有数据");
                continue;
            }

            //设置状态为待确认
            detail.setStatus(1);
            detail.setCompanyId(housingFundConfirm.getCompanyId());
            //新增公积金明细
            housingFundConfirmDetailDao.insert(detail);

            for (BizHousingFundConfirmDetailEmployee l : employees) {
                l.setHousingFundConfirmId(housingFundConfirm.getId());
                l.setHousingFundConfirmDetailId(detail.getId());
                //新增人员明细
                housingFundConfirmDetailEmployeeDao.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();
    }
}
