package com.ruoyi.mall.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.google.gson.JsonObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.JPushUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.domain.dto.MallAppOrderGoodsDTO;
import com.ruoyi.mall.domain.dto.MallAppOrderOrderDTO;
import com.ruoyi.mall.domain.vo.MallAppOrderGoodsExportVO;
import com.ruoyi.mall.mapper.*;
import com.ruoyi.mall.service.IMallAppCompanyUserService;
import com.ruoyi.mall.service.IMallAppOrderGoodsService;
import com.ruoyi.mall.service.IMallAppSalesmanAchievementService;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.service.IMallAppOrderOrderService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * app-线下订单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-29
 */
@Service
public class MallAppOrderOrderServiceImpl implements IMallAppOrderOrderService {
    @Resource
    private MallAppOrderOrderMapper mallAppOrderOrderMapper;
    @Resource
    private MallAppAddressMapper mallAppAddressMapper;
    @Resource
    private IMallAppOrderGoodsService mallAppOrderGoodsService;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private IMallAppCompanyUserService mallAppCompanyUserService;
    @Resource
    private IMallAppSalesmanAchievementService mallAppSalesmanAchievementService;
    @Resource
    private JPushUtils jPushUtils;
    @Resource
    private MallOrderOrderMapper mallOrderOrderMapper;
    @Resource
    private MallSalesmanMapper mallSalesmanMapper;
    @Resource
    private MallOrderGoodsMapper mallOrderGoodsMapper;

    /**
     * 查询app-线下订单
     *
     * @param id app-线下订单主键
     * @return app-线下订单
     */
    @Override
    public MallAppOrderOrder selectMallAppOrderOrderById(Long id) {
        return mallAppOrderOrderMapper.selectMallAppOrderOrderById(id);
    }

    /**
     * 查询app-线下订单列表
     *
     * @param mallAppOrderOrder app-线下订单
     * @return app-线下订单
     */
    @Override
    public List<MallAppOrderOrder> selectMallAppOrderOrderList(MallAppOrderOrder mallAppOrderOrder) {
        return mallAppOrderOrderMapper.selectMallAppOrderOrderList(mallAppOrderOrder);
    }

    /**
     * 新增app-线下订单
     *
     * @param mallAppOrderOrder app-线下订单
     * @return 结果
     */
    @Override
    public int insertMallAppOrderOrder(MallAppOrderOrder mallAppOrderOrder) {
        mallAppOrderOrder.setCreateAt(DateUtils.getTime());
        mallAppOrderOrder.setDelFlag("0");
        return mallAppOrderOrderMapper.insertMallAppOrderOrder(mallAppOrderOrder);
    }

    /**
     * 修改app-线下订单
     *
     * @param mallAppOrderOrder app-线下订单
     * @return 结果
     */
    @Override
    public int updateMallAppOrderOrder(MallAppOrderOrder mallAppOrderOrder) {
        return mallAppOrderOrderMapper.updateMallAppOrderOrder(mallAppOrderOrder);
    }

    /**
     * 批量删除app-线下订单
     *
     * @param ids 需要删除的app-线下订单主键
     * @return 结果
     */
    @Override
    public int deleteMallAppOrderOrderByIds(Long[] ids) {
        return mallAppOrderOrderMapper.deleteMallAppOrderOrderByIds(ids);
    }

    /**
     * 删除app-线下订单信息
     *
     * @param id app-线下订单主键
     * @return 结果
     */
    @Override
    public int deleteMallAppOrderOrderById(Long id) {
        return mallAppOrderOrderMapper.deleteMallAppOrderOrderById(id);
    }

    public static String orderIdGenerator() {
        String time = DateUtils.dateTimeNow();
        String randomString = RandomStringUtils.randomNumeric(8);
        return time + randomString;
    }

