package com.zt.plat.module.qms.resource.record.service.recordapplydetail;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailPageReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailSaveReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordapplydetail.vo.RecordApplyDetailJoinVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordrecord.vo.RecordRecordSaveReqVO;
import com.zt.plat.module.qms.resource.record.dal.dataobject.recordapply.RecordApplyDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.resource.record.dal.dataobject.recordapplydetail.RecordApplyDetailDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.resource.record.dal.mapper.RecordApplyDetailMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.*;

/**
 * 文件记录通用申请明细 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class RecordApplyDetailServiceImpl implements RecordApplyDetailService {

    @Resource
    private RecordApplyDetailMapper recordApplyDetailMapper;

    @Override
    public RecordApplyDetailRespVO createRecordApplyDetail(RecordApplyDetailSaveReqVO createReqVO) {
        // 插入
        RecordApplyDetailDO recordApplyDetail = BeanUtils.toBean(createReqVO, RecordApplyDetailDO.class);
        recordApplyDetailMapper.insert(recordApplyDetail);
        // 返回
        return BeanUtils.toBean(recordApplyDetail, RecordApplyDetailRespVO.class);
    }

    @Override
    public void updateRecordApplyDetail(RecordApplyDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateRecordApplyDetailExists(updateReqVO.getId());
        // 更新
        RecordApplyDetailDO updateObj = BeanUtils.toBean(updateReqVO, RecordApplyDetailDO.class);
        recordApplyDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecordApplyDetail(Long id) {
        // 校验存在
        validateRecordApplyDetailExists(id);
        // 删除
        recordApplyDetailMapper.deleteById(id);
    }

    @Override
        public void deleteRecordApplyDetailListByIds(List<Long> ids) {
        // 校验存在
        validateRecordApplyDetailExists(ids);
        // 删除
        recordApplyDetailMapper.deleteByIds(ids);
        }

    private void validateRecordApplyDetailExists(List<Long> ids) {
        List<RecordApplyDetailDO> list = recordApplyDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RECORD_APPLY_DETAIL_NOT_EXISTS);
        }
    }

    private void validateRecordApplyDetailExists(Long id) {
        if (recordApplyDetailMapper.selectById(id) == null) {
            throw exception(RECORD_APPLY_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public RecordApplyDetailDO getRecordApplyDetail(Long id) {
        return recordApplyDetailMapper.selectById(id);
    }

    @Override
    public PageResult<RecordApplyDetailDO> getRecordApplyDetailPage(RecordApplyDetailPageReqVO pageReqVO) {
        return recordApplyDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RecordApplyDetailDO> selectList(RecordApplyDetailPageReqVO pageReqVO) {
        PageResult<RecordApplyDetailDO> pageResult = recordApplyDetailMapper.selectPage(pageReqVO);
        return pageResult.getList();
    }

    @Override
    public boolean createRecordApplyDetailBatch(List<RecordApplyDetailDO> detailSaveReqVOList) {
        return recordApplyDetailMapper.insertBatch(detailSaveReqVOList);
    }

    @Override
    public List<RecordApplyDetailDO> selectAllList(Long applyId) {
        return recordApplyDetailMapper.selectList(RecordApplyDetailDO::getApplyId, applyId);
    }

    @Override
    public int deleteRecordApplyDetailByApplyId(Long applyId) {
        // 通过申请Id,批量删除明细数据
        LambdaQueryWrapper<RecordApplyDetailDO> query = Wrappers.lambdaQuery();
        query.eq(RecordApplyDetailDO::getApplyId,applyId);
        List<RecordApplyDetailDO> recordApplyDetailDOS = recordApplyDetailMapper.selectList(query);
        if (ObjectUtils.isEmpty(recordApplyDetailDOS)) return 0;
        // 获取明细Ids
        List<Long> ids = new ArrayList<>();
        recordApplyDetailDOS.forEach(item -> {
            ids.add(item.getId());
        });
        this.deleteRecordApplyDetailListByIds(ids);
        return 1;
    }

    // 查询分发/查看申请数据
    @Override
    public PageResult<RecordApplyDetailJoinVO> selectDetailAndApplyList(RecordApplyDetailPageReqVO reqVO){
        Page<RecordRecordSaveReqVO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        IPage<RecordApplyDetailJoinVO> recordApplyDetailJoinVOIPage = recordApplyDetailMapper.selectDetailAndApplyList(page, reqVO);
        return new PageResult<>(recordApplyDetailJoinVOIPage.getRecords(), recordApplyDetailJoinVOIPage.getTotal());
    }

    /**
     * 查询非指定明细Id/记录 数据
     * @param applyId
     * @param type
     * @param targetIds
     * @return
     */
    @Override
    public List<RecordApplyDetailDO> selectNotInDetailIdList(Long applyId, String type, List<Long> targetIds) {
        LambdaQueryWrapper<RecordApplyDetailDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RecordApplyDetailDO::getApplyId, applyId);
        if (type.equals("detailId")) queryWrapper.notIn(RecordApplyDetailDO::getId, targetIds);
        if (type.equals("recordId")) queryWrapper.notIn(RecordApplyDetailDO::getDocumentId, targetIds);
        return recordApplyDetailMapper.selectList(queryWrapper);
    }

    /**
     * 删除非指定明细Id/记录 数据
     * @param applyId
     * @param detailIds
     * @param recordIds
     * @return
     */
    @Override
    public Integer deleteNotInDetailIdList(Long applyId, List<Long> detailIds, List<Long> recordIds) {
        LambdaQueryWrapper<RecordApplyDetailDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RecordApplyDetailDO::getApplyId, applyId);
        if (!ObjectUtils.isEmpty(detailIds)) queryWrapper.notIn(RecordApplyDetailDO::getId, detailIds);
        if (!ObjectUtils.isEmpty(recordIds)) queryWrapper.notIn(RecordApplyDetailDO::getDocumentId, recordIds);
        return recordApplyDetailMapper.delete(queryWrapper);
    }

    @Override
    public boolean updateRecordApplyDetailBatch(List<RecordApplyDetailSaveReqVO> detailUpdateReqVOList) {
        List<RecordApplyDetailDO> bean = BeanUtils.toBean(detailUpdateReqVOList, RecordApplyDetailDO.class);
        return recordApplyDetailMapper.updateBatch(bean);
    }


}