package cn.superdesk.uniorder.controller.invoice.customer.fundrestream;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.invoice.customer.fundrestream.InvoiceBankStatement;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dao.entity.invoice.customer.fundrestream.InvoiceBankStatementOff;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedInvBankOffVo;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.fundrestream.InvoiceBankStatementOffDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderDubboConsumerService;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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


/**
 * @Description: 客户结算 收款核销 回款登记 收款核销
 * @Author: linjinyu 补充 修改
 * @CreateDate: 4/28/20 2:30 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 4/28/20 2:30 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/unifiedinvbankoff")
public class InvoiceBankStatementOffController extends BaseController {

	@Autowired
	UnifiedOrderDubboConsumerService       unifiedOrderDubboConsumerService;
	@Autowired
	InvoiceBankStatementOffDubboConsumerService invoiceBankStatementOffConsumerService;

	/***
	 * 客户结算 > 收款核销 列表分页
	 * @return
	 */
	@PostMapping("/getpage")
	@ResponseBody
	public R<List<UnifiedInvBankOffVo>> getdata() {

		R<List<UnifiedInvBankOffVo>> result = new R<>();

		try {
			Map<String, Object> params = new HashMap<>();

			result = invoiceBankStatementOffConsumerService.getInvoiceBankOffDataByPage(getToken(), params, getPageNum(), getPageSize());

		} catch (Exception e) {

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

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

		return result;
	}

	/**
	 * 客户结算>收款核销>回款登记 input 银行流水 opt (input 流水ID input 收/付方名称)
	 *
	 * @return
	 */
	@PostMapping("/getinvbankdata")
	@ResponseBody
	public R<List<InvoiceBankStatement>> getinvbankdata() {

		R<List<InvoiceBankStatement>> result = new R<>();

		try {
			Map<String, Object> params = new HashMap<>();

			params.put("id", getLong("id"));
			params.put("payee", getString("payname"));
			params.put("noCheckCount", 1);
			params.put("enable", 1);

			List<InvoiceBankStatement> ls = invoiceBankStatementOffConsumerService.getInvoiceBankData(params);

			result.setData(ls);
			result.setSuccess(true);

		} catch (Exception e) {

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

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

		return result;
	}


	/**
	 * 客户结算>收款核销>回款登记 input 发票 opt (input 企业名称 input 发票号)
	 *
	 * @return
	 */
	@PostMapping("/getinvdata")
	@ResponseBody
	public R<List<InvoiceApply>> getInvoiceApplyData() {

		R<List<InvoiceApply>> result = new R<>();

		try {

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

			params.put("invoiceTitle", getString("invoiceTitle"));
			params.put("invoiceNumber", getString("invoiceNumber")); // 修改标记 20211201
			params.put("makeInvoiceStatus", 1);
			params.put("noCheckCount", 1);

			List<InvoiceApply> ls = invoiceBankStatementOffConsumerService.getInvoiceApplyData(params);

			result.setData(ls);
			result.setSuccess(true);

		} catch (Exception e) {

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

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

	/**
	 * 客户结算 > 收款核销 > 回款登记 opt 提交(submit btn 核销)
	 *
	 * @param unifiedInvBankOffEntity
	 * @return
	 */
	@PostMapping("/hxsubmit")
	@ResponseBody
	public R<Object> hxsubmit(InvoiceBankStatementOff unifiedInvBankOffEntity) {

		// 表单
//		invBankIds: 330,333
//		invIds: 14,16
//		invBankAmount: 7549.5
//		invAmount: 132

		log.info("客户结算 > 收款核销 > 回款登记 opt 提交(submit btn 核销) params :\n{}", AnJsonUtils.toJson(unifiedInvBankOffEntity));

		R<Object> result = new R<>();

		try {

			// 金额判断
			Map<String, Object> resMap = invoiceBankStatementOffConsumerService.addObjTran(getToken(), unifiedInvBankOffEntity);

			if (resMap.get("code").toString().equals("1")) {

				result.setSuccess(true);
				result.setData(resMap.get("data"));

			} else {

				result.setSuccess(false);
				result.setMessage(resMap.get("msg").toString());
			}

		} catch (Exception e) {

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

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

		}

		return result;
	}

}
