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

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

import com.benyun.common.utils.DateUtils;
import com.google.gson.Gson;
import com.kingdee.bos.webapi.entity.QueryParam;
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.kingdeeDict.mapper.KingdeeDictMapper;
import com.benyun.project.third.kingdeeDict.domain.KingdeeDict;
import com.benyun.project.third.kingdeeDict.service.IKingdeeDictService;
import com.benyun.common.utils.text.Convert;
import org.thymeleaf.util.ListUtils;

/**
 * 金蝶单据类型字典Service业务层处理
 *
 * @author 冯旺
 * @date 2024-01-04
 */
@Service
@Slf4j
public class KingdeeDictServiceImpl implements IKingdeeDictService
{

    private final String SYS = "KingdeeDictServiceImpl.";

    @Autowired
    private KingdeeDictMapper kingdeeDictMapper;

    /**
     * 查询金蝶单据类型字典
     *
     * @param id 金蝶单据类型字典主键
     * @return 金蝶单据类型字典
     */
    @Override
    public KingdeeDict selectKingdeeDictById(Long id)
    {
        return kingdeeDictMapper.selectKingdeeDictById(id);
    }

    /**
     * 查询金蝶单据类型字典列表
     *
     * @param kingdeeDict 金蝶单据类型字典
     * @return 金蝶单据类型字典
     */
    @Override
    public List<KingdeeDict> selectKingdeeDictList(KingdeeDict kingdeeDict)
    {
        return kingdeeDictMapper.selectKingdeeDictList(kingdeeDict);
    }

    /**
     * 新增金蝶单据类型字典
     *
     * @param kingdeeDict 金蝶单据类型字典
     * @return 结果
     */
    @Override
    public int insertKingdeeDict(KingdeeDict kingdeeDict)
    {
        kingdeeDict.setCreateTime(DateUtils.getNowDate());
        return kingdeeDictMapper.insertKingdeeDict(kingdeeDict);
    }

    /**
     * 修改金蝶单据类型字典
     *
     * @param kingdeeDict 金蝶单据类型字典
     * @return 结果
     */
    @Override
    public int updateKingdeeDict(KingdeeDict kingdeeDict)
    {
        return kingdeeDictMapper.updateKingdeeDict(kingdeeDict);
    }

    /**
     * 批量删除金蝶单据类型字典
     *
     * @param ids 需要删除的金蝶单据类型字典主键
     * @return 结果
     */
    @Override
    public int deleteKingdeeDictByIds(String ids)
    {
        return kingdeeDictMapper.deleteKingdeeDictByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除金蝶单据类型字典信息
     *
     * @param id 金蝶单据类型字典主键
     * @return 结果
     */
    @Override
    public int deleteKingdeeDictById(Long id)
    {
        return kingdeeDictMapper.deleteKingdeeDictById(id);
    }

    @Override
    public int syncKingdeeDict() {
        try {
            K3CloudApi api = new K3CloudApi();
            Gson gson = new Gson();
            QueryParam<List<Map<String, Object>>> para = new QueryParam<>();
            para.setFormId("BOS_BillType");
            //                单据名称， 单据编码， 单据类型ID  单据所属表单ID   禁用状态
            para.setFieldKeys("FName,FNumber,FBILLTYPEID,FBillFormID");
            List<List<Object>> lists = api.executeBillQuery(para.toJson());
            List<KingdeeDict> dicList = new ArrayList<>();
            for (List<Object> objects : lists) {
                List<KingdeeDict> res = kingdeeDictMapper.selectKingdeeDictByNumber((String) objects.get(1));
                if(!ListUtils.isEmpty(res)){
                    continue;
                }
                KingdeeDict kingdeeDict = new KingdeeDict();
                kingdeeDict.setName((String) objects.get(0));
                kingdeeDict.setNumber((String)objects.get(1));
                kingdeeDict.setBillTypeId((String)objects.get(2));
                kingdeeDict.setBillFormId((String)objects.get(3));
                kingdeeDict.setCreateTime(new Date());
                dicList.add(kingdeeDict);
            }
            log.info(SYS+"syncKingdeeDict.查询所有单据类型: " + gson.toJson(dicList));
            return  kingdeeDictMapper.insertBatchKingdeeDict(dicList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
