package com.hxhq.business.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.enums.archive.ArchiveLogTypeEnum;
import com.hxhq.business.enums.study.StudyTypeEnum;
import com.hxhq.common.core.utils.DateUtils;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.ArchiveLogMapper;
import com.hxhq.business.domain.ArchiveLog;
import com.hxhq.business.service.IArchiveLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 档案记录Service业务层处理
 *
 * @author hxhq
 * @date 2026-01-23
 */
@Service
public class ArchiveLogServiceImpl extends ServiceImpl<ArchiveLogMapper, ArchiveLog> implements IArchiveLogService
{
    /**
     * 查询档案记录列表
     *
     * @param archiveLog 档案记录
     * @return 档案记录
     */
    @Override
    public List<ArchiveLog> queryList(ArchiveLog archiveLog)
    {
        QueryWrapper<ArchiveLog> queryWrapper = Wrappers.query();
        queryWrapper.eq("type",archiveLog.getType());
        if(StringUtils.isNoneBlank(archiveLog.getOperateType())){
            queryWrapper.and(q->q.eq("operate_type",archiveLog.getOperateType())
            .or().eq("operate_type_en",archiveLog.getOperateType()));
        }
        if(StringUtils.isNoneBlank(archiveLog.getSn())){
            queryWrapper.like("sn",archiveLog.getSn());
        }
        if(StringUtils.isNoneBlank(archiveLog.getName())){
            queryWrapper.like("name",archiveLog.getName());
        }
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }

    @Override
    public void saveInfo(ArchiveLogTypeEnum type, Long targetId, String name, String sn, String leader, String operateType) {
        saveInfo(type,null,targetId,name,sn,leader,operateType);
    }

    @Override
    public void saveInfo(ArchiveLogTypeEnum type, StudyTypeEnum studyType, Long targetId, String name, String sn, String leader, String operateType) {
        ArchiveLog archiveLog = new ArchiveLog();
        archiveLog.setType(type.getValue());
        archiveLog.setStudyType(studyType==null?null:studyType.getValue());
        archiveLog.setTargetId(targetId);
        archiveLog.setName(name);
        archiveLog.setSn(sn);
        archiveLog.setLeader(leader);
        archiveLog.setOperateType(operateType);
        archiveLog.setOperateTypeEn(getOperateTypeEn(operateType));
        archiveLog.setOperateUser(SecurityUtils.getNickName());
        archiveLog.setOperateUserEn(SecurityUtils.getUsername());
        save(archiveLog);
    }

    private String getOperateTypeEn(String operateType){
        return operateType.equals("申请归档")?"Apply for Archiving"
                :operateType.equals("同意归档")?"Approve Archiving"
                :operateType.equals("拒绝归档")?"Reject Archiving"

                :operateType.equals("申请解档")?"Apply for De-archiving"
                :operateType.equals("同意解档")?"Approve De-archiving"
                :operateType.equals("拒绝解档")?"Reject De-archiving"

                :operateType.equals("申请借阅")?"Apply for Check-out"
                :operateType.equals("同意借阅")?"Approve Check-out"
                :operateType.equals("拒绝借阅")?"Reject Check-out"

                :operateType.equals("确认归还")?"Check-out End Confirmation"
                :operateType.equals("到期自动归还")?"Check-out End on Time":"";
    }
}
