package cn.cslg.pas.service;


import cn.cslg.pas.common.model.PersonnelVO;
import cn.cslg.pas.common.model.dto.SWQueryDTO;
import cn.cslg.pas.common.model.dto.ScratchWordsDTO;
import cn.cslg.pas.common.model.dto.ScratchWordsUpdateDTO;
import cn.cslg.pas.common.model.vo.ScratchWordsVO;
import cn.cslg.pas.common.utils.CacheUtils;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.domain.ScratchWords;

import cn.cslg.pas.exception.XiaoShiException;
import cn.cslg.pas.mapper.ScratchWordsMapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.List;


/**
 * <p>
 * 划词高亮表 服务类
 * </p>
 *
 * @author 李仁杰
 * @since 2023-06-13
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ScratchWordsService extends ServiceImpl<ScratchWordsMapper, ScratchWords> {
    private final ScratchWordsMapper scratchWordsMapper;
    private final CacheUtils cacheUtils;
    private final LoginUtils loginUtils;

    /**
     * 新增划词高亮
     * @param scratchWordsDTO
     */
    public void add(ScratchWordsDTO scratchWordsDTO){
        //判断传入列表不为空
        if(scratchWordsDTO != null){
            if(scratchWordsDTO.getCreateFrom() == null){
                scratchWordsDTO.setCreateFrom(0);
            }
            ScratchWords scratchWords = new ScratchWords();
            BeanUtils.copyProperties(scratchWordsDTO, scratchWords);
            //获取当前登陆人信息
            PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
            //设置创建人id
            scratchWords.setCreateId(personnelVO.getId());
            //设置租户id
            scratchWords.setTenantId(personnelVO.getTenantId());
            //数据入表
            scratchWords.insert();
        } else {
            throw new XiaoShiException("传入对象不能为空");
        }
    }

    /**
     * 更新划词高亮
     * @param scratchWordsUpdateDTO
     */
    public void update(ScratchWordsUpdateDTO scratchWordsUpdateDTO){
        //判断传入列表不为空
        if(scratchWordsUpdateDTO != null){
            ScratchWords scratchWords = this.getById(scratchWordsUpdateDTO.getId());
            BeanUtils.copyProperties(scratchWordsUpdateDTO, scratchWords);
            scratchWords.updateById();
        } else {
            throw new XiaoShiException("传入对象不能为空");
        }
    }

    /**
     * 查询划词高亮
     * @param swQueryDTO
     * @return
     */
    public List<ScratchWordsVO> queryAll(SWQueryDTO swQueryDTO){
        if(swQueryDTO.getPatentNo() != null) {
            //获取当前登陆人信息
            PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
            Integer tenantId = personnelVO.getTenantId();
            Integer createId = personnelVO.getId();
            //创建来源（默认0/专题库1/报告2）
            if(swQueryDTO.getCreateFrom().equals(1)){
                Integer projectId = swQueryDTO.getId();
                Integer reportId = -1;
                List<ScratchWordsVO> scratchWords = scratchWordsMapper.querySW(swQueryDTO.getPatentNo(), tenantId, createId, projectId, reportId);
                return scratchWords;
            }
            //创建来源（默认0/专题库1/报告2）
           else if(swQueryDTO.getCreateFrom().equals(2)){
                Integer reportId = swQueryDTO.getId();
                Integer projectId = -1;
                List<ScratchWordsVO> scratchWords = scratchWordsMapper.querySW(swQueryDTO.getPatentNo(), tenantId, createId, projectId, reportId);
                return scratchWords;
            }
            //创建来源（默认0/专题库1/报告2）
           else if(swQueryDTO.getCreateFrom().equals(0)){
                Integer reportId = -1;
                Integer projectId = -1;
                List<ScratchWordsVO> scratchWords = scratchWordsMapper.querySW(swQueryDTO.getPatentNo(), tenantId, createId, projectId, reportId);
                return scratchWords;
            }else {
                throw new XiaoShiException("暂无该情况");
            }
        } else {
            throw new XiaoShiException("传入参数不可为空");
        }
    }

    /**
     * 删除划词高亮
     * @param ids
     */
    public void delete(List<Integer> ids){
        for(int i = 0; i < ids.size(); i++){
            LambdaQueryWrapper<ScratchWords> LW = new LambdaQueryWrapper<>();
            LW.eq(ScratchWords::getId, ids.get(i));
            ScratchWords scratchWords = this.getOne(LW);
            scratchWords.deleteById();
        }
    }
}
