package cn.hanghui.outapi.platform.tenant.service.impl;

import cn.hanghui.outapi.platform.common.entity.po.Account;
import cn.hanghui.outapi.platform.common.entity.po.AccountAuthConfig;
import cn.hanghui.outapi.platform.common.entity.rest.RestResult;
import cn.hanghui.outapi.platform.common.mapper.AccountAuthConfigMapper;
import cn.hanghui.outapi.platform.common.utils.ClassUtil;
import cn.hanghui.outapi.platform.common.utils.Preconditions;
import cn.hanghui.outapi.platform.tenant.entity.config.ampe.AmpeDto;
import cn.hanghui.outapi.platform.tenant.entity.config.ampe.AmpeSearch;
import cn.hanghui.outapi.platform.tenant.entity.config.ampe.AmpeVo;
import cn.hanghui.outapi.platform.tenant.entity.config.auth.DeviceConfigBean;
import cn.hanghui.outapi.platform.tenant.entity.dto.TenantVo;
import cn.hanghui.outapi.platform.tenant.entity.enums.ModuleTypeEnum;
import cn.hanghui.outapi.platform.tenant.entity.po.Ampe;
import cn.hanghui.outapi.platform.tenant.entity.po.Tenant;
import cn.hanghui.outapi.platform.tenant.mapper.AmpeMapper;
import cn.hanghui.outapi.platform.tenant.service.AmpeService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wxg
 * @since 2023-12-28
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AmpeServiceImpl implements AmpeService {

    private final AmpeMapper ampeMapper;
    private final AccountAuthConfigMapper accountAuthConfigMapper;
    private final CommonImpl commonImpl;

    @Override
    public Boolean edit(AmpeDto dto) {
        Long ampeId = dto.getAmpeId();
        if (ampeId == null) {
            Ampe ampe = BeanUtil.copyProperties(dto, Ampe.class);
            ampeMapper.insert(ampe);
        } else {
            Ampe base = ampeMapper.selectById(ampeId);
            ClassUtil.throwBusinessException(ObjectUtil.isNull(base), "ampe平台不存在");
            BeanUtil.copyProperties(dto, base);
            ampeMapper.updateById(base);
        }
        return true;
    }

    @Override
    public List<AmpeVo> ampeList() {
        LambdaQueryWrapper<Ampe> wrapper = new LambdaQueryWrapper<>();
        //查询的基础条件
        wrapper.eq(Ampe::getIsDeleted, Boolean.FALSE);
        wrapper.orderByDesc(Ampe::getId);
        List<Ampe> ampeList = ampeMapper.selectList(wrapper);

        return ampeList.stream().map(p -> {
            AmpeVo item = BeanUtil.copyProperties(p, AmpeVo.class);
            item.setAmpeId(p.getId());
            return item;
        }).collect(Collectors.toList());
    }

    @Override
    public IPage<AmpeVo> ampePage(AmpeSearch dto) {
        Page<Ampe> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        LambdaQueryWrapper<Ampe> wrapper = this.getWrapper(dto);
        IPage<Ampe> pageList = ampeMapper.selectPage(page, wrapper);
        return pageList.convert(t -> {
            AmpeVo item = BeanUtil.copyProperties(t, AmpeVo.class);
            item.setAmpeId(t.getId());
            return item;
        });
    }

    private LambdaQueryWrapper<Ampe> getWrapper(AmpeSearch dto) {
        LambdaQueryWrapper<Ampe> wrapper = new LambdaQueryWrapper<>();
        //查询的传参条件
        wrapper.eq(ObjectUtil.isNotNull(dto.getAmpeId()), Ampe::getId, dto.getAmpeId());
        wrapper.like(StrUtil.isNotBlank(dto.getProductId()), Ampe::getProductId, dto.getProductId());
        wrapper.like(StrUtil.isNotBlank(dto.getHostAppId()), Ampe::getHostAppId, dto.getHostAppId());
        wrapper.like(StrUtil.isNotBlank(dto.getAppId()), Ampe::getAppId, dto.getAppId());
        wrapper.like(StrUtil.isNotBlank(dto.getAppName()), Ampe::getAppName, dto.getAppName());
        //查询的基础条件
        wrapper.eq(Ampe::getIsDeleted, Boolean.FALSE);
        wrapper.orderByDesc(Ampe::getId);
        return wrapper;
    }

    @Override
    public Boolean ampeDelete(Long ampeId) {
        Ampe ampe = ampeMapper.selectById(ampeId);
        ClassUtil.throwBusinessException(ObjectUtil.isNull(ampe), "ampe平台不存在");
        ClassUtil.throwBusinessException(ampe.getIsDeleted(), "ampe平台已删除");
        ampe.setIsDeleted(Boolean.TRUE);
        ampeMapper.updateById(ampe);
        return Boolean.TRUE;
    }

    @Override
    public RestResult<List<AmpeVo>> ampeList(Account account) {
        List<Long> ampeIds = commonImpl.getAuthAmpeIds(account);
        if (Preconditions.isBlank(ampeIds)) {
            return RestResult.ok(Collections.emptyList());
        }
        LambdaQueryWrapper<Ampe> awrapper = new LambdaQueryWrapper<>();
        //查询的基础条件
        awrapper.eq(Ampe::getIsDeleted, Boolean.FALSE).in(Ampe::getId, ampeIds);
        awrapper.orderByDesc(Ampe::getId);
        List<Ampe> ampeList = ampeMapper.selectList(awrapper);
        List<AmpeVo> vos = ampeList.stream().map(p -> {
            AmpeVo item = BeanUtil.copyProperties(p, AmpeVo.class);
            item.setAmpeId(p.getId());
            return item;
        }).collect(Collectors.toList());
        return RestResult.ok(vos != null ? vos : Collections.emptyList());
    }

}
