package cn.superdesk.uniorder.dubbo.consumer.accounting.customer;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.accounting.unified.customer.AccountingUnifiedCustomer;
import cn.superdesk.uniorder.dao.entity.accounting.unified.customer.AccountingUnifiedCustomerOrder;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrderGoods;
import cn.superdesk.uniorder.dao.entity.sys.SysOrder;
import cn.superdesk.uniorder.dto.param.accounting.unified.customer.UnifiedCustomerCreateDimGoodsParam;
import cn.superdesk.uniorder.dto.param.accounting.unified.customer.UnifiedCustomerCreateDimOrderParam;
import cn.superdesk.uniorder.dto.vo.accounting.unified.customer.*;
import cn.superdesk.uniorder.dto.vo.accounting.unified.customer.statement.CustomerStatementOfAccountDetailItemVo;
import cn.superdesk.uniorder.dto.vo.accounting.unified.customer.statement.CustomerStatementOfAccountDetailVo;
import cn.superdesk.uniorder.dto.vo.sys.SysOrderQuery;
import cn.superdesk.uniorder.dubbo.consumer._permission.PermissionDubboCustomerService;
import cn.superdesk.uniorder.dubbo.facade.accounting.customer.DubboAccountingUnifiedCustomerService;
import cn.superdesk.uniorder.common.numerical.AnLongUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * 对帐 主单
 */
@Slf4j
@Component
public class AccountingUnifiedCustomerDubboConsumerService {

	@Reference(version = "1.0.0", timeout = 60000, retries = -1)
	private DubboAccountingUnifiedCustomerService dubboAccountingUnifiedCustomerService;

	@Resource
	private PermissionDubboCustomerService permissionDubboCustomerService;


	/**
	 * 自动 生成对账单 不能用
	 *
	 * @param token
	 * @param companys
	 * @param dateType
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public Map<String, Object> autoProduce(String token, String[] companys, Integer dateType, String startTime, String endTime) {


		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

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

		int quantity   = 0;
		int error      = 0;
		int dzquantity = 0;

		for (int i = 0; i < companys.length; i++) {

			String       company   = companys[i];
			List<String> realnames = dubboAccountingUnifiedCustomerService.findRealNamesByOrder(company, dateType, startTime, endTime);

			for (String realnameId : realnames) {
				dzquantity++;
				try {
					// 生成对账单
					AccountingUnifiedCustomer main = dubboAccountingUnifiedCustomerService.addAuto(company, AnLongUtils.getLong(realnameId), dateType, startTime, endTime, loginSysUser);

					if (main.getId() != null) {
						quantity++;
					} else {
						error++;
					}

				} catch (Exception e) {

					log.info(e.getMessage());
					error++;
				}
			}
		}

		returnMap.put("success", quantity);
		returnMap.put("fail", error);
		returnMap.put("total", dzquantity);

		return returnMap;
	}

	public R<List<DzMainAutoPage>> findAllViewPage(String token, Map<String, Object> params, int pageNo, int pageSize) {

		return dubboAccountingUnifiedCustomerService.findAllViewPage(params, pageNo, pageSize);
	}

	public R<List<ViewSysOrder>> findOrderViewPage(String token, Map<String, Object> params, int pageNo, int pageSize) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		params.put("userId", loginSysUser.getSysUserId());
		return dubboAccountingUnifiedCustomerService.findOrderViewPage(params, pageNo, pageSize);

	}

	public ReportOrderQA getReportOrder(String token, Map<String, Object> params) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		params.put("userId", loginSysUser.getSysUserId());

		return dubboAccountingUnifiedCustomerService.findReportFromOrder(params);
	}

	public List<ViewSysOrderGoods> findOrderViewGoods(String token, SysOrderQuery params) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		params.setUserId(loginSysUser.getSysUserId());

		return dubboAccountingUnifiedCustomerService.findOrderViewGoods(params);

	}

	public List<SysOrder> findAllFromOrder(String token, Map<String, Object> queryMap) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		queryMap.put("userId", loginSysUser.getSysUserId());

		return dubboAccountingUnifiedCustomerService.findAllFromOrder(queryMap);

	}

	/****************************************** 对账单 详情 start ******************************************/

	public CustomerStatementOfAccountDetailVo getCustomerStatementOfAccountDetailDimOrder2Goods(Long accountingOrderId) {

		return dubboAccountingUnifiedCustomerService.getCustomerStatementOfAccountDetailDimOrder2Goods(accountingOrderId);

	}

	public CustomerStatementOfAccountDetailVo getCustomerStatementOfAccountDetailDimGoods2Goods(Long accountingOrderId) {

		return dubboAccountingUnifiedCustomerService.getCustomerStatementOfAccountDetailDimGoods2Goods(accountingOrderId);

	}

	/****************************************** 对账单 详情   end ******************************************/


