package com.chushang.inspection.work.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chushang.common.core.constant.SecurityConstants;
import com.chushang.common.core.exception.ResultException;
import com.chushang.common.core.exception.utils.AssertUtil;
import com.chushang.common.core.util.StringUtils;
import com.chushang.common.core.web.Result;
import com.chushang.common.mybatis.utils.WrapperUtils;
import com.chushang.inspection.work.dto.ImageDTO;
import com.chushang.inspection.work.service.WrkImgService;
import com.chushang.inspection.work.vo.WrkImgVO;
import com.chushang.oss.entity.dto.UploadBytesDTO;
import com.chushang.oss.entity.vo.FileSourceVo;
import com.chushang.oss.feign.RemoteOssService;
import com.chushang.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chushang.inspection.work.mapper.WrkImgMapper;
import com.chushang.inspection.work.po.WrkImg;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
  * @auther: zhao
  * @date: 2024/6/28 11:25
  */
@Service
public class WrkImgServiceImpl extends ServiceImpl<WrkImgMapper, WrkImg> implements WrkImgService {

    @Resource
    RemoteOssService remoteOssService;
    @Override
    @Transactional
    public String uploadPicture(MultipartFile file, Integer imgType, Long wrkId, String locationAddress)
    {
        try {
            // locationAddress 用于添加水印
            Result<List<FileSourceVo>> listResult = remoteOssService.uploadBytes(UploadBytesDTO.builder()
                    .bytes(file.getBytes()).build(),
                    file.getOriginalFilename(),
                    StringUtils.isNotEmpty(locationAddress),
                    locationAddress,
                    "wrkImg",
                    SecurityConstants.INNER);
            if (listResult.isSuccess() && CollectionUtil.isNotEmpty(listResult.getData())) {
                List<FileSourceVo> data = listResult.getData();
                FileSourceVo fileSourceVo = data.get(0);
                WrkImg wrkImg = new WrkImg();
                wrkImg.setWrkId(wrkId);
                wrkImg.setImgType(imgType);
                wrkImg.setFid(fileSourceVo.getFid());
                wrkImg.setSize(fileSourceVo.getSize());
                wrkImg.setDeptId(SecurityUtils.getDeptId());
                wrkImg.setRealPath(fileSourceVo.getFilePath());
                save(wrkImg);
            }
            throw new ResultException("上传图片失败");
        } catch (IOException e) {
            log.error("{}", e);
        }
        return null;
    }

    @Override
    @Transactional
    public void removeImg(ImageDTO image)
    {
        LambdaQueryWrapper<WrkImg> imgSql = WrapperUtils.builder();
        imgSql.eq(WrkImg::getWrkId, image.getWrkId())
                .eq(WrkImg::getImgType, image.getImgType());
        List<WrkImg> imgList = list(imgSql);
        if (CollectionUtil.isNotEmpty(imgList)){
            Set<Long> imgIds = imgList.stream().map(WrkImg::getImgId).collect(Collectors.toSet());
            removeByIds(imgIds);
            Set<String> fids = imgList.stream().map(WrkImg::getFid).collect(Collectors.toSet());
            Result<String> stringResult = remoteOssService.delFile(fids, SecurityConstants.INNER);
            AssertUtil.invalidate(!stringResult.isSuccess(), stringResult.getMsg());
        }
    }

    @Override
    public List<WrkImgVO> listImgByWrkIds(Set<Long> wrkIds) {
        List<WrkImg> imgList = list(new LambdaQueryWrapper<WrkImg>().in(WrkImg::getWrkId, wrkIds));
        if (CollectionUtil.isNotEmpty(imgList))
            return BeanUtil.copyToList(imgList, WrkImgVO.class);
        return List.of();
    }
}
