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

import java.util.List;

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

/**
 * 文章管理信息Service业务层处理
 * 
 * @author jlb
 * @date 2023-05-26
 */
@Service
public class ArticleManagementServiceImpl implements IArticleManagementService {
	@Autowired
	private ArticleManagementMapper articleManagementMapper;
	
	@Override
	public Boolean add(ArticleManagement articleManagement) throws ServiceException {
		Date now = new Date();
		articleManagement.setGmtCreate(now);
		articleManagement.setGmtUpdate(now);
		articleManagement.setStatus("待发布");
		return articleManagementMapper.insert(articleManagement)>0;
	}

	@Override
	public Page<ArticleManagement> list(String articleTitle,String articleIntroduction,String articleClassify,String coverImage,String classification,String articleTemplate,String searchKeywords,String language,String status,Date gmtCreate,Date gmtUpdate,Long deleteFlag, Integer page, Integer limit)throws ServiceException {
		Wrapper<ArticleManagement> wrapper = new EntityWrapper<ArticleManagement>();
														if (!StringUtils.isEmpty(articleTitle)) {
					wrapper.like("article_title", articleTitle);
				}
												if (!StringUtils.isEmpty(articleIntroduction)) {
					wrapper.eq("article_introduction", articleIntroduction);
				}
				if (!StringUtils.isEmpty(articleClassify)) {
					wrapper.eq("article_classify", articleClassify);
				}
												if (!StringUtils.isEmpty(coverImage)) {
					wrapper.eq("cover_image", coverImage);
				}
												if (!StringUtils.isEmpty(classification)) {
					wrapper.eq("classification", classification);
				}
												if (!StringUtils.isEmpty(articleTemplate)) {
					wrapper.eq("article_template", articleTemplate);
				}
												if (!StringUtils.isEmpty(searchKeywords)) {
					wrapper.like("search_keywords", searchKeywords);
				}
				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).orderBy("gmt_update",false);
		List<ArticleManagement> list = articleManagementMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
		Integer count = articleManagementMapper.selectCount(wrapper);
		return new Page<ArticleManagement>(list, page, limit, count);
	}

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

	@Override
	public Boolean update(ArticleManagement articleManagement) throws ServiceException {
		Date now = new Date();
		articleManagement.setGmtUpdate(now);
		return articleManagementMapper.updateById(articleManagement)>0;
	}

	@Override
	public ArticleManagement get(Long id) throws ServiceException {
		return articleManagementMapper.selectById(id);
	}
	
	@Override
	public String export(String articleTitle,String articleIntroduction,String coverImage,String classification,String articleTemplate,String status,Date gmtCreate,Date gmtUpdate,Long deleteFlag, Integer page, Integer limit)throws ServiceException {
		Wrapper<ArticleManagement> wrapper = new EntityWrapper<ArticleManagement>();
														if (!StringUtils.isEmpty(articleTitle)) {
					wrapper.eq("article_title", articleTitle);
				}
												if (!StringUtils.isEmpty(articleIntroduction)) {
					wrapper.eq("article_introduction", articleIntroduction);
				}
												if (!StringUtils.isEmpty(coverImage)) {
					wrapper.eq("cover_image", coverImage);
				}
												if (!StringUtils.isEmpty(classification)) {
					wrapper.eq("classification", classification);
				}
												if (!StringUtils.isEmpty(articleTemplate)) {
					wrapper.eq("article_template", articleTemplate);
				}
												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<ArticleManagement> list = articleManagementMapper.selectList(wrapper);
		ExcelUtil<ArticleManagement> util = new ExcelUtil<ArticleManagement>(ArticleManagement.class);
		return util.exportExcel(list, "操作日志");
	}
}
