package com.njcn.advance.service.assess.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.advance.enums.AdvanceResponseEnum;
import com.njcn.advance.mapper.assess.AssessUserMapper;
import com.njcn.advance.pojo.param.assess.AssessUserQuery;
import com.njcn.advance.pojo.po.assess.AssessDicShockLoad;
import com.njcn.advance.pojo.po.assess.AssessUser;
import com.njcn.advance.service.assess.IAssessUserService;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.web.factory.PageFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cdf
 * @since 2025-06-06
 */
@Service
@RequiredArgsConstructor
public class AssessUserServiceImpl extends ServiceImpl<AssessUserMapper, AssessUser> implements IAssessUserService {

    private final DeptFeignClient deptFeignClient;
    @Override
    public Page<AssessUser> page(AssessUserQuery assessUserQuery) {
        Page<AssessUser> pageParam = new Page<>(PageFactory.getPageNum(assessUserQuery), PageFactory.getPageSize(assessUserQuery));
        LambdaQueryWrapper<AssessUser> wrapper = Wrappers.<AssessUser>lambdaQuery()
                .in(AssessUser::getDeptId,deptFeignClient.getDepSonIdtByDeptId(assessUserQuery.getDeptId()).getData())
                .eq(StrUtil.isNotBlank(assessUserQuery.getDeptName()), AssessUser::getDeptName, assessUserQuery.getDeptName())
                .in(CollUtil.isNotEmpty(assessUserQuery.getBusScaleList()),AssessUser::getBusScale,assessUserQuery.getBusScaleList())
                .in(CollUtil.isNotEmpty(assessUserQuery.getPowerstationScaleList()),AssessUser::getPowerstationScale,assessUserQuery.getPowerstationScaleList())
                .like(StrUtil.isNotBlank(assessUserQuery.getAssessName()), AssessUser::getAssessName, assessUserQuery.getAssessName())
                .eq(StrUtil.isNotBlank(assessUserQuery.getIsWindfarm()), AssessUser::getIsWindfarm, assessUserQuery.getIsWindfarm())
                .like(StrUtil.isNotBlank(assessUserQuery.getPowerstationName()), AssessUser::getPowerstationName, assessUserQuery.getPowerstationName());
        Page<AssessUser> pageData = this.page(pageParam, wrapper);
        return pageData;

    }

    @Override
    public AssessUser getByAssessId(String assessId) {
        return this.getById(assessId);
    }

    @Override
    public String add(AssessUserQuery assessUserQuery) {
        AssessUser assessUser = new AssessUser();
        BeanUtils.copyProperties(assessUserQuery,assessUser);
        assessUser.setAssessId(IdUtil.simpleUUID());
        int count = this.count(new LambdaQueryWrapper<AssessUser>()
                .eq(AssessUser::getAssessName, assessUserQuery.getAssessName())
        );
        if (count > 0) {
            throw new BusinessException(AdvanceResponseEnum.ASSESSUSER_NAME_REPETITION);
        }
        this.save(assessUser);
        return assessUser.getAssessId();
    }

    @Override
    public boolean update(AssessUserQuery.UpdateAssessUserQuery updateAssessUserQuery) {
        AssessUser assessUser = new AssessUser();
        BeanUtils.copyProperties(updateAssessUserQuery,assessUser);
        int count = this.count(new LambdaQueryWrapper<AssessUser>()
                .ne(AssessUser::getAssessId, updateAssessUserQuery.getAssessId())
                .eq(AssessUser::getAssessName, updateAssessUserQuery.getAssessName())
        );
        if (count > 0) {
            throw new BusinessException(AdvanceResponseEnum.ASSESSUSER_NAME_REPETITION);
        }
        return this.updateById(assessUser);
    }
}
