package com.iotechn.unimall.admin.api.cloud.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.util.ExcelUtil;
import com.iotechn.unimall.data.mapper.ImageManagementMapper;
import com.iotechn.unimall.data.domain.ImageManagement;
import com.iotechn.unimall.admin.api.cloud.IImageManagementService;
import com.iotechn.unimall.data.model.Page;
import java.util.Date;
import org.springframework.transaction.annotation.Transactional;

/**
 * 图片管理信息Service业务层处理
 * 
 * @author jlb
 * @date 2023-05-30
 */
@Service
public class ImageManagementServiceImpl implements IImageManagementService{
	@Autowired
	private ImageManagementMapper imageManagementMapper;
	
	@Override
	public Boolean add(ImageManagement imageManagement) throws ServiceException {
		Date now = new Date();
		imageManagement.setGmtCreate(now);
		imageManagement.setGmtUpdate(now);
		return imageManagementMapper.insert(imageManagement)>0;
	}

	@Override
	public Page<ImageManagement> list(String imageUrl,String imageTitle,String language,Date gmtCreate,Date gmtUpdate,Long deleteFlag, Integer page, Integer limit)throws ServiceException {
		Wrapper<ImageManagement> wrapper = new EntityWrapper<ImageManagement>();
														if (!StringUtils.isEmpty(imageUrl)) {
					wrapper.eq("image_url", imageUrl);
				}
												if (!StringUtils.isEmpty(imageTitle)) {
					wrapper.eq("image_title", imageTitle);
				}
												if (!StringUtils.isEmpty(language)) {
					wrapper.eq("language", language);
				}
												if (!StringUtils.isEmpty(gmtCreate)) {
					wrapper.eq("gmt_create", gmtCreate);
				}
												if (!StringUtils.isEmpty(gmtUpdate)) {
					wrapper.eq("gmt_update", gmtUpdate);
				}
												if (!StringUtils.isEmpty(deleteFlag)) {
					wrapper.eq("delete_flag", deleteFlag);
				}
							wrapper.eq("delete_flag", 0);
		List<ImageManagement> list = imageManagementMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
		Integer count = imageManagementMapper.selectCount(wrapper);
		return new Page<ImageManagement>(list, page, limit, count);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean delete(String id) {
		String[] ids = String.valueOf(id).split(",");
		for (String tt:ids) {
			ImageManagement tmp =  imageManagementMapper.selectById(Long.parseLong(tt));
			if(tmp != null){
				tmp.setDeleteFlag(1l);
				imageManagementMapper.updateById(tmp);
			}
		}
		return true;
	}

	@Override
	public Boolean update(ImageManagement imageManagement) throws ServiceException {
		Date now = new Date();
		imageManagement.setGmtUpdate(now);
		return imageManagementMapper.updateById(imageManagement)>0;
	}

	@Override
	public ImageManagement get(Long id) throws ServiceException {
		return imageManagementMapper.selectById(id);
	}
	
	@Override
	public String export(String imageUrl,String imageTitle,String language,Date gmtCreate,Date gmtUpdate,Long deleteFlag, Integer page, Integer limit)throws ServiceException {
		Wrapper<ImageManagement> wrapper = new EntityWrapper<ImageManagement>();
														if (!StringUtils.isEmpty(imageUrl)) {
					wrapper.eq("image_url", imageUrl);
				}
												if (!StringUtils.isEmpty(imageTitle)) {
					wrapper.eq("image_title", imageTitle);
				}
												if (!StringUtils.isEmpty(language)) {
					wrapper.eq("language", language);
				}
												if (!StringUtils.isEmpty(gmtCreate)) {
					wrapper.eq("gmt_create", gmtCreate);
				}
												if (!StringUtils.isEmpty(gmtUpdate)) {
					wrapper.eq("gmt_update", gmtUpdate);
				}
												if (!StringUtils.isEmpty(deleteFlag)) {
					wrapper.eq("delete_flag", deleteFlag);
				}
							List<ImageManagement> list = imageManagementMapper.selectList(wrapper);
		ExcelUtil<ImageManagement> util = new ExcelUtil<ImageManagement>(ImageManagement.class);
		return util.exportExcel(list, "操作日志");
	}
}
