package com.goldenwater.slgc.service.impl.pms;

import com.goldenwater.slgc.entity.pms.PmsAccessory;
import com.goldenwater.slgc.mapper.pms.PmsAccessoryMapper;
import com.goldenwater.slgc.service.pms.IPmsAccessoryService;
import com.goldenwater.common.utils.file.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Transactional
public class PmsAccessoryServiceImpl implements IPmsAccessoryService {
    @Autowired
    private PmsAccessoryMapper pmsAccessoryMapper;

    /**
     * 查询附件列表
     */
    @Override
    public List<PmsAccessory> selectList(PmsAccessory accessory) {
        return pmsAccessoryMapper.selectList(accessory);
    }

    /**
     * 根据主键查询附件
     */
    @Override
    public PmsAccessory selectById(Long id) {
        return pmsAccessoryMapper.selectById(id);
    }

    /**
     * 新增附件
     */
    @Override
    public int insert(PmsAccessory accessory) {
        return pmsAccessoryMapper.insert(accessory);
    }

    /**
     * 修改附件
     */
    @Override
    public int update(PmsAccessory accessory) {
        return pmsAccessoryMapper.update(accessory);
    }

    /**
     * 删除附件
     */
    @Override
    public int deleteById(Long id) {
        PmsAccessory accessory = pmsAccessoryMapper.selectById(id);
        if (accessory != null && accessory.getFilePath() != null) {
            FileUtils.deleteFile(accessory.getFilePath());
        }
        return pmsAccessoryMapper.deleteById(id);
    }

    /**
     * 批量删除附件
     */
    @Override
    public int deleteByIds(Long[] ids) {
        for (Long id : ids) {
            PmsAccessory accessory = pmsAccessoryMapper.selectById(id);
            if (accessory != null && accessory.getFilePath() != null) {
                FileUtils.deleteFile(accessory.getFilePath());
            }
        }
        return pmsAccessoryMapper.deleteByIds(ids);
    }

    /**
     * 根据父ID查询附件列表
     */
    @Override
    public List<PmsAccessory> selectByParentId(Long parentId) {
        return pmsAccessoryMapper.selectByParentId(parentId);
    }

    @Override
    public List<PmsAccessory> selectFolderAll(Long parentId, String roleType) {
        return pmsAccessoryMapper.selectByParentId(parentId);
    }

    @Override
    public int updateSeq(Long id, Long seq) {
        return pmsAccessoryMapper.updateSeq(id, seq);
    }

    @Override
    public int swapSeq(Long id1, Long id2) {
        PmsAccessory a1 = pmsAccessoryMapper.selectById(id1);
        PmsAccessory a2 = pmsAccessoryMapper.selectById(id2);
        if (a1 == null || a2 == null) return 0;
        Long tmp = a1.getSeq();
        pmsAccessoryMapper.updateSeq(id1, a2.getSeq());
        pmsAccessoryMapper.updateSeq(id2, tmp);
        return 1;
    }

    @Override
    public int updateMtId(Long id, Long mtId) {
        return pmsAccessoryMapper.updateMtId(id, mtId);
    }
}
