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

import com.aliyuncs.utils.StringUtils;
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.ProductPriceBean;
import com.zfire.mall.manager.bean.manage.product.price.ProductWalletBean;
import com.zfire.mall.manager.commonmapper.manage.product.ProductPriceReserveMapper;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.*;
import com.zfire.mall.manager.plus.service.*;
import lombok.extern.slf4j.Slf4j;
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.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ProductPriceLogic {

    @Resource
    CommonLogic commonLogic;
    @Resource
    ProductPriceReserveMapper productPriceReserveMapper;
    @Resource
    ProductPriceService productPriceService;
    @Autowired
    ProductPriceWalletService productPriceWalletService;

    @Resource
    ProductUpdPriceBillService productUpdPriceBillService;
    @Resource
    ProductUpdPriceBillItemService productUpdPriceBillItemService;
    @Resource
    ProductUpdPriceBillItemWalletService productUpdPriceBillItemWalletService;


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

        IPage page = productPriceService.lambdaQuery()
                .like(StringUtils.isNotEmpty(materialOldNumber),ProductPrice::getMaterialOldNumber,materialOldNumber)
                .like(StringUtils.isNotEmpty(materialNumber), ProductPrice::getMaterialNumber, materialNumber)
                .like(StringUtils.isNotEmpty(materialName), ProductPrice::getMaterialName, materialName)
                .le(StringUtils.isNotEmpty(startDate), ProductPrice::getStartDate, startDate)
                .eq(StringUtils.isNotEmpty(k3CategoryNumber),ProductPrice::getK3CategoryNumber,k3CategoryNumber)
                .ge(StringUtils.isNotEmpty(startDate), ProductPrice::getEndDate, endDate)
                .eq(StringUtils.isNotEmpty(saleTypeId), ProductPrice::getSaleTypeId, saleTypeId)
                .eq(StringUtils.isNotEmpty(mainId),ProductPrice::getMainId,mainId)
                .like(StringUtils.isNotEmpty(specification), ProductPrice::getSpecification, specification)
                .like(StringUtils.isNotEmpty(remark),ProductPrice::getRemark,remark)
                .eq(ProductPrice::getBillType, billType)
                .in(CollectionUtils.isNotEmpty(adminUser.getK3CategoryIds()),ProductPrice::getK3CategoryId,adminUser.getK3CategoryIds())
                .orderByDesc(ProductPrice::getCreateTime, ProductPrice::getId)
                .page(new Page<>(pageNum, pageSize));

        List<ProductPriceBean> list = new ArrayList<>();
        for(Object o : page.getRecords()){
            ProductPrice productPrice = (ProductPrice)o;

            ProductPriceBean bean = new ProductPriceBean();
            BeanUtils.copyProperties(productPrice,bean);

            List<ProductWalletBean> wallets = productPriceReserveMapper.queryWallet(productPrice.getId());
            bean.setWallets(wallets);
            list.add(bean);
        }
        page.setRecords(list);
        return page;
    }

    public void revoke(List<String> ids) {
        productPriceService.lambdaUpdate()
                .set(ProductPrice::getIsRevoke, true)
                .in(ProductPrice::getId, ids)
                .update();
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(List<String> ids) {
        List<ProductPrice> list = productPriceService.lambdaQuery().in(ProductPrice::getId, ids).list();
        for (ProductPrice productPrice : list) {
            productUpdPriceBillItemService.lambdaUpdate().eq(ProductUpdPriceBillItem::getUpdPriceBillId,productPrice.getRefId())
                    .eq(ProductUpdPriceBillItem::getMaterialNumber,productPrice.getMaterialNumber())
                    .remove();

            if (productUpdPriceBillItemService.lambdaQuery().eq(ProductUpdPriceBillItem::getUpdPriceBillId,productPrice.getRefId()).count() < 1) {
                boolean b = productUpdPriceBillService.removeById(productPrice.getRefId());

                productUpdPriceBillItemWalletService.lambdaUpdate().in(ProductUpdPriceBillItemWallet::getUpdPriceBillId , productPrice.getRefId()).remove();
            }
        }

        productPriceService.lambdaUpdate().in(ProductPrice::getId, ids).remove();
        productPriceWalletService.lambdaUpdate().in(ProductPriceWallet::getProductPriceId, ids).remove();

    }

}
