package com.zt.plat.module.qms.resource.device.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceUseRecordDetailPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceUseRecordDetailRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceUseRecordDetailSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceUseRecordDetailDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.resource.device.dal.mapper.DeviceUseRecordDetailMapper;

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 DeviceUseRecordDetailServiceImpl implements DeviceUseRecordDetailService {
    @Resource
    private DeviceUseRecordDetailMapper deviceUseRecordDetailMapper;

    @Override
    public List<DeviceUseRecordDetailDO> getListByParentId(Long parentId) {
        LambdaQueryWrapper<DeviceUseRecordDetailDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceUseRecordDetailDO::getParentId, parentId);
        return deviceUseRecordDetailMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveList(Long parentId, List<DeviceUseRecordDetailSaveReqVO> list) {
        if(CollUtil.isEmpty(list)) {
            LambdaUpdateWrapper<DeviceUseRecordDetailDO> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(DeviceUseRecordDetailDO::getParentId, parentId);
            deviceUseRecordDetailMapper.delete(updateWrapper);
            return;
        }
        List<DeviceUseRecordDetailDO> hasList = getListByParentId(parentId);
        List<DeviceUseRecordDetailSaveReqVO> updateList = new ArrayList<>();
        List<DeviceUseRecordDetailSaveReqVO> insertList = new ArrayList<>();
        List<DeviceUseRecordDetailDO> deleteList = new ArrayList<>();
        boolean exist = false;
        for(DeviceUseRecordDetailSaveReqVO detail : list){
            detail.setParentId(parentId);
        }
        for (DeviceUseRecordDetailDO has : hasList) {
            exist = false;
            for (DeviceUseRecordDetailSaveReqVO reqVO : list) {
                if (has.getId().equals(reqVO.getId())) {
                    exist = true;
                    updateList.add(reqVO);
                    break;
                }
            }
            if (!exist) {
                deleteList.add(has);
            }
        }
        for (DeviceUseRecordDetailSaveReqVO reqVO : list) {
            exist = false;
            for(DeviceUseRecordDetailDO has : hasList){
                if(has.getId().equals(reqVO.getId())){
                    exist = true;
                    break;
                }
            }
            if(!exist){
                insertList.add(reqVO);
            }
        }
        if(!insertList.isEmpty()){
            List<DeviceUseRecordDetailDO> insertDOList = BeanUtils.toBean(insertList, DeviceUseRecordDetailDO.class);
            deviceUseRecordDetailMapper.insertBatch(insertDOList);
        }
        if(!updateList.isEmpty()){
            List<DeviceUseRecordDetailDO> updateDOList = BeanUtils.toBean(updateList, DeviceUseRecordDetailDO.class);
            deviceUseRecordDetailMapper.updateBatch(updateDOList);
        }
        if(!deleteList.isEmpty()){
            List<Long> deleteIdList = convertList(deleteList, DeviceUseRecordDetailDO::getId);
            deviceUseRecordDetailMapper.deleteByIds(deleteIdList);
        }
    }



    @Override
    public DeviceUseRecordDetailRespVO createDeviceUseRecordDetail(DeviceUseRecordDetailSaveReqVO createReqVO) {
        // 插入
        DeviceUseRecordDetailDO deviceUseRecordDetail = BeanUtils.toBean(createReqVO, DeviceUseRecordDetailDO.class);
        deviceUseRecordDetailMapper.insert(deviceUseRecordDetail);
        // 返回
        return BeanUtils.toBean(deviceUseRecordDetail, DeviceUseRecordDetailRespVO.class);
    }

    @Override
    public void updateDeviceUseRecordDetail(DeviceUseRecordDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceUseRecordDetailExists(updateReqVO.getId());
        // 更新
        DeviceUseRecordDetailDO updateObj = BeanUtils.toBean(updateReqVO, DeviceUseRecordDetailDO.class);
        deviceUseRecordDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceUseRecordDetail(Long id) {
        // 校验存在
        validateDeviceUseRecordDetailExists(id);
        // 删除
        deviceUseRecordDetailMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceUseRecordDetailListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceUseRecordDetailExists(ids);
        // 删除
        deviceUseRecordDetailMapper.deleteByIds(ids);
        }

    private void validateDeviceUseRecordDetailExists(List<Long> ids) {
        List<DeviceUseRecordDetailDO> list = deviceUseRecordDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_USE_RECORD_DETAIL_NOT_EXISTS);
        }
    }

    private void validateDeviceUseRecordDetailExists(Long id) {
        if (deviceUseRecordDetailMapper.selectById(id) == null) {
            throw exception(DEVICE_USE_RECORD_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public DeviceUseRecordDetailDO getDeviceUseRecordDetail(Long id) {
        return deviceUseRecordDetailMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceUseRecordDetailDO> getDeviceUseRecordDetailPage(DeviceUseRecordDetailPageReqVO pageReqVO) {
        return deviceUseRecordDetailMapper.selectPage(pageReqVO);
    }

}