package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppBaseLedMapper;
import org.wcs.mapper.intf.AppBaseLedService;
import org.wcs.model.dto.serve.led.QueryLedReq;
import org.wcs.model.po.app.AppBaseLed;

import java.util.List;

/**
 * LED 屏基础资料数据库操作
 */
@Service
public class AppBaseLedDao extends ServiceImpl<AppBaseLedMapper, AppBaseLed> implements AppBaseLedService {

    /**
     * 查询LED屏基础资料
     * @param queryLedReq 查询LED屏基础资料请求
     * @return 查询结果
     */
    @Override
    public List<AppBaseLed> queryLed(QueryLedReq queryLedReq) {
        if(queryLedReq == null) {
            return null;
        }
        LambdaQueryWrapper<AppBaseLed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(queryLedReq.getLedIp() != null, AppBaseLed::getLedIp, queryLedReq.getLedIp())
                .like(queryLedReq.getLocation() != null, AppBaseLed::getLocation, queryLedReq.getLocation())
                .eq(queryLedReq.getLedBrand() != null, AppBaseLed::getLedBrand, queryLedReq.getLedBrand());
        queryWrapper.orderByAsc(AppBaseLed::getId);
        try {
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 添加LED屏基础资料
     * @param appBaseLed LED屏基础资料
     * @return 添加结果
     */
    @Override
    public int insert(AppBaseLed appBaseLed) {
        try {
            return super.baseMapper.insert(appBaseLed);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 修改LED屏基础资料
     * @param appBaseLed LED屏基础资料
     * @return 修改结果
     */
    @Override
    public int update(AppBaseLed appBaseLed) {
        LambdaUpdateWrapper<AppBaseLed> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppBaseLed::getId, appBaseLed.getId());
        updateWrapper.set(appBaseLed.getLedIp() != null, AppBaseLed::getLedIp, appBaseLed.getLedIp())
                .set(appBaseLed.getLedBrand() != null, AppBaseLed::getLedBrand, appBaseLed.getLedBrand())
                .set(appBaseLed.getHeight() != null, AppBaseLed::getHeight, appBaseLed.getHeight())
                .set(appBaseLed.getWidth() != null, AppBaseLed::getWidth, appBaseLed.getWidth())
                .set(appBaseLed.getColorType() != null, AppBaseLed::getColorType, appBaseLed.getColorType())
                .set(appBaseLed.getLocation() != null, AppBaseLed::getLocation, appBaseLed.getLocation())
                .set(appBaseLed.getIsExternal() != null, AppBaseLed::getIsExternal, appBaseLed.getIsExternal())
                .set(appBaseLed.getRemark() != null, AppBaseLed::getRemark, appBaseLed.getRemark());
        try {
            return super.baseMapper.update(appBaseLed, updateWrapper);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 删除LED屏基础资料
     * @param id LED屏基础资料ID
     * @return 删除结果
     */
    @Override
    public int delete(String id) {
        LambdaQueryWrapper<AppBaseLed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppBaseLed::getId, id);
        try {
            return super.baseMapper.delete(queryWrapper);
        } catch (Exception e) {
            return 0;
        }
    }
}
