package cn.superdesk.uniorder.controller.accounting.customer;

import cn.superdesk.uniorder.common.constants.ResponseMsgConstants;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.enums.accounting.AccountingOrderDimCreateByEnum;
import cn.superdesk.uniorder.common.excel.ExcelResponse;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.enums.common.CommonAccountingStateEnum;
import cn.superdesk.uniorder.common.enums.pay.UnifiedOrderPayStateEnum;
import cn.superdesk.uniorder.common.enums.pay.UnifiedOrderPayTypeEnum;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.accounting.unified.customer.AccountingUnifiedCustomer;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrderGoods;
import cn.superdesk.uniorder.dto.param.accounting.common.StatementOfAccountUpdateFieldRamarkForm;
import cn.superdesk.uniorder.dto.param.accounting.unified.customer.ExportCustomerStatementOfAccountDetailCriteria;
import cn.superdesk.uniorder.dto.vo.accounting.unified.customer.ReportOrderQA;
import cn.superdesk.uniorder.dto.vo.accounting.unified.customer.statement.CustomerStatementOfAccountDetailVo;
import cn.superdesk.uniorder.dto.vo.accounting.unified.customer.ViewDzOrderExcel;
import cn.superdesk.uniorder.dubbo.consumer.accounting.customer.AccountingUnifiedCustomerDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderGoodsDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.sys.SysInvDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.sys.SysUserDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.unified.UnifiedCustomerDubboConsumerService;
import cn.superdesk.uniorder.controller.accounting.utils.ExcelExportAccountingOrderDetailUtils;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 客户结算-对账单
 * @Author: linjinyu 补充
 * @CreateDate: 4/21/20 5:26 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 4/21/20 5:26 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "客户结算-对账单", tags = {"客户结算-对账单"})
@Slf4j
@RestController
@RequestMapping("/accounting/unified/customer/statement")
public class UnifiedCustomerStatementController extends BaseController {

	@Autowired
	private AccountingUnifiedCustomerDubboConsumerService accountingUnifiedCustomerDubboConsumerService;
	@Autowired
	private UnifiedOrderDubboConsumerService              unifiedOrderDubboConsumerService;
	@Autowired
	private UnifiedOrderGoodsDubboConsumerService         unifiedOrderGoodsDubboConsumerService;

	@Autowired
	private UnifiedCustomerDubboConsumerService unifiedCustomerDubboConsumerService;
	@Autowired
	private SysUserDubboConsumerService         sysUserDubboConsumerService;
	@Autowired
	private SysInvDubboConsumerService          sysInvDubboConsumerService;

