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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.EnumUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.ExcelData;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.manage.product.price.ProductUpdPriceBillBean;
import com.zfire.mall.manager.bean.manage.product.price.ProductUpdPriceBillItemBean;
import com.zfire.mall.manager.bean.manage.product.price.ProductUpdPriceBillListBean;
import com.zfire.mall.manager.bean.manage.product.price.ProductWalletBean;
import com.zfire.mall.manager.commonmapper.manage.product.ProductUpdPriceBillReserveMapper;
import com.zfire.mall.manager.enums.ExamineStatusEnum;
import com.zfire.mall.manager.enums.WalletTypeEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.logic.k3.K3CategoryLogic;
import com.zfire.mall.manager.logic.k3.K3MaterialLogic;
import com.zfire.mall.manager.logic.saletype.SaleTypeLogic;
import com.zfire.mall.manager.logic.wallet.WalletLogic;
import com.zfire.mall.manager.plus.entity.*;
import com.zfire.mall.manager.plus.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ProductUpdPriceBillLogic {
    @Resource
    CommonLogic commonLogic;
    @Autowired
    WalletLogic walletLogic;
    @Autowired
    SaleTypeLogic saleTypeLogic;
    @Autowired
    K3MaterialLogic k3MaterialLogic;
    @Autowired
    K3CategoryLogic k3CategoryLogic;
    @Resource
    ProductUpdPriceBillReserveMapper productUpdPriceBillReserveMapper;
    @Resource
    ProductUpdPriceBillService productUpdPriceBillService;
    @Resource
    ProductUpdPriceBillItemService productUpdPriceBillItemService;
    @Resource
    ProductUpdPriceBillItemWalletService productUpdPriceBillItemWalletService;
    @Resource
    ProductPriceWalletService productPriceWalletService;
    @Resource
    SaleTypeService saleTypeService;
    @Autowired
    AdminWebsitService adminWebsitService;
    @Autowired
    AdminUserService adminUserService;
    @Autowired
    KingDeeMaterialService kingDeeMaterialService;
    @Resource
    ProductPriceService productPriceService;

    public IPage<ProductUpdPriceBillListBean> listPage(String materialOldNumber, String materialNumber, String materialName, String remark,
                                                       String mainId, String saleTypeId, String startDate,
                                                       String specification, String billType,String k3CategoryNumber, Integer pageNum, Integer pageSize) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> adminCompanyIds = adminUser.getAdminCompanyIds();

        IPage<ProductUpdPriceBillListBean> productUpdPriceBillListBeanIPage = productUpdPriceBillReserveMapper
                .listPage(new Page<>(pageNum, pageSize), materialOldNumber,materialNumber, materialName,remark, mainId,
                        saleTypeId,startDate, adminCompanyIds,specification, billType,k3CategoryNumber, adminUser.getK3CategoryIds());
        for(ProductUpdPriceBillListBean bean : productUpdPriceBillListBeanIPage.getRecords()){
            bean.setWallets(productUpdPriceBillReserveMapper.queryWallet(Arrays.asList(bean.getItemId())));
        }

        return productUpdPriceBillListBeanIPage;
    }


    public ProductUpdPriceBillBean detail(String id) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> adminCompanyIds = adminUser.getAdminCompanyIds();
        ProductUpdPriceBillBean detail = productUpdPriceBillReserveMapper.detail(id, adminCompanyIds);
       //补充钱包
        this.supplyWallet(detail.getItems());
        return detail;
    }

    private void supplyWallet(List<ProductUpdPriceBillItemBean> items){
        if(CollectionUtils.isEmpty(items)){
            return;
        }
        List<String> updPriceBillItemIds = items.stream().map(ProductUpdPriceBillItemBean::getId).collect(Collectors.toList());
        List<ProductWalletBean> productWalletBeans = productUpdPriceBillReserveMapper.queryWallet(updPriceBillItemIds);

        Map<String,List<ProductWalletBean>> map = new HashMap<>();
        for(ProductWalletBean bean : productWalletBeans){
            if(map.get(bean.getUpdPriceBillItemId()) == null){
                List<ProductWalletBean> list = new ArrayList<>();
                list.add(bean);
                map.put(bean.getUpdPriceBillItemId(),list);
            }else{
                map.get(bean.getUpdPriceBillItemId()).add(bean);
            }
        }

        for(ProductUpdPriceBillItemBean item : items){
            item.setWallets2(map.get(item.getId()));
        }

    }


    @Transactional(rollbackFor = Exception.class)
    public String add(ProductUpdPriceBillBean productUpdPriceBillBean) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        // 保存主表
        ProductUpdPriceBill productUpdPriceBill = productUpdPriceBillBean.generateBillLogic(productUpdPriceBillBean, adminUser);
        productUpdPriceBill.setServiceId(adminUser.getAdminUserId());
        productUpdPriceBill.setServiceName(adminUser.getNickName());
        productUpdPriceBill.setAdminWebsitId(adminUser.getAdminWebsitId());
        productUpdPriceBill.setAdminWebsitName(adminUser.getAdminWebsitName());
        productUpdPriceBill.insert();

        //检查是否有存在同销售类型，同机型的商品