	/**
	 * 客户结算-对账单-List
	 *
	 * @param token
	 * @param params
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public R<List<CustomerStatementOfAccountDetailVo>> getDzMainViewByPage(String token, Map<String, Object> params, int pageNo, int pageSize) {

		// 20191205 add 数据权限
		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, params);
		if (isPass.equals(false)) {
			R<List<CustomerStatementOfAccountDetailVo>> responseResult = new R<>();
			responseResult.setSuccess(false);
			responseResult.setMessage("您没有企业数据权限！");
			return responseResult;
		}

		return dubboAccountingUnifiedCustomerService.getDzMainViewByPage(params, pageNo, pageSize);
	}

	public ReportOrderQA getDzReportOrder(String token, Map<String, Object> params) {

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

	public List<ViewSysOrderJoinGoods> findOrderViewGoodsTb(String token, Map<String, Object> params) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		params.put("userId", loginSysUser.getSysUserId());

		return dubboAccountingUnifiedCustomerService.findOrderViewGoodsTb(params);
	}

	public AccountingUnifiedCustomer findById(Long id) {

		return dubboAccountingUnifiedCustomerService.findById(id);
	}


	/**
	 * 动作 提交对账
	 * 对账单状态,0-未确认,1-已确认,-1-已取消,2-已拒绝  3,待提交
	 * setAccountingStatus 3 -> 0
	 *
	 * @param id
	 * @param token
	 */
	public void optAccountingCustomerActionSubmit(Long id, String token) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		dubboAccountingUnifiedCustomerService.optAccountingCustomerActionSubmit(id, loginSysUser);
	}

	/**
	 * 动作 取消对账
	 * 对账单状态,0-未确认,1-已确认,-1-已取消,2-已拒绝  3,待提交
	 * setAccountingStatus ? -> -1
	 *
	 * @param id
	 */
	public void optAccountingCustomerActionCancel(Long id) {

		dubboAccountingUnifiedCustomerService.optAccountingCustomerActionCancel(id);
	}

	/**
	 * 动作 拒绝对账
	 * 对账单状态,0-未确认,1-已确认,-1-已取消,2-已拒绝  3,待提交
	 * setAccountingStatus ? -> 2
	 *
	 * @param token
	 * @param id
	 */
	public void optAccountingCustomerActionRefuse(String token, Long id) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		dubboAccountingUnifiedCustomerService.optAccountingCustomerActionRefuse(id, loginSysUser);
	}

	/**
	 * 动作 确认对账
	 * 对账单状态,0-未确认,1-已确认,-1-已取消,2-已拒绝  3,待提交
	 * setAccountingStatus ? -> 1
	 *
	 * @param token
	 * @param id
	 */
	public void optAccountingCustomerActionConfirm(String token, Long id) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		dubboAccountingUnifiedCustomerService.optAccountingCustomerActionConfirm(id, loginSysUser);
	}

	public List<CustomerStatementOfAccountDetailVo> getDzMainView(String token, Map<String, Object> params) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		params.put("userId", loginSysUser.getSysUserId());

		return dubboAccountingUnifiedCustomerService.getDzMainView(params);
	}

	public List<CustomerStatementOfAccountDetailVo> getWxDzMainView(Map<String, Object> params) {

		return dubboAccountingUnifiedCustomerService.getDzMainView(params);
	}

	//	public ResponseResult<List<ViewSysOrderGoods>> getOrderViewByPage(String token, SysOrderQuery query,
