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.extension.service.impl.ServiceImpl;
import com.njcn.advance.enums.AdvanceResponseEnum;
import com.njcn.advance.mapper.govern.voltage.SgIncomingLineMapper;
import com.njcn.advance.pojo.param.govern.voltage.IncomingLineParam;
import com.njcn.advance.pojo.po.govern.voltage.SgIncomingLine;
import com.njcn.advance.pojo.vo.govern.voltage.IncomingLineVO;
import com.njcn.advance.pojo.vo.govern.voltage.SgOption;
import com.njcn.advance.service.govern.voltage.ISgIncomingLineService;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2024-02-29
 */
@Service
public class SgIncomingLineServiceImpl extends ServiceImpl<SgIncomingLineMapper, SgIncomingLine> implements ISgIncomingLineService {

    /**
     * 根据用户id查询进线数据
     * @param userId 用户id
     */
    @Override
    public List<IncomingLineVO> incomingLineList(String userId) {
        if(StrUtil.isBlank(userId)){
            throw new BusinessException(AdvanceResponseEnum.SG_USER_ID_MISS);
        }
        return this.baseMapper.queryLineByUserId(userId);
    }

    /**
     * 新增业务用户的进线数据
     *
     * @param incomingLineParam 业务用户数据
     */
    @Override
    public String addIncomingLine(IncomingLineParam incomingLineParam) {
        checkIncomingLineName(incomingLineParam, false);
        SgIncomingLine sgIncomingLine = new SgIncomingLine();
        BeanUtil.copyProperties(incomingLineParam, sgIncomingLine);
        //默认为正常状态
        sgIncomingLine.setState(DataStateEnum.ENABLE.getCode());
        this.save(sgIncomingLine);
        return sgIncomingLine.getId();
    }

    /**
     * 修改业务用户的进线数据
     *
     * @param updateParam 业务用户数据
     */
    @Override
    public boolean updateIncomingLine(IncomingLineParam.IncomingLineUpdateParam updateParam) {
        checkIncomingLineName(updateParam, true);
        SgIncomingLine sgIncomingLine = new SgIncomingLine();
        BeanUtil.copyProperties(updateParam, sgIncomingLine);
        return this.updateById(sgIncomingLine);
    }

    /**
     * 逻辑删除进线数据
     * @param ids 进线id
     */
    @Override
    public boolean deleteIncomingLineData(List<String> ids) {
        return this.lambdaUpdate()
                .set(SgIncomingLine::getState, DataStateEnum.DELETED.getCode())
                .in(SgIncomingLine::getId, ids)
                .update();
    }

    @Override
    public List<SgOption> lineList() {
        return this.baseMapper.lineList();
    }

    /**
     * 校验进线名称是否有重复的
     * @param incomingLineParam 进线数据
     * @param isExcludeSelf 是否排除自己
     */
    private void checkIncomingLineName(IncomingLineParam incomingLineParam, boolean isExcludeSelf) {
        LambdaQueryWrapper<SgIncomingLine> sgUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sgUserLambdaQueryWrapper
                .eq(SgIncomingLine::getName, incomingLineParam.getName())
                .eq(SgIncomingLine::getUserId, incomingLineParam.getUserId())
                .eq(SgIncomingLine::getState, DataStateEnum.ENABLE.getCode());
        //更新的时候，需排除当前记录
        if (isExcludeSelf) {
            if (incomingLineParam instanceof IncomingLineParam.IncomingLineUpdateParam) {
                sgUserLambdaQueryWrapper.ne(SgIncomingLine::getId, ((IncomingLineParam.IncomingLineUpdateParam) incomingLineParam).getId());
            }
        }
        int countByAccount = this.count(sgUserLambdaQueryWrapper);
        //大于等于1个则表示重复
        if (countByAccount >= 1) {
            throw new BusinessException(AdvanceResponseEnum.SG_USER_NAME_REPEAT);
        }

    }
}
