package cn.superdesk.uniorder.controller.invoice.supplier;


import cn.superdesk.uniorder.common.enums.invoice.supplier.InvoiceApplySupplierInvoiceProgressingStatusEnum;
import cn.superdesk.uniorder.common.enums.login.SysLoginTypeEnum;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
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.controller.base.BaseController;
import cn.superdesk.uniorder.dto.param.invoice.supplier.InvoiceApplySupplierPageVoListQueryCriteria;
import cn.superdesk.uniorder.dto.vo.invoice.supplier.InvoiceApplySupplierHeadVo;
import cn.superdesk.uniorder.dto.vo.invoice.supplier.InvoiceApplySupplierPageVo;
import cn.superdesk.uniorder.dubbo.consumer.accounting.supplier.AccountingUnifiedSupplierDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.invoice.supplier.InvoiceApplySupplierDubboConsumerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/12/2 下午3:49
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/12/2 下午3:49
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "[开票发票]-供应商结算-货款管理", tags = {"[开票发票]-供应商结算-货款管理"})
@Slf4j
@RestController
@RequestMapping("/invoice/manager/supplier")
public class InvoiceManagerSupplierController extends BaseController {


	@Autowired
	private InvoiceApplySupplierDubboConsumerService invoiceApplySupplierDubboConsumerService;

	@Autowired
	private AccountingUnifiedSupplierDubboConsumerService accountingUnifiedSupplierDubboConsumerService;

	@ApiOperation(value = "供应商结算-货款管理-列表(共用接口)-新版-后台专用", notes = "供应商结算2.0,伪表单计划整改")
	@PostMapping("/getInvoiceApplySupplierPageVoList/loginType/Backend")
	@ResponseBody
	public R<List<InvoiceApplySupplierPageVo>> getInvoiceApplySupplierPageVoListFacadeBackend(@RequestBody InvoiceApplySupplierPageVoListQueryCriteria queryCriteria) {

		/****************************************** 先这样做啦,虽然丑 差异 加参 start ******************************************/

		String token = getToken();

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		if (loginSysUser.getOutLogin().compareTo(SysLoginTypeEnum.统一订单.getKey()) != 0) {
			return ResponseResultOuterUtils.createErrorResult("500", "后台专用");
		} else {

			// 后台 专用
			// 指定
			queryCriteria.setOutLogin(loginSysUser.getOutLogin());

//			queryCriteria.setInvoiceStatus(InvoiceApplySupplierInvoiceProgressingStatusEnum.待付款.getKey());
			queryCriteria.setInvoiceStatusSelected(Arrays.asList(
//					InvoiceApplySupplierInvoiceProgressingStatusEnum.待开票.getKey(),
//					InvoiceApplySupplierInvoiceProgressingStatusEnum.待收票.getKey(),
					InvoiceApplySupplierInvoiceProgressingStatusEnum.待付款.getKey(),
					InvoiceApplySupplierInvoiceProgressingStatusEnum.已完成.getKey()
//					InvoiceApplySupplierInvoiceProgressingStatusEnum.已作废.getKey()
			));
		}

		/****************************************** 先这样做啦,虽然丑 差异 加参   end ******************************************/

		R<List<InvoiceApplySupplierPageVo>> response = new R<>();

		try {

			log.info("供应商结算-发票登记管理|货款管理-列表(共用接口) - 新版 queryCriteria : \n{}", AnJsonUtils.toJson(queryCriteria));

			response = invoiceApplySupplierDubboConsumerService.getInvoiceApplySupplierPageVoList(getToken(), queryCriteria);

			Map<String, Object>        params                     = AnBeanUtils.beanToMap(queryCriteria);
			InvoiceApplySupplierHeadVo invoiceApplySupplierHeadVo = invoiceApplySupplierDubboConsumerService.getInvoiceApplyListHeadSum(getToken(), params);

			response.setHeadData(AnBeanUtils.beanToMap(invoiceApplySupplierHeadVo));

		} catch (Exception e) {

			response.setCode(String.valueOf(e.hashCode()));
			response.setSuccess(false);
			response.setMessage(e.getMessage());

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}

		return response;
	}

	@ApiOperation(value = "供应商结算-货款管理-列表(共用接口)-新版-商户专用-来源-该供应商所有的开票申请数据,只含开票申请的申请状态为`待开票/待收票/待付款/已完成/已作废`的数据,不含`未申请`的数据和非当前供应商的对账单及开票申请数据", notes = "供应商结算2.0,伪表单计划整改")
	@PostMapping("/getInvoiceApplySupplierPageVoList/loginType/Supplier")
	@ResponseBody
//	public void getInvoiceApplySupplierPageVoList(@RequestBody InvoiceApplySupplierPageVoListQueryCriteria queryCriteria, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//		request
//				.getRequestDispatcher(request.getContextPath() + "/invoice/apply/supplier/getInvoiceApplySupplierPageVoList")
//				.forward(request,response);
//		response
//				.sendRedirect(request.getContextPath() + "/invoice/apply/supplier/getInvoiceApplySupplierPageVoList");
//	}
	public R<List<InvoiceApplySupplierPageVo>> getInvoiceApplySupplierPageVoListFacadeSupplier(@RequestBody InvoiceApplySupplierPageVoListQueryCriteria queryCriteria) {

		/****************************************** 先这样做啦,虽然丑 差异 加参 start ******************************************/

		String token = getToken();

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		if (loginSysUser.getOutLogin().compareTo(SysLoginTypeEnum.福利商城_侨城汇.getKey()) != 0) {
			return ResponseResultOuterUtils.createErrorResult("500", "商户端专用");
		} else {

			// 供应商 | 商户 专用
			// 指定
			queryCriteria.setOutLogin(loginSysUser.getOutLogin());
			queryCriteria.setOutSaleId(loginSysUser.getSaleId());
			queryCriteria.setInvoiceStatusSelected(Arrays.asList(
					InvoiceApplySupplierInvoiceProgressingStatusEnum.待开票.getKey(),
					InvoiceApplySupplierInvoiceProgressingStatusEnum.待收票.getKey(),
					InvoiceApplySupplierInvoiceProgressingStatusEnum.待付款.getKey(),
					InvoiceApplySupplierInvoiceProgressingStatusEnum.已完成.getKey(),
					InvoiceApplySupplierInvoiceProgressingStatusEnum.已作废.getKey()
			));
		}

		/****************************************** 先这样做啦,虽然丑 差异 加参   end ******************************************/

		R<List<InvoiceApplySupplierPageVo>> response = new R<>();

		try {

			log.info("供应商结算-发票登记管理|货款管理-列表(共用接口) - 新版 queryCriteria : \n{}", AnJsonUtils.toJson(queryCriteria));

			response = invoiceApplySupplierDubboConsumerService.getInvoiceApplySupplierPageVoList(getToken(), queryCriteria);

			Map<String, Object>        params                     = AnBeanUtils.beanToMap(queryCriteria);
			InvoiceApplySupplierHeadVo invoiceApplySupplierHeadVo = invoiceApplySupplierDubboConsumerService.getInvoiceApplyListHeadSum(getToken(), params);

			response.setHeadData(AnBeanUtils.beanToMap(invoiceApplySupplierHeadVo));

		} catch (Exception e) {

			response.setCode(String.valueOf(e.hashCode()));
			response.setSuccess(false);
			response.setMessage(e.getMessage());

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}

		return response;
	}
}
