package com.seer.teach.mp.app.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.utils.AESUtils;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mp.app.controller.req.AppMpDealerApplyQueryReq;
import com.seer.teach.mp.request.DealerApplyReq;
import com.seer.teach.mp.app.controller.resp.AppMpDealerApplyResp;
import com.seer.teach.mp.app.convert.AppDealerApplicationsConvert;
import com.seer.teach.mp.app.convert.AppMpDealerApplicationsConvert;
import com.seer.teach.mp.entity.MpDealerApplicationsEntity;
import com.seer.teach.mp.service.IMpDealerApplicationsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;

@Slf4j
@RequiredArgsConstructor
@Service
public class AppDealerApplicationsService {

    private final IMpDealerApplicationsService mpDealerApplicationsService;

    /**
     * 申请成为分销商
     * @param request 请求参数
     */

    public void applyForDealer(DealerApplyReq request,Integer userId) {
        MpDealerApplicationsEntity dealerApplicationsEntity = mpDealerApplicationsService.getOne(new LambdaQueryWrapper<>(MpDealerApplicationsEntity.class).eq(MpDealerApplicationsEntity::getApplicantUserId, userId));
        AssertUtils.isNull(dealerApplicationsEntity, ResultCodeEnum.DEALER_APPLICATION_DUPLICATE);
        mpDealerApplicationsService.apply(request, userId);
    }

    public AppMpDealerApplyResp info(Integer userId) {
        return AppDealerApplicationsConvert.INSTANCE.convertOne(mpDealerApplicationsService.getOneByUserId(userId));
    }

    public boolean updateDealerApplication(DealerApplyReq request, int userId) {
        return mpDealerApplicationsService.updateDealerApplication(request,userId);
    }

    public PageListBean<AppMpDealerApplyResp> page(int userId, AppMpDealerApplyQueryReq pageReq) {
        log.info("pageReq: {}", pageReq);
        Page<MpDealerApplicationsEntity> pageParm = new Page<>(pageReq.getPageNo(), pageReq.getPageSize());

        MpDealerApplicationsEntity dealerApplicationsQueryParam = AppMpDealerApplicationsConvert.INSTANCE.convertOne(pageReq,userId);

        IPage<MpDealerApplicationsEntity> resultPage = mpDealerApplicationsService.pageList(pageParm, dealerApplicationsQueryParam);

        return PageConverterUtils.convertPageListBean(resultPage, AppDealerApplicationsConvert.INSTANCE::convertRespList);
    }
}