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

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.k3.RequestApiBean;
import com.zfire.mall.manager.constant.KingDeeCons;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.KingDeeCategory;
import com.zfire.mall.manager.plus.service.KingDeeCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class K3CategoryLogic {

    @Resource
    KingDeeCategoryService kingDeeCategoryService;
    @Resource
    K3Logic k3Logic;
    @Resource
    CommonLogic commonLogic;

//    public void allSync(String orgId, String orgNumber, String startTime, String endTime) throws Exception {
//        // 调用存货类别接口
//        RequestApiBean request = k3Logic.getInstance(orgId, orgNumber, KingDeeCons.CATEGORY_INFO, startTime, endTime);
//        String filterStr = "FDocumentStatus = 'C' AND FUseOrgId.FNUMBER = %s "+ k3Logic.supplySql(request.getStartTime(), request.getEndTime());
//        request.setFieldKeys("FCATEGORYID,FNUMBER,FNAME,FUseOrgId,FUseOrgId.FNUMBER,FUseOrgId.FName");
//        request.setFilterString(String.format(filterStr, orgNumber));
//        request.setOrderString("FStockId ASC");
//
//        String[] fields = new String[]{"id","number","name","useOrgId","useOrgNumber","useOrgName"};
//        List<KingDeeCategory> objects = k3Logic.allSync(request, fields, KingDeeCategory.class);
//        if (!org.springframework.util.CollectionUtils.isEmpty(objects)) {
//            kingDeeCategoryService.saveOrUpdateBatch(objects);
//        }
//    }

    public IPage<KingDeeCategory> list(String keyword, Integer pageNum, Integer pageSize) {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        IPage<KingDeeCategory> page = kingDeeCategoryService.lambdaQuery()
                .like(StringUtils.isNotEmpty(keyword), KingDeeCategory::getName, keyword)
//                .in(CollectionUtils.isNotEmpty(adminUser.getUseOrgNumbers()), KingDeeCategory::getUseOrgNumber, adminUser.getUseOrgNumbers())
                .orderByDesc(KingDeeCategory::getCreateTime).page(new Page<>(pageNum, pageSize));
        return page;
    }
}
