package com.njcn.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.system.enums.SystemResponseEnum;
import com.njcn.system.mapper.ReportTemplateMapper;
import com.njcn.system.pojo.param.ReportTemplateParam;
import com.njcn.system.pojo.po.ReportTemplate;
import com.njcn.system.service.ReportTemplateService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * pqs-event
 *
 * @author cdf
 * @date 2022/8/29
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ReportTemplateServiceImpl extends ServiceImpl<ReportTemplateMapper, ReportTemplate>  implements ReportTemplateService {


    @Override
    public boolean addReportTemplate(ReportTemplateParam reportTemplateParam) {
        checkNameAndParam(reportTemplateParam,false);
        ReportTemplate reportTemplate = new ReportTemplate();
        BeanUtils.copyProperties(reportTemplateParam,reportTemplate);
        reportTemplate.setState(DataStateEnum.ENABLE.getCode());
        return this.save(reportTemplate);
    }

    @Override
    public boolean updateReportTemplate(ReportTemplateParam.UpdateReportTemplateParam updateReportTemplateParam) {
        checkNameAndParam(updateReportTemplateParam,true);
        ReportTemplate reportTemplate = new ReportTemplate();
        BeanUtils.copyProperties(updateReportTemplateParam,reportTemplate);
        return this.updateById(reportTemplate);
    }



    @Override
    public List<ReportTemplate> getAllReportTemplateList() {
        return this.list();
    }

    @Override
    public ReportTemplate getReportTemplateById(String id) {
        return this.getById(id);
    }

    @Override
    public boolean delReportTemplate(List<String> ids) {
        return this.removeByIds(ids);
    }


    /**
     * 校验单位名称是否重复
     */
    private void checkNameAndParam(ReportTemplateParam reportTemplateParam, boolean isUpdate) {



        LambdaQueryWrapper<ReportTemplate> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ReportTemplate::getName, reportTemplateParam.getName())
                .eq(ReportTemplate::getType,reportTemplateParam.getType());
        if (isUpdate) {
            //更新操作
            if (reportTemplateParam instanceof ReportTemplateParam.UpdateReportTemplateParam) {
                lambdaQueryWrapper.ne(ReportTemplate::getId, ((ReportTemplateParam.UpdateReportTemplateParam) reportTemplateParam).getId());
            }
        }
        int count = this.count(lambdaQueryWrapper);
        if (count > 0) {
            throw new BusinessException(SystemResponseEnum.EVENT_REPORT_REPEAT);
        }

        lambdaQueryWrapper.clear();
        lambdaQueryWrapper.eq(ReportTemplate::getCode, reportTemplateParam.getCode())
                .eq(ReportTemplate::getType,reportTemplateParam.getType());
        if (isUpdate) {
            //更新操作
            if (reportTemplateParam instanceof ReportTemplateParam.UpdateReportTemplateParam) {
                lambdaQueryWrapper.ne(ReportTemplate::getId, ((ReportTemplateParam.UpdateReportTemplateParam) reportTemplateParam).getId());
            }
        }
        int countUserCode = this.count(lambdaQueryWrapper);
        if (countUserCode > 0) {
            throw new BusinessException(SystemResponseEnum.EVENT_REPORT_REPEAT);
        }
    }

}
