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

import java.util.List;

import com.iotechn.unimall.admin.api.cloud.IAboutUsInfoService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
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.AboutUsInfoMapper;
import com.iotechn.unimall.data.domain.AboutUsInfo;
import com.iotechn.unimall.data.model.Page;
import java.util.Date;
import org.springframework.transaction.annotation.Transactional;

import com.iotechn.unimall.data.domain.AboutUsHxinfo;
import com.iotechn.unimall.data.mapper.AboutUsHxinfoMapper;

/**
 * 记录关于我们信息Service业务层处理
 * 
 * @author jlb
 * @date 2023-05-31
 */
@Service
public class AboutUsInfoServiceImpl implements IAboutUsInfoService {
	@Autowired
	private AboutUsInfoMapper aboutUsInfoMapper;

	@Autowired
	private AboutUsHxinfoMapper aboutUsHxinfoMapper;
	
	@Override
	public Boolean add(AboutUsInfo aboutUsInfo) throws ServiceException {
		Date now = new Date();
		aboutUsInfo.setGmtCreate(now);
		aboutUsInfo.setGmtUpdate(now);
		return aboutUsInfoMapper.insert(aboutUsInfo)>0;
	}

	@Override
	public Page<AboutUsInfo> list(String title,String deputyTitle,String coverImage,String videoUrl,String classify,String content,String details,String language,String status,Date gmtCreate,Date gmtUpdate,Long deleteFlag, Integer page, Integer limit)throws ServiceException {
		Wrapper<AboutUsInfo> wrapper = new EntityWrapper<AboutUsInfo>();
														if (!StringUtils.isEmpty(title)) {
					wrapper.eq("title", title);
				}
												if (!StringUtils.isEmpty(deputyTitle)) {
					wrapper.eq("deputy_title", deputyTitle);
				}
												if (!StringUtils.isEmpty(coverImage)) {
					wrapper.eq("cover_image", coverImage);
				}
												if (!StringUtils.isEmpty(videoUrl)) {
					wrapper.eq("video_url", videoUrl);
				}
												if (!StringUtils.isEmpty(classify)) {
					wrapper.eq("classify", classify);
				}
												if (!StringUtils.isEmpty(content)) {
					wrapper.eq("content", content);
				}
												if (!StringUtils.isEmpty(details)) {
					wrapper.eq("details", details);
				}
												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<AboutUsInfo> list = aboutUsInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);

		if (!CollectionUtils.isEmpty(list)){
			for (AboutUsInfo aboutUsInfo:list){
				List<AboutUsHxinfo> aboutUsHxinfo = aboutUsHxinfoMapper.selectList(new EntityWrapper<AboutUsHxinfo>()
						.eq("language",aboutUsInfo.getLanguage()));
				if (!CollectionUtils.isEmpty(aboutUsHxinfo)) {
					aboutUsInfo.setAboutUsHxinfoList(aboutUsHxinfo);
				}
			}
		}
		Integer count = aboutUsInfoMapper.selectCount(wrapper);
		return new Page<AboutUsInfo>(list, page, limit, count);
	}

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

	@Override
	public Boolean update(AboutUsInfo aboutUsInfo) throws ServiceException {
		Date now = new Date();
		aboutUsInfo.setGmtUpdate(now);
		return aboutUsInfoMapper.updateById(aboutUsInfo)>0;
	}

	@Override
	public AboutUsInfo get(Long id) throws ServiceException {
		return aboutUsInfoMapper.selectById(id);
	}
	
	@Override
	public String export(String title,String deputyTitle,String coverImage,String videoUrl,String classify,String content,String details,String language,String status,Date gmtCreate,Date gmtUpdate,Long deleteFlag, Integer page, Integer limit)throws ServiceException {
		Wrapper<AboutUsInfo> wrapper = new EntityWrapper<AboutUsInfo>();
														if (!StringUtils.isEmpty(title)) {
					wrapper.eq("title", title);
				}
												if (!StringUtils.isEmpty(deputyTitle)) {
					wrapper.eq("deputy_title", deputyTitle);
				}
												if (!StringUtils.isEmpty(coverImage)) {
					wrapper.eq("cover_image", coverImage);
				}
												if (!StringUtils.isEmpty(videoUrl)) {
					wrapper.eq("video_url", videoUrl);
				}
												if (!StringUtils.isEmpty(classify)) {
					wrapper.eq("classify", classify);
				}
												if (!StringUtils.isEmpty(content)) {
					wrapper.eq("content", content);
				}
												if (!StringUtils.isEmpty(details)) {
					wrapper.eq("details", details);
				}
												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);
				}
							List<AboutUsInfo> list = aboutUsInfoMapper.selectList(wrapper);
		ExcelUtil<AboutUsInfo> util = new ExcelUtil<AboutUsInfo>(AboutUsInfo.class);
		return util.exportExcel(list, "操作日志");
	}
}
