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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.controller.admin.recordassign.vo.RecordAssignPageReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordassign.vo.RecordAssignRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordassign.vo.RecordAssignSaveReqVO;
import com.zt.plat.module.qms.resource.record.dal.dataobject.recordassign.RecordAssignDO;
import com.zt.plat.module.qms.resource.record.dal.mapper.RecordAssignMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.RECORD_ASSIGN_NOT_EXISTS;

/**
 * 文件记录分发号,记录文件分发与线下对应的分发号对应，且标记到目标人物 Service 实现类
 *
 * @author YBP
 */
@Service
@Validated
public class RecordAssignServiceImpl implements RecordAssignService {

    @Resource
    private RecordAssignMapper recordAssignMapper;

    @Override
    public RecordAssignRespVO createRecordAssign(RecordAssignSaveReqVO createReqVO) {
        // 插入
        RecordAssignDO recordAssign = BeanUtils.toBean(createReqVO, RecordAssignDO.class);
        recordAssignMapper.insert(recordAssign);
        String mainId = createReqVO.getMainId();
        if (ObjectUtils.isEmpty(mainId)) recordAssign.setMainId(String.valueOf(recordAssign.getId())); //原始父级Id
        recordAssign.setCurrentFlag(1);
        recordAssignMapper.updateById(recordAssign);
        // 返回
        return BeanUtils.toBean(recordAssign, RecordAssignRespVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRecordAssign(RecordAssignSaveReqVO updateReqVO) {
        Long id = updateReqVO.getId();

        Integer collectionFlag = updateReqVO.getCollectionFlag();
        RecordAssignDO updateObj = BeanUtils.toBean(updateReqVO, RecordAssignDO.class);
        if (!ObjectUtils.isEmpty(collectionFlag) && collectionFlag == 1) { // 修改
            updateReqVO.setId(null);
            createRecordAssign(updateReqVO); // 创建新记录
            // 校验存在
            validateRecordAssignExists(id);
            // 更新
            RecordAssignDO recordAssignDO = new RecordAssignDO();
            recordAssignDO.setId(id);
            recordAssignDO.setCurrentFlag(0); // 更新成历史记录
            recordAssignMapper.updateById(recordAssignDO);
        }
        else recordAssignMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecordAssign(Long id) {
        // 校验存在
        validateRecordAssignExists(id);
        // 删除
        recordAssignMapper.deleteById(id);
    }

    @Override
        public void deleteRecordAssignListByIds(List<Long> ids) {
        // 校验存在
        validateRecordAssignExists(ids);
        // 删除
        recordAssignMapper.deleteByIds(ids);
        }

    private void validateRecordAssignExists(List<Long> ids) {
        List<RecordAssignDO> list = recordAssignMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RECORD_ASSIGN_NOT_EXISTS);
        }
    }

    private void validateRecordAssignExists(Long id) {
        if (recordAssignMapper.selectById(id) == null) {
            throw exception(RECORD_ASSIGN_NOT_EXISTS);
        }
    }

    @Override
    public RecordAssignDO getRecordAssign(Long id) {
        return recordAssignMapper.selectById(id);
    }

    @Override
    public PageResult<RecordAssignDO> getRecordAssignPage(RecordAssignPageReqVO pageReqVO) {
        return recordAssignMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RecordAssignRespVO> selectListByTargetIds(List<Long> targetIds) {
        LambdaQueryWrapper<RecordAssignDO> queryWrapper = Wrappers.lambdaQuery(RecordAssignDO.class);
        queryWrapper.eq(RecordAssignDO::getCurrentFlag, 1);
        queryWrapper.in(RecordAssignDO::getTargetId, targetIds);
        List<RecordAssignDO> recordAssignDOList = recordAssignMapper.selectList(queryWrapper);
        return BeanUtils.toBean(recordAssignDOList, RecordAssignRespVO.class);
    }

}