package cn.superdesk.uniorder.dubbo.provider.organisation;

import cn.superdesk.app.supercloud.dto.receive.organisation.CompanyReceiveParam;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.dao.entity.unified.KfkCompany;
import cn.superdesk.uniorder.dubbo.facade.organisation.DubboKfkCompanyService;
import cn.superdesk.uniorder.service.organisation.KfkCompanyService;
import cn.superdesk.uniorder.service.organisation.SysSupplierService;
import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

//import cn.superdesk.uniorder.dto.vo.kafka.CompanyEntity;

@Slf4j
@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboKfkCompanyServiceImpl implements DubboKfkCompanyService {


	@Autowired
	private KfkCompanyService  kfkCompanyService;
	@Autowired
	private SysSupplierService unifiedSupplierService;

	@Override
	public KfkCompany findById(Long id) {

		return kfkCompanyService.findById(id);
	}

	@Override
	public void updateObj(KfkCompany c) {

		kfkCompanyService.updateByPrimaryKeySelective(c);
	}

	@Override
	public void addObj(KfkCompany c) {

		kfkCompanyService.insertSelective(c);
	}

	@Override
	public KfkCompany findByCompanyId(Long id) {

		return kfkCompanyService.findByCompanyId(id);
	}

	@Override
	public void delObj(Long id) {

		kfkCompanyService.deleteObj(id);
	}

	/**
	 * 新系统 ---> 各系统
	 *
	 * @param message
	 */
	@Override
	public void mergeReceiveCompany(String message) {

		try {

			CompanyReceiveParam companyReceiveParam = AnJsonUtils.toObject(message, CompanyReceiveParam.class);

			log.info(AnJsonUtils.toJson(companyReceiveParam));

			KfkCompany companyEntity = kfkCompanyService.findByCompanyId(Long.valueOf(companyReceiveParam.getId()));

			if (null == companyEntity) {

				companyEntity = new KfkCompany();

				// src target 不能懒呀
				AnBeanUtils.copy(companyReceiveParam, companyEntity);
				// 这样行不行呀?
				companyEntity.setId(null);
				companyEntity.setCompanyId(Long.valueOf(companyReceiveParam.getId()));

//				convertReceiveParam2Entity(companyReceiveParam, companyEntity);

				kfkCompanyService.insertSelective(companyEntity);

			} else {

				Long id = companyEntity.getId();

				// src target
				AnBeanUtils.copy(companyReceiveParam, companyEntity);
				// 这样行不行呀?
				companyEntity.setId(id);
				companyEntity.setCompanyId(Long.valueOf(companyReceiveParam.getId()));


				kfkCompanyService.updateByPrimaryKeySelective(companyEntity);
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.error("新系统 同步 旧系统-Company 失败:{}", e.getMessage());
		}
	}

	/**
	 * 新系统 ---> 各系统
	 *
	 * @param message
	 */
	@Override
	public void deleteReceiveCompany(String message) {

		CompanyReceiveParam companyReceiveParam = AnJsonUtils.toObject(message, CompanyReceiveParam.class);

		KfkCompany companyEntity = kfkCompanyService.findByCompanyId(Long.valueOf(companyReceiveParam.getId()));

		// TODO 有没有别的关系要级联维护的?
		kfkCompanyService.deleteObj(Long.valueOf(companyEntity.getId()));
	}


//	private void convertReceiveParam2Entity(CompanyReceiveParam companyReceiveParam, KfkCompany companyEntity) {
//
//		companyEntity.setCompanyId();
//		companyEntity.setCode();
//		companyEntity.setName();
//		companyEntity.setShortName();
//		companyEntity.setLogo();
//		companyEntity.setLeaderName();
//		companyEntity.setLeaderPhone();
//		companyEntity.setType();
//		companyEntity.setTelephone();
//		companyEntity.setBelongGroup();
//		companyEntity.setAddress();
//		companyEntity.setRemark();
//		companyEntity.setCreatedBy();
//		companyEntity.setCreatedAt();
//		companyEntity.setUpdatedBy();
//		companyEntity.setUpdatedAt();
//		companyEntity.setEnabled();
//	}
}
