package com.benyun.project.third.kingdeeStockconfig.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.benyun.project.third.kingdeeDict.domain.KingdeeDict;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.benyun.project.third.kingdeeStockconfig.mapper.KingdeeStockConfigMapper;
import com.benyun.project.third.kingdeeStockconfig.domain.KingdeeStockConfig;
import com.benyun.project.third.kingdeeStockconfig.service.IKingdeeStockConfigService;
import com.benyun.common.utils.text.Convert;
import org.thymeleaf.util.ListUtils;

/**
 * 金蝶仓库配置信息Service业务层处理
 *
 * @author benyun
 * @date 2024-01-04
 */
@Service
@Slf4j
public class KingdeeStockConfigServiceImpl implements IKingdeeStockConfigService {

    private final String SYS = "KingdeeStockConfigServiceImpl.";

    @Autowired
    private KingdeeStockConfigMapper kingdeeStockConfigMapper;

    /**
     * 查询金蝶仓库配置信息
     *
     * @param id 金蝶仓库配置信息主键
     * @return 金蝶仓库配置信息
     */
    @Override
    public KingdeeStockConfig selectKingdeeStockConfigById(Long id) {
        return kingdeeStockConfigMapper.selectKingdeeStockConfigById(id);
    }

    /**
     * 查询金蝶仓库配置信息列表
     *
     * @param kingdeeStockConfig 金蝶仓库配置信息
     * @return 金蝶仓库配置信息
     */
    @Override
    public List<KingdeeStockConfig> selectKingdeeStockConfigList(KingdeeStockConfig kingdeeStockConfig) {
        return kingdeeStockConfigMapper.selectKingdeeStockConfigList(kingdeeStockConfig);
    }

    /**
     * 新增金蝶仓库配置信息
     *
     * @param kingdeeStockConfig 金蝶仓库配置信息
     * @return 结果
     */
    @Override
    public int insertKingdeeStockConfig(KingdeeStockConfig kingdeeStockConfig) {
        return kingdeeStockConfigMapper.insertKingdeeStockConfig(kingdeeStockConfig);
    }

    /**
     * 修改金蝶仓库配置信息
     *
     * @param kingdeeStockConfig 金蝶仓库配置信息
     * @return 结果
     */
    @Override
    public int updateKingdeeStockConfig(KingdeeStockConfig kingdeeStockConfig) {
        return kingdeeStockConfigMapper.updateKingdeeStockConfig(kingdeeStockConfig);
    }

    /**
     * 批量删除金蝶仓库配置信息
     *
     * @param ids 需要删除的金蝶仓库配置信息主键
     * @return 结果
     */
    @Override
    public int deleteKingdeeStockConfigByIds(String ids) {
        return kingdeeStockConfigMapper.deleteKingdeeStockConfigByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除金蝶仓库配置信息信息
     *
     * @param id 金蝶仓库配置信息主键
     * @return 结果
     */
    @Override
    public int deleteKingdeeStockConfigById(Long id) {
        return kingdeeStockConfigMapper.deleteKingdeeStockConfigById(id);
    }

    @Override
    public int syncKingdeeStockConfig() {

        try {
            K3CloudApi api = new K3CloudApi();
            String json = "{\n" +
                    "    \"FormId\": \"BD_STOCK\",\n" +
                    //                  仓库地址  仓库编码   名称    组织ID      使用组织     库存单位Id
                    "    \"FieldKeys\": \"FAddress,FNumber,FName,FCreateOrgId,FStockID\",\n" +
                    "}";

            List<List<Object>> lists = api.executeBillQuery(json);
            List<KingdeeStockConfig> kingdeeStockConfigList = new ArrayList<>();
            for (List<Object> objects : lists) {
                List<KingdeeStockConfig> res = kingdeeStockConfigMapper.selectKingdeeStockConfigByNumber((String) objects.get(1));
                if(!ListUtils.isEmpty(res)){
                    continue;
                }
                KingdeeStockConfig kingdeeStockConfig = new KingdeeStockConfig();
                kingdeeStockConfig.setStockAddress((String)objects.get(0));
                kingdeeStockConfig.setStockNumber((String)objects.get(1));
                kingdeeStockConfig.setStockName((String)objects.get(2));
                kingdeeStockConfig.setStockOrgId(objects.get(3).toString());
                kingdeeStockConfig.setStockId(objects.get(4).toString());
                kingdeeStockConfigList.add(kingdeeStockConfig);

            }
            if(ListUtils.isEmpty(kingdeeStockConfigList)){
                return 1;
            }
            return kingdeeStockConfigMapper.insertBatchKingdeeStockConfig(kingdeeStockConfigList);


        } catch (Exception e) {
            log.error(SYS + "syncKingdeeStockConfig.error:" + e.getMessage());
        }

        return 0;
    }
}
