package cn.cslg.pas.service.upLoadPatent;

import cn.cslg.pas.common.model.PersonnelVO;
import cn.cslg.pas.common.model.dto.RetrieveRecordDTO;
import cn.cslg.pas.common.model.outApi.RetrieveRecordDeDTO;
import cn.cslg.pas.common.model.outApi.RetrieveRecordQueryDTO;
import cn.cslg.pas.common.utils.CacheUtils;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.domain.RetrieveRecord;
import cn.cslg.pas.mapper.RetrieveRecordMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 检索记录表
 * </p>
 *
 * @author 谢翔
 * @since 2022-07-9
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class RetrieveRecordService extends ServiceImpl<RetrieveRecordMapper, RetrieveRecord> {
    private final CacheUtils cacheUtils;
    private final LoginUtils loginUtils;

    /**
     * 新增检索记录
     * @param retrieveRecordDTO
     */
    public String addRetrieveRecord(RetrieveRecordDTO retrieveRecordDTO){
        //判空
        if(retrieveRecordDTO != null){
            //新建实体类
            RetrieveRecord retrieveRecord = new RetrieveRecord();
            //复制
            BeanUtils.copyProperties(retrieveRecordDTO, retrieveRecord);
            //获取创建人信息
            PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
            retrieveRecord.setCreateId(personnelVO.getId());
            retrieveRecord.setDbType(retrieveRecordDTO.getDbType());
            retrieveRecord.setConditions(retrieveRecordDTO.getConditions());
            retrieveRecord.setTotalNum(retrieveRecordDTO.getTotalNum());
            retrieveRecord.setTenantId(personnelVO.getTenantId());
            //数据入表
            retrieveRecord.insert();
            return Response.success("新增成功");
        } else {
            return Response.error("传入参数不能为空");
        }
    }

    /**
     * 更新检索记录表
     * @param retrieveRecordDTO
     * @return
     */
    public String updateRetrieveRecord(RetrieveRecordDTO retrieveRecordDTO){
        //判空
        if(retrieveRecordDTO != null){
            Integer id = retrieveRecordDTO.getId();
            //根据传入对象的id查询出实体类
            RetrieveRecord retrieveRecord = this.getById(id);
            retrieveRecord.setDbType(retrieveRecordDTO.getDbType());
            retrieveRecord.setConditions(retrieveRecordDTO.getConditions());
            retrieveRecord.setUpdateTime(new Date());
            retrieveRecord.updateById();
            return Response.success("更新成功");
        } else {
            return Response.error("更新失败");
        }
    }

    /**
     * 删除检索历史
     * @return
     */
    public String deleteRetrieveRecord(RetrieveRecordDeDTO retrieveRecordDeDTO){
       List<Integer> ids = retrieveRecordDeDTO.getIds();
       Boolean isAll =retrieveRecordDeDTO.getIsAll();
       if(isAll!=null&& isAll){
           this.remove(new LambdaQueryWrapper<RetrieveRecord>());
       }
       else {
           //判空
           if (ids != null && ids.size() > 0) {
               this.removeByIds(ids);
               return Response.success("删除成功");
           } else {
               return Response.error("删除错误");
           }
       }
        return Response.success("删除成功");
    }

    /**
     * 查询检索历史
     * @return
     */
    public IPage<RetrieveRecord> queryRetrieveRecord( RetrieveRecordQueryDTO retrieveRecordQueryDTO){
        //获取当前登陆人信息
        PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
        Integer createId = personnelVO.getId();
        LambdaQueryWrapper<RetrieveRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RetrieveRecord::getCreateId, createId)
        .orderByDesc(RetrieveRecord::getUpdateTime);
        IPage<RetrieveRecord> retrieveRecords = this.page(new Page<>(retrieveRecordQueryDTO.getCurrent(),retrieveRecordQueryDTO.getSize()),queryWrapper);
        List<RetrieveRecord> retrieveRecordList =retrieveRecords.getRecords();
        return retrieveRecords;
    }
}