    /**
     * 新增线下订单
     *
     * @param resourceDTO app-线下订单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ApiResult insertMallAppOrderOrderByDTO(MallAppOrderOrderDTO resourceDTO) {
        List<MallAppOrderGoodsDTO> appOrderGoodsDTOList = resourceDTO.getAppOrderGoodsDTOList();
        if (appOrderGoodsDTOList.size() < 1) {
            return ApiResult.error("请添加商品");
        }
        if ("1".equals(resourceDTO.getIsPay()) && StringUtils.isBlank(resourceDTO.getPayProof())) {
            return ApiResult.error("请上传支付凭证");
        }

        MallAppOrderOrder mallAppOrderOrder = new MallAppOrderOrder();
        String orderNo = orderIdGenerator();
        mallAppOrderOrder.setOrderNo(orderNo);
        mallAppOrderOrder.setOrderType(3);
        mallAppOrderOrder.setStatus(resourceDTO.getIsPay());
        mallAppOrderOrder.setIsPay(resourceDTO.getIsPay());

        if (StringUtils.isBlank(resourceDTO.getPayProof())) {
            mallAppOrderOrder.setIsProof("0");
        } else {
            mallAppOrderOrder.setIsProof("1");
            mallAppOrderOrder.setProofAt(DateUtils.getTime());
            mallAppOrderOrder.setPayProof(resourceDTO.getPayProof());
        }

        mallAppOrderOrder.setOrderUserId(resourceDTO.getOrderUserId());
        mallAppOrderOrder.setResId(resourceDTO.getSalesmanId());
        mallAppOrderOrder.setSalesmanId(resourceDTO.getSalesmanId());

        mallAppOrderOrder.setYear(LocalDate.now().getYear() + "");
        mallAppOrderOrder.setMonth(LocalDate.now().getMonthValue() + "");
        mallAppOrderOrder.setDay(LocalDate.now().getDayOfMonth() + "");
        mallAppOrderOrder.setRemarks(resourceDTO.getRemarks());
        mallAppOrderOrder.setCreateAt(DateUtils.getTime());

        // // 寄件人地址
        // MallAppAddress senderAddress = mallAppAddressMapper.selectMallAppAddressByAddressId(resourceDTO.getSenderAddressId());
        // if (senderAddress == null) {
        //     return ApiResult.error("寄件人地址不存在");
        // }
        // mallAppOrderOrder.setSenderAddressId(resourceDTO.getSenderAddressId());
        // mallAppOrderOrder.setSenderName(senderAddress.getName());
        // mallAppOrderOrder.setSenderPhone(senderAddress.getPhone());
        // mallAppOrderOrder.setSenderProvince(senderAddress.getProvinceName());
        // mallAppOrderOrder.setSenderCity(senderAddress.getCityName());
        // mallAppOrderOrder.setSenderArea(senderAddress.getAreaName());
        // mallAppOrderOrder.setSenderAddress(senderAddress.getAddress());
        // 收件人地址
        MallAppAddress receiverAddress = mallAppAddressMapper.selectMallAppAddressByAddressId(resourceDTO.getReceiverAddressId());
        if (receiverAddress == null) {
            return ApiResult.error("收件人地址不存在");
        }
        mallAppOrderOrder.setReceiverAddressId(resourceDTO.getReceiverAddressId());
        mallAppOrderOrder.setReceiverName(receiverAddress.getName());
        mallAppOrderOrder.setReceiverPhone(receiverAddress.getPhone());
        mallAppOrderOrder.setReceiverProvince(receiverAddress.getProvinceName());
        mallAppOrderOrder.setReceiverCity(receiverAddress.getCityName());
        mallAppOrderOrder.setReceiverArea(receiverAddress.getAreaName());
        mallAppOrderOrder.setReceiverAddress(receiverAddress.getAddress());

        MallAppOrderOrderDTO calculationGoods = this.calculationGoods(appOrderGoodsDTOList);
        mallAppOrderOrder.setOrderPrice(calculationGoods.getOrderPrice());
        mallAppOrderOrder.setPayment(calculationGoods.getOrderPrice());
        mallAppOrderOrder.setGoodsNum(calculationGoods.getGoodsNum());
        mallAppOrderOrder.setCommissionPrice(calculationGoods.getCommissionPrice());

        mallAppOrderOrderMapper.insertMallAppOrderOrder(mallAppOrderOrder);
        mallAppOrderGoodsService.bachInsertMallAppOrderGoods(appOrderGoodsDTOList, mallAppOrderOrder.getId(), orderNo);

        MallAppSalesmanAchievement salesmanAchievement = new MallAppSalesmanAchievement();
        salesmanAchievement.setSalesmanId(resourceDTO.getSalesmanId());
        salesmanAchievement.setStatus("0");
        salesmanAchievement.setLinkOrderId(mallAppOrderOrder.getId());
        salesmanAchievement.setLinkOrderNo(orderNo);
        salesmanAchievement.setPrice(calculationGoods.getOrderPrice());
        salesmanAchievement.setMoney(calculationGoods.getCommissionPrice());
        mallAppSalesmanAchievementService.insertMallAppSalesmanAchievement(salesmanAchievement);

        Map<String, String> orderInfoParams = new HashMap<>();
        orderInfoParams.put("orderId", mallAppOrderOrder.getId().toString());
        orderInfoParams.put("orderType", "3");
        String orderInfo = JSON.toJSONString(orderInfoParams);

        // 极光推送
        // roleId：1-超级管理员，12-仓库管理员，13-财务，10-分公司管理员，11-业务员
        Map<String, Object> newParams = new HashMap<>();// 新增线下订单-超级管理员审核推送
        newParams.put("roleId", 1);
        String[] userName = sysUserMapper.selectUserNameByParams(newParams);
        if (userName.length > 0) {
            System.out.println("新增线下订单-超级管理员审核推送");
            try {
                String url = "intent:#Intent;action=cn.jiguang.myaction;component=com.qdw.qianlengnet/com.qdw.qianlengnet.activity.ChiefAdministratorOrderDetailsActivity;S.orderNo="+mallAppOrderOrder.getId().toString()+";i.orderType="+3+";end";
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("url",url);

                jPushUtils.jPush(userName, receiverAddress.getName() + receiverAddress.getPhone() + "下单，管理员请及时审核", "线下订单管理员审核", orderInfo, orderInfoParams, jsonObject);
            } catch (Exception e) {
                System.out.println("新增线下订单-超级管理员审核推送异常");
            }
        }

        // 已支付 推送财务
        if ("1".equals(resourceDTO.getIsPay())) {
            // 极光推送
            // roleId：1-超级管理员，12-仓库管理员，13-财务，10-分公司管理员，11-业务员
            Map<String, Object> financeParams = new HashMap<>();// 新增线下订单（已支付）-财务审核推送
            financeParams.put("roleId", 13);
            String[] userNameFinanceParams = sysUserMapper.selectUserNameByParams(financeParams);
            if (userNameFinanceParams.length > 0) {
                System.out.println("新增线下订单（已支付）-财务审核推送");
                try {
                    String url = "intent:#Intent;action=cn.jiguang.myaction;component=com.qdw.qianlengnet/com.qdw.qianlengnet.activity.ChiefAdministratorOrderDetailsActivity;S.orderNo="+mallAppOrderOrder.getId().toString()+";i.orderType="+3+";end";
                    JsonObject jsonObject = new JsonObject();
                    jsonObject.addProperty("url",url);

                    jPushUtils.jPush(userNameFinanceParams, receiverAddress.getName() + receiverAddress.getPhone() + "支付凭证已上传，财务请及时审核", "线下订单财务审核", orderInfo, orderInfoParams, jsonObject);
                } catch (Exception e) {
                    System.out.println("新增线下订单（已支付）-财务审核推送异常");
                }
            }
        }

        // 业务员下单推给 自己分公司的管理员
        if (resourceDTO.getSalesmanId().equals(resourceDTO.getOrderUserId())) {
            MallSalesman mallSalesman = mallSalesmanMapper.selectMallSalesmanBySysUserId(resourceDTO.getSalesmanId());
            if (mallSalesman != null) {
                // 极光推送
                // roleId：1-超级管理员，12-仓库管理员，13-财务，10-分公司管理员，11-业务员
                Map<String, Object> companyParams = new HashMap<>();// 业务员新增线下订单-公司管理员推送
                companyParams.put("roleId", 10);
                companyParams.put("companyId", mallSalesman.getCompanyId());
                String[] userNameCompanyParams = sysUserMapper.selectUserNameByParams(companyParams);
                if (userNameCompanyParams.length > 0) {
                    System.out.println("业务员新增线下订单-公司管理员推送");
                    try {
                        String url = "intent:#Intent;action=cn.jiguang.myaction;component=com.qdw.qianlengnet/com.qdw.qianlengnet.activity.OrderDetailsActivity;S.orderNo="+mallAppOrderOrder.getId().toString()+";i.orderType="+3+";end";
                        JsonObject jsonObject = new JsonObject();
                        jsonObject.addProperty("url",url);

                        jPushUtils.jPush(userNameCompanyParams, mallSalesman.getName() + "业务员已下单，公司管理员及时查看", "线下订单公司管理员", orderInfo, orderInfoParams, jsonObject);
                    } catch (Exception e) {
                        System.out.println("业务员新增线下订单-公司管理员推送异常");
                    }
                }
            }


        }


        return ApiResult.ok("下单成功");
    }

    /**
     * 计算商品
     *
     * @param appOrderGoodsDTOList app-线下订单商品列表
     * @return 结果
     */
    public MallAppOrderOrderDTO calculationGoods(List<MallAppOrderGoodsDTO> appOrderGoodsDTOList) {
        // 总件数
        Integer goodsNum = 0;
        // 总价
        BigDecimal orderPrice = new BigDecimal(0);
        // 总提成
        BigDecimal commissionPrice = new BigDecimal(0);

        for (MallAppOrderGoodsDTO orderGoodsDTO : appOrderGoodsDTOList) {
            // 商品数量
            Integer goodsTotal = orderGoodsDTO.getGoodsTotal();
            // 商品单价
            BigDecimal goodsPrice = orderGoodsDTO.getGoodsPrice().setScale(2, RoundingMode.HALF_UP);
            // 商品总价
            BigDecimal goodsTotalPrice = goodsPrice.multiply(new BigDecimal(goodsTotal));

            goodsNum = goodsNum + goodsTotal;
            orderPrice = orderPrice.add(goodsTotalPrice);

            if (orderGoodsDTO.getCommissionPrice() != null) {
                // 提成
                commissionPrice = commissionPrice.add(orderGoodsDTO.getCommissionPrice());
            }
        }

        MallAppOrderOrderDTO mallAppOrderOrderDTO = new MallAppOrderOrderDTO();
        mallAppOrderOrderDTO.setGoodsNum(String.valueOf(goodsNum));
        mallAppOrderOrderDTO.setOrderPrice(orderPrice);
        mallAppOrderOrderDTO.setCommissionPrice(commissionPrice);
        return mallAppOrderOrderDTO;
    }

