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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
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.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;

import com.njcn.device.biz.mapper.PqSensitiveUserMapper;
import com.njcn.device.biz.pojo.po.PqSensitiveUser;
import com.njcn.device.pq.pojo.param.PqSensitiveUserParam;
import com.njcn.device.pq.pojo.vo.PqSensitiveUserVo;
import com.njcn.device.pqUser.service.IPqSensitiveUserService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.pojo.po.DictData;
import com.njcn.web.pojo.param.BaseParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xy
 * @since 2025-11-17
 */
@DS("sjzx")
@Slf4j
@Service
@RequiredArgsConstructor
public class PqSensitiveUserServiceImpl extends ServiceImpl<PqSensitiveUserMapper, PqSensitiveUser> implements IPqSensitiveUserService {


    private final DicDataFeignClient dicDataFeignClient;

    @Override
    public Page<PqSensitiveUserVo> getList(BaseParam param) {
        Page<PqSensitiveUserVo> result = new Page<>(param.getPageNum(),param.getPageSize());
        LambdaQueryWrapper<PqSensitiveUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(PqSensitiveUser::getSort);
        if (StrUtil.isNotBlank(param.getSearchValue())) {
            lambdaQueryWrapper.like(PqSensitiveUser::getName, param.getSearchValue());
        }
        Page<PqSensitiveUser> page = this.page(new Page<>(param.getPageNum(),param.getPageSize()),lambdaQueryWrapper);
        if(CollUtil.isNotEmpty(page.getRecords())){
            List<PqSensitiveUserVo> dataGroupEventVOList = new ArrayList<>();
            page.getRecords().forEach(item->{
                PqSensitiveUserVo vo = new PqSensitiveUserVo();
                BeanUtil.copyProperties(item,vo);
                dataGroupEventVOList.add(vo);
            });
            result.setRecords(dataGroupEventVOList);
            result.setTotal(page.getTotal());
            result.setSize(page.getSize());
            result.setCurrent(page.getCurrent());
            result.setPages(page.getPages());
        }
        return result;
    }

    @Override
    public boolean save(PqSensitiveUserParam pqSensitiveUserParam) {
        checkParam(pqSensitiveUserParam,false);

        PqSensitiveUser pqSensitiveUser = new PqSensitiveUser();
        BeanUtil.copyProperties(pqSensitiveUserParam,pqSensitiveUser);
        return this.save(pqSensitiveUser);
    }

    @Override
    public boolean update(PqSensitiveUserParam.UpdatePqSensitiveUserParam pqSensitiveUserParam) {
        checkParam(pqSensitiveUserParam,true);
        PqSensitiveUser pqSensitiveUser = new PqSensitiveUser();
        BeanUtil.copyProperties(pqSensitiveUserParam,pqSensitiveUser);
        return this.updateById(pqSensitiveUser);
    }


    private void checkParam(PqSensitiveUserParam param,boolean update){
        DictData data = dicDataFeignClient.getDicDataById(param.getLoadType()).getData();
        if(Objects.isNull(data)){
            throw new BusinessException(CommonResponseEnum.FAIL,"字典负荷类型缺失！");
        }

        LambdaQueryWrapper<PqSensitiveUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PqSensitiveUser::getName,param.getName());
        if(update){
            if(param instanceof PqSensitiveUserParam.UpdatePqSensitiveUserParam){
                lambdaQueryWrapper.ne(PqSensitiveUser::getId,((PqSensitiveUserParam.UpdatePqSensitiveUserParam) param).getId());
            }
        }
        int count = this.count(lambdaQueryWrapper);
        if(count>0){
            throw new BusinessException(CommonResponseEnum.FAIL,"用户名称重复！");
        }
    }

}
