package com.rongwei.bscommon.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.AspCheckItemsDao;
import com.rongwei.bscommon.sys.service.AspCheckItemsService;
import com.rongwei.bsentity.domain.AspCheckItems;
import com.rongwei.bsentity.domain.AspCheckItemsAreaDo;
import com.rongwei.bsentity.dto.EquipmentDisposeDTO;
import com.rongwei.bsentity.enums.PlantEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 设备子系统——设备 服务实现类
 * </p>
 *
 * @author sc
 * @since 2023-11-24
 */
@Service
public class AspCheckItemsServiceImpl extends ServiceImpl<AspCheckItemsDao, AspCheckItems> implements AspCheckItemsService {

    private static final Logger logger = LoggerFactory.getLogger(AspCheckItemsServiceImpl.class);

    @Autowired
    private AspCheckItemsDao aspCheckItemsDao;

    /**
     * 根据ID修改地址信息
     *
     * @param equipmentDisposeDTO
     * @param checkItemsArea
     * @return
     * @date 2024/1/16 16:28
     * @author shangmi
     */
    @Override
    public void updateSiteById(EquipmentDisposeDTO equipmentDisposeDTO, AspCheckItemsAreaDo checkItemsArea) {
        LambdaUpdateWrapper<AspCheckItems> updateWrapper = new LambdaUpdateWrapper<>();
        boolean empty = ObjectUtil.isEmpty(checkItemsArea);
        updateWrapper.eq(AspCheckItems::getId, equipmentDisposeDTO.getEquipmentId())
                .set(AspCheckItems::getTenantid, equipmentDisposeDTO.getRollInPlantId())
                .set(AspCheckItems::getUsedeptid, equipmentDisposeDTO.getRollInDeptId())
                .set(AspCheckItems::getUsedeptname, equipmentDisposeDTO.getRollInDept())
                .set(AspCheckItems::getOwnedfactory, PlantEnum.nameOf(equipmentDisposeDTO.getRollInPlantId()))
                .set(AspCheckItems::getDeviceaddress, equipmentDisposeDTO.getRollInLocation())
                .set(empty, AspCheckItems::getCheckitemareaid, checkItemsArea.getId())
                .set(empty, AspCheckItems::getChargeorgid, checkItemsArea.getAreachargerorgid());
        aspCheckItemsDao.update(null, updateWrapper);
    }

    @Override
    public void updateStateById(String equipmentId, Date newDate) {
        LambdaUpdateWrapper<AspCheckItems> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AspCheckItems::getId, equipmentId)
                .set(AspCheckItems::getDevicestatus, "报废")
                .set(AspCheckItems::getFactobsoledate, newDate);
        aspCheckItemsDao.update(null, updateWrapper);
    }
}
