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


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.k3.RequestApiBean;
import com.zfire.mall.manager.bean.supplier.KingDeeSupplierVO;
import com.zfire.mall.manager.commonmapper.CommonCountMapper;
import com.zfire.mall.manager.constant.KingDeeCons;
import com.zfire.mall.manager.plus.entity.KingDeeSupplier;
import com.zfire.mall.manager.plus.service.KingDeeSupplierService;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class K3SupplierLogic {

    @Autowired
    KingDeeSupplierService kingDeeSupplierService;

    @Autowired
    K3Logic k3Logic;

    @Autowired
    CommonCountMapper commonCountMapper;

    public IPage<KingDeeSupplier> list(String number, String name, String useOrgNumber, String useOrgName, String shortName, String groupName, String auditName, Integer pageNum, Integer pageSize) {
       return kingDeeSupplierService.lambdaQuery()
                .eq(StringUtils.isNotBlank(number),KingDeeSupplier::getNumber,number)
                .eq(StringUtils.isNotBlank(name),KingDeeSupplier::getName,name)
                .eq(StringUtils.isNotBlank(useOrgNumber),KingDeeSupplier::getUseOrgNumber,useOrgNumber)
                .eq(StringUtils.isNotBlank(useOrgName),KingDeeSupplier::getUseOrgName,useOrgName)
                .eq(StringUtils.isNotBlank(shortName),KingDeeSupplier::getShortName,shortName)
                .eq(StringUtils.isNotBlank(groupName),KingDeeSupplier::getGroupName,groupName)
                .eq(StringUtils.isNotBlank(auditName),KingDeeSupplier::getAuditName,auditName)
                .page(new Page<>(pageNum,pageSize));
    }


//    public void allSync(String orgId, String orgNumber, String startTime, String endTime) throws Exception {
//        // 调用供应商接口
//        RequestApiBean request = k3Logic.getInstance(orgId, orgNumber, KingDeeCons.SUPPLIER_INFO, startTime, endTime);
//        String filterStr = "FDocumentStatus = 'C' AND FUseOrgId.FNUMBER = %s "+ k3Logic.supplySql("FAuditDate", request.getStartTime(), request.getEndTime());
//        request.setFieldKeys("FSupplierId,FNumber,FName,FUseOrgId,FUseOrgId.FNumber,FUseOrgId.FName,FShortName,FGroup.FNumber,FGroup.FName,FAuditorId.FName,FCreateOrgId.FName,FRegNumber,FCountry,FProvincial," +
//                "FAddress,FZip,FLanguage,FWebSite,FTrade,FFoundDate,FLegalPerson,FRegisterFund,FRegisterCode,FTendPermit,FSOCIALCRECODE,FRegisterAddress,FDeptId.FName,FStaffId.FName,"+
//                "FSupplierClassify,FSupplyClassify,FSupplierGrade,FCompanyClassify,FCompanyNature,FCompanyScale,FCorrespondOrgId.FName");
//        request.setFilterString(String.format(filterStr, orgNumber));
//        request.setOrderString("FSupplierId ASC");
//
//        String[] fields = new String[]{"id", "number", "name", "useOrgId", "useOrgNumber", "useOrgName", "shortName", "groupNumber", "groupName", "auditName", "createOrgName", "regNumber", "country", "provincial",
//               "address", "zip", "language", "webSite", "trade", "foundDate", "legalPerson", "registerFund", "registerCode", "tendPermit","socialCreCode", "registerAddress","deptName", "staffName",
//               "supplierClassify","supplyClassify", "supplierGrade", "companyClassify", "companyNature", "companyScale", "correspondOrgName"};
//        List<KingDeeSupplier> objects = k3Logic.allSync(request, fields, KingDeeSupplier.class);
//        if (CollectionUtils.isNotEmpty(objects)) {
//            kingDeeSupplierService.saveOrUpdateBatch(objects);
//        }
//    }

    public KingDeeSupplier detail(String id) {
        return kingDeeSupplierService.getById(id);
    }

    public IPage<KingDeeSupplierVO> listV2(ZfireParamBean zfireParamBean) {
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        return commonCountMapper.listV2(new Page(zfireParamBean.getPageNum(),zfireParamBean.getPageSize()),zfireParamBean);
    }
}
