package vip.xiaonuo.coldchain.modular.monitorsearchhistory.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.coldchain.modular.monitorsearchhistory.entity.SearchHistory;
import vip.xiaonuo.coldchain.modular.monitorsearchhistory.mapper.SearchHistoryMapper;
import vip.xiaonuo.coldchain.modular.monitorsearchhistory.param.SearchHistoryAddParam;
import vip.xiaonuo.coldchain.modular.monitorsearchhistory.param.SearchHistoryIdParam;
import vip.xiaonuo.coldchain.modular.monitorsearchhistory.service.SearchHistoryService;
import vip.xiaonuo.common.enums.CommonDeleteFlagEnum;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class SearchHistoryServiceImpl extends ServiceImpl<SearchHistoryMapper, SearchHistory> implements SearchHistoryService {
    @Override
    public List<SearchHistory> getSearchHistory() {
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
        LambdaQueryWrapper<SearchHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(SearchHistory::getCreateUser, loginUser.getId())
                .eq(SearchHistory::getDeleteFlag, CommonDeleteFlagEnum.NOT_DELETE)
                .groupBy(SearchHistory::getSearchKey)
                .orderByDesc(SearchHistory::getCreateTime)
                .last("limit 20");
        return list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SearchHistoryAddParam searchHistoryAddParam) {
        if (StrUtil.isNotBlank(searchHistoryAddParam.getSearchKey())) {
            SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
            LambdaQueryWrapper<SearchHistory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SearchHistory::getCreateUser, loginUser.getId())
                    .eq(SearchHistory::getSearchKey, searchHistoryAddParam.getSearchKey())
                    .eq(SearchHistory::getDeleteFlag, CommonDeleteFlagEnum.NOT_DELETE);
            SearchHistory one = getOne(queryWrapper);
            SearchHistory searchHistory = BeanUtil.copyProperties(searchHistoryAddParam, SearchHistory.class);
            if (Objects.nonNull(one)) {
                searchHistory.setCreateTime(new Date());
                searchHistory.setId(one.getId());
                updateById(searchHistory);
            } else {
                save(searchHistory);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SearchHistoryIdParam searchHistoryIdParam) {
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
        LambdaQueryWrapper<SearchHistory> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(searchHistoryIdParam.getId())) {
            this.removeById(searchHistoryIdParam.getId());
        } else {
            queryWrapper.eq(SearchHistory::getCreateUser, loginUser.getId());
            this.remove(queryWrapper);
        }
    }
}
