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.CarouselManagementMapper;
import com.iotechn.unimall.data.domain.CarouselManagement;
import com.iotechn.unimall.admin.api.cloud.ICarouselManagementService;
import com.iotechn.unimall.data.model.Page;
import java.util.Date;
import org.springframework.transaction.annotation.Transactional;

/**
 * 轮播管理信息Service业务层处理
 * 
 * @author jlb
 * @date 2023-05-29
 */
@Service
public class CarouselManagementServiceImpl implements ICarouselManagementService{
	@Autowired
	private CarouselManagementMapper carouselManagementMapper;
	
	@Override
	public Boolean add(CarouselManagement carouselManagement) throws ServiceException {
		Date now = new Date();
		carouselManagement.setGmtCreate(now);
		carouselManagement.setGmtUpdate(now);
		return carouselManagementMapper.insert(carouselManagement)>0;
	}

	@Override
	public Page<CarouselManagement> list(String title,String url,String classify,String language,String status,Date gmtCreate,Date gmtUpdate,Long deleteFlag, Integer page, Integer limit)throws ServiceException {
		Wrapper<CarouselManagement> wrapper = new EntityWrapper<CarouselManagement>();
														if (!StringUtils.isEmpty(title)) {
					wrapper.eq("title", title);
				}
												if (!StringUtils.isEmpty(url)) {
					wrapper.eq("url", url);
				}
		if (!StringUtils.isEmpty(classify)) {
			wrapper.eq("classify", classify);
		}
		if (!StringUtils.isEmpty(language)) {
			wrapper.eq("language", language);
		}
												if (!StringUtils.isEmpty(status)) {
					wrapper.eq("status", status);
				}
												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<CarouselManagement> list = carouselManagementMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
		Integer count = carouselManagementMapper.selectCount(wrapper);
		return new Page<CarouselManagement>(list, page, limit, count);
	}

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

	@Override
	public Boolean update(CarouselManagement carouselManagement) throws ServiceException {
		Date now = new Date();
		carouselManagement.setGmtUpdate(now);
		return carouselManagementMapper.updateById(carouselManagement)>0;
	}

	@Override
	public CarouselManagement get(Long id) throws ServiceException {
		return carouselManagementMapper.selectById(id);
	}
	
	@Override
	public String export(String title,String url,String status,Date gmtCreate,Date gmtUpdate,Long deleteFlag, Integer page, Integer limit)throws ServiceException {
		Wrapper<CarouselManagement> wrapper = new EntityWrapper<CarouselManagement>();
														if (!StringUtils.isEmpty(title)) {
					wrapper.eq("title", title);
				}
												if (!StringUtils.isEmpty(url)) {
					wrapper.eq("url", url);
				}
												if (!StringUtils.isEmpty(status)) {
					wrapper.eq("status", status);
				}
												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<CarouselManagement> list = carouselManagementMapper.selectList(wrapper);
		ExcelUtil<CarouselManagement> util = new ExcelUtil<CarouselManagement>(CarouselManagement.class);
		return util.exportExcel(list, "操作日志");
	}
}