//        List<ProductUpdPriceBillItemBean> items = productUpdPriceBillBean.getItems();
//        String saleMaterialId = "";

//        for(ProductUpdPriceBillItemBean item : items) {
//            Integer count = productUpdPriceBillItemService.lambdaQuery()
//                    .eq(ProductUpdPriceBillItem::getMaterialId, item.getMaterialId())
//                    .eq(ProductUpdPriceBillItem::getSaleTypeId, item.getSaleTypeId())
//                    .eq(ProductUpdPriceBillItem::getBillType,productUpdPriceBill.getBillType())
//                    .count();
//            if(count > 0){
//                throw new RemoteServiceException("已存在相同销售类型的机型");
//            }
//            if(StringUtils.equals(saleMaterialId,item.getSaleTypeId()+item.getMaterialId())){
//                throw new RemoteServiceException("不可添加相同销售类型的机型");
//            }
//            saleMaterialId = item.getSaleTypeId()+item.getMaterialId();
//        }

        // 保存明细
        productUpdPriceBillBean.insertItem(productUpdPriceBillItemWalletService,productUpdPriceBillReserveMapper,
                saleTypeService, kingDeeMaterialService, productUpdPriceBill.getBillType());

        return productUpdPriceBill.getUpdPriceBillId();
    }



    @Transactional(rollbackFor = Exception.class)
    public void edit(ProductUpdPriceBillBean productUpdPriceBillBean) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        ProductUpdPriceBill productUpdPriceBill = new ProductUpdPriceBill();
        BeanUtils.copyProperties(productUpdPriceBillBean, productUpdPriceBill);

        boolean updRes = productUpdPriceBill.update(new LambdaUpdateWrapper<ProductUpdPriceBill>()
                .set(ProductUpdPriceBill::getRemark, productUpdPriceBill.getRemark())
                .set(ProductUpdPriceBill::getUpdateName, adminUser.getNickName())
                .eq(ProductUpdPriceBill::getUpdPriceBillId, productUpdPriceBill.getUpdPriceBillId())
                .eq(ProductUpdPriceBill::getExamineStatus, ExamineStatusEnum.SAVE.toString())
                .eq(ProductUpdPriceBill::getAdminCompanyId, adminUser.getAdminCompanyId()));

        if (!updRes) {
            throw new RemoteServiceException("单据状态已发生改变，保存失败");
        }

        //检查是否有存在同销售类型，同机型的商品
