package com.njcn.device.pq.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrPool;
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.njcn.common.pojo.constant.PatternRegex;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pms.pojo.po.Monitor;
import com.njcn.device.pq.pojo.dto.CvtHarmonicCorrectionFactorsDTO;
import com.njcn.device.pq.pojo.param.CvtHarmonicCorrectionFactorsParam;
import com.njcn.device.pq.pojo.vo.CvtHarmonicCorrectionFactorsVO;
import com.njcn.web.constant.ValidMessage;
import com.njcn.web.factory.PageFactory;
import com.njcn.web.utils.RequestUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.device.pq.mapper.CvtHarmonicCorrectionFactorsMapper;
import com.njcn.device.pq.pojo.po.CvtHarmonicCorrectionFactors;
import com.njcn.device.pq.service.CvtHarmonicCorrectionFactorsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * Description:
 * Date: 2025/02/10  下午 1:45【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
public class CvtHarmonicCorrectionFactorsServiceImpl extends ServiceImpl<CvtHarmonicCorrectionFactorsMapper, CvtHarmonicCorrectionFactors> implements CvtHarmonicCorrectionFactorsService{


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(CvtHarmonicCorrectionFactorsParam cvtHarmonicCorrectionFactorsParam) {
        //校验
        checkParam(cvtHarmonicCorrectionFactorsParam, false);
        CvtHarmonicCorrectionFactors cvtHarmonicCorrectionFactors = new CvtHarmonicCorrectionFactors();
        BeanUtils.copyProperties(cvtHarmonicCorrectionFactorsParam,cvtHarmonicCorrectionFactors);
        boolean save = this.save(cvtHarmonicCorrectionFactors);
        return save;
    }

    @Override
    public boolean updateCvt(CvtHarmonicCorrectionFactorsParam.CvtHarmonicCorrectionFactorsParamUpdateParam updateParam) {
        //校验
        checkParam(updateParam, true);
        CvtHarmonicCorrectionFactors cvtHarmonicCorrectionFactors = new CvtHarmonicCorrectionFactors();
        BeanUtils.copyProperties(updateParam,cvtHarmonicCorrectionFactors);
        boolean update = this.updateById(cvtHarmonicCorrectionFactors);
        return update;
    }

    @Override
    public Page<CvtHarmonicCorrectionFactorsVO> queryPage(CvtHarmonicCorrectionFactorsParam.CvtHarmonicCorrectionFactorsParamQueryParam queryParam) {

        QueryWrapper<CvtHarmonicCorrectionFactorsVO> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(queryParam)) {
            if (StringUtils.isNotEmpty(queryParam.getSearchBeginTime()) && StringUtils.isNotEmpty(queryParam.getSearchEndTime())) {
                queryWrapper.between("cvt_harmonic_correction_factors.create_time",
                        DateUtil.beginOfDay(DateUtil.parse(queryParam.getSearchBeginTime())),
                        DateUtil.endOfDay(DateUtil.parse(queryParam.getSearchEndTime())));
            }

            queryWrapper.and(temp->temp.like("cvt_harmonic_correction_factors.cvt_name", queryParam.getSearchValue())
                    .or()
                    .like("cvt_harmonic_correction_factors.cvt_model", queryParam.getSearchValue()));


        }
        queryWrapper
                .orderByDesc("cvt_harmonic_correction_factors.Update_Time");
        Page<CvtHarmonicCorrectionFactorsVO> page = this.baseMapper.queryPage(new Page<>(PageFactory.getPageNum(queryParam), PageFactory.getPageSize(queryParam)), queryWrapper);
        return page;
    }

    @Override
    public List<CvtHarmonicCorrectionFactorsDTO> queryList(CvtHarmonicCorrectionFactorsParam.CvtHarmonicCorrectionFactorsParamQueryParam queryParam) {
        List<CvtHarmonicCorrectionFactorsDTO> result = new ArrayList<>();
        List<CvtHarmonicCorrectionFactors> list = this.lambdaQuery().and(item -> item.like(CvtHarmonicCorrectionFactors::getCvtName, queryParam.getSearchValue()).or()
                .like(CvtHarmonicCorrectionFactors::getCvtModel, queryParam.getSearchValue())).list();
        if(CollectionUtil.isNotEmpty(list)){
            result =  list.stream().map(temp->{
                CvtHarmonicCorrectionFactorsDTO cvtHarmonicCorrectionFactorsDTO = new CvtHarmonicCorrectionFactorsDTO();
                BeanUtils.copyProperties(temp,cvtHarmonicCorrectionFactorsDTO);
                return cvtHarmonicCorrectionFactorsDTO;
            }).collect(Collectors.toList());
        }

        return result;
    }

    private void checkParam(CvtHarmonicCorrectionFactorsParam cvtHarmonicCorrectionFactorsParam, Boolean updateFlag) {

//        Pattern pattern = Pattern.compile(PatternRegex.SPECIALCHARACTER);
//        if(pattern.matcher(cvtHarmonicCorrectionFactorsParam.getCvtName()).find()){
//            throw new BusinessException(ValidMessage.NAME_SPECIAL_REGEX);
//        }
//        if(pattern.matcher(cvtHarmonicCorrectionFactorsParam.getCvtModel()).find()){
//            throw new BusinessException(ValidMessage.NAME_SPECIAL_REGEX);
//        }

        LambdaQueryWrapper<CvtHarmonicCorrectionFactors> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.and(item->item.eq(CvtHarmonicCorrectionFactors::getCvtName,cvtHarmonicCorrectionFactorsParam.getCvtName()).or()
                .eq(CvtHarmonicCorrectionFactors::getCvtModel,cvtHarmonicCorrectionFactorsParam.getCvtModel()));

        if (updateFlag) {
            if (cvtHarmonicCorrectionFactorsParam instanceof CvtHarmonicCorrectionFactorsParam.CvtHarmonicCorrectionFactorsParamUpdateParam) {
                //修改
                lambdaQueryWrapper.ne(CvtHarmonicCorrectionFactors::getId, ((CvtHarmonicCorrectionFactorsParam.CvtHarmonicCorrectionFactorsParamUpdateParam) cvtHarmonicCorrectionFactorsParam).getId());
            }
        }

        int count = this.count(lambdaQueryWrapper);
        if (count > 0) {
            throw new BusinessException("存在同名cvt名称或cvt型号");
        }
    }
}
