package com.ktg.iscs.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.core.text.Convert;
import com.ktg.iscs.domain.IsHardware;
import com.ktg.iscs.domain.dto.hardware.HardwareAutomaticDTO;
import com.ktg.iscs.domain.dto.hardware.HardwarePageDTO;
import com.ktg.iscs.mapper.IsHardwareMapper;
import com.ktg.iscs.service.IIsHardwareService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 硬件Service业务层处理
 *
 * @author cgj
 * @date 2024-10-16
 */
@Service
public class IsHardwareServiceImpl extends ServiceImpl<IsHardwareMapper, IsHardware> implements IIsHardwareService
{
    @Autowired
    private IsHardwareMapper isHardwareMapper;

    /**
     * 查询硬件
     *
     * @param id 硬件主键
     * @return 硬件
     */
    @Override
    public IsHardware selectIsHardwareById(Long id)
    {
        return isHardwareMapper.selectIsHardwareById(id);
    }

    /**
     * 查询硬件列表
     *
     * @param isHardware 硬件
     * @return 硬件
     */
    @Override
    public List<IsHardware> selectIsHardwareList(IsHardware isHardware)
    {
        return isHardwareMapper.selectIsHardwareList(isHardware);
    }

    /**
     * 新增硬件
     *
     * @param isHardware 硬件
     * @return 结果
     */
    @Override
    public int insertIsHardware(IsHardware isHardware)
    {
        // 断言判断
        Assert.isFalse(StringUtils.isBlank(isHardware.getHardwareCode()), "硬件编码不可为空！");
        Assert.isFalse(StringUtils.isBlank(isHardware.getHardwareName()), "硬件名称不可为空！");
        // 检查hardwareCode是否被占用
        List<IsHardware> list = list(Wrappers.<IsHardware>lambdaQuery()
                .eq(IsHardware::getHardwareCode, isHardware.getHardwareCode()));
        Assert.isTrue(list.isEmpty(), isHardware.getHardwareCode() + "已被使用！");
        // 检查序列号是否被占用
        List<IsHardware> list1 = list(Wrappers.<IsHardware>lambdaQuery()
                .eq(IsHardware::getSerialNumber, isHardware.getSerialNumber()));
        Assert.isTrue(list1.isEmpty(), isHardware.getSerialNumber() + "已被使用！");
        return isHardwareMapper.insertIsHardware(isHardware);
    }

    /**
     * 修改硬件
     *
     * @param isHardware 硬件
     * @return 结果
     */
    @Override
    public int updateIsHardware(IsHardware isHardware)
    {
        // 断言判断
        Assert.notNull(isHardware.getId(), "id不可为空！");
        Assert.isFalse(StringUtils.isBlank(isHardware.getHardwareCode()), "硬件编码不可为空！");
        Assert.isFalse(StringUtils.isBlank(isHardware.getHardwareName()), "硬件名称不可为空！");
        // 检查hardwareCode是否被占用
        List<IsHardware> list = list(Wrappers.<IsHardware>lambdaQuery()
                .eq(IsHardware::getHardwareCode, isHardware.getHardwareCode())
                .ne(IsHardware::getId, isHardware.getId()));
        Assert.isTrue(list.isEmpty(), isHardware.getHardwareCode() + "已被使用！");
        // 检查序列号是否被占用
        List<IsHardware> list1 = list(Wrappers.<IsHardware>lambdaQuery()
                .eq(IsHardware::getSerialNumber, isHardware.getSerialNumber())
                .ne(IsHardware::getId, isHardware.getId()));
        Assert.isTrue(list1.isEmpty(), isHardware.getSerialNumber() + "已被使用！");
        return isHardwareMapper.updateIsHardware(isHardware);
    }

    /**
     * 批量删除硬件
     *
     * @param ids 需要删除的硬件主键
     * @return 结果
     */
    @Override
    public int deleteIsHardwareByIds(String ids)
    {
        Assert.notBlank(ids, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(ids);
        return isHardwareMapper.deleteIsHardwareByIds(longIds);
    }

    /**
     * 删除硬件信息
     *
     * @param id 硬件主键
     * @return 结果
     */
    @Override
    public int deleteIsHardwareById(Long id)
    {
        return isHardwareMapper.deleteIsHardwareById(id);
    }

    @Override
    public String importHardware(List<IsHardware> hardwareList, Boolean isUpdateSupport, String operName) {
        if (hardwareList == null || hardwareList.isEmpty()) {
            return "导入数据为空";
        }
        int successCount = 0;
        int failureCount = 0;
        for (IsHardware hardware : hardwareList) {
            // 去除空格
            String machineryCode = hardware.getHardwareCode().trim();
            IsHardware existing = getOne(Wrappers.<IsHardware>lambdaQuery().eq(IsHardware::getHardwareCode, machineryCode));
            if (existing != null) {
                if (isUpdateSupport) {
                    // 更新数据
                    hardware.setId(existing.getId()); // 确保使用现有 ID 进行更新
                    isHardwareMapper.updateIsHardware(hardware);
                    successCount++;
                } else {
                    // 不更新数据
                    failureCount++;
                }
            } else {
                // 新增数据
                isHardwareMapper.insertIsHardware(hardware);
                successCount++;
            }
        }
        return String.format("操作用户：%s，导入完成，成功 %d 条，失败 %d 条。", operName, successCount, failureCount);
    }

    @Override
    public Boolean insertIsHardware(HardwareAutomaticDTO dto) {
        // 断言判断
        Assert.notNull(dto.getId(), "id不可为空！");
        if (StringUtils.isBlank(dto.getStatus())
                || dto.getUsedTimes() != null
                || dto.getUsedLife() != null) {
            update(Wrappers.<IsHardware>lambdaUpdate()
                    .eq(IsHardware::getId, dto.getId())
                    .set(StringUtils.isBlank(dto.getStatus()), IsHardware::getStatus, dto.getStatus())
                    .set(dto.getUsedTimes() != null, IsHardware::getUsedTimes, dto.getUsedTimes())
                    .set(dto.getUsedLife() != null, IsHardware::getUsedLife, dto.getUsedLife())
                    .set(IsHardware::getUpdateTime, new Date()));
        }
        return true;
    }

    @Override
    public Page<IsHardware> getIsHardwarePage(Page page, HardwarePageDTO dto) {
        Page<IsHardware> result = page(page, Wrappers.<IsHardware>lambdaQuery()
                .like(StringUtils.isNotBlank(dto.getHardwareCode()), IsHardware::getHardwareCode, dto.getHardwareCode())
                .like(StringUtils.isNotBlank(dto.getHardwareName()), IsHardware::getHardwareName, dto.getHardwareName())
                .eq(StringUtils.isNotBlank(dto.getStatus()), IsHardware::getStatus, dto.getStatus())
                .eq(dto.getHardwareTypeId() != null, IsHardware::getHardwareTypeId, dto.getHardwareTypeId())
                .like(StringUtils.isNotBlank(dto.getHardwareSpec()), IsHardware::getHardwareSpec, dto.getHardwareSpec())
                .ge(StringUtils.isNotBlank(dto.getStartTime()), IsHardware::getCreateTime, dto.getStartTime())
                .le(StringUtils.isNotBlank(dto.getEndTime()), IsHardware::getCreateTime, dto.getEndTime())
                .eq(dto.getWorkshopId() != null, IsHardware::getWorkshopId, dto.getWorkshopId())
                .orderByDesc(IsHardware::getId));
        return result;
    }
}
