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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.config.FirstPartyCustomerManagementAddParam;
import com.zfire.mall.manager.bean.config.FirstPartyCustomerManagementDetailVO;
import com.zfire.mall.manager.bean.config.FirstPartyCustomerManagementEditParam;
import com.zfire.mall.manager.bean.config.FirstPartyCustomerManagementListVO;
import com.zfire.mall.manager.commonmapper.CommonMapper;
import com.zfire.mall.manager.convert.FirstPartyCustomerManagementConvert;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.PartyAInfo;
import com.zfire.mall.manager.plus.entity.Region;
import com.zfire.mall.manager.plus.service.PartyAInfoService;
import com.zfire.mall.manager.plus.service.RegionService;
import com.zfire.mall.manager.zfire.bean.QueryParamBean;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author qinrongjun
 * @description
 * @date 2023/7/26 14:10 星期三
 */
@RequiredArgsConstructor
@Service
public class FirstPartyCustomerManagementLogic {
    private final PartyAInfoService partyAInfoService;
    private final CommonMapper commonMapper;
    private final RegionService regionService;
    private final TradeConfigLogic tradeConfigLogic;
    private final CommonLogic commonLogic;

    public IPage<FirstPartyCustomerManagementListVO> list(ZfireParamBean param) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        if (adminUser.getIsCustomer()) {
            param.getParams().add(new QueryParamBean().setParam("a.create_name")
                    .setCompare("=")
                    .setValue(adminUser.getUserName()));
        }
        if (adminUser.getIsService()) {
            param.getParams().add(new QueryParamBean().setParam("a.create_name")
                    .setCompare("=")
                    .setValue(adminUser.getUserName()));
        }
        FieldUtils.supplyParam(param);
        IPage<PartyAInfo> page = commonMapper.firstPartyCustomerManagementList(new Page<>(param.getPageNum(), param.getPageSize()), param);
        return buildPageVO(page);
    }

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

    public boolean edit(FirstPartyCustomerManagementEditParam param, AdminUserCom adminUser) {
        ArrayList<Long> regionIdLIst = Lists.newArrayList(param.getProvinceRegionId(), param.getCityRegionId(), param.getAreaId());
        Map<Long, String> regionNameByIdMap = Collections.emptyMap();
        if (CollectionUtils.isNotEmpty(regionIdLIst)) {
            regionNameByIdMap = regionService.lambdaQuery()
                    .select(Region::getId, Region::getName)
                    .in(Region::getId, regionIdLIst)
                    .list()
                    .stream()
                    .collect(Collectors.toMap(Region::getId, Region::getName));
        }
        PartyAInfo partyAInfo = FirstPartyCustomerManagementConvert.INSTANCE.toDO(param,adminUser.getUserName(), regionNameByIdMap);
        return partyAInfoService.updateById(partyAInfo);
    }

    public boolean add(FirstPartyCustomerManagementAddParam param, AdminUserCom adminUser) {
        Map<Long, String> regionNameByIdMap = regionService.lambdaQuery()
                .select(Region::getId, Region::getName)
                .in(Region::getId, Lists.newArrayList(param.getProvinceRegionId(), param.getCityRegionId(), param.getAreaId()))
                .list()
                .stream()
                .collect(Collectors.toMap(Region::getId, Region::getName));

        PartyAInfo partyAInfo = FirstPartyCustomerManagementConvert.INSTANCE.toDO(param,adminUser.getUserName(), regionNameByIdMap);
        return partyAInfoService.save(partyAInfo);
    }

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

    private IPage<FirstPartyCustomerManagementListVO> buildPageVO(IPage<PartyAInfo> page) {
        if (CollectionUtils.isEmpty(page.getRecords())) {
            return new Page<>(page.getCurrent(), page.getSize());
        }
        List<String> tradeIdList = page.getRecords()
                .stream()
                .map(item -> Lists.newArrayList(item.getTradeId(), item.getTradeParentId()))
                .flatMap(Collection::stream)
                .collect(Collectors.toList());
        Map<String, String> tradeNameByIdMap = tradeConfigLogic.nameByIdMap(tradeIdList);
        return FirstPartyCustomerManagementConvert.INSTANCE.toPageVO(page, tradeNameByIdMap);
    }
}