    /**
     * 查询app-线下订单列表
     *
     * @param params 参数
     * @return app-线下订单集合
     */
    @Override
    public List<Map<String, Object>> selectMallAppOrderOrderListByParams(Map<String, Object> params) {
        long sysUserId = Long.parseLong(params.get("sysUserId").toString());
        int orderType = Integer.parseInt(params.get("orderType").toString());

        int pageNum = Integer.parseInt(params.get("pageNum").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());

        Map<String, Object> newParams = new HashMap<>();
        newParams.put("status", params.get("status"));
        newParams.put("orderNo", params.get("orderNo"));
        newParams.put("keyword", params.get("keyword"));
        newParams.put("beginCreateAt", params.get("beginCreateAt"));
        newParams.put("endCreateAt", params.get("endCreateAt"));
        newParams.put("loginUserId", sysUserId);
        // newParams.put("salesmanId", params.get("salesmanId"));
        //如果status是0，就是未付款，改为status=-1查全部，并且isPay=0查询未付款
        if (newParams.get("status").equals("0")){
            newParams.put("status", "-1");
            newParams.put("isPay", "0");
        }

        Long roleId = sysUserRoleMapper.selectUserRoleByUserId(sysUserId);
        if (roleId == null) {
            throw new ServiceException("用户角色异常");
        }
        if (roleId == 11) {
            MallSalesman mallSalesman = mallSalesmanMapper.selectMallSalesmanBySysUserId(sysUserId);
            if (mallSalesman == null) {
                throw new ServiceException("业务员信息异常");
            }
            newParams.put("salesmanId", mallSalesman.getSysUserId());
        }
        if (roleId == 10) {
            MallAppCompanyUser mallAppCompanyUser = mallAppCompanyUserService.selectMallAppCompanyUserBySysUserId(sysUserId);
            newParams.put("companyId", mallAppCompanyUser.getCompanyId());
        }
        if (roleId == 12) {
            String status = params.get("status").toString();
            if ("3".equals(status) || "4".equals(status)) {
                newParams.put("warehouseManageId", sysUserId);
            }
        }

        newParams.put("roleId", roleId);

        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> orderList = new ArrayList<>();
        if (orderType == 3) {
            orderList = mallAppOrderOrderMapper.selectMallAppOrderOrderListByParams(newParams);
        } else {
            orderList = mallAppOrderOrderMapper.selectMallOrderOrderListByParams(newParams);
        }
        return orderList;
    }

