package com.iotechn.unimall.admin.api.card.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.SearchRecordsInfoMapper;
import com.iotechn.unimall.data.domain.SearchRecordsInfo;
import com.iotechn.unimall.admin.api.card.ISearchRecordsInfoService;
import com.iotechn.unimall.data.model.Page;

import java.util.Date;

import org.springframework.transaction.annotation.Transactional;

/**
 * 搜索记录Service业务层处理
 *
 * @author searchRecordsInfo
 * @date 2023-04-10
 */
@Service
public class SearchRecordsInfoServiceImpl implements ISearchRecordsInfoService {
    @Autowired
    private SearchRecordsInfoMapper searchRecordsInfoMapper;

    @Override
    public Boolean add(SearchRecordsInfo searchRecordsInfo) throws ServiceException {
        Date now = new Date();
        searchRecordsInfo.setGmtCreate(now);
        searchRecordsInfo.setGmtUpdate(now);
        return searchRecordsInfoMapper.insert(searchRecordsInfo) > 0;
    }

    @Override
    public Page<SearchRecordsInfo> list(Long commonId, String searchContent, String functionType, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<SearchRecordsInfo> wrapper = new EntityWrapper<SearchRecordsInfo>();
        if (!StringUtils.isEmpty(commonId)) {
            wrapper.eq("common_id", commonId);
        }
        if (!StringUtils.isEmpty(searchContent)) {
            wrapper.eq("search_content", searchContent);
        }
        if (!StringUtils.isEmpty(functionType)) {
            wrapper.eq("function_type", functionType);
        }
        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<SearchRecordsInfo> list = searchRecordsInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = searchRecordsInfoMapper.selectCount(wrapper);
        return new Page<SearchRecordsInfo>(list, page, limit, count);
    }

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

    @Override
    public Boolean update(SearchRecordsInfo searchRecordsInfo) throws ServiceException {
        Date now = new Date();
        searchRecordsInfo.setGmtUpdate(now);
        return searchRecordsInfoMapper.updateById(searchRecordsInfo) > 0;
    }

    @Override
    public SearchRecordsInfo get(Long id) throws ServiceException {
        return searchRecordsInfoMapper.selectById(id);
    }

    @Override
    public String export(Long commonId, String searchContent, String functionType, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<SearchRecordsInfo> wrapper = new EntityWrapper<SearchRecordsInfo>();
        if (!StringUtils.isEmpty(commonId)) {
            wrapper.eq("common_id", commonId);
        }
        if (!StringUtils.isEmpty(searchContent)) {
            wrapper.eq("search_content", searchContent);
        }
        if (!StringUtils.isEmpty(functionType)) {
            wrapper.eq("function_type", functionType);
        }
        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<SearchRecordsInfo> list = searchRecordsInfoMapper.selectList(wrapper);
        ExcelUtil<SearchRecordsInfo> util = new ExcelUtil<SearchRecordsInfo>(SearchRecordsInfo.class);
        return util.exportExcel(list, "操作日志");
    }
}
