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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ym.mec.biz.dal.dao.TenantPreJoinDao;
import com.ym.mec.biz.dal.dto.TenantPreJoinDto;
import com.ym.mec.biz.dal.entity.SysArea;
import com.ym.mec.biz.dal.entity.SysUserTsign;
import com.ym.mec.biz.dal.entity.TenantPreJoin;
import com.ym.mec.biz.dal.vo.TenantPreJoinVo;
import com.ym.mec.biz.service.SysUserTsignService;
import com.ym.mec.biz.service.TenantPreJoinService;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.PageUtil;
import com.ym.mec.common.page.WrapperUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 机构入驻表(预加入)(TenantPreJoin)表服务实现类
 *
 * @author hgw
 * @since 2022-02-09 14:38:28
 */
@Service("tenantPreJoinService")
public class TenantPreJoinServiceImpl extends ServiceImpl<TenantPreJoinDao, TenantPreJoin> implements TenantPreJoinService {

    private final static Logger log = LoggerFactory.getLogger(TenantPreJoinServiceImpl.class);

    @Autowired
    private SysAreaServiceImpl sysAreaService;
    @Autowired
    private SysUserTsignService sysUserTsignService;

    /**
     * 分页查询
     *
     * @param param 条件
     */
    @Override
    public PageInfo<TenantPreJoinVo> queryPage(Map<String, Object> param) {
        Page<TenantPreJoinVo> pageInfo = PageUtil.getPageInfo(param);
        pageInfo.setDesc("a.created_time_");
        return PageUtil.pageInfo(baseMapper.queryPage(pageInfo, param));
    }

    @Override
    public int insert(TenantPreJoinDto dto) {
        //校验营业信息
        SysUserTsign sysUserTsign = sysUserTsignService.queryByCardNo(dto.getTsignCode());
        if (Objects.nonNull(sysUserTsign)) {
            throw new BizException("营业执照编号已被注册!");
        }
        TenantPreJoin obj = new TenantPreJoin();
        BeanUtils.copyProperties(dto, obj);
        //根据code获取市级数据
        opsAreaData(dto.getAreaCode(), sysAreaService::queryByCode,
                () -> new BizException("未查询到区域编号为 [" + dto.getAreaCode() + "]的数据！"),
                obj::setAreaId, obj::setAreaName);

        //根据市级数据获取省级数据
        opsAreaData(obj.getAreaId(), sysAreaService::getParentArea,
                () -> new BizException("未查询到区域id为 [" + obj.getAreaId() + "]的数据！"),
                obj::setProvinceId, obj::setProvinceName);
        obj.setState(0);
        obj.setCreatedTime(new Date());
        return baseMapper.insert(obj);
    }

    private <O> void opsAreaData(O param, Function<O, SysArea> fun, Supplier<BizException> supplier,
                                 Consumer<Integer> setId, Consumer<String> setName) {
        Optional<SysArea> optional = Optional.ofNullable(fun.apply(param));
        optional.orElseThrow(supplier);
        optional.ifPresent(a -> {
            setId.accept(a.getId());
            setName.accept(a.getName());
        });
    }

}

