package com.zksy.park.service.imp;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.common.core.domain.Result;
import com.zksy.park.domain.FileGeneral;
import com.zksy.park.domain.ParkInfo;
import com.zksy.park.domain.dto.PackInfoDto;
import com.zksy.park.mapper.ParkInfoMapper;
import com.zksy.park.service.FileGeneralService;
import com.zksy.park.service.ParkInfoService;
import com.zksy.service.MinioFileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 园区信息Service业务层处理
 *
 * @author sy
 * @date 2024-12-30
 */
@Service
public class ParkInfoServiceImpl extends ServiceImpl<ParkInfoMapper, ParkInfo> implements ParkInfoService {
    @Autowired
    private FileGeneralService fileGeneralService;
    @Autowired
    private MinioFileStorageService minioFileStorageService;

    @Override
    @Transactional
    public Result<Object> saveParkInfo(PackInfoDto dto) {
        try {
            if (dto.getFile() != null) {
                String filePath = minioFileStorageService.uploadFile(dto.getFile(), dto.getBusinessType());
                dto.setFilePath(filePath);
            }
            ParkInfo entity = BeanUtil.copyProperties(dto, ParkInfo.class);
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            baseMapper.insert(entity);

            if (StrUtil.isNotBlank(dto.getFilePath())) {
                FileGeneral fileGeneral = new FileGeneral();
                fileGeneral.setFormId(entity.getId());
                fileGeneral.setFilePath(dto.getFilePath());
                fileGeneralService.save(fileGeneral);
            }
            return Result.ok("新增成功");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    @Transactional
    public Result<String> updateByParkInfoId(PackInfoDto dto) {
        try {
            if (dto.getFile() != null) {
                LambdaQueryWrapper<FileGeneral> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(FileGeneral::getFormId, dto.getId());
                fileGeneralService.remove(wrapper);
                String filePath = minioFileStorageService.uploadFile(dto.getFile(), dto.getBusinessType());
                dto.setFilePath(filePath);
                FileGeneral fileGeneral = new FileGeneral();
                fileGeneral.setFormId(dto.getId());
                fileGeneral.setFilePath(dto.getFilePath());
                fileGeneralService.save(fileGeneral);
            }
            ParkInfo entity = BeanUtil.copyProperties(dto, ParkInfo.class);
            entity.setUpdateTime(LocalDateTime.now());
            this.updateById(entity);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.ok("更新成功");
    }

    @Override
    @Transactional
    public Result<String> deleteById(String id) {
        if (StrUtil.isNotBlank(id)) {
            LambdaQueryWrapper<FileGeneral> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(FileGeneral::getFormId, id);
            FileGeneral one = fileGeneralService.getOne(wrapper);
            if(one!=null){
                minioFileStorageService.deleteFile(one.getFilePath());
                fileGeneralService.remove(wrapper);
            }
            this.removeById(id);
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    @Override
    public Result<ParkInfo> getByIdWithFile(String id) {
        if (StrUtil.isNotBlank(id)) {
            ParkInfo parkInfo = this.getById(id);
            LambdaQueryWrapper<FileGeneral> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(FileGeneral::getFormId, id);
            FileGeneral one = fileGeneralService.getOne(wrapper);
            if(one!=null){
                parkInfo.setFilePath(one.getFilePath());
            }
            return Result.ok(parkInfo);
        }
        return Result.error("获取失败");
    }
}
