package com.liquidnet.client.admin.zhengzai.sweet.service.impl;

import com.liquidnet.client.admin.common.core.domain.AjaxResult;
import com.liquidnet.client.admin.common.core.domain.entity.SysDictData;
import com.liquidnet.client.admin.common.utils.DictUtils;
import com.liquidnet.client.admin.common.utils.StringUtils;
import com.liquidnet.client.admin.zhengzai.sweet.service.ISweetFormSubmissionAdminService;
import com.liquidnet.client.admin.zhengzai.sweet.utils.SweetFormSubmissionExcelUtils;
import com.liquidnet.service.sweet.dto.param.admin.SweetFormSubmissionActivityListParam;
import com.liquidnet.service.sweet.dto.param.admin.SweetFormSubmissionExportParam;
import com.liquidnet.service.sweet.dto.admin.SweetFormSubmissionActivityVo;
import com.liquidnet.service.sweet.entity.SweetFormSubmission;
import com.liquidnet.service.sweet.mapper.SweetFormSubmissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SweetFormSubmissionAdminServiceImpl implements ISweetFormSubmissionAdminService {

    public static final String DICT_TYPE_SWEET_FORM_ACTIVITY = "sweet_form_activity";

    @Autowired
    private SweetFormSubmissionMapper sweetFormSubmissionMapper;

    @Override
    public List<SweetFormSubmissionActivityVo> activityList(SweetFormSubmissionActivityListParam param) {
        String activityName = StringUtils.isEmpty(param.getActivityName()) ? null : param.getActivityName();
        List<SweetFormSubmissionActivityVo> list = sweetFormSubmissionMapper.selectActivityGroupList(activityName);
        for (SweetFormSubmissionActivityVo vo : list) {
            vo.setActivityLabel(getActivityLabel(vo.getActivityName()));
        }
        return list;
    }

    private String getActivityLabel(String activityName) {
        if (StringUtils.isEmpty(activityName)) {
            return "";
        }
        List<SysDictData> datas = DictUtils.getDictCache(DICT_TYPE_SWEET_FORM_ACTIVITY);
        if (datas != null) {
            for (SysDictData dict : datas) {
                if (activityName.equals(dict.getDictValue())) {
                    return dict.getDictLabel();
                }
            }
        }
        return activityName;
    }

    @Override
    public AjaxResult export(SweetFormSubmissionExportParam param) {
        if (StringUtils.isEmpty(param.getActivityName())) {
            return AjaxResult.error("请选择活动");
        }
        String beginTime = StringUtils.isEmpty(param.getBeginTime()) ? null : param.getBeginTime();
        String endTime = StringUtils.isEmpty(param.getEndTime()) ? null : param.getEndTime();
        List<SweetFormSubmission> submissions = sweetFormSubmissionMapper.selectForExport(
                param.getActivityName(), beginTime, endTime);
        String activityLabel = getActivityLabel(param.getActivityName());
        String exportName = SweetFormSubmissionExcelUtils.buildExportName(activityLabel, beginTime, endTime);
        String filename = SweetFormSubmissionExcelUtils.export(submissions, exportName);
        return AjaxResult.success(filename);
    }
}
