package com.zfire.mall.manager.logic.config;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.config.MerchantLoginQuotaLimitAddParam;
import com.zfire.mall.manager.bean.config.MerchantLoginQuotaLimitDetailVO;
import com.zfire.mall.manager.bean.config.MerchantLoginQuotaLimitEditParam;
import com.zfire.mall.manager.bean.config.MerchantLoginQuotaLimitListVO;
import com.zfire.mall.manager.commonmapper.CommonMapper;
import com.zfire.mall.manager.convert.MerchantLoginQuotaLimitConvert;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.plus.entity.CustomerBillLimitConfig;
import com.zfire.mall.manager.plus.entity.KingDeeCustomer;
import com.zfire.mall.manager.plus.service.CustomerBillLimitConfigService;
import com.zfire.mall.manager.plus.service.KingDeeCustomerService;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author qinrongjun
 * @description
 * @date 2023/7/26 14:13 星期三
 */
@RequiredArgsConstructor
@Service
public class MerchantLoginQuotaLimitLogic {
    private final KingDeeCustomerService kingDeeCustomerService;
    private final CustomerBillLimitConfigService customerBillLimitConfigService;
    private final CommonMapper commonMapper;

    public IPage<MerchantLoginQuotaLimitListVO> list(ZfireParamBean param) {
        IPage<CustomerBillLimitConfig> page = commonMapper.merchantLoginQuotaLimitList(new Page<>(param.getPageNum(), param.getPageSize()), param);
        return buildPageVO(page);
    }

    public boolean del(List<String> idList) {
        return customerBillLimitConfigService.removeByIds(idList);
    }

    public boolean add(MerchantLoginQuotaLimitAddParam param, AdminUserCom adminUser) {
        CustomerBillLimitConfig customerBillLimitConfig = MerchantLoginQuotaLimitConvert.INSTANCE.toDO(param,adminUser.getUserName());
        return customerBillLimitConfigService.save(customerBillLimitConfig);
    }

    public boolean edit(MerchantLoginQuotaLimitEditParam param, AdminUserCom adminUser) {
        CustomerBillLimitConfig customerBillLimitConfig = MerchantLoginQuotaLimitConvert.INSTANCE.toDO(param, adminUser.getUserName());
        return customerBillLimitConfigService.updateById(customerBillLimitConfig);
    }

    public MerchantLoginQuotaLimitDetailVO detail(String id) {
        return customerBillLimitConfigService.lambdaQuery()
                .eq(CustomerBillLimitConfig::getId, id)
                .oneOpt()
                .map(MerchantLoginQuotaLimitConvert.INSTANCE::toVO)
                .orElseThrow(() -> new RemoteServiceException("查询信息不存在"));
    }

    private Page<MerchantLoginQuotaLimitListVO> buildPageVO(IPage<CustomerBillLimitConfig> page) {
        if (CollectionUtils.isEmpty(page.getRecords())) {
            return new Page<>(page.getCurrent(), page.getSize());
        }
        List<String> customerIdList = page.getRecords()
                .stream()
                .map(CustomerBillLimitConfig::getCustomerId)
                .collect(Collectors.toList());
        Map<String, String> nameByIdMap = kingDeeCustomerService.lambdaQuery()
                .in(KingDeeCustomer::getId, customerIdList)
                .list()
                .stream()
                .collect(Collectors.toMap(KingDeeCustomer::getId, KingDeeCustomer::getName));
        return MerchantLoginQuotaLimitConvert.INSTANCE.toPageVO(page, nameByIdMap);
    }
}
