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.SgMachineMapper;
import com.njcn.advance.pojo.param.govern.voltage.SgMachineParam;
import com.njcn.advance.pojo.param.govern.voltage.SgUserParam;
import com.njcn.advance.pojo.po.govern.voltage.SgMachine;
import com.njcn.advance.pojo.po.govern.voltage.SgSensitiveUnit;
import com.njcn.advance.pojo.po.govern.voltage.SgUser;
import com.njcn.advance.pojo.vo.govern.voltage.SgMachineVO;
import com.njcn.advance.service.govern.voltage.ISgMachineService;
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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2024-03-14
 */
@Service
@RequiredArgsConstructor
public class SgMachineServiceImpl extends ServiceImpl<SgMachineMapper, SgMachine> implements ISgMachineService {

    private final ISgSensitiveUnitService sgSensitiveUnitService;

    @Override
    public Page<SgMachineVO> sgMachineListByProductLineId(SgMachineParam.SgMachineQueryParam sgMachineQueryParam) {
        if(StrUtil.isBlank(sgMachineQueryParam.getProductLineId())){
            throw new BusinessException(AdvanceResponseEnum.SG_PRODUCT_LINE_ID_MISS);
        }
        QueryWrapper<SgMachineVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sg_machine.product_line_id",sgMachineQueryParam.getProductLineId())
                .eq("sg_machine.state", DataStateEnum.ENABLE.getCode())
                .orderByDesc("sg_machine.create_time");

        return this.baseMapper.page(new Page<>(PageFactory.getPageNum(sgMachineQueryParam), PageFactory.getPageSize(sgMachineQueryParam)), queryWrapper);
    }

    /**
     * 新增设备
     * @param sgMachineParam 设备数据
     */
    @Override
    public String addMachine(SgMachineParam sgMachineParam) {
        SgMachine sgMachine = new SgMachine();
        checkMachineName(sgMachineParam, false);

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

    /**
     * 更新设备
     * @param updateParam 设备数据
     */
    @Override
    public boolean updateSgMachine(SgMachineParam.SgMachineUpdateParam updateParam) {
        SgMachine sgMachine = new SgMachine();
        checkMachineName(updateParam, true);

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

    /**
     * 根据id逻辑删除设备
     */
    @Override
    public boolean deleteSgMachineData(List<String> ids) {
        LambdaQueryWrapper<SgSensitiveUnit> sgSensitiveUnitLambdaQueryWrapper;
        for (String id : ids) {
            sgSensitiveUnitLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sgSensitiveUnitLambdaQueryWrapper.eq(SgSensitiveUnit::getMachineId, id)
                    .eq(SgSensitiveUnit::getState, DataStateEnum.ENABLE.getCode());
            int count = sgSensitiveUnitService.count(sgSensitiveUnitLambdaQueryWrapper);
            if (count > 0) {
                throw new BusinessException(AdvanceResponseEnum.MACHINE_HAS_UNIT);
            } else {
                this.lambdaUpdate().set(SgMachine::getState, DataStateEnum.DELETED.getCode())
                        .eq(SgMachine::getId, id)
                        .update();
            }

        }
        return true;
    }

    /**
     * 根据生产线获取其所有的设备信息
     * @param productLineId 生产线id
     */
    @Override
    public List<SgMachineVO> listAllMachineByProductLineId(String productLineId) {
        return this.baseMapper.listAllMachineByProductLineId(productLineId);
    }


}
