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.MessageManagementMapper;
import com.iotechn.unimall.data.domain.MessageManagement;
import com.iotechn.unimall.admin.api.cloud.IMessageManagementService;
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 MessageManagementServiceImpl implements IMessageManagementService{
	@Autowired
	private MessageManagementMapper messageManagementMapper;
	
	@Override
	public Boolean add(MessageManagement messageManagement) throws ServiceException {
		Date now = new Date();
		messageManagement.setGmtCreate(now);
		messageManagement.setGmtUpdate(now);
		return messageManagementMapper.insert(messageManagement)>0;
	}

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

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

	@Override
	public Boolean update(MessageManagement messageManagement) throws ServiceException {
		Date now = new Date();
		messageManagement.setGmtUpdate(now);
		return messageManagementMapper.updateById(messageManagement)>0;
	}

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