package com.vetti.hotake.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.vetti.common.core.service.BaseServiceImpl;
import com.vetti.common.enums.FillTypeEnum;
import com.vetti.common.utils.SecurityUtils;
import com.vetti.hotake.domain.HotakeCvInfo;
import com.vetti.hotake.domain.HotakeReferenceCheck;
import com.vetti.hotake.domain.dto.HotakeCvInfoDto;
import com.vetti.hotake.domain.dto.VcDto.VcExperienceDto;
import com.vetti.hotake.mapper.HotakeCvInfoMapper;
import com.vetti.hotake.mapper.HotakeReferenceCheckMapper;
import com.vetti.hotake.service.IHotakeReferenceCheckService;
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 vetti
 * @date 2026-01-29
 */
@Service
public class HotakeReferenceCheckServiceImpl extends BaseServiceImpl implements IHotakeReferenceCheckService {

    @Autowired
    private HotakeReferenceCheckMapper hotakeReferenceCheckMapper;

    @Autowired
    private HotakeCvInfoMapper hotakeCvInfoMapper;

    /**
     * 查询参考检查
     *
     * @param id 参考检查主键
     * @return 参考检查
     */
    @Transactional(readOnly = true)
    @Override
    public HotakeReferenceCheck selectHotakeReferenceCheckById(Integer id) {
        return hotakeReferenceCheckMapper.selectHotakeReferenceCheckById(id);
    }

    /**
     * 查询参考检查列表
     *
     * @param hotakeReferenceCheck 参考检查
     * @return 参考检查
     */
    @Transactional(readOnly = true)
    @Override
    public List<HotakeReferenceCheck> selectHotakeReferenceCheckList(HotakeReferenceCheck hotakeReferenceCheck) {
        return hotakeReferenceCheckMapper.selectHotakeReferenceCheckList(hotakeReferenceCheck);
    }

    /**
     * 新增参考检查
     *
     * @param hotakeReferenceCheck 参考检查
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public HotakeReferenceCheck insertHotakeReferenceCheck(HotakeReferenceCheck hotakeReferenceCheck) {
        fill(FillTypeEnum.INSERT.getCode(), hotakeReferenceCheck);
        hotakeReferenceCheckMapper.insertHotakeReferenceCheck(hotakeReferenceCheck);
        return hotakeReferenceCheck;
    }

    /**
     * 批量新增参考检查
     *
     * @param hotakeReferenceCheckList 参考检查列表
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchInsertHotakeReferenceCheck(List<HotakeReferenceCheck> hotakeReferenceCheckList) {
        if (CollectionUtil.isEmpty(hotakeReferenceCheckList)) {
            return 0;
        }
        for (HotakeReferenceCheck hotakeReferenceCheck : hotakeReferenceCheckList) {
            fill(FillTypeEnum.INSERT.getCode(), hotakeReferenceCheck);
        }
        return hotakeReferenceCheckMapper.batchInsertHotakeReferenceCheck(hotakeReferenceCheckList);
    }

    /**
     * 修改参考检查
     *
     * @param hotakeReferenceCheck 参考检查
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public HotakeReferenceCheck updateHotakeReferenceCheck(HotakeReferenceCheck hotakeReferenceCheck) {
        fill(FillTypeEnum.UPDATE.getCode(), hotakeReferenceCheck);
        hotakeReferenceCheckMapper.updateHotakeReferenceCheck(hotakeReferenceCheck);
        return hotakeReferenceCheck;
    }

    /**
     * 批量删除参考检查
     *
     * @param ids 需要删除的参考检查主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeReferenceCheckByIds(Integer[] ids) {
        return hotakeReferenceCheckMapper.deleteHotakeReferenceCheckByIds(ids);
    }

    /**
     * 删除参考检查信息
     *
     * @param id 参考检查主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeReferenceCheckById(Integer id) {
        return hotakeReferenceCheckMapper.deleteHotakeReferenceCheckById(id);
    }

    /**
     * 获取当前登录人的工作经验列表
     *
     * @return 工作经验列表
     */
    @Transactional(readOnly = true)
    @Override
    public List<VcExperienceDto> getCurrentUserExperienceList() {
        List<VcExperienceDto> experienceList = new ArrayList<>();
        
        // 查询当前用户的简历信息
        HotakeCvInfo queryCv = new HotakeCvInfo();
        queryCv.setUserId(SecurityUtils.getUserId());
        queryCv.setCvFileType("cv");
        List<HotakeCvInfo> cvInfoList = hotakeCvInfoMapper.selectHotakeCvInfoList(queryCv);
        
        if (CollectionUtil.isNotEmpty(cvInfoList)) {
            HotakeCvInfo cvInfo = cvInfoList.get(0);
            if (StrUtil.isNotEmpty(cvInfo.getCvTemplateJson())) {
                HotakeCvInfoDto cvInfoDto = JSONUtil.toBean(cvInfo.getCvTemplateJson(), HotakeCvInfoDto.class);
                if (cvInfoDto != null && CollectionUtil.isNotEmpty(cvInfoDto.getExperience())) {
                    experienceList = cvInfoDto.getExperience();
                }
            }
        }
        
        return experienceList;
    }
}
