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

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.PowerClientMapper;
import com.njcn.device.pms.pojo.param.PowerClientParam;
import com.njcn.device.pms.pojo.param.PowerGenerationUserParam;
import com.njcn.device.pms.pojo.param.PowerUserParam;
import com.njcn.device.pms.pojo.po.PowerClient;
import com.njcn.device.pms.pojo.po.PowerGenerationUser;
import com.njcn.device.pms.pojo.vo.PowerClientVO;
import com.njcn.device.pms.service.majornetwork.IDistributionMonitorService;
import com.njcn.device.pms.service.majornetwork.IPowerClientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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 org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2022-10-14
 */
@Service
@RequiredArgsConstructor
public class PowerClientServiceImpl extends ServiceImpl<PowerClientMapper, PowerClient> implements IPowerClientService {

    private final DeptFeignClient deptFeignClient;

    private final IDistributionMonitorService iDistributionMonitorService;

    @Override
    public boolean addPowerClient(PowerClientParam powerClientParam) {
        LambdaQueryWrapper<PowerClient> che = new LambdaQueryWrapper<>();
        che.eq(PowerClient::getId,powerClientParam.getId());
        int powerClientCount = this.count(che);
        if(powerClientCount>0){
            throw new BusinessException(PmsDeviceResponseEnum.POWERCLINET_SAME);
        }

        checkParam(powerClientParam,false);
        PowerClient powerClient = new PowerClient();
        BeanUtils.copyProperties(powerClientParam,powerClient);
        powerClient.setStatus(DataStateEnum.ENABLE.getCode());
        return this.save(powerClient);
    }

    @Override
    public boolean updatePowerClient(PowerClientParam powerClientParam) {
        PowerClient powerClient = this.getById(powerClientParam.getId());
        if(Objects.isNull(powerClient)){
            throw new BusinessException(PmsDeviceResponseEnum.POWER_CLIENT_NOT_FIND);
        }
        checkParam(powerClientParam,true);
        BeanUtils.copyProperties(powerClientParam,powerClient);
        return this.updateById(powerClient);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean delPowerClient(List<String> powerClientIds) {
        iDistributionMonitorService.removeByIds(powerClientIds);
        return this.removeByIds(powerClientIds);
    }

    @Override
    public PowerClient getPowerClientById(String powerClientId) {
        return this.getById(powerClientId);
    }

    @Override
    public List<PowerClientVO> getPowerClientList(PowerClientParam powerClientParam) {
        List<String> deptCodes = new ArrayList<>();
        if(StrUtil.isNotBlank(powerClientParam.getOrgId())){
            deptCodes = deptFeignClient.getDepSonSelfCodetByDeptId(powerClientParam.getOrgId()).getData();
        }
        return this.baseMapper.getPowerClientSelect(deptCodes,powerClientParam.getId());
    }


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

    @Override
    public List<PowerClient> getPowerUtilizationUserList(PowerUserParam powerUserParam) {
        LambdaQueryWrapper<PowerClient> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(powerUserParam.getId())){
            queryWrapper.eq(PowerClient::getId,powerUserParam.getId());
        }
        return this.list(queryWrapper);
    }

    private void checkParam(PowerClientParam powerClientParam, Boolean updateFlag){

        LambdaQueryWrapper<PowerClient> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PowerClient::getName,powerClientParam.getName());

        if(updateFlag){
            lambdaQueryWrapper.ne(PowerClient::getId,powerClientParam.getId());
        }else {
            lambdaQueryWrapper.or().eq(PowerClient::getId,powerClientParam.getId());
        }

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

    }
}
