package com.willalp.event.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.core.domain.ReturnEntity;
import com.willalp.common.core.domain.entity.SysDictData;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.ObjectToTypes;
import com.willalp.common.utils.StringUtils;
import com.willalp.event.domain.HsWelfareApplyRecord;
import com.willalp.event.domain.vo.WelfareApplyStatVo;
import com.willalp.event.mapper.HsWelfareApplyRecordMapper;
import com.willalp.event.service.IHsWelfareApplyRecordService;
import com.willalp.system.service.ISysDictTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 福利报名记录Service业务层处理
 *
 * @author willalp
 * @date 2023-04-17
 */
@Service
public class HsWelfareApplyRecordServiceImpl extends ServiceImpl<HsWelfareApplyRecordMapper, HsWelfareApplyRecord> implements IHsWelfareApplyRecordService {
    @Resource
    private HsWelfareApplyRecordMapper hsWelfareApplyRecordMapper;

    @Resource
    private ISysDictTypeService dictTypeService;

    /**
     * 查询福利报名记录列表
     *
     * @param hsWelfareApplyRecord 福利报名记录
     * @return 福利报名记录
     */
    @Override
    public List<HsWelfareApplyRecord> selectHsWelfareApplyRecordList(HsWelfareApplyRecord hsWelfareApplyRecord) {
        return hsWelfareApplyRecordMapper.selectHsWelfareApplyRecordList(hsWelfareApplyRecord);
    }

    @Override
    public List<HsWelfareApplyRecord> importInitDataTemp() {
        return hsWelfareApplyRecordMapper.importInitDataTemp();
    }

    @Override
    public List<HsWelfareApplyRecord> exportDataAndInterpret(HsWelfareApplyRecord hsWelfareApplyRecord) {
        List<HsWelfareApplyRecord> list = hsWelfareApplyRecordMapper.selectHsWelfareApplyRecordList(hsWelfareApplyRecord);
        List<SysDictData> applyAddr = dictTypeService.selectDictDataByType("welfare_apply_addr");
        List<SysDictData> applyStatus = dictTypeService.selectDictDataByType("welfare_apply_status");
        list.forEach(item -> {
            applyAddr.forEach(dict -> {
                if (StringUtils.isNotBlank(item.getAddress()) && item.getAddress().equals(dict.getDictValue())) {
                    item.setAddress(dict.getDictLabel());
                }
            });
            applyStatus.forEach(dict -> {
                if (StringUtils.isNotBlank(item.getNowStatus()) && item.getNowStatus().equals(dict.getDictValue())) {
                    item.setNowStatus(dict.getDictLabel());
                }
            });
        });
        return list;
    }

    @Override
    public String importData(List<HsWelfareApplyRecord> list, String fileName) {
        if (StringUtils.isBlank(fileName)) {
            if (count(new QueryWrapper<HsWelfareApplyRecord>().eq("source", fileName)) > 0) {
                throw new BaseException("该Excel数据已经存在，请检查或重命名");
            }
        }
        int successNum = 0;
        int failureNum = 0;
        for (HsWelfareApplyRecord welfareApplyRecord : list) {
            try {
                welfareApplyRecord.setSource(fileName);
                welfareApplyRecord.setNowStatus("1");
                save(welfareApplyRecord);
                successNum++;
            } catch (Exception e) {
                failureNum++;
                log.error(e.getMessage(), e);
            }
        }
        ReturnEntity<String> res = new ReturnEntity<>();
        res.setReturnMessage("event.club.successCode", new String[]{
                ObjectToTypes.parseString(successNum), ObjectToTypes.parseString(failureNum)
        });
        return res.getMessage();
    }

    @Override
    public List<WelfareApplyStatVo> stat(String source) {
        if (StringUtils.isBlank(source)) {
            return new ArrayList<>();
        }
        return baseMapper.stat(source);
    }

    @Override
    public int doUnregistered(String source) {
        if (StringUtils.isBlank(source)) {
            return 0;
        }
        return baseMapper.doUnregistered(source);
    }

}
