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

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.k3.KingDeeMaterialBean;
import com.zfire.mall.manager.bean.k3.RequestApiBean;
import com.zfire.mall.manager.bean.listV2.K3MaterialVO;
import com.zfire.mall.manager.commonmapper.K3Mapper;
import com.zfire.mall.manager.commonmapper.listV2.CommonCMapper;
import com.zfire.mall.manager.commonmapper.manage.material.ProductComposeReserveMapper;
import com.zfire.mall.manager.constant.KingDeeCons;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.logic.manage.product.category.ProductCategoryLogic;
import com.zfire.mall.manager.plus.entity.KingDeeMaterial;
import com.zfire.mall.manager.plus.entity.KingDeeWallet;
import com.zfire.mall.manager.plus.entity.ProductCategory;
import com.zfire.mall.manager.plus.entity.SaleType;
import com.zfire.mall.manager.plus.service.KingDeeMaterialService;
import com.zfire.mall.manager.plus.service.KingDeeWalletService;
import com.zfire.mall.manager.plus.service.SaleTypeService;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class K3MaterialLogic {


    @Autowired
    K3Logic k3Logic;
    @Autowired
    CommonLogic commonLogic;
    @Autowired
    ProductCategoryLogic productCategoryLogic;
    @Resource
    ProductComposeReserveMapper productComposeReserveMapper;
    @Resource
    KingDeeMaterialService kingDeeMaterialService;
    @Autowired
    KingDeeWalletService kingDeeWalletService;
    @Autowired
    SaleTypeService saleTypeService;
    @Autowired
    K3Mapper k3Mapper;
    @Autowired
    CommonCMapper commonCMapper;

    /**
     * 物料列表
     */
    public IPage<K3MaterialVO> listV2(ZfireParamBean zfireParamBean){
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        IPage<K3MaterialVO> k3MaterialVOIPage = commonCMapper.queryK3Material(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
        return k3MaterialVOIPage;
    }

    /**
     * 查询物料所有单位
     * @return
     */
    public List<KingDeeMaterial> allUnit(){
        List<KingDeeMaterial> kingDeeMaterials = k3Mapper.queryMaterialUnit();
        return kingDeeMaterials;
    }

    /**
     * 查询单位编号
     * @return
     */
    public String queryUnitCode(List<KingDeeMaterial> units,String unit){
//        Map<String, KingDeeMaterial> map = units.stream()
//                .collect(Collectors.toMap(KingDeeMaterial::getBaseUnitCode, v -> v));
        for(KingDeeMaterial material : units){
            if(material.getBaseUnitId().equals(unit)){
                return material.getBaseUnitCode();
            }else if(material.getBaseUnitCode().equals(unit)){
                //兼容旧数据
                return material.getBaseUnitCode();
            }
        }
        return "";
    }


    public List<KingDeeMaterial> allList(){
        return kingDeeMaterialService.list();
    }


    public IPage<KingDeeMaterialBean> list(String number, String oldNumber, String keyword, String specification, Boolean bindMain, Integer pageNum, Integer pageSize){
        AdminUserCom adminUser = commonLogic.getAdminUser();
        //查询绑定了供应链品类的金蝶物料
        List<String> categoryIds = null;
        if(bindMain != null && bindMain){
            categoryIds = productCategoryLogic.queryCategoryIds();
            if(categoryIds == null){
                return new Page(1,10);
            }
        }
        IPage page = kingDeeMaterialService.lambdaQuery()
                .like(StringUtils.isNotEmpty(number), KingDeeMaterial::getNumber, number)
                .like(StringUtils.isNotEmpty(oldNumber), KingDeeMaterial::getOldNumber, oldNumber)
                .like(StringUtils.isNotEmpty(keyword), KingDeeMaterial::getName, keyword)
                .like(StringUtils.isNotEmpty(specification),KingDeeMaterial::getSpecification,specification)
//                .in(CollectionUtils.isNotEmpty(categoryIds),KingDeeMaterial::getCategoryId,categoryIds)
//                .in(CollectionUtils.isNotEmpty(adminUser.getUseOrgNumbers()), KingDeeMaterial::getUseOrgNumber, adminUser.getUseOrgNumbers())
                .orderByDesc(KingDeeMaterial::getCreateTime).page(new Page<>(pageNum, pageSize));

        List<KingDeeWallet> wallets = kingDeeWalletService.lambdaQuery().eq(KingDeeWallet::getForbidStatus, "A").list();
        List<SaleType> saleTypes = saleTypeService.lambdaQuery().eq(SaleType::getStatus, true).list();

        List<KingDeeMaterialBean> list = new ArrayList<>();
        for(Object o : page.getRecords()){
            KingDeeMaterial kingDeeMaterial = (KingDeeMaterial)o;
            KingDeeMaterialBean bean = new KingDeeMaterialBean();
            BeanUtils.copyProperties(kingDeeMaterial,bean);

            //this.supplyData(kingDeeMaterial.getCategoryId(),bean,wallets,saleTypes);
            bean.setWallets(wallets);
            //可选的销售类型
            bean.setSaleTypes(saleTypes);
            list.add(bean);
        }
        page.setRecords(list);
        return page;
    }


    public KingDeeMaterialBean detail(String id) {
        KingDeeMaterial material = kingDeeMaterialService.getById(id);
        KingDeeMaterialBean bean = new KingDeeMaterialBean();
        BeanUtils.copyProperties(material, bean);
//        bean.setItems(productComposeReserveMapper.getMaterialDetail(id));

        List<KingDeeWallet> wallets = kingDeeWalletService.lambdaQuery().eq(KingDeeWallet::getForbidStatus, "A").list();
        List<SaleType> saleTypes = saleTypeService.lambdaQuery().eq(SaleType::getStatus, true).list();

        this.supplyData(material.getCategoryId(),bean,wallets,saleTypes);
        return bean;
    }

    //补充可选的现金钱包，可选的销售类型
    private KingDeeMaterialBean supplyData(String kingDeeCategoryId,KingDeeMaterialBean bean,List<KingDeeWallet> wallets,List<SaleType> saleTypes){
        ProductCategory productCategory = productComposeReserveMapper.queryOne(kingDeeCategoryId);
        if(productCategory != null) {
            bean.setMainId(productCategory.getProductCategoryId());
            bean.setMainName(productCategory.getProductCategoryName());

            //可选的现金钱包
            //bean.setWallets(wallets.stream().filter(v -> v.getMainId().equals(bean.getMainId())).collect(Collectors.toList()));
            bean.setWallets(wallets);
            //可选的销售类型
            bean.setSaleTypes(saleTypes);
        }
        return bean;
    }

    public void edit(KingDeeMaterialBean kingDeeMaterialBean) {
        kingDeeMaterialService.lambdaUpdate()
                .set(KingDeeMaterial::getIsDisplace, kingDeeMaterialBean.getIsDisplace())
                .eq(KingDeeMaterial::getId, kingDeeMaterialBean.getId())
                .update();
    }
}
