package com.gree.mall.manager.logic.order;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.gree.mall.manager.bean.ExcelData;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.order.OrderShareReportBean;
import com.gree.mall.manager.bean.order.OrderShareVO;
import com.gree.mall.manager.commonmapper.CommonPageV2Mapper;
import com.gree.mall.manager.commonmapper.OrderShareReportMapper;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.enums.OrderShareStatusEnum;
import com.gree.mall.manager.enums.OrderStatusEnum;
import com.gree.mall.manager.enums.RefundFlagEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.logic.common.WechatLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.*;
import com.gree.mall.manager.utils.DateUtils;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class OrderShareLogic {

    @Autowired
    private WechatLogic wechatLogic;
    @Autowired
    CommonLogic commonLogic;
    @Autowired
    AdminCompanyWechatService adminCompanyWechatService;
    @Autowired
    OrderShareService orderShareService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderShareReportMapper orderShareReportMapper;
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private OrderRefundService orderRefundService;
    @Autowired
    CommonPageV2Mapper commonPageV2Mapper;
    @Resource
    OrderShareFinishRecordService orderShareFinishRecordService;


    public IPage<OrderShare> list(HttpServletRequest request,String userId, String websitId, String orderShareId, String orderId, String workUserName, String phone, String status,
                                  String startTime, String endTime, String startSettleTime, String endSettleTime, String position,
                                  String startPayTime,String endPayTime,Integer pageNo, Integer pageSize) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        log.info("结算管理:companyWechatId:{},adminWebsitIds:{}",JSONObject.toJSONString(adminUser.getCompanyWechatIds()),JSONObject.toJSONString(adminUser.getAdminWebsitIds()));
        IPage<OrderShare> page = orderShareService.lambdaQuery()
                .in(CollectionUtils.isNotEmpty(adminUser.getCompanyWechatIds()),OrderShare::getCompanyWechatId,adminUser.getCompanyWechatIds())
                ////.//in(CollectionUtils.isNotEmpty(adminUser.getAdminWebsitIds()),OrderShare::getWebsitId,adminUser.getAdminWebsitIds())
                .eq(StringUtils.isNotBlank(userId), OrderShare::getUserId, userId)
                .like(StringUtils.isNotBlank(orderShareId), OrderShare::getOrderShareId, orderShareId)
                .like(StringUtils.isNotBlank(orderId), OrderShare::getOrderId, orderId)
                .like(StringUtils.isNotBlank(workUserName), OrderShare::getWorkUserName, workUserName)
                .like(StringUtils.isNotBlank(phone), OrderShare::getPhone, phone)
                .eq(StringUtils.isNotBlank(status), OrderShare::getStatus, status)
                .between(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime), OrderShare::getCreateTime, startTime, endTime)
                .between(StringUtils.isNotBlank(startSettleTime) && StringUtils.isNotBlank(endSettleTime), OrderShare::getSettleTime, startSettleTime, endSettleTime)
                .between(StringUtils.isNotBlank(startPayTime) && StringUtils.isNotBlank(endPayTime),OrderShare::getPayTime,startPayTime,endPayTime)
                .orderByDesc(OrderShare::getCreateTime)
                .page(new Page<>(pageNo, pageSize));
        return page;
    }


    public void export(HttpServletRequest request, HttpServletResponse response, String userId, String websitId, String orderShareId, String orderId,
                       String workUserName, String phone, String status, String startTime, String endTime, String startSettleTime, String endSettleTime,
                       String position,String startPayTime,String endPayTime) throws Exception {
        List<OrderShare> list = list(request,userId, websitId, orderShareId, orderId, workUserName, phone, status, startTime, endTime,
                startSettleTime, endSettleTime, position,startPayTime,endPayTime,1,-1).getRecords();
        String[] titles = {"序号","结算编号","订单类型", "订单编号", "店铺名称", "商品信息", "单价", "数量", "总金额",
                "商品佣金", "订单佣金","优惠金额","结算金额","平台优惠券名称", "交易单号",
                "售后服务", "下单账号", "下单时间", "支付时间", "支付方式", "佣金结算状态",
                "结算时间","分销员", "所属网点","商品佣金类型","订单状态", "职位",
                "买家备注", "收货人",
                "手机号", "收货地址", "物流公司", "物流单号", "发票类型", "发票抬头", "发票内容",
                "纳税人识别号", "名称", "公司注册地址", "公司注册电话", "开户银行", "银行账号", "收票人姓名",
                "收票人地址", "收票人手机", "收票人邮箱", "商户单号",
                "订单结算状态","支付时间","备注"};
        ExcelData excelData = new ExcelData();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if(CollectionUtils.isEmpty(list)){
            String fileName = String.format("结算导出_%s.xlsx", simpleDateFormat.format(new Date()));
            excelData.setTitles(Arrays.asList(titles));
            excelData.setName("结算订单");
            excelData.setRows(new ArrayList<>());
            ExcelUtils.exportExcel(request, response, fileName, excelData, -1, null);
            return;
        }

        List<String> orderIds = list.stream().map(OrderShare::getOrderId).collect(Collectors.toList());

        List<OrderShareReportBean> orderShareReportBeans = orderShareReportMapper.queryReport(orderIds);

        List<List<Object>> rows = new ArrayList<>();

        final String SHOP_NAME = "格匠商城";
        for (int i = 0; i < orderShareReportBeans.size(); i++) {
            OrderShareReportBean bean = orderShareReportBeans.get(i);
            List<Object> row = new ArrayList<>();
            row.add(i + 1);
            row.add(bean.getOrderShareId());
           // row.add(bean.getMainNumber());
            row.add(bean.getOrderId());
            row.add(SHOP_NAME);
         /*   row.add(bean.getGoodsName());
            row.add(bean.getPrice());
            row.add(bean.getNum());*/
            row.add(bean.getTotalPrice());

          //  row.add(bean.getShareAmount());//"商品佣金"
            row.add(bean.getAmount());//"订单佣金"
            row.add(bean.getDiscountAmount());//优惠金额
           // row.add(bean.getPayAmount().subtract(bean.getShareAmount()));//"结算金额"
            row.add(bean.getCouponName());//"平台优惠券名称"
            row.add(bean.getTransactionId());//"交易单号"

            row.add(bean.getOrderStatus().equals(OrderStatusEnum.REFUND.toString()) ? "退款关闭" : "无");
            row.add(bean.getUserId());
            row.add(bean.getCreateTime() == null ? "" : simpleDateFormat.format(bean.getCreateTime()));
            row.add(bean.getPayTime() == null ? "" : simpleDateFormat.format(bean.getPayTime()));
            row.add(bean.getPayType());


            row.add(Enum.valueOf(OrderShareStatusEnum.class, bean.getShareStatus().trim().toUpperCase()).getName());//"佣金结算状态"
            row.add(bean.getSettleTime() == null ? "" : simpleDateFormat.format(bean.getSettleTime()));//"结算时间"
            row.add(bean.getWorkUserName());//"分销员"
            row.add(bean.getWebsitName());//"所属网点"
            row.add("");//"商品佣金类型"
            row.add(Enum.valueOf(OrderStatusEnum.class, bean.getOrderStatus().trim().toUpperCase()).getName());//"订单状态"
            row.add(bean.getPosition());// "职位"

            row.add(bean.getRemark());
            row.add(bean.getReceUserName());
            row.add(bean.getRecePhone());
            row.add(bean.getAddress());
            row.add(bean.getName());
            row.add(bean.getLogisticsNo());

            row.add(bean.getType() == null || !bean.getType() ? "个人发票" : "公司发票");
            row.add(bean.getTaxName());
            row.add(bean.getContent());
            row.add(bean.getTaxNo());
            row.add(bean.getTaxName());

            row.add(bean.getRegisterAddress());
            row.add(bean.getRegisterPhone());
            row.add(bean.getBank());
            row.add(bean.getAccount());
            row.add(bean.getReceiverName());
            row.add(bean.getReceiverAddress());
            row.add(bean.getReceiverPhone());
            row.add(bean.getReceiverEmail());

            row.add("");
            row.add("");
            row.add(simpleDateFormat.format(bean.getPayTime()));
            row.add(bean.getRemark() == null ? "" : bean.getRemark());
            rows.add(row);
        }

        String fileName = String.format("结算导出_%s.xlsx", simpleDateFormat.format(new Date()));
        excelData.setTitles(Arrays.asList(titles));
        excelData.setName("结算订单");
        excelData.setRows(rows);
//        final int[] mergeRows = new int[]{1,2,7, 9, 10, 11, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 41, 42, 43, 44};
        ExcelUtils.exportExcel(request, response, fileName, excelData, -1, null);

    }


    public void export2(HttpServletRequest request, HttpServletResponse response, String userId, String websitId, String orderShareId, String orderId,
                        String workUserName, String phone, String status, String startTime, String endTime, String startSettleTime, String endSettleTime,
                        String position, String startPayTime, String endPayTime) throws Exception {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String[] titles = {"序号", "商城名称", "结算编号", "订单号" , "订单佣金", "总价", "折扣金额", "实付金额", "运费", "订单状态", "退款标记", "下单用户账号",
                "下单时间", "支付方式", "支付时间", "买家备注", "收货人", "收货电话", "收货地址", "物流单号", "物流公司", "发票类型",
                "发票抬头", "发票内容", "订单发票纳税人识别号", "订单发票公司注册地址", "订单发票公司注册电话", "订单发票开户行", "订单发票开户账号", "订单发票接收邮箱", "订单发票接收手机号",
                "订单发票收件人", "订单发票收件地址", "优惠券名称", "订单交易号", "佣金结算状态", "分销员", "订单所属网点",
                "结算时间", "职位"};

        ExcelData excelData = new ExcelData();
        String fileName = String.format("结算导出_%s.xlsx", simpleDateFormat.format(new Date()));
        excelData.setTitles(Arrays.asList(titles));
        excelData.setName("结算订单");

        List<OrderShare> list = list(request, userId, websitId, orderShareId, orderId, workUserName, phone, status, startTime, endTime,
                startSettleTime, endSettleTime, position, startPayTime, endPayTime, 1, -1).getRecords();

        if (CollectionUtils.isEmpty(list)) {
            excelData.setRows(new ArrayList<>());
            ExcelUtils.exportExcel(request, response, fileName, excelData, -1, null);
            return;
        }
        //todo  延用原export()中的实现方案  如果出现太慢还是推荐在mybatis中设置条件
        List<String> orderShareIdList = list.stream().map(OrderShare::getOrderShareId).collect(Collectors.toList());

        List<OrderShareReportBean> orderShareReportBeans = orderShareReportMapper.queryReport2(orderShareIdList);

        List<List<Object>> rows = new ArrayList<>();
        for (int i = 0; i < orderShareReportBeans.size(); i++) {
            OrderShareReportBean bean = orderShareReportBeans.get(i);
            List<Object> row = new ArrayList<>();
            rows.add(row);

            row.add(i + 1);
            row.add(bean.getShopName());
            row.add(bean.getOrderShareId());
            row.add(bean.getOrderId());
            row.add(bean.getAmount());
            row.add(bean.getTotalPrice());
            row.add(bean.getDiscountAmount());
            row.add(bean.getPayAmount());
            row.add(bean.getFreight());
            row.add(Enum.valueOf(OrderStatusEnum.class, bean.getOrderStatus().trim().toUpperCase()).getName());
            row.add(StringUtils.isBlank(bean.getRefundFlag())?"":RefundFlagEnum.valueOf(bean.getRefundFlag()).getName());
            row.add(bean.getUserId());

            row.add(bean.getCreateTime() == null ? "" : simpleDateFormat.format(bean.getCreateTime()));
            row.add(bean.getPayType());
            row.add(bean.getPayTime() == null ? "" : simpleDateFormat.format(bean.getPayTime()));
            row.add(bean.getRemark() == null ? "" : bean.getRemark());
            row.add(bean.getReceUserName());
            row.add(bean.getRecePhone());
            row.add(bean.getAddress());
            row.add(bean.getLogisticsNo());
            row.add(bean.getName());
            row.add(bean.getType() == null || !bean.getType() ? "个人发票" : "公司发票");

            row.add(bean.getTaxName());
            row.add(bean.getContent());
            row.add(bean.getTaxNo());
            row.add(bean.getRegisterAddress());
            row.add(bean.getRegisterPhone());
            row.add(bean.getBank());
            row.add(bean.getAccount());
            row.add(bean.getReceiverEmail());
            row.add(bean.getReceiverPhone());

            row.add(bean.getReceiverName());
            row.add(bean.getReceiverAddress());
            row.add(bean.getCouponName());
            row.add(bean.getTransactionId());
            row.add(Enum.valueOf(OrderShareStatusEnum.class, bean.getShareStatus().trim().toUpperCase()).getName());
            row.add(bean.getWorkUserName());
            row.add(bean.getWebsitName());

            row.add(bean.getSettleTime() == null ? "" : simpleDateFormat.format(bean.getSettleTime()));
            row.add(bean.getPosition());

        }
        excelData.setRows(rows);
        ExcelUtils.exportExcel(request, response, fileName, excelData, -1, null);

    }

    public void updateOrderStatus(String orderId, String orderStatus) {
        orderShareService.lambdaUpdate()
                .set(OrderShare::getOrderStatus, orderStatus)
                .set(orderStatus.equals(OrderStatusEnum.CLOSE.toString()), OrderShare::getStatus, OrderShareStatusEnum.CANCEL.toString())
                .ne(OrderShare::getStatus,OrderShareStatusEnum.OVER.toString())
                .eq(OrderShare::getOrderId, orderId)
                .update();
    }

    public void updateOrderShare(String orderId, String orderStatus) {
        orderShareService.lambdaUpdate()
                .eq(OrderShare::getOrderId, orderId)
                .set(OrderShare::getStatus, orderStatus)
                .set(OrderShare::getSettleTime, new Date())
                .update();
    }

    public void remark(String orderShareId,String remark){
        OrderShare orderShare = orderShareService.getById(orderShareId);
        orderShare.setRemark(remark);
        orderShare.updateById();
    }

    /**
     * 师傅本月的结算数
     */
    public Integer getShareNum(String mobile){
        User user = userService.lambdaQuery()
                .eq(User::getMobile, mobile)
                .eq(User::getCompanyWechatId,Constant.GD_COMPANY_WECHAT_ID).last("limit 1").one();
        if(user == null){
            return 0;
        }
        Date startMonth = DateUtils.getStartMonth();
        Date endMonth = DateUtils.getEndMonth();
        Integer count = orderShareService.lambdaQuery()
                .eq(OrderShare::getOpenId, user.getOpenId())
                .ne(OrderShare::getStatus, OrderShareStatusEnum.CANCEL.toString())
                .between(OrderShare::getCreateTime, startMonth, endMonth)
                .count();
        return count;
    }

    /**
     * 确认分账
     * @param orderId
     * @throws RemoteServiceException
     */
    public void confirm(String orderId) throws RemoteServiceException {
        List<OrderShare> list = orderShareService.lambdaQuery()
                .eq(OrderShare::getOrderId, orderId)
                .eq(OrderShare::getStatus,OrderShareStatusEnum.ING.toString())
                .list();

        OrderInfo orderInfo = orderInfoService.getById(orderId);
        if (orderInfo == null || !orderInfo.getOrderStatus().equals(OrderStatusEnum.OVER.toString())) {
            throw new RemoteServiceException("订单不符合结算条件");
        }
        if(list.size() == 0){
            throw new RemoteServiceException("不存在未结算的信息");
        }
        try {
            if(StringUtils.isBlank(orderInfo.getCompanyWechatId())){
                return;
            }
            //查询是否存在未处理完成的售后单
            Integer count = orderRefundService.lambdaQuery()
                    .eq(OrderRefund::getOrderId,orderId)
                    .in(OrderRefund::getOrderStatus, "DSJCL", "DSJSH", "DMJCL").count();
            if(count > 0){
                throw new RemoteServiceException("订单"+orderId+"存在未完结的维权订单");
            }
            wechatLogic.shareAmount(orderId, orderInfo.getTransactionId(), list, orderInfo.getCompanyWechatId());
            this.updateOrderShare(orderId, OrderShareStatusEnum.OVER.toString());

        } catch (WxPayException e) {
            log.error("异常结算", e);
            this.saveOrderShareRemark(orderId,OrderShareStatusEnum.EXCEPTION.toString(),"异常结算："+e.getErrCodeDes());
            throw new RemoteServiceException("转账支付失败");
        }
    }


    public Boolean confirmAuto(List<OrderShare> orderShares,OrderInfo orderInfo) {
        try {
            if(StringUtils.isBlank(orderInfo.getCompanyWechatId())){
                return false;
            }

            wechatLogic.shareAmount(orderInfo.getOrderId(), orderInfo.getTransactionId(), orderShares,orderInfo.getCompanyWechatId());
            this.updateOrderShare(orderInfo.getOrderId(), OrderShareStatusEnum.OVER.toString());
            return true;
        } catch (WxPayException e) {
            log.error("异常结算", e);
            this.saveOrderShareRemark(orderInfo.getOrderId(),OrderShareStatusEnum.EXCEPTION.toString(),"异常结算："+e.getErrCodeDes());
            return false;
        }

    }

    public void sharingFinish(OrderInfo orderInfo) {
        try {
            if(StringUtils.isBlank(orderInfo.getCompanyWechatId())){
                return;
            }
            wechatLogic.profitSharingFinish(orderInfo);

            this.saveOrderShareFinishRecord(orderInfo.getOrderId(), "");
        } catch (WxPayException e) {
            log.error("完结分账异常结算", e);
            this.saveOrderShareFinishRecord(orderInfo.getOrderId(), e.getErrCodeDes());
        }
    }

    /**
     * 用户总收益
     *
     * @param userId
     * @return
     */
    public BigDecimal totalAmount(String userId) {
        List<OrderShare> list = orderShareService.lambdaQuery().eq(OrderShare::getUserId, userId).list();
        return list.stream().map(OrderShare::getAmount).reduce(BigDecimal::add).orElse(new BigDecimal(0));
    }


    /**
     * 刷新分佣金额
     * @param orderId 订单号
     * @param totalShareAmount 需要扣除的分佣金额
     * @param flag //1=非团购商品  2=团购商品
     */
    public void reloadAmount(String orderId, BigDecimal totalShareAmount,Integer flag) {
        OrderShare orderShare = orderShareService.lambdaQuery()
                .eq(OrderShare::getOrderId, orderId)
                .eq(OrderShare::getFlag,flag).one();
        if(orderShare == null){
            return;
        }
        if(orderShare.getStatus().equals(OrderShareStatusEnum.OVER.toString())){
            return;
        }

        OrderInfo orderInfo = orderInfoService.getById(orderShare.getOrderId());
        orderInfo.setOrderId(orderShare.getOrderId());
        if (totalShareAmount == null || orderShare.getAmount().subtract(totalShareAmount).doubleValue() < 0.01) {
            orderShare.setTotalAmount(BigDecimal.ZERO);
            orderShare.setAmount(BigDecimal.ZERO);
            orderShare.setHandleFees(BigDecimal.ZERO);
            orderShare.setStatus(OrderShareStatusEnum.CANCEL.toString());
            orderInfo.setTotalShareAmount(BigDecimal.valueOf(0));
        } else {
            orderShare.setTotalAmount(orderShare.getTotalAmount().subtract(totalShareAmount));
            orderShare.setAmount(orderShare.getTotalAmount());
            //扣除千分之六的手续费(如果存在手续费则要重新算)
            if(orderShare.getHandleFees().doubleValue() > 0) {
                BigDecimal shareTax = totalShareAmount.multiply(BigDecimal.valueOf(0.006));
                if(shareTax.doubleValue() < 0.01 && shareTax.doubleValue() > 0){
                    shareTax = BigDecimal.valueOf(0.01);
                }
                orderShare.setHandleFees(shareTax);
                orderShare.setAmount(totalShareAmount.subtract(shareTax));
            }

            orderInfo.setTotalShareAmount(orderInfo.getTotalShareAmount().subtract(totalShareAmount));
        }
        orderShare.updateById();
        orderInfo.updateById();


    }



    public List<OrderShare> getOrderShareList(String orderId){
        List<OrderShare> list = orderShareService.lambdaQuery().eq(OrderShare::getOrderId, orderId).list();
        return list;
    }


    /**
     * 记录失败原因
     */
    public void saveOrderShareRemark(String orderId,String orderShareStatus,String remark){
        orderShareService.lambdaUpdate()
                .set(OrderShare::getStatus,orderShareStatus)
                .set(OrderShare::getRemark,remark)
                .eq(OrderShare::getOrderId,orderId)
                .update();
    }

    public void saveOrderShareFinishRecord(String orderId, String remark) {
        OrderShareFinishRecord record = new OrderShareFinishRecord();
        record.setOrderId(orderId)
                .setRemark(remark)
                .insert();
    }


    public IPage<OrderShareVO> pageV2(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> companyWechatIds = adminUser.getCompanyWechatIds();

        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean, OrderShareVO.class);
        IPage<OrderShareVO> orderShareVOIPage = commonPageV2Mapper.pageV2OrderShare(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()),
                zfireParam
                , companyWechatIds);

        return orderShareVOIPage;
    }
}