//			int pageNum, int pageSize) {
//
//		String userId = JwtUtil.checkToken(token);
//		query.setUserId(loginSysUser.getSysUserId());
//		return dubboMainService.getOrderViewByPage(query,pageNum,pageSize);
//	}

	public R<List<ExpSysOrder>> getOrderViewByPage(String token, SysOrderQuery query, int pageNum, int pageSize) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		query.setUserId(loginSysUser.getSysUserId());

		return dubboAccountingUnifiedCustomerService.getOrderViewByPage(query, pageNum, pageSize);
	}

	public R<List<ExpSysOrder>> getOrderViewByPageProject(String token, SysOrderQuery query, int pageNum, int pageSize) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		query.setUserId(loginSysUser.getSysUserId());

		return dubboAccountingUnifiedCustomerService.getOrderViewByPageProject(query, pageNum, pageSize);
	}

	/**
	 * 导出 excel ?
	 *
	 * @param token
	 * @param params
	 * @return
	 */
	@Deprecated
	public List<CustomerStatementOfAccountDetailItemVo> getDzMainViewExcel(String token, Map<String, Object> params) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		params.put("userId", loginSysUser.getSysUserId());
		return dubboAccountingUnifiedCustomerService.getDzMainViewExcel(params);
	}

	public ReportOrderQA getOrderReportData(String token, SysOrderQuery query) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		query.setUserId(loginSysUser.getSysUserId());

		return dubboAccountingUnifiedCustomerService.getOrderReportData(query);
	}

	public ReportOrderQA getOrderReportDataDeath(String token, SysOrderQuery query) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		query.setUserId(loginSysUser.getSysUserId());

		return dubboAccountingUnifiedCustomerService.getOrderReportDataDeath(query);
	}

	public void updateEmailTime(String token, Long parseLong) {

		dubboAccountingUnifiedCustomerService.updateFieldSendEmailTime(parseLong);
	}

	public List<ExpSysOrder> findOrderViewExcel(String token, Map<String, Object> params) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		params.put("userId", loginSysUser.getSysUserId());
		return dubboAccountingUnifiedCustomerService.findOrderViewExcel(params);
	}

	public List<ExpSysOrder> getOrderViewExcel(String token, SysOrderQuery query) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		query.setUserId(loginSysUser.getSysUserId());
		return dubboAccountingUnifiedCustomerService.getOrderViewExcel(query);
	}


	/**
	 * 旧版 创建 对账单 ---- 客户结算
	 *
	 * @param token
	 * @param orders
	 * @param startTime
	 * @param endTime
	 * @param datetype
	 * @param sendEmail
	 * @return
	 * @throws Exception
	 */
	public AccountingUnifiedCustomer createProduce(String token, List<SysOrder> orders, String startTime, String endTime, Integer datetype, Integer sendEmail) throws Exception {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboAccountingUnifiedCustomerService.addDzMain(loginSysUser.getSysUserId(), orders, startTime, endTime, datetype, sendEmail);
	}

	/**
	 * 新版 创建 对账单 ---- 客户结算
	 *
	 * @param token
	 * @param unifiedOrderList
	 * @param unifiedCustomerCreateDimOrderParam
	 * @return
	 * @throws NumberFormatException
	 * @throws Exception
	 */
	public AccountingUnifiedCustomer opCreateAccountingDimOrder(String token, List<UnifiedOrder> unifiedOrderList, UnifiedCustomerCreateDimOrderParam unifiedCustomerCreateDimOrderParam) throws NumberFormatException, Exception {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboAccountingUnifiedCustomerService.opCreateAccountingDimOrder(loginSysUser.getSysUserId(), unifiedOrderList, unifiedCustomerCreateDimOrderParam);
	}

	/**
	 * 效率优化 mark by linjinyu at 20211203
	 *
	 * @param token
	 * @param unifiedCustomerCreateDimGoodsParam
	 * @return
	 */
	public AccountingUnifiedCustomer opCreateAccountingDimGoods(String token, UnifiedCustomerCreateDimGoodsParam unifiedCustomerCreateDimGoodsParam) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		return dubboAccountingUnifiedCustomerService.opCreateAccountingDimGoods(unifiedCustomerCreateDimGoodsParam, loginSysUser);
	}


	public void opEditAccountingDimOrder(String token, AccountingUnifiedCustomer accountingUnifiedCustomer, UnifiedOrder unifiedOrder, String add) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		dubboAccountingUnifiedCustomerService.opEditAccountingDimOrder(accountingUnifiedCustomer, unifiedOrder, add, loginSysUser);
	}

	public void opEditAccountingDimGoods(String token, AccountingUnifiedCustomer accountingUnifiedCustomer, UnifiedOrder unifiedOrder, UnifiedOrderGoods unifiedOrderGoods, String add) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		dubboAccountingUnifiedCustomerService.opEditAccountingDimGoods(accountingUnifiedCustomer, unifiedOrder, unifiedOrderGoods, add, loginSysUser);
	}


	/**
	 * 客户结算-对账单-opt 导出报表(对账单)
	 *
	 * @param token
	 * @param params
	 * @return
	 */
	public List<ViewDzOrderExcel> getDzOrderExcel(String token, Map<String, Object> params) {

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

		return dubboAccountingUnifiedCustomerService.getDzOrderExcel(params);
	}

	public AccountingUnifiedCustomerOrder findOrderById(Long dzOrderId) {
		return dubboAccountingUnifiedCustomerService.findOrderById(dzOrderId);
	}

	public void updateObj(String token, AccountingUnifiedCustomer statementOfAccount4Update) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		dubboAccountingUnifiedCustomerService.updateObj(statementOfAccount4Update, loginSysUser);
	}

	public void updateFieldRemark(String token, AccountingUnifiedCustomer statementOfAccount4Update) {

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		dubboAccountingUnifiedCustomerService.updateFieldRemark(statementOfAccount4Update, loginSysUser);
	}

	public List<UnifiedOrder> getUnifiedOrderByDzId(Map<String, Object> params) {
		return dubboAccountingUnifiedCustomerService.getUnifiedOrderByDzId(params);
	}

	public List<AccountingUnifiedCustomerOrder> getDzOrderAll(Map<String, Object> dzOrderMap) {
		return dubboAccountingUnifiedCustomerService.getDzOrderAll(dzOrderMap);
	}

	public AccountingUnifiedCustomer findByAccountingOrderSn(String accountingOrderSn) {
		return dubboAccountingUnifiedCustomerService.findByAccountingOrderSn(accountingOrderSn);
	}

	/****************************************** 补丁程序 start ******************************************/

	public void patchXDimOrder2DimGoods(String accountingOrderSn) {
		dubboAccountingUnifiedCustomerService.patchXDimOrder2DimGoods(accountingOrderSn);
	}


	public void patchXAccountingAmountAndQuantityOrderAndQuantityGoods(String accountingOrderSn) {
		dubboAccountingUnifiedCustomerService.patchXAccountingAmountAndQuantityOrderAndQuantityGoods(accountingOrderSn);
	}

	/****************************************** 补丁程序   end ******************************************/


}
