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

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.PowerGenerationUserMapper;
import com.njcn.device.pms.pojo.param.PowerDistributionareaParam;
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.PowerGenerationUserVO;
import com.njcn.device.pms.service.majornetwork.IDistributionMonitorService;
import com.njcn.device.pms.service.majornetwork.IPowerGenerationUserService;
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.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2022-10-14
 */
@Service
@RequiredArgsConstructor
public class PowerGenerationUserServiceImpl extends ServiceImpl<PowerGenerationUserMapper, PowerGenerationUser> implements IPowerGenerationUserService {

    private final DeptFeignClient deptFeignClient;

    private final IDistributionMonitorService iDistributionMonitorService;

    @Override
    public boolean addPowerGenerationUser(PowerGenerationUserParam powerGenerationUserParam) {
        LambdaQueryWrapper<PowerGenerationUser> che = new LambdaQueryWrapper<>();
        che.eq(PowerGenerationUser::getId,powerGenerationUserParam.getId());
        int powerClientCount = this.count(che);
        if(powerClientCount>0){
            throw new BusinessException(PmsDeviceResponseEnum.POWERGENERATIONUSER_SAME);
        }
        checkParam(powerGenerationUserParam,false);
        PowerGenerationUser powerGenerationUser = new PowerGenerationUser();
        BeanUtils.copyProperties(powerGenerationUserParam,powerGenerationUser);
        powerGenerationUser.setConnectionDate(LocalDate.parse(powerGenerationUserParam.getConnectionDate()));
        powerGenerationUser.setStatus(DataStateEnum.ENABLE.getCode());
        return this.save(powerGenerationUser);
    }

    @Override
    public boolean updatePowerGenerationUser(PowerGenerationUserParam powerGenerationUserParam) {
        PowerGenerationUser powerGenerationUser = this.getById(powerGenerationUserParam.getId());
        if(Objects.isNull(powerGenerationUser)){
            throw new BusinessException(PmsDeviceResponseEnum.POWER_GEN_NOT_FIND);
        }
        checkParam(powerGenerationUserParam,true);
        BeanUtils.copyProperties(powerGenerationUserParam,powerGenerationUser);
        powerGenerationUser.setConnectionDate(LocalDate.parse(powerGenerationUserParam.getConnectionDate()));
        return this.updateById(powerGenerationUser);
    }

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

    @Override
    public PowerGenerationUser getPowerGenerationUserById(String powerGenerationUserId) {
        return this.getById(powerGenerationUserId);
    }

    @Override
    public List<PowerGenerationUserVO> getPowerGenerationUserList(PowerDistributionareaParam powerDistributionareaParam) {
        LambdaQueryWrapper<PowerGenerationUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<String> deptIds =new ArrayList<>();
        if(StrUtil.isNotBlank(powerDistributionareaParam.getOrgId())){
            deptIds = deptFeignClient.getDepSonSelfCodetByDeptId(powerDistributionareaParam.getOrgId()).getData();
        }
        return this.baseMapper.getPowerGenerationUserSelect(deptIds,powerDistributionareaParam.getId());
    }

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

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


    private void  checkParam(PowerGenerationUserParam powerGenerationUserParam,Boolean updateFlag){

        LambdaQueryWrapper<PowerGenerationUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PowerGenerationUser::getName,powerGenerationUserParam.getName());

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

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

    }


}
