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.PowerDistributionareaParam;
import com.njcn.device.pms.pojo.param.TaiZhangParam;
import com.njcn.device.pq.mapper.PowerDistributionAreaMapper;
import com.njcn.device.pq.pojo.po.jb.DistributionArea;
import com.njcn.device.pq.service.IPowerDistributionAreaService;
import com.njcn.system.api.DicDataFeignClient;
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;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  台区信息
 * </p>
 *
 * @author hongawen
 * @since 2022-10-14
 */
@Service
@RequiredArgsConstructor
public class PowerDistributionAreaServiceImpl extends ServiceImpl<PowerDistributionAreaMapper, DistributionArea> implements IPowerDistributionAreaService {



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





    /**
     * 新增台区信息
     * @author hany
     * @date 2022/10/26
     */
    @Override
    public boolean add(PowerDistributionareaParam newParam) {
        LambdaQueryWrapper<DistributionArea> che = new LambdaQueryWrapper<>();
        che.eq(DistributionArea::getId,newParam.getId());
        int count = this.count(che);
        if(count>0){
            throw new BusinessException(PmsDeviceResponseEnum.POWER_CODE_SAME);
        }
        DistributionArea powerDistributionarea = new DistributionArea();
        BeanUtils.copyProperties(newParam,powerDistributionarea);
        //设为正常状态
        powerDistributionarea.setStatus(DataStateEnum.ENABLE.getCode());
        return this.save(powerDistributionarea);
    }

    /**
     * 修改台区信息
     * @author hany
     * @date 2022/10/26
     */
    @Override
    public boolean update(PowerDistributionareaParam updateParam) {
        DistributionArea powerDistributionarea = this.getById(updateParam.getId());
        if(Objects.isNull(powerDistributionarea)){
            throw new BusinessException(PmsDeviceResponseEnum.POWER_DIS_NOT_FIND);
        }
        BeanUtils.copyProperties(updateParam,powerDistributionarea);
        return this.updateById(powerDistributionarea);
    }

    /**
     * 删除
     * @param ids
     */
    @Override
    @Transactional(rollbackFor=RuntimeException.class)
    public boolean delete(List<String> ids) {
        return this.removeByIds(ids);
    }

}
