package com.zfire.mall.manager.logic.manage.v2.product.price;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.manage.product.price.ProductWalletBean;
import com.zfire.mall.manager.bean.manage.v2.product.price.ProductUpdPriceBillVO;
import com.zfire.mall.manager.commonmapper.manage.product.ProductUpdPriceBillReserveMapper;
import com.zfire.mall.manager.enums.WalletTypeEnum;
import com.zfire.mall.manager.logic.common.CommonLogic;
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.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ProductUpdPriceBillV2Logic {

    @Resource
    CommonLogic commonLogic;
    @Resource
    ProductUpdPriceBillReserveMapper productUpdPriceBillReserveMapper;

    public IPage<ProductUpdPriceBillVO> v2ListPage(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (CollectionUtils.isNotEmpty(adminUser.getAdminCompanyIds())) {
//            String join = adminUser.getAdminCompanyIds().stream()
//                    .map(v -> "'" + v + "'")
//                    .collect(Collectors.joining(",", "(", ")"));
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.admin_company_id")
                    .setCompare("IN")
                    .setValue(adminUser.getAdminCompanyIds()));
        }
        if (CollectionUtils.isNotEmpty(adminUser.getK3CategoryIds())) {
//            String join = adminUser.getK3CategoryIds().stream()
//                    .map(v -> "'" + v + "'")
//                    .collect(Collectors.joining(",", "(", ")"));
            zfireParamBean.getParams().add(new QueryParamBean().setParam("b.k3_category_id")
                    .setCompare("IN")
                    .setValue(adminUser.getK3CategoryIds()));
        }
        FieldUtils.supplyParam(zfireParamBean);
        IPage<ProductUpdPriceBillVO> page = productUpdPriceBillReserveMapper.v2ListPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
        for (ProductUpdPriceBillVO record : page.getRecords()) {
            List<ProductWalletBean> wallets = productUpdPriceBillReserveMapper.queryWallet(Collections.singletonList(record.getItemId()));
            record.setRebateWallet(wallets.stream()
                    .filter(v -> v.getType().equals(WalletTypeEnum.REBATE.getKey()))
                    .map(ProductWalletBean::getWalletName)
                    .collect(Collectors.joining(",")));
            record.setCommonlyWallet(wallets.stream()
                    .filter(v -> v.getType().equals(WalletTypeEnum.COMMONLY.getKey()))
                    .map(ProductWalletBean::getWalletName)
                    .collect(Collectors.joining(",")));
        }
        return page;
    }
}
