package cn.cslg.report.service.business.InvalidReReport;

import cn.cslg.report.common.model.dto.invalidReReport.ProofDetailDTO;
import cn.cslg.report.common.model.vo.PersonnelVO;
import cn.cslg.report.common.utils.CacheUtils;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.report.entity.Template;
import cn.cslg.report.entity.invalidReReport.ProofDetail;
import cn.cslg.report.exception.XiaoShiException;
import cn.cslg.report.mapper.InvalidReReport.ProofDetailMapper;
import cn.cslg.report.mapper.TemplateMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProofDetailService extends ServiceImpl<ProofDetailMapper,ProofDetail> {
    private final CacheUtils cacheUtils;
    private final LoginUtils loginUtils;

    /**
     * 添加证据详情
     * @param proofDetailDTO
     */
    public void addProofDetail(ProofDetailDTO proofDetailDTO) {
        //检查证据id，权要id，特征id是否为null
        if(proofDetailDTO.getProofId() != null && proofDetailDTO.getRightId() != null && proofDetailDTO.getFeatureId() != null){
            //将DTO里的属性装载到ProofDetail里并插入
            ProofDetail proofDetail = new ProofDetail();
            BeanUtils.copyProperties(proofDetailDTO, proofDetail);
            //获取当前登录人信息
            PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
            proofDetail.setCreateId(personnelVO.getId());
            proofDetail.insert();
        } else {
            throw new XiaoShiException("传入数据不可为空");
        }
    }

    /**
     * 批量删除证据详情
     * @param ids
     */
    public void deleteProofDetail(List<Integer> ids) {
        //检测ids是否为null或size=0
        if(ids != null && ids.size() != 0){
            this.removeByIds(ids);
        } else {
            throw new XiaoShiException("删除错误");
        }
    }

    /**
     * 更新证据详情
     * @param proofDetailDTO
     */
    public void updateProofDetail(ProofDetailDTO proofDetailDTO) {
        //检查证据id，权要id，特征id是否为null
        if(proofDetailDTO.getProofId() != null && proofDetailDTO.getRightId() != null && proofDetailDTO.getFeatureId() != null){
            //将DTO里的属性装载到ProofDetail里并插入
            //查出该条数据
            ProofDetail proofDetail = this.getById(proofDetailDTO.getId());
            BeanUtils.copyProperties(proofDetailDTO, proofDetail);
            //获取当前登录人信息
            PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
            proofDetail.setCreateId(personnelVO.getId());
            proofDetail.updateById();
        } else {
            throw new XiaoShiException("传入数据值不可为空");
        }
    }

    /**
     * 查询证据详情
     * @param proofId
     * @return
     */
    public List<ProofDetail> queryProofDetail(Integer proofId) {
        //根据证据id查询证据详情
        LambdaQueryWrapper<ProofDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProofDetail::getProofId, proofId);
        List<ProofDetail> proofDetails = this.list(queryWrapper);
        return proofDetails;
    }
}