package cn.superdesk.uniorder.dubbo.consumer.order._leaveover;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dto.vo.inv.ViewBackAccountManage;
import cn.superdesk.uniorder.dto.vo.inv.ViewInvoiceDetailsHead;
import cn.superdesk.uniorder.dto.vo.inv.ViewInvoiceDetailsList;
import cn.superdesk.uniorder.dto.vo.inv.ViewInvoiceManage;
import cn.superdesk.uniorder.dubbo.facade.order._leaveover.DubboSysInvoiceService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

@Slf4j
@Component
public class SysInvoiceDubboConsumerService {


	@Reference(version = "1.0.0", timeout = 20000, retries = -1)
	private DubboSysInvoiceService dubboSysInvoiceService;


	public R<List<ViewInvoiceManage>> findInvoiceManageViewPage(String token, Map<String, Object> params, int pageNo, int pageSize) {
		return dubboSysInvoiceService.findInvoiceManageViewPage(params, pageNo, pageSize);
	}

	public R<List<ViewBackAccountManage>> getBackAccountManageData(String token, Map<String, Object> params, int pageNo, int pageSize) {

		try {

			R<List<ViewBackAccountManage>> ls = dubboSysInvoiceService.getBackAccountManageData(params, pageNo, pageSize);
			return ls;

		} catch (Exception e) {

		}
		return dubboSysInvoiceService.getBackAccountManageData(params, pageNo, pageSize);
	}


	public List<ViewBackAccountManage> getBackAccountManageList(String token, Map<String, Object> params) {
		return dubboSysInvoiceService.getBackAccountManageList(params);
	}

	public List<ViewBackAccountManage> getBackAccountDataRegisterData(String token, Map<String, Object> params) {
		return dubboSysInvoiceService.getBackAccountDataRegisterData(params);
	}


	public List<ViewInvoiceManage> findInvoiceManageList(String token, Map<String, Object> params) {
		return dubboSysInvoiceService.findInvoiceManageList(params);
	}

	public List<ViewInvoiceDetailsList> findInvoiceDetailsList(String token, Map<String, Object> params) {
		return dubboSysInvoiceService.findInvoiceDetailsList(params);
	}

	public List<ViewInvoiceDetailsHead> getInvoiceDetailByInvoiceId(String token, Map<String, Object> params) {
		return dubboSysInvoiceService.getInvoiceDetailByInvoiceId(params);
	}


	/**
	 * @param 重新开票
	 * @return
	 */
	public Map<String, Object> reInvoiceRegister(String token, String mdzId, String orderId) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboSysInvoiceService.reInvoiceRegister(mdzId, orderId);
	}


	/**
	 * @param 重新开票
	 * @return
	 */
	public Map<String, Object> importDZExcel(String token, List<Map<String, String>> list) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboSysInvoiceService.importDZExcel(list);
	}

	/**
	 * @param token
	 * @param mailTickRegisterList 快递信息或发票信息
	 * @param operationStr         操作快递或发票信息
	 * @return
	 */
	public Map<String, Object> mailTickRegister(String token, List<Map> mailTickRegisterList, String operationStr) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		Map<String, Object> returnMap = new HashMap<>();

		int quantity   = 0;
		int error      = 0;
		int dzquantity = 0;

		for (Map map : mailTickRegisterList) {

			Integer successQuantity = dubboSysInvoiceService.mailTickRegister(map, operationStr);

			++dzquantity;
			if (successQuantity == 1) {
				++quantity;
			} else {
				++error;
			}
		}

		returnMap.put("success", quantity);
		returnMap.put("fail", error);
		returnMap.put("total", dzquantity);

		return returnMap;
	}

	/**
	 * @param token 确认回款操作
	 * @return
	 */
	public Map<String, Object> confirmBackAccount(String token, List<Map> mailTickRegisterList) {

		//String userId = JwtUtil.checkToken(token);

		Map<String, Object> returnMap = new HashMap<>();

		int quantity   = 0;
		int error      = 0;
		int dzquantity = 0;

		Integer successQuantity = dubboSysInvoiceService.confirmBackAccount(mailTickRegisterList, token);


		returnMap.put("success", successQuantity);
		returnMap.put("fail", error);
		returnMap.put("total", mailTickRegisterList.size());

		return returnMap;
	}
}
