package cn.superdesk.uniorder.dubbo.consumer.invoice.bak;

import cn.superdesk.uniorder.common.exception.UnifiedOrderRuntimeException;
import cn.superdesk.uniorder.common.json.JsonUtils;
import cn.superdesk.uniorder.common.rest.R;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dao.entity.order.ExpressCompany;
import cn.superdesk.uniorder.dto.param.invoice.*;
import cn.superdesk.uniorder.dto.vo.invoice.customer.*;
import cn.superdesk.uniorder.dubbo.consumer._permission.PermissionDubboCustomerService;
import cn.superdesk.uniorder.dubbo.facade.invoice.DubboInvoiceApplyService;
import cn.superdesk.uniorder.utils.numerical.AnIntegerUtils;
import cn.superdesk.uniorder.utils.numerical.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class InvoiceApplyDubboConsumerService {

	@Reference(version = "1.0.0", timeout = 300000, retries = -1)
	private DubboInvoiceApplyService dubboInvoiceApplyService;

	@Resource
	private PermissionDubboCustomerService permissionDubboCustomerService;

	public int updateInvoiceApplyByPrimaryKeySelective(InvoiceApply invoiceApply) {
		return dubboInvoiceApplyService.updateInvoiceApplyByPrimaryKeySelective(invoiceApply);
	}

	public List<InvoiceApplySelectScoreVo> invoiceApplySelectFocusUnifiedDimOrder(Map<String, Object> params) throws UnifiedOrderRuntimeException {
		return dubboInvoiceApplyService.invoiceApplySelectFocusUnifiedDimOrder(params);
	}

	public List<InvoiceApplySelectScoreVo> invoiceApplySelectFocusUnifiedDimGoods(Map<String, Object> params) throws UnifiedOrderRuntimeException {
		return dubboInvoiceApplyService.invoiceApplySelectFocusUnifiedDimGoods(params);
	}

	public List<InvoiceApplySelectScoreVo> invoiceApplySelectFocusScoreDimCredit(ExportExcelInvoiceApplyQueryCriteria criteria) throws UnifiedOrderRuntimeException {
		return dubboInvoiceApplyService.invoiceApplySelectFocusScoreDimCredit(criteria);
	}

	/**
	 * 客户结算 > 发票申请管理 > opt 申请开票 -> 编辑页面 opt 提交
	 *
	 * @param token
	 * @param params
	 * @throws UnifiedOrderRuntimeException
	 */
	public R invoiceApplyFocusUnified(String token, InvoiceApplyFocusUnifiedParams params) throws UnifiedOrderRuntimeException {


		// TODO 这个有可能是空的
		String jsonStr = params.get("orderGoodsObj").toString();

		Integer accountingOrderTypeDim = AnIntegerUtils.getInteger(params.get("accountingOrderTypeDim"));
		Integer dimCreateBy            = AnIntegerUtils.getInteger(params.get("dimCreateBy"));


		// TODO 问题点
		List<InvoiceApplyOrderGoodsFocusUnifiedParams> applyOrderGoodsParamsList = JsonUtils.toList(jsonStr, InvoiceApplyOrderGoodsFocusUnifiedParams.class);

		if (CollectionUtils.isEmpty(applyOrderGoodsParamsList)) {
			return new R("500", "商品错误码", false);
		}

		InvoiceApplyCommonParams invoiceApplyParams = new InvoiceApplyCommonParams();

		invoiceApplyParams.setApplyOrderGoodsFocusUnifiedParamsList(applyOrderGoodsParamsList);

		invoiceApplyParams.setDimCreateBy(dimCreateBy);
		invoiceApplyParams.setAccountingOrderType(accountingOrderTypeDim);

		invoiceApplyParams.setInvoiceTitle(params.get("invoiceCompanyName").toString());
		invoiceApplyParams.setInvoiceType(Integer.valueOf(params.get("invoiceType").toString()));

		invoiceApplyParams.setTaxRate(params.get("taxRate").toString());

		invoiceApplyParams.setInvoiceApplyAmount();
		invoiceApplyParams.setFreeAmount(new BigDecimal(params.get("freeAmount").toString()));
		invoiceApplyParams.setRealAmount(new BigDecimal(params.get("realAmount").toString()));
		invoiceApplyParams.setNoCheckCount(new BigDecimal(params.get("realAmount").toString()));

		invoiceApplyParams.setRemark(params.get("remark").toString());

		dubboInvoiceApplyService.invoiceApply(token, invoiceApplyParams);

		return new R();
	}

	public R invoiceApplyFocusScore(String token, Map<String, Object> params) throws UnifiedOrderRuntimeException {


		// TODO 这个有可能是空的
		String  jsonStr                = params.get("orderGoodsObj").toString();
		Integer accountingOrderTypeDim = Integer.valueOf(params.get("accountingOrderTypeDim").toString());
		Integer dimCreateBy            = AnIntegerUtils.getInteger(params.get("dimCreateBy"));

		List<InvoiceApplyOrderGoodsFocusScoreParams> applyOrderGoodsParamsList = JsonUtils.toList(jsonStr, InvoiceApplyOrderGoodsFocusScoreParams.class);

		if (CollectionUtils.isEmpty(applyOrderGoodsParamsList)) {
			return new R("500", "商品错误码", false);
		}

		InvoiceApplyCommonParams invoiceApplyParams = new InvoiceApplyCommonParams();

		invoiceApplyParams.setApplyOrderGoodsFocusScoreParamsList(applyOrderGoodsParamsList);

		// 发票申请维度 1-一般订单 2-积分订单
		invoiceApplyParams.setAccountingOrderType(accountingOrderTypeDim);
		invoiceApplyParams.setDimCreateBy(dimCreateBy);

		invoiceApplyParams.setInvoiceTitle(params.get("invoiceCompanyName").toString());
		invoiceApplyParams.setInvoiceType(Integer.valueOf(params.get("invoiceType").toString()));

		invoiceApplyParams.setTaxRate(params.get("taxRate").toString());

		invoiceApplyParams.setFreeAmount(new BigDecimal(params.get("freeAmount").toString()));
		invoiceApplyParams.setRealAmount(new BigDecimal(params.get("realAmount").toString()));
		invoiceApplyParams.setNoCheckCount(new BigDecimal(params.get("realAmount").toString()));

		invoiceApplyParams.setRemark(params.get("remark").toString());

		dubboInvoiceApplyService.invoiceApply(token, invoiceApplyParams);

		return new R();
	}


	public R<List<InvoiceApplyPage>> findViewByPageFocusUnified(String token, Map<String, Object> params, int pageNum, int pageSize) throws UnifiedOrderRuntimeException {

		// 20191205 add 数据权限
		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, params);
		if (isPass.equals(false)) {
			R<List<InvoiceApplyPage>> responseResult = new R<>();
			responseResult.setMessage("您没有企业数据权限！");
			responseResult.setSuccess(false);
			return responseResult;
		}

		return dubboInvoiceApplyService.findViewByPageFocusUnified(params, pageNum, pageSize);

	}
