package com.njcn.device.pms.service.gwPush.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.mapper.majornetwork.PmsNewEnergyMapper;
import com.njcn.device.pms.pojo.param.NewEnergyQueryParam;
import com.njcn.device.pms.pojo.po.*;
import com.njcn.device.pms.service.gwPush.IPmsNewEnergyService;
import com.njcn.device.pms.service.majornetwork.IMonitorService;
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 java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wr
 * @since 2024-03-26
 */
@Service
@RequiredArgsConstructor
public class PmsNewEnergyServiceImpl extends ServiceImpl<PmsNewEnergyMapper, PmsNewEnergy> implements IPmsNewEnergyService {

    private final DeptFeignClient deptFeignClient;
    private final IMonitorService monitorService;
    @Override
    public Page<PmsNewEnergy> getNewEnergyList(NewEnergyQueryParam baseParam) {
        List<String> orgIds = new ArrayList<>();
        if(StrUtil.isNotBlank(baseParam.getOrgId())){
            orgIds.addAll(deptFeignClient.getDepSonSelfCodetByDeptId(baseParam.getOrgId()).getData());
        }

        LambdaQueryWrapper<PmsNewEnergy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsNewEnergy::getState,DataStateEnum.ENABLE.getCode())
                .in(CollUtil.isNotEmpty(orgIds),PmsNewEnergy::getOrgId,orgIds);
        if(StrUtil.isNotBlank(baseParam.getSearchValue())) {
            lambdaQueryWrapper.and(i -> i.like(PmsNewEnergy::getObjName, baseParam.getSearchValue())
                    .or()
                    .like(PmsNewEnergy::getOrgName, baseParam.getSearchValue())
                    .or()
                    .like(PmsNewEnergy::getPowerrName, baseParam.getSearchValue()));
        }
        lambdaQueryWrapper.orderByDesc(PmsNewEnergy::getCreateTime);
        //初始化分页数据
        return this.page(new Page<>(PageFactory.getPageNum(baseParam), PageFactory.getPageSize(baseParam)), lambdaQueryWrapper);
    }

    @Override
    public List<PmsNewEnergy> getNewEnergySelectList(String orgId,String type) {
        LambdaQueryWrapper<PmsNewEnergy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsNewEnergy::getState,DataStateEnum.ENABLE.getCode());
        lambdaQueryWrapper.eq(PmsNewEnergy::getObjType,type);
        if(StrUtil.isNotBlank(orgId)){
            List<String> deptCodes = deptFeignClient.getDepSonSelfCodetByDeptId(orgId).getData();
            lambdaQueryWrapper.in(PmsNewEnergy::getOrgId,deptCodes);
        }
        return this.list(lambdaQueryWrapper);
    }



    @Override
    public boolean add(NewEnergyQueryParam.Info param) {
        List<PmsNewEnergy> list = this.list(new LambdaQueryWrapper<PmsNewEnergy>()
                .eq(PmsNewEnergy::getState,DataStateEnum.ENABLE.getCode())
                .eq(PmsNewEnergy::getId, param.getId())
                .or(wrapper ->
                        wrapper.eq(PmsNewEnergy::getObjName, param.getOrgName())
                )
        );
        if (CollectionUtil.isNotEmpty(list)) {
            List<String> collect = list.stream().map(PmsNewEnergy::getObjName).collect(Collectors.toList());
            if(collect.contains(param.getObjName())){
                throw new BusinessException(PmsDeviceResponseEnum.MODEL_NAME_REPEAT);
            }
            throw new BusinessException(PmsDeviceResponseEnum.NE_ENERGY_ID);
        }
        PmsNewEnergy pmsNewEnergy = new PmsNewEnergy();
        BeanUtils.copyProperties(param, pmsNewEnergy);
        return this.save(pmsNewEnergy);
    }

    /**
     * 修改监测终端台账
     * @author hany
     * @date 2022/10/27
     */
    @Override
    public boolean update(NewEnergyQueryParam.Info param) {
        List<PmsNewEnergy> list = this.list(new LambdaQueryWrapper<PmsNewEnergy>()
                .eq(PmsNewEnergy::getState,DataStateEnum.ENABLE.getCode())
                .ne(PmsNewEnergy::getId, param.getId())
                .and(wrapper ->
                        wrapper.eq(PmsNewEnergy::getObjName, param.getObjName())
                )
        );
        if (CollectionUtil.isNotEmpty(list)) {
            List<String> collect = list.stream().map(PmsNewEnergy::getObjName).collect(Collectors.toList());
            if(collect.contains(param.getObjName())){
                throw new BusinessException(PmsDeviceResponseEnum.MODEL_NAME_REPEAT);
            }
        }
        PmsNewEnergy pmsNewEnergy = BeanUtil.copyProperties(param, PmsNewEnergy.class);
        return this.updateById(pmsNewEnergy);
    }

    /**
     * 删除
     * @param ids
     */
    @Override
    public boolean delete(List<String> ids) {
        //删除前需要判断依赖终端的监测点是否存在
        LambdaQueryWrapper<Monitor> monitorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        monitorLambdaQueryWrapper.in(Monitor::getTractionId,ids).eq(Monitor::getStatus,DataStateEnum.ENABLE.getCode());
        int mCount = monitorService.count(monitorLambdaQueryWrapper);
        if(mCount>0){
            throw new BusinessException(PmsDeviceResponseEnum.MONITOR_NE_ENERGY_FIND);
        }
        return this.update(new LambdaUpdateWrapper<PmsNewEnergy>()
                .set(PmsNewEnergy::getState, DataStateEnum.DELETED.getCode())
                .in(PmsNewEnergy::getId, ids));
    }

}
