package com.yh.saas.plugin.yiliangyiyun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.plugin.yiliangyiyun.constant.NumberConstant;
import com.yh.saas.plugin.yiliangyiyun.entity.ContractManagementInfo;
import com.baomidou.mybatisplus.plugins.Page;
import com.yh.saas.plugin.yiliangyiyun.entity.SearchRecordsInfo;
import com.yh.saas.plugin.yiliangyiyun.entity.TranProcessInfo;
import com.yh.saas.plugin.yiliangyiyun.mapper.SearchRecordsInfoMapper;
import com.yh.saas.plugin.yiliangyiyun.service.ISearchRecordsInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * <p>
 * 搜索记录 服务实现类
 * </p>
 *
 * @author Gongdc
 * @since 2021-07-20
 */
@Service
public class SearchRecordsInfoServiceImpl extends ServiceImpl<SearchRecordsInfoMapper, SearchRecordsInfo> implements ISearchRecordsInfoService {
    @Autowired
    private ISearchRecordsInfoService searchRecordsInfoService;

    /**
     * 删除搜索记录
     *
     * @param searchRecordsInfo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSearchRecordsInfo(SearchRecordsInfo searchRecordsInfo) {
        //查询搜索记录
        //判断记录是否存在
        List<SearchRecordsInfo> searchRecordsInfos =
                this.selectList(new EntityWrapper<SearchRecordsInfo>()
                        .eq(SearchRecordsInfo.QueryFiles.COMMON_ID, searchRecordsInfo.getCommonId())
                        .eq(SearchRecordsInfo.QueryFiles.FUNCTION_TYPE, searchRecordsInfo.getFunctionType())
                        .eq(SearchRecordsInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0));
        //已存在
        if (searchRecordsInfos.size() > 0) {
            //删除搜索记录
            for (SearchRecordsInfo searchRecordsInfo1 : searchRecordsInfos) {
                searchRecordsInfoService.deleteById(searchRecordsInfo1.getId());
            }
        }

    }

    /**
     * 新增搜索记录
     *
     * @param searchRecordsInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addInfo(SearchRecordsInfo searchRecordsInfo) {
        //判断记录是否存在
        SearchRecordsInfo searchRecordsInfo1 =
                this.selectOne(new EntityWrapper<SearchRecordsInfo>()
                        .eq(SearchRecordsInfo.QueryFiles.COMMON_ID, searchRecordsInfo.getCommonId())
                        .eq("search_content", searchRecordsInfo.getSearchContent())
                        .eq(SearchRecordsInfo.QueryFiles.FUNCTION_TYPE, searchRecordsInfo.getFunctionType())
                        .eq(SearchRecordsInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0));
        //已存在，次数加1
        if (searchRecordsInfo1 != null) {
            Long count = searchRecordsInfo1.getSearchCount() + 1;
            searchRecordsInfo1.setSearchCount(count);
            this.updateById(searchRecordsInfo1);
        }
        //新增
        else {
            //新增主键id
            searchRecordsInfo.setId(IdGenerator.generateUUID());
            // 操作主表数据
            this.insert(searchRecordsInfo);
        }
        return "OK";
    }

    /**
     * 搜索记录列表
     *
     * @param searchRecordsInfo
     * @return
     */
    @Override
    public Page<SearchRecordsInfo> selectSearchRecordsInfo(SearchRecordsInfo searchRecordsInfo) {
        //查询搜索记录
        Page<SearchRecordsInfo> searchRecordsInfoPage = this.selectPage(searchRecordsInfo.getQueryPage(), new EntityWrapper<SearchRecordsInfo>()
                .eq(SearchRecordsInfo.QueryFiles.COMMON_ID, searchRecordsInfo.getCommonId())
                .eq(SearchRecordsInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0)
                .eq(SearchRecordsInfo.QueryFiles.FUNCTION_TYPE, searchRecordsInfo.getFunctionType())
                .orderBy("update_date", false));
        return searchRecordsInfoPage;
    }

    /**
     * 推荐搜索列表
     *
     * @param searchRecordsInfo
     * @return
     */
    public Page<SearchRecordsInfo> selectRecommendedSearchRecordsInfo(SearchRecordsInfo searchRecordsInfo) {
        Page<SearchRecordsInfo> recommendedSearchRecordsInfoPage = this.selectPage(searchRecordsInfo.getQueryPage(), new EntityWrapper<SearchRecordsInfo>()
                .eq(SearchRecordsInfo.QueryFiles.COMMON_ID, searchRecordsInfo.getCommonId())
                .gt("search_count", "5")
                .eq(SearchRecordsInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0)
                .eq(SearchRecordsInfo.QueryFiles.FUNCTION_TYPE, searchRecordsInfo.getFunctionType())
                .orderBy("update_date", false));
        return recommendedSearchRecordsInfoPage;
    }
}