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

import java.util.List;

import com.iotechn.unimall.admin.api.cloud.ICloudClassifyInfoService;
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.CloudClassifyInfoMapper;
import com.iotechn.unimall.data.domain.CloudClassifyInfo;
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 CloudClassifyInfoServiceImpl implements ICloudClassifyInfoService {
	@Autowired
	private CloudClassifyInfoMapper cloudClassifyInfoMapper;
	
	@Override
	public Boolean add(CloudClassifyInfo cloudClassifyInfo) throws ServiceException {
		Date now = new Date();
		cloudClassifyInfo.setGmtCreate(now);
		cloudClassifyInfo.setGmtUpdate(now);
		return cloudClassifyInfoMapper.insert(cloudClassifyInfo)>0;
	}

	@Override
	public Page<CloudClassifyInfo> list(String classifyType,String classifyName,String language,Date gmtCreate,Date gmtUpdate,Long deleteFlag, Integer page, Integer limit)throws ServiceException {
		Wrapper<CloudClassifyInfo> wrapper = new EntityWrapper<CloudClassifyInfo>();
														if (!StringUtils.isEmpty(classifyType)) {
					wrapper.eq("classify_type", classifyType);
				}
												if (!StringUtils.isEmpty(classifyName)) {
					wrapper.eq("classify_name", classifyName);
				}
				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).orderBy("gmt_update",false);
		List<CloudClassifyInfo> list = cloudClassifyInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
		Integer count = cloudClassifyInfoMapper.selectCount(wrapper);
		return new Page<CloudClassifyInfo>(list, page, limit, count);
	}

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

	@Override
	public Boolean update(CloudClassifyInfo cloudClassifyInfo) throws ServiceException {
		Date now = new Date();
		cloudClassifyInfo.setGmtUpdate(now);
		return cloudClassifyInfoMapper.updateById(cloudClassifyInfo)>0;
	}

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