	/**
	 * 客户结算-对账单
	 *
	 * @param
	 * @return
	 */
	@ApiOperation(value = "客户结算-对账单-列表")
	@RequestMapping(value = "/pagedata", method = RequestMethod.POST)
	@ResponseBody
	public R<List<CustomerStatementOfAccountDetailVo>> getDzPage() {

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

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

			// 前端这个默认就是 1
			// 1 也就是用 create_time between
			// 非1 send_email_time between
			if (StringUtils.isNotEmpty(getString("dateType"))) {
				params.put("dateType", getInteger("dateType"));
			} else {
				params.put("dateType", 1);
			}

			params.put("startTime", getString("startTime"));
			params.put("endTime", getString("endTime"));

			// 微信端用到
			// 年月
			params.put("searchYear", getString("searchYear"));
			params.put("searchMonth", getString("searchMonth"));

			// 订单类型
			params.put("serviceNo", getString("serviceNo"));

			// 对账单ID
			params.put("id", getLong("id"));
			// 对账单SN
			params.put("accountingOrderSn", getString("accountingOrderSn"));

			params.put("orderSn", getString("orderSn"));

			// 对账单状态,0-未确认,1-已确认,-1-已取消,2-已拒绝  3待提交
			// 如果不传 默认为 accounting_status not in (-1,2)
			if (StringUtils.isNotEmpty(getString("accountingStatus"))) {
				params.put("accountingStatus", getInteger("accountingStatus"));
			}

			// 微信端 专用 区别是 传 与 不传 判定为 test="dzStatusWx != null "
			// 传 and accounting_status not in (-1,2)
			params.put("dzStatusWx", getString("dzStatusWx"));

			// 发票抬头
			params.put("invoiceCompanyName", getString("invoiceCompanyName"));
			// 申请开票状态
			if (StringUtils.isNotEmpty(getString("invoiceStatus"))) {
				params.put("invoiceStatus", getInteger("invoiceStatus"));
			}

			// 下单人 企业
			params.put("purchaseVirtId", getString("purchaseVirtId"));
			params.put("purchaseVirtName", getString("purchaseVirtName"));

			if (StringUtils.isNotEmpty(getString("isSendEmail"))) {
				params.put("isSendEmail", getInteger("isSendEmail"));
			}

			// 下单人
			params.put("purchaseUserId", getString("purchaseUserId"));
			params.put("purchaseUserName", getString("purchaseUserName"));
			params.put("purchasePhone", getString("purchasePhone"));

			// 对账单创建维度:0-订单;1-商品;2-流水;
			params.put("dimCreateBy", getInteger("dimCreateBy"));

			log.info("客户结算-对账单-列表 params : \n{}", AnJsonUtils.toJson(params));

			// TODO 前端 客户结算 > 对账单 table list 部分
			result = accountingUnifiedCustomerDubboConsumerService.getDzMainViewByPage(getToken(), params, getPageNum(), getPageSize());


			// TODO 前端 客户结算>对账单 header部份 对账单数:5580 订单数:27983 账单金额:¥ 51991810.1
			ReportOrderQA reportQA = accountingUnifiedCustomerDubboConsumerService.getDzReportOrder(getToken(), params);

			Map<String, Object> headData = new HashMap<>();
			headData.put("amount", reportQA.getAmount());
			headData.put("orderCount", reportQA.getDzquantity());
			headData.put("quantity", reportQA.getQuantity());

			result.setHeadData(headData);

		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);

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

