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


import cn.hutool.core.collection.CollUtil;
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.mapper.majornetwork.PmsGeneratrixWireMapper;
import com.njcn.device.pms.pojo.dto.GeneratrixAndPowerStationDTO;
import com.njcn.device.pms.pojo.dto.GeneratrixAndPowerStationSonDTO;
import com.njcn.device.pms.pojo.dto.PmsGeneratrixDTO;
import com.njcn.device.pms.pojo.param.ConditionParam;
import com.njcn.device.pms.pojo.param.GeneratrixWireParam;
import com.njcn.device.pms.pojo.param.PmsBaseParam;
import com.njcn.device.pms.pojo.param.PmsGeneratrixParam;
import com.njcn.device.pms.pojo.po.GeneratrixWire;
import com.njcn.device.pms.pojo.po.StatationStat;
import com.njcn.device.pms.service.majornetwork.IGeneratrixWireService;
import com.njcn.user.api.DeptFeignClient;
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;

/**
 * pms-device
 *
 * @author cdf
 * @date 2022/10/26
 */
@Service
@RequiredArgsConstructor
public class GeneratrixWireImpl extends ServiceImpl<PmsGeneratrixWireMapper, GeneratrixWire> implements IGeneratrixWireService {

    private final DeptFeignClient deptFeignClient;

    @Override
    public boolean addGeneratrixWire(GeneratrixWireParam generatrixWireParam) {
        checkName(generatrixWireParam, false);

        GeneratrixWire generatrixWire = new GeneratrixWire();
        BeanUtils.copyProperties(generatrixWireParam, generatrixWire);
        generatrixWire.setStatus(DataStateEnum.ENABLE.getCode());
        return this.save(generatrixWire);
    }

    @Override
    public boolean updateGeneratrixWire(GeneratrixWireParam generatrixWireParam) {
        checkName(generatrixWireParam, true);
        GeneratrixWire generatrixWireVa = this.getById(generatrixWireParam.getId());
        if (Objects.isNull(generatrixWireVa)) {
            throw new BusinessException(PmsDeviceResponseEnum.VOLTAGE_EMPTY);
        }
        GeneratrixWire generatrixWire = new GeneratrixWire();
        BeanUtils.copyProperties(generatrixWireParam, generatrixWire);
        return this.updateById(generatrixWire);
    }

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

    @Override
    public GeneratrixWire getGeneratrixWireById(String generatrixWireId) {
        return this.getById(generatrixWireId);
    }

    @Override
    public List<GeneratrixWire> getGeneratrixWireList(PmsBaseParam pmsBaseParam) {
        LambdaQueryWrapper<GeneratrixWire> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(pmsBaseParam.getGeneratrixId())) {
            lambdaQueryWrapper.eq(GeneratrixWire::getId, pmsBaseParam.getGeneratrixId());
        }

        if (StrUtil.isNotBlank(pmsBaseParam.getStationId())) {
            lambdaQueryWrapper.eq(GeneratrixWire::getStationId, pmsBaseParam.getStationId());
        }
        lambdaQueryWrapper.eq(GeneratrixWire::getStatus, DataStateEnum.ENABLE.getCode())
                .orderByAsc(GeneratrixWire::getCreateTime);
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public Page<GeneratrixWire> getGeneratrixWirePageList(BaseParam baseParam) {
        LambdaQueryWrapper<GeneratrixWire> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GeneratrixWire::getStatus, DataStateEnum.ENABLE.getCode())
                .and(StrUtil.isNotBlank(baseParam.getSearchValue()), i -> i.like(StrUtil.isNotBlank(baseParam.getSearchValue()), GeneratrixWire::getGeneratrixName, baseParam.getSearchValue())
                        .or(StrUtil.isNotBlank(baseParam.getSearchValue()))
                        .like(StrUtil.isNotBlank(baseParam.getSearchValue()), GeneratrixWire::getName, baseParam.getSearchValue())
                        .or(StrUtil.isNotBlank(baseParam.getSearchValue()))
                        .like(StrUtil.isNotBlank(baseParam.getSearchValue()), GeneratrixWire::getStationName, baseParam.getSearchValue()))
                .orderByDesc(GeneratrixWire::getCreateTime);
        return this.page(new Page<>(PageFactory.getPageNum(baseParam), PageFactory.getPageSize(baseParam)), lambdaQueryWrapper);
    }


    private void checkName(GeneratrixWireParam generatrixWireParam, Boolean updateFlag) {
        LambdaQueryWrapper<GeneratrixWire> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GeneratrixWire::getName, generatrixWireParam.getName())
                .eq(GeneratrixWire::getStationId, generatrixWireParam.getStationId())
                .eq(GeneratrixWire::getStatus, DataStateEnum.ENABLE.getCode());

        if (updateFlag) {
            //更新
            lambdaQueryWrapper.ne(GeneratrixWire::getId, generatrixWireParam.getId());
        } else {
            //新增校验
            if (StrUtil.isNotBlank(generatrixWireParam.getId())) {
                LambdaQueryWrapper<GeneratrixWire> lambdaQuery = new LambdaQueryWrapper<>();
                lambdaQuery.eq(GeneratrixWire::getId, generatrixWireParam.getId());
                int count = this.count(lambdaQuery);
                if (count > 0) {
                    throw new BusinessException(PmsDeviceResponseEnum.WIRE_SAME);

                }
            }
        }
        int count = this.count(lambdaQueryWrapper);
        if (count > 0) {
            throw new BusinessException(PmsDeviceResponseEnum.GENERATRIXWIRE_NAME_REPEAT);
        }
    }

    /**
     * 获取母线信息
     *
     * @param param 入参
     * @return 母线信息
     */
    @Override
    public List<PmsGeneratrixDTO> getGeneratrixInfo(PmsGeneratrixParam param) {
        if (CollUtil.isEmpty(param.getGeneratrixIds())) {
            return null;
        }
        return this.baseMapper.getGeneratrixInfo(param);
    }

    /**
     * 获取母线与电站关联信息
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.device.pms.pojo.dto.PmsGeneratrixDTO>
     * @author yzh
     * @date 2022/11/3
     */
    @Override
    public List<GeneratrixAndPowerStationDTO> getGeneratrixAndPowerStationInfo(PmsGeneratrixParam param) {
        return this.baseMapper.getGeneratrixAndPowerStationInfo(param);
    }

    /**
     * @param param
     * @Description: 普测计划页面调用根据条件查询母线信息
     * @Param: [param]
     * @return: java.util.List<com.njcn.device.pms.pojo.dto.GeneratrixAndPowerStationDTO>
     * @Author: clam
     * @Date: 2022/12/5
     */
    @Override
    public List<GeneratrixAndPowerStationSonDTO> getGeneratrixByCondition(ConditionParam param) {

        List<String> data = deptFeignClient.getDepSonSelfCodetByDeptId(param.getDeptId()).getData();

        return this.baseMapper.getGeneratrixByCondition(data, param);
    }

}