    /**
     * 查询app-线下订单
     *
     * @param params params 参数
     * @return app-线下订单
     */
    @Override
    public ApiResult selectMallAppOrderOrderByParams(Map<String, Object> params) {
        long orderId = Long.parseLong(params.get("orderId").toString());
        int orderType = Integer.parseInt(params.get("orderType").toString());

        MallAppOrderOrder mallAppOrderOrder = new MallAppOrderOrder();
        if (orderType == 3) {
            // 线下订单
            MallAppOrderOrder appOrderOrder = mallAppOrderOrderMapper.selectMallAppOrderOrderById(orderId);
            if (appOrderOrder == null || "1".equals(appOrderOrder.getDelFlag())) {
                return ApiResult.error("订单已删除");
            }
            appOrderOrder.setSalesmanInfo(mallAppCompanyUserService.selectMallAppCompanyUserBySysUserId(appOrderOrder.getSalesmanId()));
            appOrderOrder.setAppOrderGoodsDTOList(mallAppOrderGoodsService.selectMallAppOrderGoodsListByOrderId(orderId));
            mallAppOrderOrder = appOrderOrder;
        } else {
            // 线上订单
            MallOrderOrder order = mallOrderOrderMapper.selectMallOrderOrderById(orderId);
            if (order == null || "1".equals(order.getDelFlag())) {
                return ApiResult.error("订单已删除");
            }
            mallAppOrderOrder.setId(order.getId());
            mallAppOrderOrder.setOrderNo(order.getOrderNo());
            mallAppOrderOrder.setOrderType(0);
            mallAppOrderOrder.setOrderPrice(order.getOrderPrice());
            mallAppOrderOrder.setGoodsNum(mallOrderGoodsMapper.orderGoodsTotal(order.getId()));
            mallAppOrderOrder.setStatus(order.getOrderStatus());
            mallAppOrderOrder.setIsPay(order.getIsPay());
            mallAppOrderOrder.setIsProof(order.getIsProof());
            mallAppOrderOrder.setIsCar(order.getIsCar());
            mallAppOrderOrder.setCreateAt(order.getCreateAt());
            mallAppOrderOrder.setIsExamine(order.getIsExamine());
            mallAppOrderOrder.setExamineAt(order.getExamineAt());
            mallAppOrderOrder.setIsFinance(order.getIsFinance());
            mallAppOrderOrder.setReceiveAt(order.getReceiveAt());
            mallAppOrderOrder.setReceiverName(order.getReceiverName());
            mallAppOrderOrder.setReceiverPhone(order.getReceiverPhone());
            mallAppOrderOrder.setReceiverProvince(order.getReceiverProvince());
            mallAppOrderOrder.setReceiverCity(order.getReceiverCity());
            mallAppOrderOrder.setReceiverArea(order.getReceiverArea());
            mallAppOrderOrder.setReceiverAddress(order.getReceiverAddress());
            mallAppOrderOrder.setReceiverAddressId(0L);
            mallAppOrderOrder.setCarVideo(order.getCarVideo());
            mallAppOrderOrder.setBuyerMsg(order.getBuyerMsg());
            mallAppOrderOrder.setRemarks(order.getRemarks());

            MallSalesman mallSalesman = mallSalesmanMapper.selectMallSalesmanById(order.getSalesmanId());
            MallAppCompanyUser appCompanyUser = new MallAppCompanyUser();
            if (mallSalesman != null) {
                appCompanyUser.setSysUserId(mallSalesman.getId().longValue());
                appCompanyUser.setCompanyUserPortrait(mallSalesman.getAvatar());
                appCompanyUser.setCompanyUserName(mallSalesman.getName());
                appCompanyUser.setCompanyUserPhone(mallSalesman.getPhone());
            }
            mallAppOrderOrder.setSalesmanInfo(appCompanyUser);

            if(order.getWarehouseManageId() != null) {
                SysUser user = sysUserMapper.selectUserById(order.getWarehouseManageId());
                if (user != null) {
                    mallAppOrderOrder.setWarehouseManageId(order.getWarehouseManageId());
                    mallAppOrderOrder.setWarehouseManageName(user.getNickName());
                }
            }

            List<Map<String, Object>> mapList = mallOrderGoodsMapper.selectMallOrderGoodsListByOrderId(order.getId());
            List<MallAppOrderGoods> appOrderGoodsDTOList = new ArrayList<>();
            if (mapList.size() > 0) {
                for (Map<String, Object> stringObjectMap : mapList) {
                    MallAppOrderGoods appOrderGoods = new MallAppOrderGoods();
                    appOrderGoods.setId(Long.valueOf(stringObjectMap.get("id").toString()));
                    appOrderGoods.setOrderId(Long.valueOf(stringObjectMap.get("orderId").toString()));
                    appOrderGoods.setOrderNo(stringObjectMap.get("orderNo").toString());
                    appOrderGoods.setGoodsName(stringObjectMap.get("goodsName").toString());
                    appOrderGoods.setGoodsProduct(stringObjectMap.get("goodsProduct").toString());
                    appOrderGoods.setGoodsUnit(stringObjectMap.get("goodsUnit").toString());
                    appOrderGoods.setGoodsPrice(new BigDecimal(stringObjectMap.get("goodsPrice").toString()));
                    appOrderGoods.setGoodsTotal(Integer.valueOf(stringObjectMap.get("goodsTotal").toString()));
                    appOrderGoods.setTotalPrice(new BigDecimal(stringObjectMap.get("totalPrice").toString()));
                    appOrderGoods.setCommissionPrice(new BigDecimal(stringObjectMap.get("commissionPrice").toString()));
                    appOrderGoods.setReturnStatus(stringObjectMap.get("returnStatus").toString());
                    appOrderGoods.setReturnTotal(Integer.valueOf(stringObjectMap.get("returnTotal").toString()));

                    appOrderGoodsDTOList.add(appOrderGoods);
                }
            }
            mallAppOrderOrder.setAppOrderGoodsDTOList(appOrderGoodsDTOList);
        }
        Long roleId = sysUserRoleMapper.selectUserRoleByUserId(Long.parseLong(params.get("sysUserId").toString()));
        if (roleId != null) {
            if (roleId == 10 || roleId == 11) {
                mallAppOrderOrder.setRemarks("");
            }
        }

        return ApiResult.ok().put("data", mallAppOrderOrder);
    }

