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.core.conditions.query.QueryWrapper;
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.ParkStemScreen;
import com.zksy.park.domain.dto.ParkStemScreenDto;
import com.zksy.park.mapper.ParkStemScreenMapper;
import com.zksy.park.service.FileGeneralService;
import com.zksy.park.service.ParkStemScreenService;
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;
import java.util.ArrayList;
import java.util.List;

/**
 * 园区灯杆大屏Service业务层处理
 *
 * @author sy
 * @date 2024-05-08
 */
@Service
public class ParkStemScreenServiceImpl extends ServiceImpl<ParkStemScreenMapper, ParkStemScreen> implements ParkStemScreenService {
    @Autowired
    private FileGeneralService fileGeneralService;
    @Autowired
    private MinioFileStorageService minioFileStorageService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> saveParkStemScreen(ParkStemScreenDto dto) {
        try {
            if (dto.getFiles() != null) {
                List<String> filePath = minioFileStorageService.uploadFileBatch(dto.getFiles(), "yqdgdp");
                dto.setFilePath(filePath);
            }
            ParkStemScreen entity = BeanUtil.copyProperties(dto, ParkStemScreen.class);
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            baseMapper.insert(entity);
            if (dto.getFilePath() != null) {
                dto.getFilePath().forEach(q ->{
                    FileGeneral fileGeneral = new FileGeneral();
                    fileGeneral.setFormId(entity.getId());
                    fileGeneral.setFilePath(q);
                    fileGeneralService.save(fileGeneral);
                });
            }
            return Result.ok("新增成功");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> updateByParkStemScreenId(ParkStemScreenDto dto) {
        try {
            if (dto.getFiles() != null) {
                if(dto.getFilePath() != null){
                    dto.getFilePath().forEach(q->{
                        minioFileStorageService.deleteFile(q);
                    });
                }else {
                    LambdaQueryWrapper<FileGeneral> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(FileGeneral::getFormId, dto.getId());
                    List<FileGeneral> list = fileGeneralService.list(wrapper);
                    list.forEach(w->{
                        minioFileStorageService.deleteFile(w.getFilePath());
                    });
                }
                LambdaQueryWrapper<FileGeneral> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(FileGeneral::getFormId, dto.getId());
                fileGeneralService.remove(wrapper);
                List<String> filePath = minioFileStorageService.uploadFileBatch(dto.getFiles(), "yqdgdp");
                dto.setFilePath(filePath);
                if (filePath != null){
                    filePath.forEach(q ->{
                        FileGeneral fileGeneral = new FileGeneral();
                        fileGeneral.setFormId(dto.getId());
                        fileGeneral.setFilePath(q);
                        fileGeneralService.save(fileGeneral);
                    });
                }
            }
            ParkStemScreen entity = BeanUtil.copyProperties(dto, ParkStemScreen.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);
            List<FileGeneral> list = fileGeneralService.list(wrapper);
            list.forEach(q->{
                if(q!=null){
                    minioFileStorageService.deleteFile(q.getFilePath());
                    fileGeneralService.remove(wrapper);
                }
            });
            this.removeById(id);
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    @Override
    public Result<ParkStemScreen> getByIdWithFile(String id) {
        if (StrUtil.isNotBlank(id)) {
            ParkStemScreen parkStemScreen = this.getById(id);
            LambdaQueryWrapper<FileGeneral> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(FileGeneral::getFormId, id);
            List<FileGeneral> fileGeneralList = fileGeneralService.list(wrapper);
            if(fileGeneralList != null){
                List<String> stringList = new ArrayList<>();
                fileGeneralList.forEach(q ->{
                    stringList.add(q.getFilePath());
                });
                    parkStemScreen.setFilePath(stringList);
            }
            return Result.ok(parkStemScreen);
        }
        return Result.error("获取失败");
    }

    @Override
    public List<ParkStemScreen> listWithImg(QueryWrapper<ParkStemScreen> queryWrapper) {
        List<ParkStemScreen> list = this.list(queryWrapper);
        list.forEach(q->{
            LambdaQueryWrapper<FileGeneral> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(FileGeneral::getFormId, q.getId());
            List<FileGeneral> fileGeneralList = fileGeneralService.list(wrapper);
            ArrayList<String> fileList = new ArrayList<>();
            fileGeneralList.forEach(w->{
                fileList.add(w.getFilePath());
            });
            q.setFilePath(fileList);
        });
        return list;
    }
}
