package com.njcn.supervision.service.databank.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
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.supervision.enums.SupervisionResponseEnum;
import com.njcn.supervision.mapper.databank.LibModelMapper;
import com.njcn.supervision.pojo.param.databank.LibModelParam;
import com.njcn.supervision.pojo.po.databank.LibModel;
import com.njcn.supervision.service.databank.ILibModelService;
import com.njcn.web.factory.PageFactory;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wr
 * @since 2025-09-10
 */
@Service
public class LibModelServiceImpl extends ServiceImpl<LibModelMapper, LibModel> implements ILibModelService {


    @Override
    public Boolean add(LibModelParam param) {
        checkName(param, false);
        LibModel model = BeanUtil.copyProperties(param, LibModel.class);
        model.setStatus(DataStateEnum.ENABLE.getCode());
        return this.save(model);
    }

    @Override
    public Boolean update(LibModelParam.LibModelParamUpdate param) {
        checkName(param,true);
        LibModel model = BeanUtil.copyProperties(param, LibModel.class);
        return this.updateById(model);
    }

    @Override
    public Page<LibModel> page(LibModelParam.LibModelParamQuery param) {
        LambdaQueryWrapper<LibModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LibModel::getStatus,DataStateEnum.ENABLE.getCode());
        queryWrapper.eq(LibModel::getType,param.getType());
        //添加上时间范围
        queryWrapper.like(StrUtil.isNotBlank(param.getSearchValue()),LibModel::getName,param.getSearchValue());
        if(StrUtil.isNotBlank(param.getSearchBeginTime())&&StrUtil.isNotBlank(param.getSearchEndTime())){
            queryWrapper.between(
                    LibModel::getCreateTime,
                    DateUtil.beginOfDay(DateUtil.parse(param.getSearchBeginTime())),
                    DateUtil.endOfDay(DateUtil.parse(param.getSearchEndTime())));
        }
        queryWrapper.orderByDesc(LibModel::getName);
        return this.page(new Page<>(PageFactory.getPageNum(param), PageFactory.getPageSize(param)), queryWrapper);
    }

    private void checkName(LibModelParam param, boolean isExcludeSelf) {
        LambdaQueryWrapper<LibModel> sgUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sgUserLambdaQueryWrapper
                .eq(LibModel::getName, param.getName())
                .eq(LibModel::getStatus, DataStateEnum.ENABLE.getCode());
        //更新的时候，需排除当前记录
        if (isExcludeSelf) {
            if (param instanceof LibModelParam.LibModelParamUpdate) {
                sgUserLambdaQueryWrapper.ne(LibModel::getId, ((LibModelParam.LibModelParamUpdate) param).getId());
            }
        }
        int countByAccount = this.count(sgUserLambdaQueryWrapper);
        //大于等于1个则表示重复
        if (countByAccount >= 1) {
            throw new BusinessException(SupervisionResponseEnum.NAME_EXISTS);
        }
    }
}
