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

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.invoice.common.InvoiceType;
import cn.superdesk.uniorder.dao.entity.marketing.department.UnifiedAreaCityVirt;
import cn.superdesk.uniorder.dao.entity.organisation.SysEnterprise;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedCustomer;
import cn.superdesk.uniorder.dao.entity.invoice.common.InvoiceTaxRate;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedCustomerAdd;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedCustomerDetail;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedCustomerVo;
import cn.superdesk.uniorder.dubbo.facade.unified.DubboUnifiedCustomerService;
import cn.superdesk.uniorder.service.order.base.UnifiedOrderService;
import cn.superdesk.uniorder.service.invoice.common.InvoiceTaxRateService;
import cn.superdesk.uniorder.service.marketing.department.UnifiedAreaCityService;
import cn.superdesk.uniorder.service.sys.SysUserService;
import cn.superdesk.uniorder.service.unified.UnifiedCustomerContactsService;
import cn.superdesk.uniorder.service.unified.UnifiedCustomerService;
import cn.superdesk.uniorder.service.invoice.common.InvoiceTypeService;
import cn.superdesk.uniorder.service.unified.UnifiedProjectService;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboUnifiedCustomerServiceImpl implements DubboUnifiedCustomerService {

	@Autowired
	private UnifiedCustomerService         unifiedCustomerService;
	@Autowired
	private UnifiedProjectService          unifiedProjectService;
	@Autowired
	private SysUserService                 sysUserService;
	@Autowired
	private UnifiedAreaCityService         unifiedAreaCityService;
	@Autowired
	private UnifiedOrderService            unifiedOrderService;
	@Autowired
	private UnifiedCustomerContactsService unifiedCustomerContactsService;
	@Autowired
	private InvoiceTaxRateService unifiedTaxRateService;
	@Autowired
	private InvoiceTypeService    unifiedInvTypeService;

	@Override
	public UnifiedCustomer addObjTran(SysEnterprise virt, UnifiedCustomerAdd model, LoginSysUser loginSysUser) {

		UnifiedCustomer customer = new UnifiedCustomer();

		customer.setCreateTime(DateUtil.date());
		customer.setCreateUserId(loginSysUser.getSysUserId());

		if (StringUtils.isNotEmpty(model.getCredit())) {
			customer.setCredit(new BigDecimal(model.getCredit()));
		}
		if (StringUtils.isNotEmpty(model.getCycleDays())) {
			customer.setCycleDays(Integer.parseInt(model.getCycleDays()));
		}

		customer.setProjectId(virt.getOrganizationId());

		customer.setUpdateTime(DateUtil.date());
		customer.setUpdateUserId(loginSysUser.getSysUserId());

		customer.setVirtId(virt.getEnterpriseId());
		customer.setVirtName(virt.getEnterpriseName());

		UnifiedAreaCityVirt av = new UnifiedAreaCityVirt();
		av.setVirtId(model.getVirtId());
		av.setAreaCityId(model.getAreaId());
		//change 20191017
//		String[] users = model.getUserIds().split(",");
//		List<UnifiedCustomerContactsKey> contactskeys = new ArrayList<>();
//		if(users.length>0) {
//			for (int i = 0; i < users.length; i++) {
//				UnifiedCustomerContactsKey item = new UnifiedCustomerContactsKey();
//				item.setSysUserId(Long.parseLong(users[i]));
//				item.setVirtId(virt.getEnterpriseId());
//				contactskeys.add(item);
//			}
//		}

		//return customerService.addObjTran(customer,contactskeys);
		return unifiedCustomerService.addObjTranNew(customer, av);
	}

	@Override
	public UnifiedCustomer updateObjTran(UnifiedCustomerAdd model, LoginSysUser loginSysUser) {


		UnifiedCustomer customer = new UnifiedCustomer();

		if (StringUtils.isNotEmpty(model.getCredit())) {
			customer.setCredit(new BigDecimal(model.getCredit()));
		}
		if (StringUtils.isNotEmpty(model.getCycleDays())) {
			customer.setCycleDays(Integer.parseInt(model.getCycleDays()));
		}

		customer.setUpdateTime(DateUtil.date());
		customer.setUpdateUserId(loginSysUser.getSysUserId());
		customer.setVirtId(model.getVirtId());

		//change 20191017
		UnifiedAreaCityVirt av = new UnifiedAreaCityVirt();
		av.setVirtId(model.getVirtId());
		av.setAreaCityId(model.getAreaId());
//		List<UnifiedCustomerContactsKey> contactskeys = new ArrayList<>();
//		if(StringUtils.isNotEmpty(model.getUserIds())) {
//			String[] users = model.getUserIds().split(",");
//			if(users.length>0) {
//				for (int i = 0; i < users.length; i++) {
//					UnifiedCustomerContactsKey item = new UnifiedCustomerContactsKey();
//					item.setSysUserId(Long.parseLong(users[i]));
//					item.setVirtId(virt.getEnterpriseId());
//					contactskeys.add(item);
//				}
//			}
//		}


		//return customerService.updateObjTran(customer,contactskeys);
		return unifiedCustomerService.updateObjTranNew(customer, av);
	}

	@Override
	public UnifiedCustomerDetail getDetail(Long virtId) {

//		UnifiedCustomerDetail detail = new UnifiedCustomerDetail();
//		UnifiedCustomer customer = customerService.findById(virtId);
//		UnifiedProject project = projectService.findById(customer.getProjectId());
//		if (StringUtils.isNotEmpty(project.getCityName())) {
//			Map<String, Object> params = new HashMap<>();
//			params.put("cityName", project.getCityName());
//			List<UnifiedAreaCityDetail> citys = areaCityService.findDetail(params);
//			if (citys != null && citys.size() > 0) {
//				detail.setAreaName(citys.get(0).getAreaName());
//				detail.setCityName(citys.get(0).getCityName());
//			}
//		}
//
//
//		detail.setCreateTime(customer.getCreateTime());
//		detail.setCreateUserId(customer.getCreateUserId());
//		detail.setCredit(customer.getCredit());
//		detail.setCycleDays(customer.getCycleDays());
//		detail.setBackAmount(new BigDecimal(0));//待完成
//		detail.setInvAmount(new BigDecimal(0));//待完成
//		Map<String, Object> amountQuery = new HashMap<>();
//		amountQuery.put("virtId", customer.getVirtId());
//		BigDecimal orderAmount = orderService.getAmount(amountQuery);
//		detail.setOrderAmount(orderAmount);
//		detail.setProjectId(customer.getProjectId());
//		detail.setProjectName(project.getProjectName());
//		if (project.getCustomerUserId() != null) {
//			SysUser user = userService.findById(project.getCustomerUserId());
//			detail.setCustomerUserName(user.getFullname());
//		}
//		if (project.getPurchaseUserId() != null) {
//			SysUser user = userService.findById(project.getPurchaseUserId());
//			detail.setPurchaseUserName(user.getFullname());
//		}
//
//		detail.setUpdateTime(customer.getUpdateTime());
//		detail.setUpdateUserId(customer.getUpdateUserId());
//		detail.setVirtId(customer.getVirtId());
//		detail.setVirtName(customer.getVirtName());
//		List<UnifiedCustomerContactsDetail> contacts = customerContactsService.getContacts(customer.getVirtId());
//		detail.setContacts(contacts);

		return null;
	}

	@Override
	public R<List<UnifiedCustomerVo>> getDataByPage(Map<String, Object> params, int pageNum,
	                                                int pageSize) {

		return unifiedCustomerService.getDataByPage(params, pageNum, pageSize);
	}

	@Override
	public List<InvoiceTaxRate> getInvoiceTaxDataList(Map<String, Object> params) {

		return unifiedTaxRateService.findAll(params);
	}

	@Override
	public List<InvoiceType> getInvoiceTypeList(Map<String, Object> params) {

		return unifiedInvTypeService.findAll(params);
	}

	@Override
	public UnifiedCustomer findById(Long virtId) {

		return unifiedCustomerService.findById(virtId);
	}
}
