package com.hzya.frame.u9c.category.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.u9c.category.entity.VU9cCategoryEntity;
import com.hzya.frame.u9c.category.dao.IVU9cCategoryDao;
import com.hzya.frame.u9c.category.service.IVU9cCategoryService;
import com.hzya.frame.u9c.util.CrmUtil;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import com.hzya.frame.basedao.service.impl.BaseService;

import java.util.List;

/**
 * (VU9cCategory)表服务实现类
 *
 * @author makejava
 * @since 2023-10-26 09:22:30
 */
@Service("vU9cCategoryService")
public class VU9cCategoryServiceImpl extends BaseService<VU9cCategoryEntity, String> implements IVU9cCategoryService {
     
    private IVU9cCategoryDao vU9cCategoryDao;
    
    @Autowired
	public void setVU9cCategoryDao(IVU9cCategoryDao dao) {
		this.vU9cCategoryDao = dao;
		this.dao = dao;
	}
    //上一次同步时间
    private String LAST_SYNCHRONISED_TIME = "";
    @Override
    public String sendU9cCategoryToCRM() {
        JSONObject resultObj = new JSONObject();
        //获取待推送的U9c
        VU9cCategoryEntity category = new VU9cCategoryEntity();
        if (StrUtil.isBlank(LAST_SYNCHRONISED_TIME)) {
            LAST_SYNCHRONISED_TIME = DateUtil.now();
        }
        //暂存时间
        String temp_time = LAST_SYNCHRONISED_TIME;
        category.setUpdateTime(temp_time);
        List<VU9cCategoryEntity> VU9cCategoryEntityList = vU9cCategoryDao.getu9cCategory(category);
        if(VU9cCategoryEntityList.size()>0){
            LAST_SYNCHRONISED_TIME = DateUtil.now();
            for(VU9cCategoryEntity itemmaster : VU9cCategoryEntityList){
                try {
                    String permanentCode = "FSUID_DB2D1775B48BCDF893538F8CAB2A0EC7";
                    JSONObject tokenObj = CrmUtil.getToken(permanentCode);
                    //拼接传递参数
                    JSONObject main = new JSONObject();
                    main.put("corpAccessToken",tokenObj.getString("corpAccessToken"));//token
                    main.put("corpId",tokenObj.getString("corpId"));//公司
                    main.put("currentOpenUserId",permanentCode);//操作员主键
                    main.put("duplicateCheck",true);
                    JSONObject data = new JSONObject();
                    //根据CRM主键判断是新增还是更新
                    String crmId = itemmaster.getCrmId();
                    if(StrUtil.isNotEmpty(crmId)){
                        data.put("editFlag","2");//状态 2是更新
                        data.put("id",crmId);//状态 2是更新-
                    }else{
                        data.put("editFlag","1");//状态1 是新增
                    }

                    String crmUpId = itemmaster.getCrmUpId();
                    if(StrUtil.isNotEmpty(crmUpId)){
                        data.put("pid",itemmaster.getCrmUpId());//上级主键
                    }
                    data.put("name",itemmaster.getName());//分类名称
                    data.put("category_code",itemmaster.getCode());//分类编码
                    data.put("orderField","1");//排序号 暂时默认1
                    main.put("data",data);
                    String apiCode = "8000090002";
                    resultObj = CrmUtil.sendU9cTOCrmEsb(main.toString(),apiCode);

                    String errorDescription = resultObj.getString("errorDescription");
                    if("success".equals(errorDescription)){
                        if(StrUtil.isEmpty(crmId)){
                            String dataId = resultObj.getString("dataId");
                            if(StrUtil.isNotEmpty(dataId)){
                                itemmaster.setDataId(dataId);
                                int state = vU9cCategoryDao.updateCategory(itemmaster);
                            }
                        }
                    }
                } catch (Exception e) {
                    itemmaster.setDataId("接口错误，请联系管理员");
                    vU9cCategoryDao.updateCategory(itemmaster);
                    e.printStackTrace();
                }

            }
        }
        return resultObj.toString();
    }
}
