package com.njcn.device.pms.service.majornetwork.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
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.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pms.enums.PmsDeviceResponseEnum;
import com.njcn.device.pms.mapper.majornetwork.TransformerMapper;
import com.njcn.device.pms.pojo.param.MonitorAuditParam;
import com.njcn.device.pms.pojo.param.MonitorStatus;
import com.njcn.device.pms.pojo.param.TransformerParam;
import com.njcn.device.pms.pojo.po.Transformer;
import com.njcn.device.pms.service.majornetwork.ITransformerService;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.po.Dept;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 变压器台账 服务实现类
 * </p>
 *
 * @author wr
 * @since 2023-02-20
 */
@Service
@RequiredArgsConstructor
public class TransformerServiceImpl extends ServiceImpl<TransformerMapper, Transformer> implements ITransformerService {

    private final DeptFeignClient deptFeignClient;

    @Override
    public Page<Transformer> getAllTransformerList(MonitorAuditParam baseParam) {
        LambdaQueryWrapper<Transformer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .like(StrUtil.isNotBlank(baseParam.getSubmitName()), Transformer::getName, baseParam.getSubmitName())
                .eq(baseParam.getStatus() != null, Transformer::getType, baseParam.getStatus())
                .eq(Transformer::getStatus, DataStateEnum.ENABLE.getCode());
        return this.page(new Page<>(baseParam.getPageNum(), baseParam.getPageSize()),lambdaQueryWrapper);
    }

    @Override
    public boolean addTransformer(TransformerParam param) {
        int count = this.count(new LambdaQueryWrapper<Transformer>()
                .eq(Transformer::getId, param.getId())
                .eq(Transformer::getStatus, DataStateEnum.ENABLE.getCode())

        );
        if(count>0){
            throw new BusinessException(PmsDeviceResponseEnum.TRANSFORMER_ID_REPEAT);
        }
        Transformer transformer = BeanUtil.copyProperties(param, Transformer.class);
        transformer.setStatus(1);
        return this.save(transformer);
    }

    @Override
    public boolean updateTransformer(TransformerParam param) {
        Transformer transformer = BeanUtil.copyProperties(param, Transformer.class);
        return this.updateById(transformer);
    }

    @Override
    public boolean delTransformer(List<String> ids) {
        Assert.isTrue(CollectionUtil.isNotEmpty(ids) , "删除变压器id为空");
        return  this.removeByIds(ids);
    }

    @Override
    public boolean updateStatus(MonitorStatus.Status monitorParam) {
        LambdaUpdateWrapper<Transformer> update = new LambdaUpdateWrapper<>();
        update.set(Transformer::getStatus, monitorParam.getStatus())
                .in(Transformer::getId, monitorParam.getId());
        return this.update(update);
    }

}
