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

import cn.hutool.core.util.ObjectUtil;
import cn.superdesk.uniorder.common.enums.login.SysLoginTypeEnum;
import cn.superdesk.uniorder.common.exception.UnifiedOrderRuntimeException;
import cn.superdesk.uniorder.common.numerical.AnLongUtils;
import cn.superdesk.uniorder.common.pagination.AnBaseQueryCriteria;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.rest.response.ResponseResultOuterUtils;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.invoice.supplier.InvoiceApplySupplier;
import cn.superdesk.uniorder.dto.param.invoice.supplier.InvoiceApplySupplierOpApplyForm;
import cn.superdesk.uniorder.dto.param.invoice.supplier.InvoiceApplySupplierPageVoListQueryCriteria;
import cn.superdesk.uniorder.dto.param.invoice.supplier.InvoiceApplySupplierParam;
import cn.superdesk.uniorder.dto.param.invoice.supplier.InvoiceApplySupplierQueryCriteria;
import cn.superdesk.uniorder.dto.param.invoice.supplier.status.InvoiceApplySupplierOpCommonDimAccountingOrderForm;
import cn.superdesk.uniorder.dto.vo.invoice.supplier.InvoiceApplySupplierDetailDimGoodsVo;
import cn.superdesk.uniorder.dto.vo.invoice.supplier.InvoiceApplySupplierHeadVo;
import cn.superdesk.uniorder.dto.vo.invoice.supplier.InvoiceApplySupplierPageVo;
import cn.superdesk.uniorder.dto.vo.invoice.supplier.InvoiceApplySupplierSelectGoodsVo;
import cn.superdesk.uniorder.dubbo.consumer._permission.PermissionDubboCustomerService;
import cn.superdesk.uniorder.dubbo.consumer.common.RedisDubboConsumerService;
import cn.superdesk.uniorder.dubbo.facade.invoice.supplier.DubboInvoiceApplySupplierService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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


