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

import cn.hutool.core.util.StrUtil;
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.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pms.enums.PmsDeviceResponseEnum;
import com.njcn.device.pms.pojo.param.PowerGenerationUserParam;
import com.njcn.device.pq.mapper.PowerGenerationUserMapper;
import com.njcn.device.pq.pojo.po.jb.GenerationUser;
import com.njcn.device.pq.service.IPowerGenerationUserService;
import com.njcn.web.factory.PageFactory;
import com.njcn.web.pojo.param.BaseParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


/**
 * <p>
 *  台区信息
 * </p>
 *
 * @author hongawen
 * @since 2022-10-14
 */
@Service
@RequiredArgsConstructor
public class PowerGenerationUserServiceImpl extends ServiceImpl<PowerGenerationUserMapper, GenerationUser> implements IPowerGenerationUserService {


    @Override
    public boolean addPowerGenerationUser(PowerGenerationUserParam powerGenerationUserParam) {
        LambdaQueryWrapper<GenerationUser> che = new LambdaQueryWrapper<>();
        che.eq(GenerationUser::getId,powerGenerationUserParam.getId());
        int count = this.count(che);
        if(count>0){
            throw new BusinessException(PmsDeviceResponseEnum.POWER_CODE_SAME);
        }
        GenerationUser powerDistributionarea = new GenerationUser();
        BeanUtils.copyProperties(powerGenerationUserParam,powerDistributionarea);
        //设为正常状态
        powerDistributionarea.setStatus(DataStateEnum.ENABLE.getCode());
        return this.save(powerDistributionarea);
    }

    @Override
    public boolean updatePowerGenerationUser(PowerGenerationUserParam powerGenerationUserParam) {
        GenerationUser powerDistributionarea = this.getById(powerGenerationUserParam.getId());
        if(Objects.isNull(powerDistributionarea)){
            throw new BusinessException(PmsDeviceResponseEnum.POWER_DIS_NOT_FIND);
        }
        BeanUtils.copyProperties(powerGenerationUserParam,powerDistributionarea);
        return this.updateById(powerDistributionarea);
    }

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

    @Override
    public Page<GenerationUser> getPowerGenerationUserPageList(BaseParam baseParam) {
        LambdaQueryWrapper<GenerationUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GenerationUser::getStatus, DataStateEnum.ENABLE.getCode())
                .and(StrUtil.isNotBlank(baseParam.getSearchValue()),
                        i->i.like(GenerationUser::getName,baseParam.getSearchValue())
                                .or()
                                .like(GenerationUser::getOrgName,baseParam.getSearchValue())).orderByDesc(GenerationUser::getCreateTime);
        //初始化分页数据
        return this.page(new Page<>(PageFactory.getPageNum(baseParam), PageFactory.getPageSize(baseParam)), lambdaQueryWrapper);
    }
}
