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.core.conditions.query.QueryWrapper;
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.LibCaseMapper;
import com.njcn.supervision.pojo.param.databank.LibCaseParam;
import com.njcn.supervision.pojo.po.databank.LibCase;
import com.njcn.supervision.service.databank.LibCaseService;
import com.njcn.web.factory.PageFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * Description:
 * Date: 2024/9/10  14:17【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
public class LibCaseServiceImpl extends ServiceImpl<LibCaseMapper, LibCase> implements LibCaseService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LibCase add(LibCaseParam libCaseParam) {
        checkName(libCaseParam,false);
        LibCase libCase = new LibCase();
        BeanUtils.copyProperties(libCaseParam, libCase);
        libCase.setStatus(1);
        this.save(libCase);
        return libCase;

    }

    private void checkName(LibCaseParam libCaseParam, boolean isExcludeSelf) {


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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateCaseLibrary(LibCaseParam.LibCaseParamUpdate paramUpdate) {

        checkName(paramUpdate,true);

        LibCase libCase = new LibCase();
        BeanUtil.copyProperties(paramUpdate, libCase);
        this.updateById(libCase);
        return libCase.getId();
    }

    @Override
    public Page<LibCase> pageQuery(LibCaseParam.LibCaseParamQuery param) {
        QueryWrapper<LibCase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("lib_case.status",1);
        //添加上时间范围
        queryWrapper.like(StrUtil.isNotBlank(param.getSearchValue()),"lib_case.name",param.getSearchValue());
        if(StrUtil.isNotBlank(param.getSearchBeginTime())&&StrUtil.isNotBlank(param.getSearchEndTime())){
            queryWrapper.between("lib_case.Create_Time",
                    DateUtil.beginOfDay(DateUtil.parse(param.getSearchBeginTime())),
                    DateUtil.endOfDay(DateUtil.parse(param.getSearchEndTime())));
        }
        queryWrapper.orderByDesc("lib_case.Update_Time");
        return this.baseMapper.page(new Page<>(PageFactory.getPageNum(param), PageFactory.getPageSize(param)), queryWrapper);
    }
}
