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.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.TenantApplyCallRecordDao;
import com.ym.mec.biz.dal.entity.TenantApply;
import com.ym.mec.biz.dal.entity.TenantApplyCallRecord;
import com.ym.mec.biz.service.TenantApplyCallRecordService;
import com.ym.mec.biz.service.TenantApplyService;
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.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Optional;

/**
 * 机构申请记录(TenantApplyCallRecord)表服务实现类
 *
 * @author hgw
 * @since 2022-02-09 10:44:26
 */
@Service("tenantApplyCallRecordService")
public class TenantApplyCallRecordServiceImpl extends ServiceImpl<TenantApplyCallRecordDao, TenantApplyCallRecord> implements TenantApplyCallRecordService {

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

    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private TenantApplyService tenantApplyService;

    @Override
    public PageInfo<TenantApplyCallRecord> queryPage(Map<String, Object> param) {
        int pageSize = PageUtil.getPage(param, "rows").orElse(50);
        int pageIndex = PageUtil.getPage(param, "page").orElse(1);
        Page<TenantApplyCallRecord> pageInfo = new Page<>(pageIndex, pageSize);
        pageInfo.setDesc("create_time_");
        IPage<TenantApplyCallRecord> page = this.page(pageInfo,
                new WrapperUtil<TenantApplyCallRecord>()
                        .hasEq("tenant_apply_id_", param.get("applyId"))
                        .hasGe("create_time_", param.get("startTime"))
                        .hasLe("create_time_", param.get("endTime"))
                        .queryWrapper());
        return PageUtil.pageInfo(page);
    }

    @Override
    public int insert(TenantApplyCallRecord obj) {
        Optional<TenantApplyCallRecord> optional = Optional.ofNullable(obj);
        optional.orElseThrow(() -> new BizException("参数异常！"));
        optional.map(TenantApplyCallRecord::getTenantApplyId)
                .orElseThrow(() -> new BizException("参数异常"));
        optional.map(TenantApplyCallRecord::getCallBackLog)
                .filter(a -> StringUtils.isNotBlank(a) && a.length() <= 140)
                .orElseThrow(() -> new BizException("请填写小于140字的沟通记录！"));
        obj.setCallBackPeople(getUser().getRealName());
        //修改申请表数据
        TenantApply tenantApply = new TenantApply();
        tenantApply.setId(Long.parseLong(obj.getTenantApplyId().toString()));
        tenantApply.setCallBackPeople(getUser().getRealName());
        tenantApply.setCallBackTime(obj.getCreateTime());
        tenantApplyService.update(tenantApply);

        return baseMapper.insert(obj);
    }

    private SysUser getUser() {
        //修改机构基础信息
        return Optional.ofNullable(sysUserFeignService.queryUserInfo())
                .orElseThrow(() -> new BizException("用户信息获取失败,请刷新页面或者重新登录!"));
    }


}

