package com.njcn.gather.icd.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.exception.BusinessException;
import com.njcn.gather.icd.mapper.PqIcdPathMapper;
import com.njcn.gather.icd.pojo.param.PqIcdPathParam;
import com.njcn.gather.icd.pojo.po.PqIcdPath;
import com.njcn.gather.icd.service.IPqIcdPathService;
import com.njcn.gather.pojo.enums.DetectionResponseEnum;
import com.njcn.web.factory.PageFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author caozehui
 * @date 2025-02-10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PqIcdPathServiceImpl extends ServiceImpl<PqIcdPathMapper, PqIcdPath> implements IPqIcdPathService {

    @Override
    public List<PqIcdPath> listAll() {
        return this.lambdaQuery().eq(PqIcdPath::getState, DataStateEnum.ENABLE.getCode()).list();
    }

    @Override
    public Page<PqIcdPath> listIcd(PqIcdPathParam.QueryParam param) {
        LambdaQueryWrapper<PqIcdPath> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PqIcdPath::getState, DataStateEnum.ENABLE.getCode())
                .like(StrUtil.isNotBlank(param.getName()), PqIcdPath::getName, param.getName())
                .orderByDesc(PqIcdPath::getCreateTime);
        Page<PqIcdPath> page = this.page(new Page<>(PageFactory.getPageNum(param), PageFactory.getPageSize(param)), wrapper);
        return page;
    }

    @Override
    @Transactional
    public boolean addIcd(PqIcdPathParam param) {
        param.setName(param.getName().trim());
        this.checkRepeat(param, false);
        PqIcdPath pqIcdPath = new PqIcdPath();
        BeanUtils.copyProperties(param, pqIcdPath);
        pqIcdPath.setState(DataStateEnum.ENABLE.getCode());
        return this.save(pqIcdPath);
    }

    @Override
    @Transactional
    public boolean updateIcd(PqIcdPathParam.UpdateParam param) {
        param.setName(param.getName().trim());
        this.checkRepeat(param, true);
        PqIcdPath pqIcdPath = new PqIcdPath();
        BeanUtils.copyProperties(param, pqIcdPath);
        return this.updateById(pqIcdPath);
    }

    @Override
    @Transactional
    public boolean deleteIcd(List<String> ids) {
        return this.lambdaUpdate().in(PqIcdPath::getId, ids).set(PqIcdPath::getState, DataStateEnum.DELETED.getCode()).update();
    }

    @Override
    public PqIcdPath getIcdByDevType(String devTypeId) {
        return this.baseMapper.selectIcdByDevType(devTypeId);
    }

    private void checkRepeat(PqIcdPathParam param, boolean isExcludeSelf) {
        LambdaQueryWrapper<PqIcdPath> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PqIcdPath::getName, param.getName())
                .eq(PqIcdPath::getState, DataStateEnum.ENABLE.getCode());
        if (isExcludeSelf) {
            if (param instanceof PqIcdPathParam.UpdateParam) {
                wrapper.ne(PqIcdPath::getId, ((PqIcdPathParam.UpdateParam) param).getId());
            }
        }
        int count = this.count(wrapper);
        if (count > 0) {
            throw new BusinessException(DetectionResponseEnum.ICD_PATH_NAME_REPEAT);
        }
    }
}
