package digital.laboratory.platform.entrustment.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.common.aop.annotation.DlpResultProc;
import digital.laboratory.platform.entrustment.entity.EntrustApproveRecord;
import digital.laboratory.platform.entrustment.entity.Entrustment;
import digital.laboratory.platform.entrustment.mapper.EntrustApproveRecordMapper;
import digital.laboratory.platform.entrustment.query.BaseQuery;
import digital.laboratory.platform.entrustment.service.EntrustApproveRecordService;
import digital.laboratory.platform.entrustment.vo.EntrustApproveRecordVO;
import digital.laboratory.platform.sewage.entity.UpdateInfo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
* @author ChenJiangBao
* @description 针对表【b_entrust_approve_record(委托审核记录)】的数据库操作Service实现
* @createDate 2025-01-23 09:26:25
*/
@Service
public class EntrustApproveRecordServiceImpl extends ServiceImpl<EntrustApproveRecordMapper, EntrustApproveRecord>
    implements EntrustApproveRecordService{

    /**
     * 分页查询-委托审核记录
     * @param query
     * @return
     */
    @Override
    @DlpResultProc
    public IPage<EntrustApproveRecordVO> voPage(BaseQuery query) {
        QueryWrapper<EntrustApproveRecord> queryWrapper = appendQueryCriteria(query);
        return baseMapper.voPage(new Page<>(query.getCurrent(), query.getSize()), queryWrapper);
    }

    /**
     * 保存审核不通过记录
     * @param entrust
     * @param pi
     */
    @Override
    public void saveRecord(Entrustment entrust, UpdateInfo pi) {
        EntrustApproveRecord entrustApproveRecord = new EntrustApproveRecord();
        entrustApproveRecord.setApproveTime(LocalDateTime.now());
        entrustApproveRecord.setApproveType(0);
        entrustApproveRecord.setEntrustId(entrust.getId());
        entrustApproveRecord.setComments(pi.getComments());
        entrustApproveRecord.setUserId(pi.getUserId());
        entrustApproveRecord.setOrgId(pi.getOrgId());
        this.save(entrustApproveRecord);
    }

    private QueryWrapper<EntrustApproveRecord> appendQueryCriteria(BaseQuery query) {
        QueryWrapper<EntrustApproveRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(query.getKeywords()), "c.case_name", query.getKeywords());
        queryWrapper.ge(query.getStartDate() != null, "ear.approve_time", query.getStartDate());
        queryWrapper.le(query.getEndDate() != null, "ear.approve_time", query.getEndDate());
        queryWrapper.orderByDesc("ear.approve_time");
        return queryWrapper;
    }
}