		return result;
	}

	/**
	 * 客户结算-对账单-opt 导出报表(对账单)
	 *
	 * @param response
	 * @throws IOException
	 */
	@Deprecated
	@ApiOperation(value = "客户结算-对账单-opt 导出报表(对账单)")
	@RequestMapping(value = "/getDzMainViewExcel", method = RequestMethod.GET)
	public void getDzMainViewExcel(HttpServletResponse response) throws IOException {
		try {

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

			// 前端这个默认就是 1
			// 1 也就是用 create_time between
			// 非1 send_email_time between
			if (StringUtils.isNotEmpty(getString("dateType"))) {
				params.put("dateType", getInteger("dateType"));
			} else {
				params.put("dateType", 1);
			}

			params.put("startTime", getString("startTime"));
			params.put("endTime", getString("endTime"));

			// 微信端用到
			// 年月
			params.put("searchYear", getString("searchYear"));
			params.put("searchMonth", getString("searchMonth"));

			// 订单类型
			params.put("serviceNo", getString("serviceNo"));

			// 对账单ID
			params.put("id", getLong("id"));

			// 对账单SN
			params.put("accountingOrderSn", getString("accountingOrderSn"));

			// 对账单状态,0-未确认,1-已确认,-1-已取消,2-已拒绝  3待提交
			// 如果不传 默认为 accounting_status not in (-1,2)
			if (StringUtils.isNotEmpty(getString("accountingStatus"))) {
				params.put("accountingStatus", getInteger("accountingStatus"));
			}

			// 微信端 专用 区别是 传 与 不传 判定为 test="dzStatusWx != null "
			// 传 and accounting_status not in (-1,2)
			params.put("dzStatusWx", getString("dzStatusWx"));

			// 发票抬头
			params.put("invoiceCompanyName", getString("invoiceCompanyName"));

			// 申请开票状态
			if (StringUtils.isNotEmpty(getString("invoiceStatus"))) {
				params.put("invoiceStatus", getInteger("invoiceStatus"));
			}

			// 下单人 企业
			params.put("purchaseVirtId", getString("purchaseVirtId"));
			params.put("purchaseVirtName", getString("purchaseVirtName"));

			if (StringUtils.isNotEmpty(getString("isSendEmail"))) {
				params.put("isSendEmail", getInteger("isSendEmail"));
			}

			// 下单人
			params.put("purchaseUserId", getString("purchaseUserId"));
			params.put("purchaseUserName", getString("purchaseUserName"));
			params.put("purchasePhone", getString("purchasePhone"));

			log.info("客户结算-对账单-opt 导出报表(对账单) params : \n{}", AnJsonUtils.toJson(params));

			List<ViewDzOrderExcel> dataList = accountingUnifiedCustomerDubboConsumerService.getDzOrderExcel(getToken(), params);

			for (ViewDzOrderExcel vo : dataList) {
				vo.setPayTypeName(UnifiedOrderPayTypeEnum.getDesc(vo.getPayType()));
				vo.setStatusName(UnifiedOrderPayStateEnum.getDesc(vo.getStatus()));
			}

			String[] field = {"createTime",
					"finishTime",
					"merchName",
					"orderSn",
					"companyName", "invoiceTaxpayersIDCode",
					"addressMobile", "invoiceAccount", "remark",
					"goodsName", "unit", "optionname", "total",
					"totalPrice", "unitPrice", "invoiceTypeName", "taxCode", "fiscalCode", "payTypeName", "statusName",
					"realname", "realname", "dispatchPrice", "orderPrice"};

			String[] fieldZhCn = {"下单时间",
					"完成时间",
					"商户名",
					"主订单编号(有二级订单取二级订单编号",
					"购方名称(开票抬头)", "购方税号",
					"购方地址电话", "购方银行帐号", "备注(项目名称)",
					"货物名称", "计量单位", "规格", "数量",
					"商品金额", "单价", "发票", "商品编码2", "商品编码", "支付方式", "订单状态",
					"客户姓名", "会员名称", "运费", "订单金额"};

			ExcelResponse.out(dataList, fieldZhCn, field, "电子账单", "客户结算" + "_" + "数据列表_" + "电子账单", response);

		} catch (Exception e) {

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


	/**
	 * 客户结算-对账单-详情(订单维度)
	 * 返回对账单详细
	 *
	 * @param
	 * @return
	 */
	@ApiOperation(value = "客户结算-对账单-详情(订单维度)")
	@RequestMapping(value = "/statementOfAccountDetailDimOrder", method = RequestMethod.POST)
	@ResponseBody
	public R<CustomerStatementOfAccountDetailVo> statementOfAccountDetailDimOrder() {

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

		try {
			Long id = getLong("id");

			if (id == null) {
				result.setSuccess(false);
				result.setMessage("请选择查看记录。");
				return result;
			}

			CustomerStatementOfAccountDetailVo customerStatementOfAccountDetailVo = accountingUnifiedCustomerDubboConsumerService.getCustomerStatementOfAccountDetailDimOrder2Goods(id);

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

		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 客户结算-对账单-详情(订单维度)
	 * 返回对账单详细
	 *
	 * @param
	 * @return
	 */
	@ApiOperation(value = "客户结算-对账单-详情(商品维度)")
	@RequestMapping(value = "/statementOfAccountDetailDimGoods", method = RequestMethod.POST)
	@ResponseBody
	public R<CustomerStatementOfAccountDetailVo> statementOfAccountDetailDimGoods() {

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

		try {
			Long id = getLong("id");

			if (id == null) {
				result.setSuccess(false);
				result.setMessage("请选择查看记录。");
				return result;
			}

			CustomerStatementOfAccountDetailVo customerStatementOfAccountDetailVo = accountingUnifiedCustomerDubboConsumerService.getCustomerStatementOfAccountDetailDimGoods2Goods(id);

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

		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}


	/**
	 * 客户结算-对账单 opt 提交对账 (对账单 审批流程向)
	 *
	 * @param
	 * @return
	 */
	@ApiOperation(value = "客户结算-对账单-opt 提交对账 (对账单 审批流程向)")
	@RequestMapping(value = "/submit", method = RequestMethod.POST)
	@ResponseBody
	public R<?> optAccountingCustomerActionSubmit(Long id) {

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

		try {
			AccountingUnifiedCustomer main = accountingUnifiedCustomerDubboConsumerService.findById(id);

			if (main.getAccountingStatus() != 3) {
				result.setSuccess(false);
				result.setMessage("该对账单不是未提交状态！");
				return result;
			}

			accountingUnifiedCustomerDubboConsumerService.optAccountingCustomerActionSubmit(id, getToken());

			result.setSuccess(true);

		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);

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


	/**
	 * 客户结算-对账单-opt 提交对账
	 * | ---- 客户结算-对账单 opt 拒绝对账
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "客户结算-对账单-opt 拒绝对账")
	@RequestMapping(value = "/refuseDz", method = RequestMethod.POST)
	@ResponseBody
	public R<?> optAccountingCustomerActionRefuse(Long id) {
		R<?> result = new R<>();
		try {
			AccountingUnifiedCustomer main = accountingUnifiedCustomerDubboConsumerService.findById(id);


			if (main.getAccountingStatus() != 0) {
				result.setSuccess(false);
				result.setMessage("拒绝失败,该对账单已操作！");
				return result;
			}

			accountingUnifiedCustomerDubboConsumerService.optAccountingCustomerActionRefuse(getToken(), id);
			result.setSuccess(true);
		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}


	/**
	 * 客户结算-对账单 opt 提交对账
	 * | ---- 客户结算-对账单 opt 确认对账
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "客户结算-对账单-opt 确认对账")
	@RequestMapping(value = "/confirmDz", method = RequestMethod.POST)
	@ResponseBody
	public R<?> optAccountingCustomerActionConfirm(Long id) {
		R<?> result = new R<>();
		try {
			AccountingUnifiedCustomer main = accountingUnifiedCustomerDubboConsumerService.findById(id);
			if (main.getAccountingStatus() != 0) {
				result.setSuccess(false);
				result.setMessage("确认失败,该对账单已操作！");
				return result;
			}

			accountingUnifiedCustomerDubboConsumerService.optAccountingCustomerActionConfirm(getToken(), id);
			result.setSuccess(true);
		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}


	/**
	 * 客户结算-对账单 opt 提交对账
	 * | ---- 客户结算-对账单 opt 确认对账
	 * | ---- | ---- 客户结算-对账单 opt 重新对账
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "客户结算-对账单-opt 重新对账 | 取消对账")
	@RequestMapping(value = "/cancelDz", method = RequestMethod.POST)
	@ResponseBody
	public R<?> optAccountingCustomerActionCancel(Long id) {

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

		try {
			AccountingUnifiedCustomer accountingUnifiedCustomer = accountingUnifiedCustomerDubboConsumerService.findById(id);

			if (accountingUnifiedCustomer.getAccountingStatus() == -1) {
				result.setSuccess(false);
				result.setMessage("该对账单已取消,不可重复操作！");
				return result;
			}
			if (accountingUnifiedCustomer.getInvoiceStatus() != 0) {
				result.setSuccess(false);
				result.setMessage("该对账单已开票,不可重新对账！");
				return result;
			}
			if (accountingUnifiedCustomer.getBackAccountStatus() != 0) {
				result.setSuccess(false);
				result.setMessage("该对账单已回款,不可重新对账！");
				return result;
			}

			accountingUnifiedCustomerDubboConsumerService.optAccountingCustomerActionCancel(id);
			result.setSuccess(true);
		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}


	/**
	 * 供应商结算-对账单-opt编辑对帐
	 *
	 * @return
	 */
	@ApiOperation(value = "客户结算-对账单-opt编辑对帐(订单维度)", notes = "")
	@RequestMapping(value = "/opEditAccountingDimOrder", method = RequestMethod.POST)
	@ResponseBody
	public R<Object> opEditAccountingDimOrder() {

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

		try {
			// 对账单Id
			Long accountingOrderId = getLong("accountingOrderId");
			// 订单 orderSn
			String orderSn = getString("orderSn");
			// 是否新增 默认为 0
			String add = getString("add");

			AccountingUnifiedCustomer statementOfAccount = accountingUnifiedCustomerDubboConsumerService.findById(accountingOrderId);

			if (null == statementOfAccount) {
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("对账单 查找不到? 给错参数? 正确的参数为[id]");
				return result;
			}

			// TODO 写死
			if (statementOfAccount.getInvoiceStatus() == 1) {
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("已开票 对账单 不可继续操作！");
				result.setData(statementOfAccount);
				return result;
			}

			UnifiedOrder unifiedOrder = unifiedOrderDubboConsumerService.findByOrderSn(orderSn);

			if (null == unifiedOrder) {
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("订单 查找不到? 给错参数? 正确的参数为[orderSn]");
				return result;
			}

			if (null == statementOfAccount.getEnterpriseId()) {
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("对账单 没有 设定企业? 请联系管理员! 异常对账单Id:" + accountingOrderId);
				result.setData(unifiedOrder);
				return result;
			}

			if (null == unifiedOrder.getPurchaseVirtId()) {
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("要添加的订单 没有 设定企业? 请联系管理员! 异常订单Sn:" + orderSn);
				result.setData(unifiedOrder);
				return result;
			}

			if (!statementOfAccount.getEnterpriseId().equals(unifiedOrder.getPurchaseVirtId())) {
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("订单 的企业 与 对账单 的企业 不一样!");
				result.setData(unifiedOrder);
				return result;
			}

			if (add.equals("1") && unifiedOrder.getAccountingStateCustomer().equals(CommonAccountingStateEnum.已对账.getKey())) {
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("该订单已对账,不可继续操作！");
				result.setData(unifiedOrder);
				return result;
			}

			accountingUnifiedCustomerDubboConsumerService.opEditAccountingDimOrder(getToken(), statementOfAccount, unifiedOrder, add);

			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编辑对帐
	 *
	 * @return
	 */
	@ApiOperation(value = "客户结算-对账单-opt编辑对帐(商品维度)", notes = "")
	@RequestMapping(value = "/opEditAccountingDimGoods", method = RequestMethod.POST)
	@ResponseBody
	public R<Object> opEditAccountingDimGoods() {

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

		try {
			// 对账单Id
			Long accountingOrderId = getLong("accountingOrderId");

			Long unifiedOrderId = getLong("unifiedOrderId");
			// 订单 orderSn
			String orderSn             = getString("orderSn");
			Long   unifiedOrderGoodsId = getLong("unifiedOrderGoodsId");

			// 是否新增 默认为 0
			String add = getString("add");

			AccountingUnifiedCustomer statementOfAccount = accountingUnifiedCustomerDubboConsumerService.findById(accountingOrderId);

			if (null == statementOfAccount) {
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("对账单 查找不到? 给错参数? 正确的参数为[id]");
				return result;
			}

			// TODO 写死 逻辑有点问题 mark by linjinyu 20210106
			if (statementOfAccount.getInvoiceStatus() == 1) {
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("对账单 已开票 不可继续操作！");
				result.setData(statementOfAccount);
				return result;
			}

			UnifiedOrder unifiedOrder = unifiedOrderDubboConsumerService.findByOrderSn(orderSn);


			if (null == unifiedOrder) {
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("商品所属订单 查找不到? 给错参数? 正确的参数为[orderSn]");
				return result;
			}

			if (null == statementOfAccount.getEnterpriseId()) {
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("对账单 没有 设定企业? 请联系管理员! 异常对账单Id:" + accountingOrderId);
				result.setData(unifiedOrder);
				return result;
			}

			if (null == unifiedOrder.getPurchaseVirtId()) {
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("要添加的商品所属订单 没有 设定企业? 请联系管理员! 异常订单Sn:" + orderSn);
				result.setData(unifiedOrder);
				return result;
			}

			if (!statementOfAccount.getEnterpriseId().equals(unifiedOrder.getPurchaseVirtId())) {
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("商品所属订单 的企业 与 对账单 的企业 不一样!");
				result.setData(unifiedOrder);
				return result;
			}

//			if (add.equals("1") && unifiedOrder.getAccountingStateCustomer().equals(CommonAccountingStateEnum.已对账.getKey())) {
//				result.setCode(500);
//				result.setSuccess(false);
//				result.setMessage("该订单已对账,不可继续操作！");
//				result.setData(unifiedOrder);
//				return result;
//			}

			UnifiedOrderGoods unifiedOrderGoods = null;
			if (null != unifiedOrderGoodsId) {

				unifiedOrderGoods = unifiedOrderGoodsDubboConsumerService.findUnifiedOrderGoodsById(unifiedOrderGoodsId);

				if (null == unifiedOrderGoods) {
					result.setCode(500);
					result.setSuccess(false);
					result.setMessage("商品 查找不到? 给错参数? 正确的参数为[id]");
					return result;
				}

				if (add.equals("1") && unifiedOrderGoods.getAccountingStateCustomer().equals(CommonAccountingStateEnum.已对账.getKey())) {
					result.setCode(500);
					result.setSuccess(false);
					result.setMessage("该商品已对账,不可继续操作！");
					result.setData(unifiedOrder);
					return result;
				}
			}


			accountingUnifiedCustomerDubboConsumerService.opEditAccountingDimGoods(getToken(), statementOfAccount, unifiedOrder, unifiedOrderGoods, add);

			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 导出报表
	 * eg: http://119.23.39.237/avicfile/ordermanger/#/customer/accountGoodsDetail?id=8331&serviceNo=FLSC
	 *
	 * @param response
	 * @throws IOException
	 */
	@Deprecated
	@ApiOperation(value = "客户结算-对账单-对账单详情-电子对账单-opt 导出-弃用")
	@RequestMapping(value = "/ExcelDownload", method = RequestMethod.GET)
	public void excelDownload(HttpServletResponse response) throws IOException {

		try {

			Long id = getLong("accountingOrderId");

			// TODO 20210223_功能测试_发现缺失兼容_客户结算_对账单_对账单详情_opt导出报表.md
			AccountingUnifiedCustomer accountingUnifiedCustomer = accountingUnifiedCustomerDubboConsumerService.findById(id);

			CustomerStatementOfAccountDetailVo customerStatementOfAccountDetailVo = null;

			if (null != accountingUnifiedCustomer) {
				switch (AccountingOrderDimCreateByEnum.get(accountingUnifiedCustomer.getDimCreateBy())) {
					case 订单:
						customerStatementOfAccountDetailVo = accountingUnifiedCustomerDubboConsumerService.getCustomerStatementOfAccountDetailDimOrder2Goods(id);
						break;
					case 商品:
						customerStatementOfAccountDetailVo = accountingUnifiedCustomerDubboConsumerService.getCustomerStatementOfAccountDetailDimGoods2Goods(id);
						break;
					case 流水:
						break;
					default:
						break;
				}
			}

			if (null == customerStatementOfAccountDetailVo) {
				return;
			}

			// TODO 20200709 清理过
			HSSFWorkbook workbook = ExcelExportAccountingOrderDetailUtils.unified(customerStatementOfAccountDetailVo);
			ExcelResponse.out(workbook, "客户结算" + "_" + "电子账单" + "_" + customerStatementOfAccountDetailVo.getAccountingOrderSn(), response);

		} catch (Exception e) {

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

	}

	/***
	 * @url /accounting/unified/customer/statement/exportStatementOfAccountDetail
	 *
	 * /exportgoods
	 *
	 * @return
	 */
	@ApiOperation(value = "客户结算-对账单-对账单详情-电子对账单-opt 导出-新版 20210525")
	@RequestMapping(value = "/exportStatementOfAccountDetail", method = RequestMethod.POST)
	public R<String> exportCustomerStatementOfAccountDetail(@RequestBody ExportCustomerStatementOfAccountDetailCriteria criteria) throws IOException {

		log.info("供应商结算-未对账订单-对账单详情-详情导出 params: \n{}", AnJsonUtils.toJson(criteria));

		return new R<String>(unifiedOrderDubboConsumerService.exportCustomerStatementOfAccountDetailCompatible(criteria, getToken()));

	}


	@ApiOperation(value = "设置备注-对账单-客户结算-超级前台 TODO remark 字段还没有加")
	@RequestMapping(value = "/setRemark", method = RequestMethod.POST)
	@ResponseBody
	public R setRemark(@RequestBody StatementOfAccountUpdateFieldRamarkForm form) {

		AccountingUnifiedCustomer statementOfAccount4Update = new AccountingUnifiedCustomer();
		statementOfAccount4Update.setId(form.getId());
//		statementOfAccount4Update.setRemark(form.getRemark());

		accountingUnifiedCustomerDubboConsumerService.updateFieldRemark(getToken(), statementOfAccount4Update);

		return new R();
	}
}
