package com.yc.ship.module.otc.service.auth;

import cn.hutool.core.map.MapUtil;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.framework.security.core.LoginUser;
import com.yc.ship.framework.security.core.util.SecurityFrameworkUtils;
import com.yc.ship.framework.tenant.core.context.TenantContextHolder;
import com.yc.ship.module.ota.api.OtaDistributorApi;
import com.yc.ship.module.otc.api.agency.dto.AgencyUserLoginInfoReqDTO;
import com.yc.ship.module.otc.api.agency.dto.AgencyUserLoginInfoRespDTO;
import com.yc.ship.module.otc.controller.admin.agencyLoginLog.vo.AgencyLoginLogSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.log.AgencyLoginLogDO;
import com.yc.ship.module.otc.dal.mysql.log.AgencyLoginLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.otc.enums.ErrorCodeConstants.NOT_ASSOCIATED_DISTRIBUTOR;

@Service
@Validated
public class AgencyLoginLogServiceImpl implements AgencyLoginLogService {

    @Resource
    private AgencyLoginLogMapper agencyLoginLogMapper;

    @Resource
    private OtaDistributorApi otaDistributorApi;

    @Override
    public AgencyUserLoginInfoRespDTO getLoginLog(AgencyUserLoginInfoReqDTO reqDTO) {
        LambdaQueryWrapperX<AgencyLoginLogDO> queryWrapper = new LambdaQueryWrapperX<AgencyLoginLogDO>()
                .eq(AgencyLoginLogDO::getUserId, reqDTO.getUserId())
                .eq(AgencyLoginLogDO::getAccessToken, reqDTO.getAccessToken());
        List<AgencyLoginLogDO> agencyLoginLogDOs = agencyLoginLogMapper.selectList(queryWrapper);
        if(agencyLoginLogDOs!=null && !agencyLoginLogDOs.isEmpty()){
            return BeanUtils.toBean(agencyLoginLogDOs.get(0), AgencyUserLoginInfoRespDTO.class);
        }
        return null;
    }

    @Override
    public Long recordLog(AgencyLoginLogSaveReqVO reqVO) {
        Long tenantId = TenantContextHolder.getTenantId();
        Map<String, Object> distributorMap = agencyLoginLogMapper.selectDistributorInfo(tenantId);
        if (distributorMap==null || distributorMap.isEmpty()) {
           throw exception(NOT_ASSOCIATED_DISTRIBUTOR);
        }
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        assert loginUser != null;

        AgencyLoginLogDO loginLogDO = BeanUtils.toBean(reqVO, AgencyLoginLogDO.class);
        loginLogDO.setAgencyId(MapUtil.getLong(distributorMap, "id"));
        loginLogDO.setAgencyName(MapUtil.getStr(distributorMap, "distributorName"));
        loginLogDO.setAccessToken(loginUser.getAccessToken());
        loginLogDO.setUserId(loginUser.getId());
        loginLogDO.setDeleted(false);
        agencyLoginLogMapper.insert(loginLogDO);
        return loginLogDO.getId();
    }
}