//	public ResponseResult<List<InvoiceApplyPage>> findInvoiceByPage(String token, Map<String, Object> params, int pageNum, int pageSize) throws SuperdeskUniOrderException {
//
//		// 20191205 add 数据权限
//		Map<String, Object> tv = JwtUtil.GetTokenVo(token);
//
//		if (tv.get("outLogin").toString().equals("1")) {
//
//			String[] virtIds = tv.get("virtIds").toString().split(",");
//
//			if (virtIds.length > 0) {
//
//				params.put("outVirtIds", virtIds);
//				String[] services = {"LNSS"};
//				params.put("outServices", services);
//
//			} else {
//
//				ResponseResult<List<InvoiceApplyPage>> responseResult = new ResponseResult<>();
//				responseResult.setMessage("您没有企业数据权限！");
//				responseResult.setSuccess(false);
//
//				return responseResult;
//			}
//		}
//
//		return dubboInvoiceApplyService.findViewByPageFocusUnified(params, pageNum, pageSize);
//	}

	public InvoiceApplyDataVo findViewHeadDataFocusUnified(String token, Map<String, Object> params) throws UnifiedOrderRuntimeException {

		// 20191205 add 数据权限
		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, params);
		if (isPass.equals(false)) {
			return null;
		}
		return dubboInvoiceApplyService.findViewHeadDataFocusUnified(params);
	}


	public R<List<InvoiceApplyPage>> findViewByPageFocusScore(String token, ExportExcelInvoiceApplyQueryCriteria criteria, int pageNum, int pageSize) throws UnifiedOrderRuntimeException {

		// 20191205 add 数据权限
		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, criteria);
		if (isPass.equals(false)) {
			R<List<InvoiceApplyPage>> responseResult = new R<>();
			responseResult.setMessage("您没有企业数据权限！");
			responseResult.setSuccess(false);
			return responseResult;
		}

		return dubboInvoiceApplyService.findViewByPageFocusScore(criteria, pageNum, pageSize);

	}

	public InvoiceApplyDataVo findViewHeadDataFocusScore(String token, ExportExcelInvoiceApplyQueryCriteria criteria) throws UnifiedOrderRuntimeException {

		// 20191205 add 数据权限
		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, criteria);
		if (isPass.equals(false)) {
			return null;
		}
		return dubboInvoiceApplyService.findViewHeadDataFocusScore(criteria);
	}


	public void check(String token, Map<String, Object> params) throws UnifiedOrderRuntimeException {
		dubboInvoiceApplyService.check(token, params);
	}


	/****************************************** 客户结算(对账) 开票申请 列表导出 Excel数据 start ******************************************/
	public List<ExportExcelInvoiceApplyForceUnifiedDimGoodsVo> findInvoiceApplyFocusUnifiedDimGoods(String token, Map<String, Object> params) throws UnifiedOrderRuntimeException {

		// 20191205 add 数据权限
		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, params);
		if (isPass.equals(false)) {
			return null;
		}

		return dubboInvoiceApplyService.findInvoiceApplyFocusUnifiedDimGoods(params);
	}

	/**
	 * 积分结算 - 发票申请管理 - opt 导出报表
	 *
	 * @param token
	 * @param criteria
	 * @return
	 * @throws UnifiedOrderRuntimeException
	 */
	public List<ExportExcelInvoiceApplyForceScoreDimCreditVo> findInvoiceApplyFocusScoreDimCredit(String token, ExportExcelInvoiceApplyQueryCriteria criteria) throws UnifiedOrderRuntimeException {

		// 20191205 add 数据权限
		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, criteria);
		if (isPass.equals(false)) {
			return null;
		}
		return dubboInvoiceApplyService.findInvoiceApplyFocusScoreDimCredit(criteria);
	}

	/**
	 * TODO 未实现 目前还是 controller 里实现的
	 *
	 * @param token
	 * @param params
	 * @return
	 * @throws UnifiedOrderRuntimeException
	 */
	public String exportExcelInvoiceApplyFocusUnifiedDimGoods(String token, Map<String, Object> params) throws UnifiedOrderRuntimeException {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		// 20191205 add 数据权限
		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, params);
		if (isPass.equals(false)) {
			return null;
		}

		return dubboInvoiceApplyService.exportExcelInvoiceApplyFocusUnifiedDimGoods(params, loginSysUser);
	}

	public String exportExcelInvoiceApplyFocusScoreDimCredit(String token, ExportExcelInvoiceApplyQueryCriteria criteria) throws UnifiedOrderRuntimeException {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		// 20191205 add 数据权限
		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, criteria);
		if (isPass.equals(false)) {
			return null;
		}

		return dubboInvoiceApplyService.exportExcelInvoiceApplyFocusScoreDimCredit(criteria, loginSysUser);
	}

	/****************************************** 客户结算(对账) 开票申请 列表导出 Excel数据   end ******************************************/


	/****************************************** 发票申请管理 查看详情 start ******************************************/
	/**
	 * 开票申请 一般订单 查看详情
	 *
	 * @param params
	 * @return
	 * @throws UnifiedOrderRuntimeException
	 */
	public InvoiceApplyDetailDimGoodsVo getUnifiedCustomerInvoice2GoodsDetail(Map<String, Object> params) throws UnifiedOrderRuntimeException {
		return dubboInvoiceApplyService.getUnifiedCustomerInvoice2GoodsDetail(params);
	}

	public String exportUnifiedCustomerInvoice2GoodsDetail(Map<String, Object> params, String token) throws UnifiedOrderRuntimeException {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboInvoiceApplyService.exportExcelUnifiedCustomerInvoice2GoodsDetail(params, loginSysUser);
	}

	public InvoiceApplyDetailDimCreditVo getUnifiedCustomerInvoice2CreditDetail(String token, ExportExcelInvoiceApplyQueryCriteria criteria) throws UnifiedOrderRuntimeException {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		return dubboInvoiceApplyService.getUnifiedCustomerInvoice2CreditDetail(criteria, loginSysUser);
	}

	public String exportUnifiedCustomerInvoice2CreditDetail(ExportExcelInvoiceApplyQueryCriteria criteria, String token) throws UnifiedOrderRuntimeException {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboInvoiceApplyService.exportExcelUnifiedCustomerInvoice2CreditDetail(criteria, loginSysUser);
	}

	/****************************************** 发票申请管理 查看详情   end ******************************************/


	/****************************************** 第三方软件导出类 start ******************************************/


	public String exportExcelDimUnifiedInvoiceRegister4E6(String token, Long invoiceApplyId) throws UnifiedOrderRuntimeException {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboInvoiceApplyService.exportExcelDimUnifiedInvoiceRegister4E6(invoiceApplyId, loginSysUser);
	}

	/****************************************** 第三方软件导出类   end ******************************************/


	public void deleteInvoiceApply(Long invoiceApplyId) throws UnifiedOrderRuntimeException {
		dubboInvoiceApplyService.deleteInvoiceApply(invoiceApplyId);
	}

	public void invoiceRegister(String token, Map<String, Object> params) throws UnifiedOrderRuntimeException {
		dubboInvoiceApplyService.invoiceRegister(token, params);
	}


	/**
	 * 寄票登记
	 *
	 * @param token
	 * @param sendInvoiceRegisterList
	 * @throws UnifiedOrderRuntimeException
	 */
	public void sendInvoiceRegister(String token, List<Map> sendInvoiceRegisterList) throws UnifiedOrderRuntimeException {
		dubboInvoiceApplyService.sendInvoiceRegister(token, sendInvoiceRegisterList);
	}

	public String importInvoiceExcel(String token, List<Map<String, Object>> list) throws UnifiedOrderRuntimeException {
		return dubboInvoiceApplyService.importInvoiceExcel(token, list);
	}

	public String importSendInvoiceExcel(String token, List<Map<String, Object>> list) throws UnifiedOrderRuntimeException {
		return dubboInvoiceApplyService.importSendInvoiceExcel(token, list);
	}

	/**
	 * 操作 opt 积分结算 - 发票管理 - 重新开票 (过 20200730 1711)
	 *
	 * @param token
	 * @param params
	 * @throws UnifiedOrderRuntimeException
	 */
	public void invoiceAgain(String token, Map<String, Object> params) throws UnifiedOrderRuntimeException {
		dubboInvoiceApplyService.invoiceAgain(token, params);
	}

	public List<ExpressCompany> findExpressCompany() throws UnifiedOrderRuntimeException {
		return dubboInvoiceApplyService.findExpressCompany();
	}

	public R<List<InvoiceApply>> findInvoiceByPageNew(String token, Map<String, Object> params, int pageNum, int pageSize) {
		return dubboInvoiceApplyService.findInvoiceByPageNew(params, pageNum, pageSize);
	}

	public List<InvoiceApply> findInvoiceByOrderSn(Map<String, Object> invQuery) {
		return dubboInvoiceApplyService.findInvoiceByOrderSn(invQuery);
	}


	public Map<String, InvoiceApply> findInvoiceByOrderSnList(List<String> orderSnList) {
		return dubboInvoiceApplyService.findInvoiceByOrderSnList(orderSnList);
	}


	/****************************************** 验证程序 start ******************************************/

	public void validatedInvoiceApplyRelationTable() {
		dubboInvoiceApplyService.validatedInvoiceApplyRelationTable();
	}

	/****************************************** 验证程序   end ******************************************/
}