//        List<ProductUpdPriceBillItemBean> items = productUpdPriceBillBean.getItems();
//        String saleMaterialId = "";
//        for(ProductUpdPriceBillItemBean item : items) {
//            Integer count = productUpdPriceBillItemService.lambdaQuery()
//                    .eq(ProductUpdPriceBillItem::getMaterialId, item.getMaterialId())
//                    .eq(ProductUpdPriceBillItem::getSaleTypeId, item.getSaleTypeId())
//                    .eq(ProductUpdPriceBillItem::getBillType,productUpdPriceBill.getBillType())
//                    .ne(ProductUpdPriceBillItem::getUpdPriceBillId,productUpdPriceBill.getUpdPriceBillId())
//                    .count();
//            if(count > 0){
//                throw new RemoteServiceException("已存在相同销售类型的机型");
//            }
//            if(StringUtils.equals(saleMaterialId,item.getSaleTypeId()+item.getMaterialId())){
//                throw new RemoteServiceException("不可添加相同销售类型的机型");
//            }
//            saleMaterialId = item.getSaleTypeId()+item.getMaterialId();
//        }

        // 删除明细
        productUpdPriceBillItemService.remove(new LambdaQueryWrapper<ProductUpdPriceBillItem>()
                .eq(ProductUpdPriceBillItem::getUpdPriceBillId, productUpdPriceBill.getUpdPriceBillId()));
        productUpdPriceBillItemWalletService.remove(new LambdaQueryWrapper<ProductUpdPriceBillItemWallet>()
                .eq(ProductUpdPriceBillItemWallet::getUpdPriceBillId, productUpdPriceBill.getUpdPriceBillId()));

        // 插入明细数据
        productUpdPriceBillBean.insertItem(productUpdPriceBillItemWalletService,productUpdPriceBillReserveMapper,
                saleTypeService, kingDeeMaterialService, productUpdPriceBill.getBillType());
    }

    public void submit(List<String> ids) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        productUpdPriceBillService.lambdaUpdate()
                .set(ProductUpdPriceBill::getExamineStatus, ExamineStatusEnum.WAIT.toString())
                .set(ProductUpdPriceBill::getSubmitBy, adminUser.getAdminUserId())
                .set(ProductUpdPriceBill::getSubmitName, adminUser.getNickName())
                .set(ProductUpdPriceBill::getSubmitTime, DateUtil.date())
                .in(ProductUpdPriceBill::getUpdPriceBillId, ids)
                .in(ProductUpdPriceBill::getAdminCompanyId, adminUser.getAdminCompanyId())
                .eq(ProductUpdPriceBill::getExamineStatus, ExamineStatusEnum.SAVE.toString()).update();

    }

    public void revoke(String id) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        boolean updRes = productUpdPriceBillService.update(new LambdaUpdateWrapper<ProductUpdPriceBill>()
                .eq(ProductUpdPriceBill::getUpdPriceBillId, id)
                .in(ProductUpdPriceBill::getAdminCompanyId, adminUser.getAdminCompanyId())
                .eq(ProductUpdPriceBill::getExamineStatus, ExamineStatusEnum.WAIT.toString())
                .set(ProductUpdPriceBill::getExamineStatus, ExamineStatusEnum.SAVE.toString())
                .set(ProductUpdPriceBill::getSubmitBy, "")
                .set(ProductUpdPriceBill::getSubmitName, "")
                .set(ProductUpdPriceBill::getSubmitTime, ""));
        if (!updRes) {
            throw new RemoteServiceException("单据状态已发生改变，撤消失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void confirm(List<String> ids, String examineStatus,String examineNote) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        ids = ids.stream().distinct().collect(Collectors.toList());;

        List<ProductUpdPriceBill> list = productUpdPriceBillService.lambdaQuery()
                .in(ProductUpdPriceBill::getUpdPriceBillId, ids).list();

        for(ProductUpdPriceBill bill : list){
            if(!bill.getExamineStatus().equals(ExamineStatusEnum.WAIT.toString())){
                throw new RemoteServiceException("存在非待审核状态的单据，无法审核");
            }
            bill.setExamineStatus(examineStatus);
            bill.setExamineNote(examineNote);
            bill.setConfirmBy(adminUser.getAdminUserId());
            bill.setConfirmName(adminUser.getNickName());
            bill.setConfirmTime(new Date());
        }
        productUpdPriceBillService.updateBatchById(list);

        for(String id : ids) {
            if (examineStatus.equals(ExamineStatusEnum.OK.toString())) {
                //ProductUpdPriceBillBean detail = productUpdPriceBillReserveMapper.detail(id, adminUser.getAdminCompanyIds());
                ProductUpdPriceBill detail = productUpdPriceBillService.getById(id);
                List<ProductUpdPriceBillItem> items = productUpdPriceBillItemService.lambdaQuery().eq(ProductUpdPriceBillItem::getUpdPriceBillId, id).list();

                // 查询未作废价格旧记录
                List<ProductPrice> oldProductPrices = productPriceService.lambdaQuery()
                        .eq(ProductPrice::getBillType, detail.getBillType())
                        .in(ProductPrice::getMaterialNumber, items.stream()
                                .map(ProductUpdPriceBillItem::getMaterialNumber)
                                .collect(Collectors.toList()))
                        .eq(ProductPrice::getIsRevoke, false)
                        .list();
                Map<String, ProductPrice> oldProductPriceMap = oldProductPrices.stream()
                        .collect(Collectors.toMap(v -> v.getSaleTypeId() + "|" + v.getMaterialNumber(), Function.identity()));
                // 装入价格旧记录
                List<ProductPrice> revokeProductPriceList = new ArrayList<>();
                items.forEach(item -> {
                    ProductPrice productPrice = new ProductPrice();
                    BeanUtils.copyProperties(item, productPrice);
                    productPrice.setId(null)
                            .setBillType(detail.getBillType())
                            .setAdminCompanyId(detail.getAdminCompanyId())
                            .setRefId(item.getUpdPriceBillId())
                            .setMainId(item.getMainId())
                            .setMainName(item.getMainName())
                            .setServiceId(detail.getServiceId())
                            .setServiceName(detail.getServiceName())
                            .setAdminWebsitId(detail.getAdminWebsitId())
                            .setAdminWebsitName(detail.getAdminWebsitName())
                            .setQty(item.getQty())
                            .setRemark(detail.getRemark())
                            .setIsRevoke(false)
                            .setK3CategoryId(item.getK3CategoryId())
                            .setK3CategoryNumber(item.getK3CategoryNumber())
                            .setK3CategoryName(item.getK3CategoryName());
                    productPrice.insert();

                    ProductPrice oldPrice = oldProductPriceMap.get(productPrice.getSaleTypeId() + "|" + productPrice.getMaterialNumber());
                    if (Objects.nonNull(oldPrice)) {
                        revokeProductPriceList.add(oldPrice.setIsRevoke(true));
                    }

                    List<ProductPriceWallet> wallets = new ArrayList<>();

                    List<ProductWalletBean> productWalletBeans = productUpdPriceBillReserveMapper.queryWallet(Arrays.asList(item.getId()));
                    productWalletBeans.forEach(wallet -> {
                        ProductPriceWallet priceWallet = new ProductPriceWallet();
                        priceWallet.setProductPriceId(productPrice.getId());
                        priceWallet.setWalletId(wallet.getWalletId());
                        priceWallet.setType(wallet.getType());
                        wallets.add(priceWallet);
                    });
                    productPriceWalletService.saveBatch(wallets);
                    // 批量作废价格旧记录
                    if (CollectionUtils.isNotEmpty(revokeProductPriceList)) {
                        productPriceService.updateBatchById(revokeProductPriceList);
                    }
                });
            }
        }

    }

    @Transactional(rollbackFor = Exception.class)
    public void del(List<String> ids) {
        productUpdPriceBillService.lambdaUpdate().in(ProductUpdPriceBill::getUpdPriceBillId, ids).remove();
        productUpdPriceBillItemService.lambdaUpdate().in(ProductUpdPriceBillItem::getUpdPriceBillId,ids).remove();
        productUpdPriceBillItemWalletService.lambdaUpdate().in(ProductUpdPriceBillItemWallet::getUpdPriceBillId,ids).remove();
        productPriceService.lambdaUpdate().in(ProductPrice::getRefId,ids).remove();
    }

    /**
     * 导入产品调价单
     */
    @Transactional
    public int importData(List<Object> list, String billType) throws Exception {
        //所有销售类型code
        List<SaleType> saleTypes = saleTypeLogic.listAll();
        Map<String, SaleType> saleTypeMap = saleTypes.stream().collect(Collectors.toMap(SaleType::getSaleCode, v -> v));
        //所有物料
        List<KingDeeMaterial> kingDeeMaterials = k3MaterialLogic.allList();
        Map<String, KingDeeMaterial> materialMap = kingDeeMaterials.stream().collect(Collectors.toMap(KingDeeMaterial::getNumber, v -> v));
        //查询所有现金钱包
        List<KingDeeWallet> wallets = walletLogic.list(null);
        Map<String, KingDeeWallet> walletMap = wallets.stream().collect(Collectors.toMap(KingDeeWallet::getName, v -> v));
        //查询所有返利钱包
        List<WalletRebate> walletRebates = walletLogic.queryWalletRebateList();
        Map<String, WalletRebate> walletRebateMap = walletRebates.stream().collect(Collectors.toMap(WalletRebate::getName, v -> v));

        List<String> ids = new ArrayList<>();

        for(Object o : list){
            List<ProductUpdPriceBillItemBean> resultList = new ArrayList<>();
            List<Object> row = (List<Object>)o;
            String materialNumber = (String)row.get(0);
            if(StringUtils.isEmpty(materialNumber)){
                continue;
            }
            String saleTypeCode = (String)row.get(3);
            BigDecimal price = new BigDecimal(row.get(5).toString());
            String remark = (String)row.get(6);
            BigDecimal discAmount = new BigDecimal(row.get(7) == null ? "0" : row.get(7).toString());
            String walletNames = (String)row.get(8);
            String rebateWalletNames = (String)row.get(9);
            String[] walletName = walletNames.trim().replaceAll("，",",").split(",");
            String startDate  = (String)row.get(10);
            String endDate = (String)row.get(11);
            if(StringUtils.isEmpty(startDate)){
                startDate = DateUtils.formatDate(new Date());
            }
            if(StringUtils.isEmpty(endDate)){
                endDate = "2100-01-01 23:59:59";
            }
            if(StringUtils.isEmpty(materialNumber) || StringUtils.isEmpty(saleTypeCode) || price == null
                    || StringUtils.isEmpty(walletNames)){
                throw new RemoteServiceException("检测到物料编码/销售类型编码/单价/现金钱包有为空的情况");
            }
            SaleType saleType = saleTypeMap.get(saleTypeCode);
            if(saleType == null){
                throw new RemoteServiceException("销售类型编码:"+saleTypeCode+"不存在");
            }
            KingDeeMaterial kingDeeMaterial = materialMap.get(materialNumber);
            if(kingDeeMaterial == null){
                throw new RemoteServiceException("物料编码:"+materialNumber+"不存在");
            }
            ProductUpdPriceBillBean productUpdPriceBillBean = new ProductUpdPriceBillBean();
            productUpdPriceBillBean.setBillType(billType);
            productUpdPriceBillBean.setRemark(remark);

            ProductUpdPriceBillItemBean item = new ProductUpdPriceBillItemBean();
            item.setSaleTypeId(saleType.getId());
            item.setSaleTypeCode(saleTypeCode);
            item.setSaleTypeName(saleType.getSaleName());
            item.setBatchPrice(price);
            item.setDiscAmount(discAmount);
            item.setStartDate(DateUtil.parseDate(startDate));
            item.setEndDate(DateUtil.parseDate(endDate));
            item.setIsPublish(true);
            item.setMaterialId(kingDeeMaterial.getId());
            item.setMaterialNumber(kingDeeMaterial.getNumber());
            item.setMaterialName(kingDeeMaterial.getName());
            item.setMaterialOldNumber(kingDeeMaterial.getOldNumber());
            item.setSpecification(kingDeeMaterial.getSpecification());
            item.setUnit(kingDeeMaterial.getBaseUnitId());

            List<ProductUpdPriceBillItemWallet> wallet2 = new ArrayList<>();
            for(String name : walletName){
                KingDeeWallet wallet = walletMap.get(name);
                if(wallet == null){
                    throw new RemoteServiceException("钱包名称："+name+"，不存在");
                }
                ProductUpdPriceBillItemWallet productWalletBean = new ProductUpdPriceBillItemWallet();
                productWalletBean.setWalletId(wallet.getId());
                productWalletBean.setType(WalletTypeEnum.COMMONLY.getKey());
                wallet2.add(productWalletBean);
            }

            if(StringUtils.isNotEmpty(rebateWalletNames)) {
                String[] rebateWalletName = rebateWalletNames.trim().replaceAll("，",",").split(",");
                for (String name : rebateWalletName) {
                    WalletRebate walletRebate = walletRebateMap.get(name);
                    if (walletRebate == null) {
                        throw new RemoteServiceException("返利钱包名称：" + name + "，不存在");
                    }
                    Integer count = commonLogic.getRebateSaleTypeCount(saleType.getId(), walletRebate.getWalletRebateId());
                    if (count == 0) {
                        throw new RemoteServiceException("返利钱包+【"+walletRebate.getName() + "】与销售类型【" + saleType.getSaleName() + "】对应关系不存在");
                    }
                    ProductUpdPriceBillItemWallet productWalletBean = new ProductUpdPriceBillItemWallet();
                    productWalletBean.setWalletId(walletRebate.getWalletRebateId());
                    productWalletBean.setType(WalletTypeEnum.REBATE.getKey());
                    wallet2.add(productWalletBean);
                }
            }
            item.setWallets(wallet2);
            resultList.add(item);
            productUpdPriceBillBean.setItems(resultList);

            String billId = this.add(productUpdPriceBillBean);

            ids.add(billId);
        }

        if (CollectionUtils.isNotEmpty(ids)) {
            this.submit(ids);
            this.confirm(ids, ExamineStatusEnum.OK.toString(), "批量导入");
        }

        return ids.size();
    }



    /**
     *
     *导出产品调价列表
     * @param
     * @return
     */
    public ExcelData export(List<ProductUpdPriceBillListBean> productUpdPriceBillListBeans) {
        String[] titles = new String[]{"单据号","备注", "调价日期", "销售类型编码","销售类型", "物料编码", "厂产品代码", "产品名称",
                "规格型号", "计量单位", "数量", "批发价", "格力折扣", "生效日期",
                "失效日期", "是否促销价", "返利类型（钱包）", "现金钱包", "调价部门",
                "调价业务员","制单日期","审核人","审核日期","状态"};
        List<List<Object>> rows = new ArrayList<>();
        for (ProductUpdPriceBillListBean p : productUpdPriceBillListBeans) {
            List<Object> row = new ArrayList<>();
            row.add(p.getUpdPriceBillId());
            row.add(p.getRemark());
            row.add(DateUtil.formatDate(p.getCreateTime()));
            row.add(p.getSaleTypeCode());
            row.add(p.getSaleTypeName());
            row.add(p.getMaterialNumber());
            row.add(p.getMaterialOldNumber());
            row.add(p.getMaterialName());
            row.add(p.getSpecification());
            row.add(p.getUnit());
            row.add(p.getQty());
            row.add(p.getBatchPrice());
            row.add(p.getDiscAmount());
            row.add(p.getStartDate());
            row.add(p.getEndDate());
            row.add(p.getIsPromote() == null ? "否" : "是");

            List<String> rebateWalletNames = p.getWallets().stream().filter(v -> v.getType().equals(WalletTypeEnum.REBATE.getKey()))
                    .map(ProductWalletBean::getWalletName).collect(Collectors.toList());
            List<String> commonlyWalletNames = p.getWallets().stream().filter(v -> v.getType().equals(WalletTypeEnum.COMMONLY.getKey()))
                    .map(ProductWalletBean::getWalletName).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(rebateWalletNames)){
                row.add(StringUtils.join(rebateWalletNames,","));
            }else{
                row.add("");
            }
            if(CollectionUtils.isNotEmpty(commonlyWalletNames)){
                row.add(StringUtils.join(commonlyWalletNames,","));
            }else{
                row.add("");
            }
            row.add(p.getAdminWebsitName());
            row.add(p.getServiceName());
            row.add(DateUtil.formatDate(p.getCreateTime()));
            row.add(p.getConfirmName());
            row.add(DateUtil.formatDate(p.getConfirmTime()));
//            row.add(ExamineStatusEnum.valueOf(p.getExamineStatus()).getMsg());
            row.add(EnumUtil.getNameFieldMap(ExamineStatusEnum.class, "value").get(p.getExamineStatus()));
            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setRows(rows);
        excelData.setTitles(Arrays.asList(titles));
        return excelData;
    }

    public void review(String id) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        boolean updRes = productUpdPriceBillService.update(new LambdaUpdateWrapper<ProductUpdPriceBill>()
                .eq(ProductUpdPriceBill::getUpdPriceBillId, id)
                .in(ProductUpdPriceBill::getAdminCompanyId, adminUser.getAdminCompanyId())
                .eq(ProductUpdPriceBill::getExamineStatus, ExamineStatusEnum.OK.toString())
                .set(ProductUpdPriceBill::getExamineStatus, ExamineStatusEnum.SAVE.toString())
                .set(ProductUpdPriceBill::getSubmitBy, "")
                .set(ProductUpdPriceBill::getSubmitName, "")
                .set(ProductUpdPriceBill::getSubmitTime, null));
        if (!updRes) {
            throw new RemoteServiceException("单据状态已发生改变，撤消失败");
        }
        productPriceService.lambdaUpdate().eq(ProductPrice::getRefId,id).remove();
    }
}
