package com.ym.mec.biz.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.TenantConfigDao;
import com.ym.mec.biz.dal.dto.TenantConfigDto;
import com.ym.mec.biz.dal.entity.TenantConfig;
import com.ym.mec.biz.dal.entity.TenantConfigDetail;
import com.ym.mec.biz.service.TenantConfigService;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.WrapperUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * @author hgw
 * Created by 2021-12-02
 */
@Service
public class TenantConfigServiceImpl extends ServiceImpl<TenantConfigDao, TenantConfig> implements TenantConfigService {

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addConfig(TenantConfigDto dto) {
        setUserId(dto.getUpdatedBy(), dto::setUpdatedBy);
        TenantConfig info = new TenantConfig();
        info.setUpdatedTime(new Date());
        BeanUtils.copyProperties(dto, info);
        this.save(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateConfig(TenantConfigDto dto) {
        setUserId(dto.getUpdatedBy(), dto::setUpdatedBy);
        TenantConfig info = new TenantConfig();
        BeanUtils.copyProperties(dto, info);
        this.updateById(info);
    }

    @Override
    public TenantConfig queryByTenantId(Integer tenantId) {
        return getOne(new WrapperUtil<TenantConfig>().hasEq("tenant_id_", tenantId)
                .queryWrapper());
    }

    @Override
    public Map<String, BigDecimal> getRule(Integer tenantId) {
        //查询云教室扣费标准
        TenantConfig tenantConfig = this.getOne(new WrapperUtil<TenantConfig>()
                .hasEq("tenant_id_", tenantId).queryWrapper());
        if (Objects.isNull(tenantConfig)) {
            return null;
        }
        return getCloudRoomRule(tenantConfig.getConfig());
    }

    @Override
    public Map<String, BigDecimal> getCloudRoomRule(String config) {
        return Optional.ofNullable(config)
                .map(c -> JSON.parseObject(c, TenantConfigDetail.class))
                .map(TenantConfigDetail::getCloud_room_rule)
                .map(TenantConfigDetail.CloudRoomRule::getCloud_room_config)
                .orElse(null);
    }

    @Override
    public Boolean autoActivationFlag(Integer tenantId) {
        TenantConfig tenantConfig = this.getOne(new WrapperUtil<TenantConfig>()
                .hasEq("tenant_id_", tenantId).queryWrapper());
        TenantConfigDetail configDetail = JSON.parseObject(tenantConfig.getConfig(), TenantConfigDetail.class);
        TenantConfigDetail.MemberConfig memberConfig = configDetail.getMember_config();
        if(memberConfig.getDay_divide().compareTo(BigDecimal.ZERO) == 0 &&
                memberConfig.getMonth_divide().compareTo(BigDecimal.ZERO) == 0 &&
                memberConfig.getHalf_year_divide().compareTo(BigDecimal.ZERO) == 0 &&
                memberConfig.getQuarter_divide().compareTo(BigDecimal.ZERO) == 0 &&
                memberConfig.getYear_divide().compareTo(BigDecimal.ZERO) == 0){
            return true;
        }
        return false;
    }

    private void setUserId(Integer id, Consumer<Integer> setOption) {
        if (Objects.isNull(id)) {
            Integer userId = Optional.ofNullable(sysUserFeignService.queryUserInfo())
                    .map(SysUser::getId)
                    .orElseThrow(() -> new BizException("用户信息获取失败"));
            setOption.accept(userId);
        }
    }
}
