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.LibTemplateMapper;
import com.njcn.supervision.pojo.param.databank.LibTemplateParam;
import com.njcn.supervision.pojo.po.databank.LibTemplate;
import com.njcn.supervision.service.databank.LibTemplateService;
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 LibTemplateServiceImpl extends ServiceImpl<LibTemplateMapper, LibTemplate> implements LibTemplateService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LibTemplate add(LibTemplateParam libTemplateParam) {
        checkName(libTemplateParam,false);
        LibTemplate libTemplate = new LibTemplate();
        BeanUtils.copyProperties(libTemplateParam, libTemplate);
        libTemplate.setStatus(1);
        this.save(libTemplate);
        return libTemplate;
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateTemplate(LibTemplateParam.LibTemplateParamUpdate paramUpdate) {
        checkName(paramUpdate,true);

        LibTemplate libTemplate = new LibTemplate();
        BeanUtil.copyProperties(paramUpdate, libTemplate);
        this.updateById(libTemplate);
        return libTemplate.getId();
    }

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