/**
 * @Description: Java类作用描述
 * @Author: linjinyu 重构
 * @CreateDate: 2021/12/1 下午6:31
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/12/1 下午6:31
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class InvoiceApplySupplierDubboConsumerService {

	@Reference(version = "1.0.0", timeout = 30000, retries = -1)
	private DubboInvoiceApplySupplierService dubboInvoiceApplySupplierService;

	@Resource
	private PermissionDubboCustomerService permissionDubboCustomerService;
	@Resource
	private RedisDubboConsumerService      redisDubboConsumerService;


	/****************************************** extends BaseService 部分 start ******************************************/

	public void addObj(InvoiceApplySupplierParam param) {

		dubboInvoiceApplySupplierService.addObj(param);

	}

	public void updateObj(InvoiceApplySupplierParam param) {

		dubboInvoiceApplySupplierService.updateObj(param);
	}

	public void deleteObj(Long id) {

		dubboInvoiceApplySupplierService.deleteObj(id);
	}

	public InvoiceApplySupplier findById(Long id) {

		return dubboInvoiceApplySupplierService.findById(id);
	}

	public List<InvoiceApplySupplier> findByIds(List<Long> invoiceApplyIds) {
		return dubboInvoiceApplySupplierService.findByIds(invoiceApplyIds);
	}

	public List<InvoiceApplySupplier> findByAccountingOrderSn(String accountingOrderSn) {
		return dubboInvoiceApplySupplierService.findByAccountingOrderSn(accountingOrderSn);
	}

	public PageInfo<InvoiceApplySupplier> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {

		return dubboInvoiceApplySupplierService.findByPaging(conditions, pageNun, pageSize);
	}

	public List<InvoiceApplySupplier> findAll(Map<String, Object> queryMap) {

		return dubboInvoiceApplySupplierService.findAll(queryMap);
	}

	/****************************************** extends BaseService 部分   end ******************************************/


	public void addInvoiceApplySupplier(InvoiceApplySupplierParam param) {

		dubboInvoiceApplySupplierService.addInvoiceApplySupplier(param);
	}


	public void updateInvoiceApplySupplier(InvoiceApplySupplierParam param) {

		dubboInvoiceApplySupplierService.updateInvoiceApplySupplier(param);
	}

	public int updateInvoiceApplySupplierByPrimaryKeySelective(InvoiceApplySupplier entity) {

		return dubboInvoiceApplySupplierService.updateInvoiceApplySupplierByPrimaryKeySelective(entity);
	}


	public void deleteInvoiceApplySupplier(Long id) {
		dubboInvoiceApplySupplierService.deleteInvoiceApplySupplier(id);
	}


	public InvoiceApplySupplier findInvoiceApplySupplierById(Long id) {

		return dubboInvoiceApplySupplierService.findInvoiceApplySupplierById(id);
	}


	public R<List<InvoiceApplySupplier>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {

		return dubboInvoiceApplySupplierService.pageQuery(conditions, pageNun, pageSize);
	}


	public R<List<InvoiceApplySupplier>> pageQueryGenericParam(InvoiceApplySupplierQueryCriteria queryCriteria) {

		return dubboInvoiceApplySupplierService.pageQueryGenericParam(queryCriteria);
	}


	/****************************************** 重构前存在 start ******************************************/

	@Deprecated
	public R<List<InvoiceApplySupplier>> getInvoiceApplySupplierList(String token, Map<String, Object> params, int pageNum, int pageSize) {

		// 20191205 add 数据权限
		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, params);
		if (isPass.equals(false)) {
			return ResponseResultOuterUtils.createErrorResult("500", "您没有企业数据权限！");
		}

		return dubboInvoiceApplySupplierService.getInvoiceApplySupplierList(params, pageNum, pageSize);
	}

	public R<List<InvoiceApplySupplierPageVo>> getInvoiceApplySupplierPageVoList(String token, InvoiceApplySupplierPageVoListQueryCriteria queryCriteria) {

		// 20191205 add 数据权限
		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, queryCriteria);

		if (isPass.equals(false)) {
			return ResponseResultOuterUtils.createErrorResult("500", "您没有企业数据权限！");
		}

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		if (ObjectUtil.isNotNull(loginSysUser)
				&& ObjectUtil.isNotNull(loginSysUser.getOutLogin())
				&& loginSysUser.getOutLogin().compareTo(SysLoginTypeEnum.福利商城_侨城汇.getKey()) == 0) {
			Long permOutSaleId = AnLongUtils.getLong(permissionDubboCustomerService.getPermOutSaleId(token));
			queryCriteria.setOutSaleId(permOutSaleId);
			loginSysUser.setSaleId(permOutSaleId);
		}


		return dubboInvoiceApplySupplierService.getInvoiceApplySupplierPageVoList(queryCriteria, loginSysUser);
	}


	public InvoiceApplySupplierHeadVo getInvoiceApplyListHeadSum(String token, Map<String, Object> params) {

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

		return dubboInvoiceApplySupplierService.getInvoiceApplyListHeadSum(params);
	}

	public InvoiceApplySupplierDetailDimGoodsVo getInvoiceApplySupplierDetailByInvoiceApplySupplierId(String token, Long invoiceApplySupplierId) {

		AnBaseQueryCriteria queryCriteria = new AnBaseQueryCriteria();

		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, queryCriteria);

		if (isPass.equals(false)) {
//			return ResponseResultOuterUtils.createErrorResult("500", "您没有企业数据权限！");
			return null;
		}

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboInvoiceApplySupplierService.getInvoiceApplySupplierDetailByInvoiceApplySupplierId(invoiceApplySupplierId, loginSysUser);
	}

	public List<InvoiceApplySupplierSelectGoodsVo> getStatementOfAccountGoodsListByAccountingOrderSn(String accountingOrderSn) {

		return dubboInvoiceApplySupplierService.getStatementOfAccountGoodsListByAccountingOrderSn(accountingOrderSn);
	}

	public List<InvoiceApplySupplierSelectGoodsVo> getStatementOfAccountGoodsListBySysUnifiedOrderGoodsIds(String accountingOrderSn, List<Long> processSysUnifiedOrderGoodsIds) {
		return dubboInvoiceApplySupplierService.getStatementOfAccountGoodsListBySysUnifiedOrderGoodsIds(accountingOrderSn, processSysUnifiedOrderGoodsIds);
	}

	public List<InvoiceApplySupplierSelectGoodsVo> invoiceApplySelectFocusUnified(String accountingOrderSn) {

		return dubboInvoiceApplySupplierService.invoiceApplySelectFocusUnified(accountingOrderSn);
	}

	public InvoiceApplySupplier opInvoiceApply(String token, InvoiceApplySupplierOpApplyForm invoiceApplyParams) {

//		Map<String, Object> tokenVo = JwtUtil.getTokenVo(token);
//
//		return dubboUnifiedSupplierInvService.opInvoiceApply(invoiceApplyParams,
//				tokenVo.get("userId").toString(),
//				tokenVo.get("userName").toString(),
//				tokenVo.get("roleType").toString()
//		);

		// mark by linjinyu at 20210312
		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		return dubboInvoiceApplySupplierService.opInvoiceApply(invoiceApplyParams, loginSysUser);
	}

	public InvoiceApplySupplier updateObj(String token, InvoiceApplySupplier update) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		update.setMakeInvoiceUserId(loginSysUser.getSysUserId());
		update.setMakeInvoiceUserName(loginSysUser.getSysUserName());

		return dubboInvoiceApplySupplierService.updateObj(update);
	}

	@Deprecated
	public InvoiceApplySupplier opConfirmPay(String token, InvoiceApplySupplier update) {

		return dubboInvoiceApplySupplierService.updateObj(update);
	}


	public String opPhysicsDelete(String token, InvoiceApplySupplier entity) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboInvoiceApplySupplierService.opPhysicsDelete(entity, loginSysUser);
	}

	/**
	 * 关于 供应商结算2.0 开票申请 操作逻辑说明
	 * "重新开票" "取消开票" 重定义为 作废
	 * "删除" 重定义为 物理删除
	 * opBecomeInvalid(作废) 与 opPhysicsDelete(物理删除) 差异
	 * 1. opBecomeInvalid 会把 开票申请 流程状态 改成 未正常结束 (流程方向思考) ---- 留底
	 * |---- 申请状态 -> 已作废 && 开票状态 -> 已撤消 分别记录 操作人 操作时间
	 * |---- 清除 关系 X
	 * |---- 关联 更新 "对账单" 开票申请状态
	 * 2. opPhysicsDelete 会把 本身 抹除 硬删除 ---- 消失
	 * |---- 清除 关系 X
	 * |---- 关联 更新 "对账单" 开票申请状态
	 * |---- |---- 怎样溯源|留痕? 目前没考虑 也没有开放使用
	 */

	public String opBecomeInvalid(String token, InvoiceApplySupplier entity) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboInvoiceApplySupplierService.opBecomeInvalid(entity, loginSysUser);
	}

	public String opBecomeInvalid(String token, List<InvoiceApplySupplier> entitys, InvoiceApplySupplierOpCommonDimAccountingOrderForm form) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboInvoiceApplySupplierService.opBecomeInvalid(entitys, form, loginSysUser);
	}


	public void patchInvoiceApplyFieldStatistics() {
		dubboInvoiceApplySupplierService.patchInvoiceApplyFieldStatistics();
	}


	/****************************************** 开票申请页面 导出数据 start ******************************************/


	public String exportExcelInvoiceApplySupplierFocusScoreDimGoods(String token, InvoiceApplySupplierPageVoListQueryCriteria queryCriteria) throws UnifiedOrderRuntimeException {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, queryCriteria);
		if (isPass.equals(false)) {
			return null;
		}

		if (ObjectUtil.isNotNull(loginSysUser)
				&& ObjectUtil.isNotNull(loginSysUser.getOutLogin())
				&& loginSysUser.getOutLogin().compareTo(SysLoginTypeEnum.福利商城_侨城汇.getKey()) == 0) {
			Long permOutSaleId = AnLongUtils.getLong(permissionDubboCustomerService.getPermOutSaleId(token));
			queryCriteria.setOutSaleId(permOutSaleId);
			loginSysUser.setSaleId(permOutSaleId);
		}

		return dubboInvoiceApplySupplierService.exportExcelInvoiceApplySupplierFocusScoreDimGoods(queryCriteria, loginSysUser);
	}

	/****************************************** 开票申请页面 导出数据 start ******************************************/

}
