package com.vetti.hotake.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.vetti.common.core.domain.entity.SysDictData;
import com.vetti.common.core.service.BaseServiceImpl;
import com.vetti.common.enums.FillTypeEnum;
import com.vetti.hotake.domain.HotakeSettingsJob;
import com.vetti.hotake.domain.dto.HotakeSettingsJobDictDto;
import com.vetti.hotake.mapper.HotakeSettingsJobMapper;
import com.vetti.hotake.service.IHotakeSettingsJobService;
import com.vetti.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户首选工作设置Service业务层处理
 *
 * @author wangxiangshun
 * @date 2025-11-02
 */
@Service
public class HotakeSettingsJobServiceImpl extends BaseServiceImpl implements IHotakeSettingsJobService {

    @Autowired
    private HotakeSettingsJobMapper hotakeSettingsJobMapper;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 查询用户首选工作设置
     *
     * @param id 用户首选工作设置主键
     * @return 用户首选工作设置
     */
    @Transactional(readOnly = true)
    @Override
    public HotakeSettingsJob selectHotakeSettingsJobById(Long id) {
        return hotakeSettingsJobMapper.selectHotakeSettingsJobById(id);
    }

    /**
     * 查询用户首选工作设置列表
     *
     * @param hotakeSettingsJob 用户首选工作设置
     * @return 用户首选工作设置
     */
    @Transactional(readOnly = true)
    @Override
    public List<HotakeSettingsJob> selectHotakeSettingsJobList(HotakeSettingsJob hotakeSettingsJob) {
        return hotakeSettingsJobMapper.selectHotakeSettingsJobList(hotakeSettingsJob);
    }

    /**
     * 新增用户首选工作设置
     *
     * @param hotakeSettingsJob 用户首选工作设置
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public HotakeSettingsJob insertHotakeSettingsJob(HotakeSettingsJob hotakeSettingsJob) {
        fill(FillTypeEnum.INSERT.getCode(), hotakeSettingsJob);
        hotakeSettingsJobMapper.insertHotakeSettingsJob(hotakeSettingsJob);
        return hotakeSettingsJob;
    }

    /**
     * 修改用户首选工作设置
     *
     * @param hotakeSettingsJob 用户首选工作设置
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public HotakeSettingsJob updateHotakeSettingsJob(HotakeSettingsJob hotakeSettingsJob) {
        fill(FillTypeEnum.UPDATE.getCode(), hotakeSettingsJob);
        hotakeSettingsJobMapper.updateHotakeSettingsJob(hotakeSettingsJob);
        return hotakeSettingsJob;
    }

    /**
     * 批量删除用户首选工作设置
     *
     * @param ids 需要删除的用户首选工作设置主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeSettingsJobByIds(Long[] ids) {
        return hotakeSettingsJobMapper.deleteHotakeSettingsJobByIds(ids);
    }

    /**
     * 删除用户首选工作设置信息
     *
     * @param id 用户首选工作设置主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeSettingsJobById(Long id) {
        return hotakeSettingsJobMapper.deleteHotakeSettingsJobById(id);
    }

    /**
     * 获取用户首选工作设置字典对照内容
     *
     * @return 字典对照内容
     */
    @Transactional(readOnly = true)
    @Override
    public HotakeSettingsJobDictDto getDictData() {
        HotakeSettingsJobDictDto dto = new HotakeSettingsJobDictDto();

        dto.setHotakeLanguagesType(convertToDictItems(dictTypeService.selectDictDataByType("hotake_languages_type")));
        dto.setHotakeSkillsToolType(convertToDictItems(dictTypeService.selectDictDataByType("hotake_skills_tool_type")));
        dto.setRoleLevel(convertToDictItems(dictTypeService.selectDictDataByType("role_level")));
        dto.setRoleLocationType(convertToDictItems(dictTypeService.selectDictDataByType("role_location_type")));
        dto.setRoleSelectJobType(convertToDictItems(dictTypeService.selectDictDataByType("role_select_job_type")));
        dto.setRoleType(convertToDictItems(dictTypeService.selectDictDataByType("role_type")));
        dto.setRoleRequiredSkills(convertToDictItems(dictTypeService.selectDictDataByType("role_required_skills")));
        dto.setRoleNiceToHaveSkills(convertToDictItems(dictTypeService.selectDictDataByType("role_nice_to_have_skills")));
        dto.setRoleEducationRequirementsType(convertToDictItems(dictTypeService.selectDictDataByType("role_education_requirements_type")));
        dto.setRoleCertificationLicenses(convertToDictItems(dictTypeService.selectDictDataByType("role_certification_licenses")));
        dto.setRoleBenefits(convertToDictItems(dictTypeService.selectDictDataByType("role_benefits")));
        dto.setRolePublishingChannels(convertToDictItems(dictTypeService.selectDictDataByType("role_publishing_channels")));
        dto.setRoleInitialScreeningQuestionsType(convertToDictItems(dictTypeService.selectDictDataByType("role_initial_screening_questions_type")));
        dto.setExpectedPayRange(convertToDictItems(dictTypeService.selectDictDataByType("expected_pay_range")));
        dto.setWillingnessToTravel(convertToDictItems(dictTypeService.selectDictDataByType("willingness_to_travel")));

        return dto;
    }

    private List<HotakeSettingsJobDictDto.DictItem> convertToDictItems(List<SysDictData> dictDataList) {
        List<HotakeSettingsJobDictDto.DictItem> items = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(dictDataList)) {
            for (SysDictData dictData : dictDataList) {
                HotakeSettingsJobDictDto.DictItem item = new HotakeSettingsJobDictDto.DictItem();
                item.setDictCode(dictData.getDictCode());
                item.setDictLabel(dictData.getDictLabel());
                item.setDictValue(dictData.getDictValue());
                item.setDictType(dictData.getDictType());
                item.setCssClass(dictData.getCssClass());
                item.setListClass(dictData.getListClass());
                item.setIsDefault(dictData.getIsDefault());
                item.setStatus(dictData.getStatus());
                item.setDataType(dictData.getDataType());
                item.setFileUrl(dictData.getFileUrl());
                item.setAvatarUrl(dictData.getAvatarUrl());
                items.add(item);
            }
        }
        return items;
    }
}
