package cn.superdesk.uniorder.controller.sys;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dto.param.accounting.unified.customer.DzMainAutoParam;
import cn.superdesk.uniorder.dto.vo.inv.ViewBackAccountDataRegisterHead;
import cn.superdesk.uniorder.dto.vo.inv.ViewBackAccountManage;
import cn.superdesk.uniorder.dto.vo.sys.SysBackAccountVo;
import cn.superdesk.uniorder.dubbo.consumer.order._leaveover.SysBackAccountDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.order._leaveover.SysInvoiceDubboConsumerService;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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;

@Slf4j
@RestController
@RequestMapping("/backaccount")
public class SysBackAccountController extends BaseController {


	@Autowired
	private SysInvoiceDubboConsumerService     sysInvoiceDubboService;
	@Autowired
	private SysBackAccountDubboConsumerService sysBackAccountDubboService;

	/**
	 * 获得未回款或已回款数据 -按帐单级汇总
	 *
	 * @return
	 */
	@PostMapping("/getBackAccountManageData")
	@ResponseBody
	public R<List<ViewBackAccountManage>> getBackAccountManageData() {

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

		try {

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


			// 1-下单时间;2-对帐时间;3-开票时间;4-寄票时间;5-回款时间;
			if (StringUtils.isNotEmpty(getString("dateType"))) {
				params.put("dateType", Byte.valueOf(getString("dateType")));
			}
			params.put("startTime", getString("startTime"));
			params.put("endTime", getString("endTime"));

			params.put("enterpriseName", getString("enterpriseName"));//企业名称
			params.put("dzSn", getString("dzSn"));//帐单编码
			params.put("orderSn", getString("orderSn"));//订单编码
			params.put("invoiceNumber", getString("invoiceNumber"));//发票号


			if (StringUtils.isNotEmpty(getString("backStatus"))) {//0未回款\1部分回款\2全部回款
				params.put("backStatus", Byte.valueOf(getString("backStatus")));
			}

			/*
			if(StringUtils.isNotEmpty(getString("invoiceStatus"))) {//发票状态 0未邮寄 1已邮寄 2已作废
				params.put("invoiceStatus", Byte.valueOf(getString("invoiceStatus")));
			}
			*/

			params.put("companyName", getString("companyName"));//开票抬头
			params.put("merchName", getString("merchName"));//商户名称	1

			params.put("realname", getString("realname"));//下单人
			params.put("address_realname", getString("address_realname"));//收件人


			// 已回款页面参数 2全部回款
			String backStatusList = request.getParameter("backStatusList");

			params.put("backStatusList", backStatusList.split(","));

			result = sysInvoiceDubboService.getBackAccountManageData(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;
	}


	/**
	 * //未回款 回款登记 页面数据
	 *
	 * @return
	 */
	@PostMapping("/getBackAccountDataRegisterData")
	@ResponseBody
	public R<ViewBackAccountDataRegisterHead> getBackAccountDataRegisterData() {

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

		try {

			//帐单ID
			String mdzId = request.getParameter("mdzId");

			Map<String, Object> params = new HashMap<>();
			params.put("mdzId", mdzId);

			ViewBackAccountDataRegisterHead backAccountDataRegisterHead = new ViewBackAccountDataRegisterHead();


			List<ViewBackAccountManage> listViewBackAccountManage = sysInvoiceDubboService.getBackAccountManageList(getToken(), params);
			if (listViewBackAccountManage.size() >= 0) {
				ViewBackAccountManage viewBackAccountManage = listViewBackAccountManage.get(0);

				backAccountDataRegisterHead.setOrderNum(viewBackAccountManage.getOrderNum());
				backAccountDataRegisterHead.setInvoiceNum(viewBackAccountManage.getOrderNum());
				backAccountDataRegisterHead.setDZAccount(viewBackAccountManage.getInvoiceAccount());
				backAccountDataRegisterHead.setDzSn(viewBackAccountManage.getDzSn());
				backAccountDataRegisterHead.setEnterpriseName(viewBackAccountManage.getEnterpriseName());
				backAccountDataRegisterHead.setBackAccount(viewBackAccountManage.getBackAccount());
				backAccountDataRegisterHead.setNoBackAccount(viewBackAccountManage.getNoBackAccount());
			}


			List<ViewBackAccountManage> listViewBackAccountRegister = sysInvoiceDubboService.getBackAccountDataRegisterData(getToken(), params);

			////未回款 回款登记  回款相关数据
			backAccountDataRegisterHead.setListViewBackAccountRegister(listViewBackAccountRegister);

			////未回款 回款登记  回款记录


			List<SysBackAccountVo> listSysBackAccount = sysBackAccountDubboService.findAll(params);

			backAccountDataRegisterHead.setListSysBackAccountData(listSysBackAccount);

			result.setData(backAccountDataRegisterHead);

		} 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;
	}

	/**
	 * 未回款>回款登记 确认回款
	 *
	 * @param role
	 * @return
	 */
	@PostMapping("/confirmBackAccount")
	@ResponseBody
	public R confirmBackAccount(DzMainAutoParam model) {
		R result = new R<>();


		/*mdzId
		orderId
		backAccountTime
		backAccountRemark
		backAccount
		*/

		String    confirmBackAccountListJson = request.getParameter("confirmBackAccountList");//申请放行物品列表
		List<Map> mailTickRegisterList       = com.alibaba.fastjson.JSON.parseArray(confirmBackAccountListJson, Map.class);
		try {

			Map<String, Object> returnMap = sysInvoiceDubboService.confirmBackAccount(getToken(), mailTickRegisterList);
			result.setData(returnMap);
			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;
	}

}