    /**
     * 上传支付凭证
     *
     * @param resourceDTO app-线下订单
     * @return app-线下订单
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ApiResult uploadOrderPayProof(MallAppOrderOrderDTO resourceDTO) {
        long orderId = resourceDTO.getId();
        MallAppOrderOrder mallAppOrderOrder = mallAppOrderOrderMapper.selectMallAppOrderOrderById(orderId);
        if (mallAppOrderOrder == null || "1".equals(mallAppOrderOrder.getDelFlag())) {
            return ApiResult.error("订单已删除");
        }

        MallAppOrderOrder appOrderOrder = new MallAppOrderOrder();
        appOrderOrder.setId(orderId);
        appOrderOrder.setIsProof("1");
        appOrderOrder.setPayProof(resourceDTO.getPayProof());
        appOrderOrder.setProofAt(DateUtils.getTime());
        appOrderOrder.setIsPay("1");
        if ("0".equals(mallAppOrderOrder.getStatus())) {
            appOrderOrder.setStatus("1");
        }
        mallAppOrderOrderMapper.updateMallAppOrderOrder(appOrderOrder);

        Map<String, String> orderInfoParams = new HashMap<>();
        orderInfoParams.put("orderId", mallAppOrderOrder.getId().toString());
        orderInfoParams.put("orderType", "3");
        String orderInfo = JSON.toJSONString(orderInfoParams);

        // 极光推送
        // roleId：1-超级管理员，12-仓库管理员，13-财务，10-分公司管理员，11-业务员
        Map<String, Object> newParams = new HashMap<>();// 上传支付凭证-财务审核推送
        newParams.put("roleId", 13);
        String[] userName = sysUserMapper.selectUserNameByParams(newParams);
        if (userName.length > 0) {
            System.out.println("上传支付凭证-财务审核推送");
            try {
                String url = "intent:#Intent;action=cn.jiguang.myaction;component=com.qdw.qianlengnet/com.qdw.qianlengnet.activity.ChiefAdministratorOrderDetailsActivity;S.orderNo="+mallAppOrderOrder.getId().toString()+";i.orderType="+3+";end";
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("url",url);

                jPushUtils.jPush(userName, mallAppOrderOrder.getReceiverName() + mallAppOrderOrder.getReceiverPhone() + "支付凭证已上传，财务请及时审核", "线下订单财务审核", orderInfo, orderInfoParams, jsonObject);
            } catch (Exception e) {
                System.out.println("上传支付凭证-财务审核推送异常");
            }
        }

        return ApiResult.ok("上传成功");
    }

    /**
     * 编辑订单
     *
     * @param resourceDTO app-线下订单
     * @return app-线下订单
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ApiResult uploadOrder(MallAppOrderOrderDTO resourceDTO) {
        List<MallAppOrderGoodsDTO> appOrderGoodsDTOList = resourceDTO.getAppOrderGoodsDTOList();
        if (appOrderGoodsDTOList.size() < 1) {
            return ApiResult.error("请添加商品");
        }

        long orderId = resourceDTO.getId();
        MallAppOrderOrder mallAppOrderOrder = mallAppOrderOrderMapper.selectMallAppOrderOrderById(orderId);
        if (mallAppOrderOrder == null || "1".equals(mallAppOrderOrder.getDelFlag())) {
            return ApiResult.error("订单已删除");
        }

        // if (!mallAppOrderOrder.getSenderAddressId().equals(resourceDTO.getSenderAddressId())) {
        //     // 寄件人地址
        //     MallAppAddress senderAddress = mallAppAddressMapper.selectMallAppAddressByAddressId(resourceDTO.getSenderAddressId());
        //     if (senderAddress == null) {
        //         return ApiResult.error("寄件人地址不存在");
        //     }
        //     mallAppOrderOrder.setSenderAddressId(senderAddress.getAddressId());
        //     mallAppOrderOrder.setSenderName(senderAddress.getName());
        //     mallAppOrderOrder.setSenderPhone(senderAddress.getPhone());
        //     mallAppOrderOrder.setSenderProvince(senderAddress.getProvinceName());
        //     mallAppOrderOrder.setSenderCity(senderAddress.getCityName());
        //     mallAppOrderOrder.setSenderArea(senderAddress.getAreaName());
        //     mallAppOrderOrder.setSenderAddress(senderAddress.getAddress());
        // }

        if (!mallAppOrderOrder.getReceiverAddressId().equals(resourceDTO.getReceiverAddressId())) {
            // 收件人地址
            MallAppAddress receiverAddress = mallAppAddressMapper.selectMallAppAddressByAddressId(resourceDTO.getReceiverAddressId());
            if (receiverAddress == null) {
                return ApiResult.error("收件人地址不存在");
            }
            mallAppOrderOrder.setReceiverAddressId(receiverAddress.getAddressId());
            mallAppOrderOrder.setReceiverName(receiverAddress.getName());
            mallAppOrderOrder.setReceiverPhone(receiverAddress.getPhone());
            mallAppOrderOrder.setReceiverProvince(receiverAddress.getProvinceName());
            mallAppOrderOrder.setReceiverCity(receiverAddress.getCityName());
            mallAppOrderOrder.setReceiverArea(receiverAddress.getAreaName());
            mallAppOrderOrder.setReceiverAddress(receiverAddress.getAddress());
        }

        // 删除app-线下订单商品信息 重新添加
        mallAppOrderGoodsService.deleteMallAppOrderGoodsByOrderId(mallAppOrderOrder.getId());
        MallAppOrderOrderDTO calculationGoods = this.calculationGoods(appOrderGoodsDTOList);
        mallAppOrderOrder.setOrderPrice(calculationGoods.getOrderPrice());
        mallAppOrderOrder.setPayment(calculationGoods.getOrderPrice());
        mallAppOrderOrder.setGoodsNum(calculationGoods.getGoodsNum());
        mallAppOrderOrder.setCommissionPrice(calculationGoods.getCommissionPrice());
        mallAppOrderGoodsService.bachInsertMallAppOrderGoods(appOrderGoodsDTOList, mallAppOrderOrder.getId(), mallAppOrderOrder.getOrderNo());

        mallAppOrderOrder.setRemarks(resourceDTO.getRemarks());
        mallAppOrderOrderMapper.updateMallAppOrderOrder(mallAppOrderOrder);

        MallAppSalesmanAchievement appSalesmanAchievement = mallAppSalesmanAchievementService.selectMallAppSalesmanAchievementByOrderId(orderId);
        if (appSalesmanAchievement != null) {
            appSalesmanAchievement.setPrice(calculationGoods.getOrderPrice());
            appSalesmanAchievement.setMoney(calculationGoods.getCommissionPrice());
            mallAppSalesmanAchievementService.updateMallAppSalesmanAchievement(appSalesmanAchievement);
        }
        return ApiResult.ok("编辑成功");
    }

    /**
     * 管理员审核订单
     *
     * @param resourceDTO app-线下订单
     * @return app-线下订单
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ApiResult adminExamineOrder(MallAppOrderOrderDTO resourceDTO) {
        if (resourceDTO.getSysUserId() != 1) {
            return ApiResult.error("只有管理员才可审核订单");
        }
        long orderId = resourceDTO.getId();
        Integer orderType = resourceDTO.getOrderType();

        Map<String, String> orderInfoParams = new HashMap<>();
        String receiverName = "";
        String receiverPhone = "";

        // 线下订单
        if (orderType == 3) {
            MallAppOrderOrder mallAppOrderOrder = mallAppOrderOrderMapper.selectMallAppOrderOrderById(orderId);
            if (mallAppOrderOrder == null || "1".equals(mallAppOrderOrder.getDelFlag())) {
                return ApiResult.error("订单已删除");
            }
            if ("1".equals(mallAppOrderOrder.getIsExamine())) {
                return ApiResult.error("该订单已审核");
            }
            if (!"0".equals(mallAppOrderOrder.getStatus()) && !"1".equals(mallAppOrderOrder.getStatus())) {
                return ApiResult.error("该订单状态不可审核");
            }

            MallAppOrderOrder appOrderOrder = new MallAppOrderOrder();
            appOrderOrder.setId(orderId);
            appOrderOrder.setIsExamine("1");
            appOrderOrder.setExamineAt(DateUtils.getTime());
            appOrderOrder.setStatus("2");
            mallAppOrderOrderMapper.updateMallAppOrderOrder(appOrderOrder);

            orderInfoParams.put("orderId", mallAppOrderOrder.getId().toString());
            orderInfoParams.put("orderType", "3");
            receiverName = mallAppOrderOrder.getReceiverName();
            receiverPhone = mallAppOrderOrder.getReceiverPhone();

        } else {
            // 线上订单
            MallOrderOrder onlineOrder = mallOrderOrderMapper.selectMallOrderOrderById(orderId);
            if (onlineOrder == null || "1".equals(onlineOrder.getDelFlag())) {
                return ApiResult.error("订单已删除");
            }
            if ("1".equals(onlineOrder.getIsExamine())) {
                return ApiResult.error("该订单已审核");
            }
            if (!"1".equals(onlineOrder.getOrderStatus())) {
                return ApiResult.error("该订单状态不可审核");
            }

            MallOrderOrder order = new MallOrderOrder();
            order.setId(orderId);
            order.setIsExamine("1");
            order.setExamineAt(DateUtils.getTime());
            order.setOrderStatus("2");
            mallOrderOrderMapper.updateMallOrderOrder(order);

            orderInfoParams.put("orderId", onlineOrder.getId().toString());
            orderInfoParams.put("orderType", "0");
            receiverName = onlineOrder.getReceiverName();
            receiverPhone = onlineOrder.getReceiverPhone();

        }

        // 极光推送
        String orderInfo = JSON.toJSONString(orderInfoParams);

        // roleId：1-超级管理员，12-仓库管理员，13-财务，10-分公司管理员，11-业务员
        Map<String, Object> newParams = new HashMap<>();// 超级管理员审核订单-仓库管理员接单推送
        newParams.put("roleId", 12);
        String[] userName = sysUserMapper.selectUserNameByParams(newParams);
        if (userName.length > 0) {
            System.out.println("超级管理员审核订单-仓库管理员接单推送");
            try {
                String url = "intent:#Intent;action=cn.jiguang.myaction;component=com.qdw.qianlengnet/com.qdw.qianlengnet.activity.OrderDetailsActivity;S.orderNo="+orderId+";i.orderType="+orderType+";end";
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("url",url);

                jPushUtils.jPush(userName, receiverName + receiverPhone + "已通过审核，请及时接单", "仓库管理员接单", orderInfo, orderInfoParams, jsonObject);
            } catch (Exception e) {
                System.out.println("超级管理员审核订单-仓库管理员接单推送异常");
            }
        }

        long salesmanId = -1L;
        if (orderType == 3) {
            MallAppOrderOrder mallAppOrderOrder = mallAppOrderOrderMapper.selectMallAppOrderOrderById(orderId);
            if (mallAppOrderOrder.getSalesmanId() != null) {
                MallSalesman mallSalesman = mallSalesmanMapper.selectMallSalesmanBySysUserId(mallAppOrderOrder.getSalesmanId());
                if (mallSalesman != null) {
                    salesmanId = mallSalesman.getId().longValue();
                }
            }
        } else {
            // 线上订单
            MallOrderOrder onlineOrder = mallOrderOrderMapper.selectMallOrderOrderById(orderId);
            if (onlineOrder.getSalesmanId() != null) {
                salesmanId = onlineOrder.getSalesmanId().longValue();
            }
        }

        // roleId：1-超级管理员，12-仓库管理员，13-财务，10-分公司管理员，11-业务员
        Map<String, Object> salesmanParams = new HashMap<>();// 超级管理员审核订单-业务员查看推送
        salesmanParams.put("roleId", 11);
        salesmanParams.put("salesmanId", salesmanId);
        String[] salesmanName = sysUserMapper.selectUserNameByParams(salesmanParams);
        if (salesmanName.length > 0) {
            System.out.println("超级管理员审核订单-业务员查看推送");
            try {
                String url = "intent:#Intent;action=cn.jiguang.myaction;component=com.qdw.qianlengnet/com.qdw.qianlengnet.activity.OrderDetailsActivity;S.orderNo="+orderId+";i.orderType="+orderType+";end";
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("url",url);

                jPushUtils.jPush(salesmanName, receiverName + receiverPhone + "已通过管理审核，请及时查看", "业务员查看", orderInfo, orderInfoParams, jsonObject);
            } catch (Exception e) {
                System.out.println("超级管理员审核订单-业务员查看推送异常");
            }
        }

        return ApiResult.ok("审核成功");
    }

    /**
     * 仓管接单
     *
     * @param resourceDTO app-线下订单
     * @return app-线下订单
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ApiResult warehouseManageOrder(MallAppOrderOrderDTO resourceDTO) {
        Long sysUserId = resourceDTO.getSysUserId();
        long orderId = resourceDTO.getId();

        Integer orderType = resourceDTO.getOrderType();

        Long roleId = sysUserRoleMapper.selectUserRoleByUserId(sysUserId);
        if (roleId != 12) {
            return ApiResult.error("该角色不可接单");
        }

        Map<String, String> orderInfoParams = new HashMap<>();
        // String receiverName = "";
        // String receiverPhone = "";

        // 线下订单
        if (orderType == 3) {
            MallAppOrderOrder mallAppOrderOrder = mallAppOrderOrderMapper.selectMallAppOrderOrderById(orderId);
            if (mallAppOrderOrder == null || "1".equals(mallAppOrderOrder.getDelFlag())) {
                return ApiResult.error("订单已删除");
            }

            if (!"2".equals(mallAppOrderOrder.getStatus()) || mallAppOrderOrder.getWarehouseManageId() != null) {
                return ApiResult.error("该订单状态不可接单");
            }

            orderInfoParams.put("orderId", mallAppOrderOrder.getId().toString());
            orderInfoParams.put("orderType", "3");
            // receiverName = mallAppOrderOrder.getReceiverName();
            // receiverPhone = mallAppOrderOrder.getReceiverPhone();

            MallAppOrderOrder appOrderOrder = new MallAppOrderOrder();
            appOrderOrder.setId(orderId);
            appOrderOrder.setReceiveAt(DateUtils.getTime());
            appOrderOrder.setStatus("3");
            appOrderOrder.setWarehouseManageId(sysUserId);
            mallAppOrderOrderMapper.updateMallAppOrderOrder(appOrderOrder);
        } else {
            // 线上订单
            MallOrderOrder onlineOrder = mallOrderOrderMapper.selectMallOrderOrderById(orderId);
            if (onlineOrder == null || "1".equals(onlineOrder.getDelFlag())) {
                return ApiResult.error("订单已删除");
            }

            if (!"2".equals(onlineOrder.getOrderStatus()) || onlineOrder.getWarehouseManageId() != null) {
                return ApiResult.error("该订单状态不可接单");
            }

            orderInfoParams.put("orderId", onlineOrder.getId().toString());
            orderInfoParams.put("orderType", "0");
            // receiverName = onlineOrder.getReceiverName();
            // receiverPhone = onlineOrder.getReceiverPhone();

            MallOrderOrder order = new MallOrderOrder();
            order.setId(orderId);
            order.setReceiveAt(DateUtils.getTime());
            order.setWarehouseManageId(sysUserId);
            order.setOrderStatus("3");
            mallOrderOrderMapper.updateMallOrderOrder(order);
        }

        // 极光推送
        String orderInfo = JSON.toJSONString(orderInfoParams);

        // roleId：1-超级管理员，12-仓库管理员，13-财务，10-分公司管理员，11-业务员
        Map<String, Object> adminParams = new HashMap<>();// 仓管接单-超级管理员查看推送
        adminParams.put("roleId", 1);
        String[] adminName = sysUserMapper.selectUserNameByParams(adminParams);
        if (adminName.length > 0) {
            System.out.println("仓管接单-超级管理员查看推送");
            try {
                String url = "intent:#Intent;action=cn.jiguang.myaction;component=com.qdw.qianlengnet/com.qdw.qianlengnet.activity.ChiefAdministratorOrderDetailsActivity;S.orderNo="+orderId+";i.orderType="+orderType+";end";
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("url",url);

                jPushUtils.jPush(adminName, "仓管已接单，请及时查看", "超级管理员查看", orderInfo, orderInfoParams, jsonObject);
            } catch (Exception e) {
                System.out.println("仓管接单-超级管理员查看推送异常");
            }
        }


        return ApiResult.ok("接单成功");
    }

    /**
     * 上传装车视频
     *
     * @param resourceDTO app-线下订单
     * @return app-线下订单
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ApiResult uploadOrderCarVideoDTO(MallAppOrderOrderDTO resourceDTO) {
        Long sysUserId = resourceDTO.getSysUserId();
        long orderId = resourceDTO.getId();

        Map<String, String> orderInfoParams = new HashMap<>();
        String receiverName = "";
        String receiverPhone = "";

        Integer orderType = resourceDTO.getOrderType();

        if ("".equals(resourceDTO.getCarVideo())) {
            return ApiResult.error("请上传装车视频");
        }

        // 线下订单
        if (orderType == 3) {
            MallAppOrderOrder mallAppOrderOrder = mallAppOrderOrderMapper.selectMallAppOrderOrderById(orderId);
            if (mallAppOrderOrder == null || "1".equals(mallAppOrderOrder.getDelFlag())) {
                return ApiResult.error("订单已删除");
            }

            if (!mallAppOrderOrder.getWarehouseManageId().equals(sysUserId)) {
                return ApiResult.error("登录正确用户上传装车视频");
            }

            if (!"3".equals(mallAppOrderOrder.getStatus()) || mallAppOrderOrder.getWarehouseManageId() == null) {
                return ApiResult.error("该订单状态不可上传装车视频");
            }

            orderInfoParams.put("orderId", mallAppOrderOrder.getId().toString());
            orderInfoParams.put("orderType", "3");
            receiverName = mallAppOrderOrder.getReceiverName();
            receiverPhone = mallAppOrderOrder.getReceiverPhone();

            MallAppOrderOrder appOrderOrder = new MallAppOrderOrder();
            appOrderOrder.setId(orderId);
            appOrderOrder.setIsCar("1");
            appOrderOrder.setCarVideo(resourceDTO.getCarVideo());
            appOrderOrder.setStatus("4");
            appOrderOrder.setCarAt(DateUtils.getTime());
            mallAppOrderOrderMapper.updateMallAppOrderOrder(appOrderOrder);
        } else {
            // 线上订单
            MallOrderOrder onlineOrder = mallOrderOrderMapper.selectMallOrderOrderById(orderId);
            if (onlineOrder == null || "1".equals(onlineOrder.getDelFlag())) {
                return ApiResult.error("订单已删除");
            }

            if (!onlineOrder.getWarehouseManageId().equals(sysUserId)) {
                return ApiResult.error("登录正确用户上传装车视频");
            }

            if (!"3".equals(onlineOrder.getOrderStatus()) || onlineOrder.getWarehouseManageId() == null) {
                return ApiResult.error("该订单状态不可上传装车视频");
            }

            orderInfoParams.put("orderId", onlineOrder.getId().toString());
            orderInfoParams.put("orderType", "0");
            receiverName = onlineOrder.getReceiverName();
            receiverPhone = onlineOrder.getReceiverPhone();

            MallOrderOrder order = new MallOrderOrder();
            order.setId(orderId);
            order.setIsCar("1");
            order.setCarVideo(resourceDTO.getCarVideo());
            order.setStatus("2");
            order.setCarAt(DateUtils.getTime());
            order.setOrderStatus("4");
            mallOrderOrderMapper.updateMallOrderOrder(order);
        }

        long salesmanId = -1L;
        if (orderType == 3) {
            MallAppOrderOrder mallAppOrderOrder = mallAppOrderOrderMapper.selectMallAppOrderOrderById(orderId);
            if (mallAppOrderOrder.getSalesmanId() != null) {
                MallSalesman mallSalesman = mallSalesmanMapper.selectMallSalesmanBySysUserId(mallAppOrderOrder.getSalesmanId());
                if (mallSalesman != null) {
                    salesmanId = mallSalesman.getId().longValue();
                }
            }
        } else {
            // 线上订单
            MallOrderOrder onlineOrder = mallOrderOrderMapper.selectMallOrderOrderById(orderId);
            if (onlineOrder.getSalesmanId() != null) {
                salesmanId = onlineOrder.getSalesmanId().longValue();
            }
        }

        // 极光推送
        String orderInfo = JSON.toJSONString(orderInfoParams);

        // roleId：1-超级管理员，12-仓库管理员，13-财务，10-分公司管理员，11-业务员
        Map<String, Object> adminParams = new HashMap<>();// 仓库管理上传装车视频（发货）-超级管理员查看推送
        adminParams.put("roleId", 1);
        String[] adminName = sysUserMapper.selectUserNameByParams(adminParams);
        if (adminName.length > 0) {
            System.out.println("仓库管理上传装车视频（发货）-超级管理员查看推送");
            try {
                String url = "intent:#Intent;action=cn.jiguang.myaction;component=com.qdw.qianlengnet/com.qdw.qianlengnet.activity.ChiefAdministratorOrderDetailsActivity;S.orderNo="+orderId+";i.orderType="+orderType+";end";
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("url",url);

                jPushUtils.jPush(adminName, receiverName + receiverPhone + "已装车，请及时查看", "超级管理员查看", orderInfo, orderInfoParams, jsonObject);
            } catch (Exception e) {
                System.out.println("仓库管理上传装车视频（发货）-超级管理员查看推送异常");
            }
        }

        // roleId：1-超级管理员，12-仓库管理员，13-财务，10-分公司管理员，11-业务员
        Map<String, Object> salesmanParams = new HashMap<>();// 仓库管理上传装车视频（发货）-业务员查看推送
        salesmanParams.put("roleId", 11);
        salesmanParams.put("salesmanId", salesmanId);
        String[] salesmanName = sysUserMapper.selectUserNameByParams(salesmanParams);
        if (salesmanName.length > 0) {
            System.out.println("仓库管理上传装车视频（发货）-业务员查看推送");
            try {
                String url = "intent:#Intent;action=cn.jiguang.myaction;component=com.qdw.qianlengnet/com.qdw.qianlengnet.activity.OrderDetailsActivity;S.orderNo="+orderId+";i.orderType="+orderType+";end";
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("url",url);

                jPushUtils.jPush(salesmanName, receiverName + receiverPhone + "已装车，请及时查看", "业务员查看", orderInfo, orderInfoParams, jsonObject);
            } catch (Exception e) {
                System.out.println("仓库管理上传装车视频（发货）-业务员查看推送异常");
            }
        }

        // roleId：1-超级管理员，12-仓库管理员，13-财务，10-分公司管理员，11-业务员
        Map<String, Object> financeParams = new HashMap<>();// 仓库管理上传装车视频（发货）-财务查看推送
        financeParams.put("roleId", 13);
        String[] financeName = sysUserMapper.selectUserNameByParams(financeParams);
        if (financeName.length > 0) {
            System.out.println("仓库管理上传装车视频（发货）-财务查看推送");
            try {
                String url = "intent:#Intent;action=cn.jiguang.myaction;component=com.qdw.qianlengnet/com.qdw.qianlengnet.activity.ChiefAdministratorOrderDetailsActivity;S.orderNo="+orderId+";i.orderType="+orderType+";end";
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("url",url);

                jPushUtils.jPush(financeName, receiverName + receiverPhone + "已装车，请及时查看", "财务查看", orderInfo, orderInfoParams, jsonObject);
            } catch (Exception e) {
                System.out.println("仓库管理上传装车视频（发货）-财务查看推送异常");
            }
        }


        return ApiResult.ok("上传装车视频成功");
    }

    /**
     * 财务审核
     *
     * @param resourceDTO app-线下订单
     * @return app-线下订单
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ApiResult financeExamineOrder(MallAppOrderOrderDTO resourceDTO) {
        Long sysUserId = resourceDTO.getSysUserId();
        long orderId = resourceDTO.getId();

        Long roleId = sysUserRoleMapper.selectUserRoleByUserId(sysUserId);
        if (roleId != 13) {
            return ApiResult.error("该角色不可审核");
        }

        MallAppOrderOrder mallAppOrderOrder = mallAppOrderOrderMapper.selectMallAppOrderOrderById(orderId);
        if (mallAppOrderOrder == null || "1".equals(mallAppOrderOrder.getDelFlag())) {
            return ApiResult.error("订单已删除");
        }

        if ("1".equals(mallAppOrderOrder.getIsFinance())) {
            return ApiResult.error("该订单财务已审核");
        }

        MallAppOrderOrder appOrderOrder = new MallAppOrderOrder();
        appOrderOrder.setId(orderId);
        appOrderOrder.setIsFinance("1");
        appOrderOrder.setFinanceId(sysUserId);
        appOrderOrder.setFinanceExamineAt(DateUtils.getTime());
        appOrderOrder.setIsPay("1");
        if ("0".equals(mallAppOrderOrder.getStatus())) {
            appOrderOrder.setStatus("1");
        }
        mallAppOrderOrderMapper.updateMallAppOrderOrder(appOrderOrder);

        MallAppSalesmanAchievement appSalesmanAchievement = mallAppSalesmanAchievementService.selectMallAppSalesmanAchievementByOrderId(orderId);
        if (appSalesmanAchievement != null) {
            appSalesmanAchievement.setStatus("1");
            mallAppSalesmanAchievementService.updateMallAppSalesmanAchievement(appSalesmanAchievement);
        }

        Map<String, String> orderInfoParams = new HashMap<>();
        orderInfoParams.put("orderId", mallAppOrderOrder.getId().toString());
        orderInfoParams.put("orderType", "3");
        String orderInfo = JSON.toJSONString(orderInfoParams);

        // 极光推送
        // roleId：1-超级管理员，12-仓库管理员，13-财务，10-分公司管理员，11-业务员
        Map<String, Object> newParams = new HashMap<>();// 财务审核-超级管理员查看推送
        newParams.put("roleId", 1);
        String[] userName = sysUserMapper.selectUserNameByParams(newParams);
        if (userName.length > 0) {
            System.out.println("财务审核-超级管理员查看推送");
            try {
                String url = "intent:#Intent;action=cn.jiguang.myaction;component=com.qdw.qianlengnet/com.qdw.qianlengnet.activity.ChiefAdministratorOrderDetailsActivity;S.orderNo="+mallAppOrderOrder.getId().toString()+";i.orderType="+3+";end";
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("url",url);

                jPushUtils.jPush(userName, mallAppOrderOrder.getReceiverName() + mallAppOrderOrder.getReceiverPhone() + "订单财务已审核，请及时查看", "线下订单管理员查看", orderInfo, orderInfoParams, jsonObject);
            } catch (Exception e) {
                System.out.println("财务审核-超级管理员查看推送异常");
            }
        }

        return ApiResult.ok("审核成功");
    }

    /**
     * 订单商品列表
     *
     * @param mallAppOrderOrder app-线下订单
     * @return app-线下订单
     */
    @Override
    public List<MallAppOrderGoodsExportVO> selectExportAppOrderList(MallAppOrderOrder mallAppOrderOrder) {
        return mallAppOrderOrderMapper.selectExportAppOrderList(mallAppOrderOrder);
    }

    /**
     * 导出订单列表
     *
     * @param orderIds 订单ID
     * @return app-线下订单集合
     */
    @Override
    public List<MallAppOrderGoodsExportVO> selectExportAppOrderAppOrderOrderList(Long[] orderIds) {
        return mallAppOrderOrderMapper.selectExportAppOrderAppOrderOrderList(orderIds);
    }
}
