package com.njcn.advance.service.govern.voltage.impl;

import cn.hutool.core.bean.BeanUtil;
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 com.njcn.advance.enums.AdvanceResponseEnum;
import com.njcn.advance.mapper.govern.voltage.SgSensitiveUnitMapper;
import com.njcn.advance.pojo.param.govern.voltage.SgMachineParam;
import com.njcn.advance.pojo.param.govern.voltage.SgSensitiveUnitParam;
import com.njcn.advance.pojo.po.govern.voltage.SgMachine;
import com.njcn.advance.pojo.po.govern.voltage.SgSensitiveUnit;
import com.njcn.advance.pojo.vo.govern.voltage.SgSensitiveUnitVO;
import com.njcn.advance.service.govern.voltage.ISgSensitiveUnitService;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.web.factory.PageFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2024-03-14
 */
@Service
public class SgSensitiveUnitServiceImpl extends ServiceImpl<SgSensitiveUnitMapper, SgSensitiveUnit> implements ISgSensitiveUnitService {

    /**
     * 根据设备id查询下属所有的元器件数据
     *
     * @param sgSensitiveUnitQueryParam 查询条件
     */
    @Override
    public Page<SgSensitiveUnitVO> sgSensitiveListByMachineId(SgSensitiveUnitParam.SgSensitiveUnitQueryParam sgSensitiveUnitQueryParam) {
        if(StrUtil.isBlank(sgSensitiveUnitQueryParam.getMachineId())){
            throw new BusinessException(AdvanceResponseEnum.SG_MACHINE_ID_MISS);
        }
        QueryWrapper<SgSensitiveUnitVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sg_sensitive_unit.machine_id",sgSensitiveUnitQueryParam.getMachineId())
                .eq("sg_sensitive_unit.state", DataStateEnum.ENABLE.getCode())
                .orderByDesc("sg_sensitive_unit.create_time");
        return this.baseMapper.page(new Page<>(PageFactory.getPageNum(sgSensitiveUnitQueryParam), PageFactory.getPageSize(sgSensitiveUnitQueryParam)), queryWrapper);
    }

    /**
     * 新增元器件
     * @param sgSensitiveUnitParam 元器件数据
     */
    @Override
    public String addSensitiveUnit(SgSensitiveUnitParam sgSensitiveUnitParam) {
        SgSensitiveUnit sgSensitiveUnit = new SgSensitiveUnit();
        checkSensitiveUnitName(sgSensitiveUnitParam, false);

        BeanUtil.copyProperties(sgSensitiveUnitParam, sgSensitiveUnit);
        //默认为正常状态
        sgSensitiveUnit.setState(DataStateEnum.ENABLE.getCode());
        this.save(sgSensitiveUnit);
        return sgSensitiveUnit.getId();
    }
    /**
     * 校验参数，检查是否存在相同名称的业务用户
     */
    private void checkSensitiveUnitName(SgSensitiveUnitParam sgSensitiveUnitParam, boolean isExcludeSelf) {
        LambdaQueryWrapper<SgSensitiveUnit> sgUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sgUserLambdaQueryWrapper
                .eq(SgSensitiveUnit::getName, sgSensitiveUnitParam.getName())
                .eq(SgSensitiveUnit::getState, DataStateEnum.ENABLE.getCode());
        //更新的时候，需排除当前记录
        if (isExcludeSelf) {
            if (sgSensitiveUnitParam instanceof SgSensitiveUnitParam.SgSensitiveUnitUpdateParam) {
                sgUserLambdaQueryWrapper.ne(SgSensitiveUnit::getId, ((SgSensitiveUnitParam.SgSensitiveUnitUpdateParam) sgSensitiveUnitParam).getId());
            }
        }
        int countByAccount = this.count(sgUserLambdaQueryWrapper);
        //大于等于1个则表示重复
        if (countByAccount >= 1) {
            throw new BusinessException(AdvanceResponseEnum.SG_USER_NAME_REPEAT);
        }
    }

    /**
     * 更新元器件
     *
     * @param updateParam 元器件数据
     */
    @Override
    public boolean updateSgSensitiveUnit(SgSensitiveUnitParam.SgSensitiveUnitUpdateParam updateParam) {
        SgSensitiveUnit sgSensitiveUnit = new SgSensitiveUnit();
        checkSensitiveUnitName(updateParam, true);

        BeanUtil.copyProperties(updateParam, sgSensitiveUnit);
        return this.updateById(sgSensitiveUnit);
    }

    /**
     * 删除元器件
     */
    @Override
    public boolean deleteSgSensitiveUnitData(List<String> ids) {
        return this.lambdaUpdate()
                .set(SgSensitiveUnit::getState, DataStateEnum.DELETED.getCode())
                .in(SgSensitiveUnit::getId, ids)
                .update();
    }

    @Override
    public List<SgSensitiveUnitVO> listAllUnitByMachineId(String machineId) {
        if(StrUtil.isBlank(machineId)){
            return new ArrayList<>();
        }
        return this.baseMapper.listAllUnitByMachineId(machineId);
    }
}
