package com.zcloud.modules.sys.service.impl;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.zcloud.common.utils.FileUploadUtil;
 import com.zcloud.common.utils.PageUtils;
 import com.zcloud.common.utils.Tools;
 import com.zcloud.modules.sys.dao.BusImgfilesDao;
 import com.zcloud.modules.sys.entity.BusImgfilesEntity;
 import com.zcloud.modules.sys.service.BusImgfilesService;
 import com.zcloud.modules.sys.vo.TemporaryDataVo;
 import com.zcloud.modules.sys.vo.TemporaryVo;
 import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.web.multipart.MultipartFile;

 import java.util.Map;

/**
 * @Author fangjiakai
 * @Desc 图片附件(BusImgfiles)表服务实现类
 * @Date 2023-10-19 15:09:20
 */
@Service("busImgfilesService")
public class BusImgfilesServiceImpl extends ServiceImpl<BusImgfilesDao, BusImgfilesEntity> implements BusImgfilesService {

    @Override
    public TemporaryVo uploadTemporary(MultipartFile file) throws Exception {
        try {
            // 保存临时文件
            BusImgfilesEntity img = new BusImgfilesEntity();
            img.setImgfilesId(Tools.get32UUID());
            img.setFilepath(Tools.uploadFile(file, "temporary"));
            img.setType(4);
            img.setForeignKey(Tools.get32UUID());
            baseMapper.insert(img);

            TemporaryVo Vo = new TemporaryVo();
            Vo.setErrno(0);
            TemporaryDataVo dataVo = new TemporaryDataVo();
            Vo.setData(dataVo);

            Vo.getData().setAlt(img.getImgfilesId());
            Vo.getData().setUrl(img.getFilepath());

            Vo.setCode("200");
            Vo.setResult("success");
            return Vo;
        } catch (Exception e) {
            e.printStackTrace();
            return new TemporaryVo(200, null, "9999", e.getMessage());
        }
    }
    @Override
    public PageUtils listPage(Map<String, Object> params) {
        Page<Map<String,Object>> page = new Page<>(Integer.parseInt(params.get("curPage").toString()),Integer.parseInt(params.get("limit").toString()));
        params.put("isDelete",0);
        IPage<Map<String,Object>> ipage = baseMapper.listPage(page,params);
        return new PageUtils(ipage);
    }


   @Override
	public void delete(String imgfilesId) {
		BusImgfilesEntity busImgfiles = getById(imgfilesId);
        FileUploadUtil.deleteFile(busImgfiles.getFilepath());

		this.removeById(imgfilesId);
	}
}
