package com.usky.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.iot.domain.DmpProductAttribute;
import com.usky.iot.domain.DmpProductCommand;
import com.usky.iot.mapper.DmpProductCommandMapper;
import com.usky.iot.service.DmpProductCommandService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.iot.service.vo.DmpProductCommandRequestVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 产品命令表 服务实现类
 * </p>
 *
 * @author han
 * @since 2024-10-30
 */
@Service
public class DmpProductCommandServiceImpl extends AbstractCrudService<DmpProductCommandMapper, DmpProductCommand> implements DmpProductCommandService {

    public boolean checkNameUnique(DmpProductCommand dmpProductCommand) {
        Integer id = null == dmpProductCommand.getId() ? -1 : dmpProductCommand.getId();
        LambdaQueryWrapper<DmpProductCommand> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DmpProductCommand::getCommandName,dmpProductCommand.getCommandName())
                .eq(DmpProductCommand::getTenantId, SecurityUtils.getTenantId())
                .eq(DmpProductCommand::getDeleteFlag,0);
        DmpProductCommand one = this.getOne(queryWrapper);
        return null != one && !Objects.equals(one.getId(), id);
    }

    @Override
    public void add(DmpProductCommand dmpProductCommand){
        if (checkNameUnique(dmpProductCommand)){
            throw new BusinessException("新增产品命令信息'" + dmpProductCommand.getCommandName() + "'失败，产品命令信息已存在");
        }
        dmpProductCommand.setDeleteFlag(0);
        dmpProductCommand.setCreatedBy(SecurityUtils.getUsername());
        dmpProductCommand.setCreatedTime(LocalDateTime.now());
        dmpProductCommand.setTenantId(SecurityUtils.getTenantId());
        this.save(dmpProductCommand);
    }

    @Override
    public void edit(DmpProductCommand dmpProductCommand){
        dmpProductCommand.setUpdatedBy(SecurityUtils.getUsername());
        dmpProductCommand.setUpdatedTime(LocalDateTime.now());
        if (checkNameUnique(dmpProductCommand)){
            throw new BusinessException("修改产品命令信息'" + dmpProductCommand.getCommandName() + "'失败，产品命令信息已存在");
        }
        this.updateById(dmpProductCommand);
    }

    @Override
    public CommonPage<DmpProductCommand> page(DmpProductCommandRequestVO requestVO){
        IPage<DmpProductCommand> page = new Page<>(requestVO.getCurrent(), requestVO.getSize());
        LambdaQueryWrapper<DmpProductCommand> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery
                .like(StringUtils.isNotBlank(requestVO.getProductCode()),DmpProductCommand::getProductCode,requestVO.getProductCode())
                .like(StringUtils.isNotBlank(requestVO.getCommandCode()),DmpProductCommand::getCommandCode,requestVO.getCommandCode())
                .like(StringUtils.isNotBlank(requestVO.getCommandName()),DmpProductCommand::getCommandName,requestVO.getCommandName())
                .like(StringUtils.isNotBlank(requestVO.getCommandDescribe()),DmpProductCommand::getCommandDescribe,requestVO.getCommandDescribe())
                .eq(DmpProductCommand::getTenantId,SecurityUtils.getTenantId())
                .eq(DmpProductCommand::getDeleteFlag,0);
        page = this.page(page,lambdaQuery);
        return new CommonPage<>(page.getRecords(),page.getTotal(),page.getCurrent(),page.getSize());
    }

    @Override
    public void remove(Integer id){
        DmpProductCommand dmpProductCommand = this.getById(id);
        Optional.ofNullable(dmpProductCommand).orElseThrow(() -> new BusinessException("产品命令信息不存在"));
        dmpProductCommand.setDeleteFlag(1);
        this.updateById(dmpProductCommand);
    }
}
