package vip.xiaonuo.coldchain.modular.monitordevicepower.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.coldchain.modular.monitordevice.entity.MonitorDevice;
import vip.xiaonuo.coldchain.modular.monitordevice.service.MonitorDeviceService;
import vip.xiaonuo.coldchain.modular.monitordevicepower.entity.MonitorDevicePower;
import vip.xiaonuo.coldchain.modular.monitordevicepower.mapper.MonitorDevicePowerMapper;
import vip.xiaonuo.coldchain.modular.monitordevicepower.param.MonitorDevicePowerAddParam;
import vip.xiaonuo.coldchain.modular.monitordevicepower.param.MonitorDevicePowerEditParam;
import vip.xiaonuo.coldchain.modular.monitordevicepower.param.MonitorDevicePowerIdParam;
import vip.xiaonuo.coldchain.modular.monitordevicepower.param.MonitorDevicePowerPageParam;
import vip.xiaonuo.coldchain.modular.monitordevicepower.service.MonitorDevicePowerService;
import vip.xiaonuo.common.enums.CommonDeleteFlagEnum;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;

import java.util.List;

@Service
public class MonitorDevicePowerServiceImpl extends ServiceImpl<MonitorDevicePowerMapper, MonitorDevicePower> implements MonitorDevicePowerService {

    @Resource
    private MonitorDeviceService monitorDeviceService;

    @Override
    public Page<MonitorDevicePower> page(MonitorDevicePowerPageParam monitorDevicePowerPageParam) {
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
        QueryWrapper<MonitorDevicePower> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(StrUtil.isNotBlank(monitorDevicePowerPageParam.getPowerName())
                , MonitorDevicePower::getPowerName, monitorDevicePowerPageParam.getPowerName())
                .eq(MonitorDevicePower::getCreateOrg,loginUser.getOrgId())
                .eq(StrUtil.isNotBlank(monitorDevicePowerPageParam.getPowerCode())
                , MonitorDevicePower::getPowerCode, monitorDevicePowerPageParam.getPowerCode())
                .orderByAsc(MonitorDevicePower::getSortCode);
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(MonitorDevicePowerAddParam monitorDevicePowerAddParam) {
        if (!getPowerListByName(monitorDevicePowerAddParam.getPowerName()).isEmpty()) {
            throw new CommonException("该电源编号已存在！请重新输入");
        }
        MonitorDevicePower monitorDevicePower = BeanUtil.copyProperties(monitorDevicePowerAddParam, MonitorDevicePower.class);
        this.save(monitorDevicePower);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(MonitorDevicePowerEditParam monitorDevicePowerEditParam) {
        MonitorDevicePower monitorDevicePower = this.queryEntity(monitorDevicePowerEditParam.getId());
        BeanUtil.copyProperties(monitorDevicePowerEditParam, monitorDevicePower);
        this.updateById(monitorDevicePower);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<MonitorDevicePowerIdParam> monitorDevicePowerIdParamList) {
        monitorDevicePowerIdParamList.forEach(idParam -> {
            MonitorDevicePower monitorDevicePower = queryEntity(idParam.getId());
            List<MonitorDevice> monitorDeviceList = monitorDeviceService.getMonitorDeviceByPowerCode(monitorDevicePower.getPowerCode());
            if (!monitorDeviceList.isEmpty()) {
                throw new CommonException("该电源已被使用，无法删除！");
            }
        });
        this.removeByIds(CollStreamUtil.toList(monitorDevicePowerIdParamList, MonitorDevicePowerIdParam::getId));
    }

    @Override
    public MonitorDevicePower detail(MonitorDevicePowerIdParam monitorDevicePowerIdParam) {
        return this.queryEntity(monitorDevicePowerIdParam.getId());
    }

    @Override
    public MonitorDevicePower queryEntity(String id) {
        MonitorDevicePower monitorDevicePower = this.getById(id);
        if (ObjectUtil.isEmpty(monitorDevicePower)) {
            throw new CommonException("电源不存在，id值为：{}", id);
        }
        return monitorDevicePower;
    }

    @Override
    public List<MonitorDevicePower> myList() {
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
        LambdaQueryWrapper<MonitorDevicePower> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MonitorDevicePower::getCreateOrg,loginUser.getOrgId())
                .eq(MonitorDevicePower::getDeleteFlag, CommonDeleteFlagEnum.NOT_DELETE);
        return list(queryWrapper);
    }

    public List<MonitorDevicePower> getPowerListByName(String powerName) {
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
        LambdaQueryWrapper<MonitorDevicePower> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MonitorDevicePower::getCreateOrg,loginUser.getOrgId())
                .eq(MonitorDevicePower::getPowerName, powerName)
                .eq(MonitorDevicePower::getDeleteFlag, CommonDeleteFlagEnum.NOT_DELETE);
        return list(queryWrapper);
    }
}
