package net.chenlin.dp.modules.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.chenlin.dp.common.openapi4j.exception.OpenAPIException;
import net.chenlin.dp.common.support.orm.db.DynamicDataSource;
import net.chenlin.dp.common.utils.SpringContextUtils;
import net.chenlin.dp.modules.api.dao.CustomerMapper;
import net.chenlin.dp.modules.api.service.CustomerService;
import net.chenlin.dp.modules.api.vo.ICustomer;
import net.chenlin.dp.modules.api.vo.ICustomerEdit;
import net.chenlin.dp.modules.api.vo.YonyouSaleOrder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomerController {
    private CustomerService customerService=new CustomerService();
    private CustomerMapper customerMapper= SpringContextUtils.getBean("customerMapper",CustomerMapper.class);

    /**
     * u8客户档案新增接口
     * @throws OpenAPIException
     */
    public void addCustomer() throws OpenAPIException {
        customerService.getToaccount();
        try {
            List<Map<String,Object>> iCustomers = customerService.CrmCustomer();
            if (iCustomers!=null &&iCustomers.size()>0){
                for (Map<String,Object> i:iCustomers){
                    String s = customerService.addCustomer(i);
                    Object customer = i.get("customer");
                    ObjectMapper objectMapper = new ObjectMapper();
                    ICustomer zfsp = objectMapper.convertValue(customer, ICustomer.class);
                    if (s.equals("0")){
                        DynamicDataSource.setDataSource("last-data-source");
                        customerMapper.UpdateCustomer(zfsp.getCode(),"ok","1");
                        DynamicDataSource.setDataSource("");
                    }else {
                        DynamicDataSource.setDataSource("last-data-source");
                        customerMapper.UpdateCustomer(zfsp.getCode(),s,"1");
                        DynamicDataSource.setDataSource("");
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 客户档案修改接口
     * @throws OpenAPIException
     */
    public void ICustomerEdit() throws OpenAPIException {
        customerService.getToaccount();
        DynamicDataSource.setDataSource("last-data-source");
        List<ICustomerEdit> iCustomerEdits = customerMapper.ICustomerEditList();
        DynamicDataSource.setDataSource("");
        if (iCustomerEdits!=null&&iCustomerEdits.size()>0){
            for (ICustomerEdit i:iCustomerEdits){
                HashMap customer = customerService.getCustomer(i.getCode());
                if (customer!=null&&customer.size()>0) {
                    String s = customerService.editCustomer(i);
                    if (s.equals("0")) {
                        DynamicDataSource.setDataSource("last-data-source");
                       // customerMapper.UpdateCustomer(i.getCode());
                        DynamicDataSource.setDataSource("");
                    }
                }
            }
        }
    }
}