package com.hai.service.impl;

import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.utils.DateUtil;
import com.hai.config.WxOrderConfig;
import com.hai.dao.HighOrderMapper;
import com.hai.dao.HighOrderPreMapper;
import com.hai.entity.HighChildOrder;
import com.hai.entity.HighOrder;
import com.hai.entity.HighOrderPre;
import com.hai.entity.HighOrderPreExample;
import com.hai.model.HighOrderPreModel;
import com.hai.model.OrderRefundModel;
import com.hai.service.HighOrderPreService;
import com.hai.service.HighOrderService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

@Service("highOrderPreService")
public class HighOrderPreServiceImpl implements HighOrderPreService {

    @Resource
    private HighOrderPreMapper highOrderPreMapper;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private HighOrderMapper highOrderMapper;

    @Resource
    private HighOrderPreService highOrderPreService;

    @Override
    public List<HighOrderPre> getListOrderPre(Map<String, Object> map) {
        HighOrderPreExample example  = new HighOrderPreExample();
        HighOrderPreExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "preUserId") != null) {
            criteria.andPreUserIdEqualTo(MapUtils.getLong(map, "preUserId"));
        }

        if (MapUtils.getInteger(map, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
        }

        if (MapUtils.getInteger(map, "companyId") != null) {
            criteria.andCompanyIdEqualTo(MapUtils.getLong(map, "companyId"));
        }

        if (MapUtils.getInteger(map, "merchantId") != null) {
            criteria.andMerchantIdEqualTo(MapUtils.getLong(map, "merchantId"));
        }

        if (MapUtils.getInteger(map, "merchantStoreId") != null) {
            criteria.andMerchantStoreIdEqualTo(MapUtils.getLong(map, "merchantStoreId"));
        }

        if (MapUtils.getString(map, "orderNo") != null) {
            criteria.andOrderNoLike("%" + MapUtils.getString(map, "orderNo") + "%");
        }

        if (MapUtils.getString(map, "preOrderNo") != null) {
            criteria.andPreOrderNoLike("%" + MapUtils.getString(map, "preOrderNo") + "%");
        }

        if (MapUtils.getString(map, "goodsName") != null) {
            criteria.andGoodsNameLike("%" + MapUtils.getString(map, "goodsName") + "%");
        }

        if (MapUtils.getString(map, "userPhone") != null) {
            criteria.andPreUserPhoneEqualTo(MapUtils.getString(map, "userPhone"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map,"createTimeS"))
                && StringUtils.isNotBlank(MapUtils.getString(map,"createTimeE"))) {
            criteria.andCreateTimeBetween(
                    DateUtil.format(MapUtils.getString(map,"createTimeS") , "yyyy-MM-dd HH:mm:ss") ,
                    DateUtil.format(MapUtils.getString(map,"createTimeE") , "yyyy-MM-dd HH:mm:ss"));
        }

        if (MapUtils.getString(map, "preOrderNo") != null) {
            criteria.andPreOrderNoLike("%" + MapUtils.getString(map, "preOrderNo") + "%");
        }

        if (MapUtils.getString(map, "preUserName") != null) {
            criteria.andPreUserNameLike("%" + MapUtils.getString(map, "preUserName") + "%");
        }

        example.setOrderByClause("create_time desc");

        return highOrderPreMapper.selectByExample(example);
    }

    @Override
    public List<HighOrderPreModel> ExportPreOrderList(Map<String, Object> map) {
        return highOrderMapper.ExportPreOrderList(map);
    }

    @Override
    public HighOrderPre findByOrderId(Long orderId) {
        return highOrderPreMapper.selectByPrimaryKey(orderId);
    }

    @Override
    public HighOrderPre getPreByOrderId(Long orderId) {
        HighOrderPreExample example = new HighOrderPreExample();
        example.createCriteria().andOrderIdEqualTo(orderId);
        List<HighOrderPre> list = highOrderPreMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public HighOrderPre findByOrderNo(String orderNo) {
        HighOrderPreExample example = new HighOrderPreExample();
        example.createCriteria().andOrderNoEqualTo(orderNo);
        List<HighOrderPre> list = highOrderPreMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public HighOrderPre findByRefund(String orderNo) {
        HighOrderPreExample example = new HighOrderPreExample();
        example.createCriteria().andOrderNoEqualTo(orderNo).andExt1EqualTo("退款中");
        List<HighOrderPre> list = highOrderPreMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void insertOrderPre(HighOrderPre highOrderPre) {
        highOrderPre.setCreateTime(new Date());
        highOrderPre.setUpdateTime(new Date());
        highOrderPreMapper.insert(highOrderPre);
    }

    @Override
    public void updateOrderPre(HighOrderPre highOrderPre) {
        highOrderPreMapper.updateByPrimaryKey(highOrderPre);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void orderComplete(Long preOrderId) {
        // 查询预约订单详情
        HighOrderPre orderPre = findByOrderId(preOrderId);
        if (orderPre == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到预约订单");
        }
        if (orderPre.getStatus() != 2) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.STATUS_ERROR, "");
        }
        orderPre.setStatus(3);
        orderPre.setUpdateTime(new Date());
        updateOrderPre(orderPre);

        highOrderService.childOrderComplete(orderPre.getChildOrderId());
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void orderToRefund(HighChildOrder highChildOrder, HighOrder highOrder, HighOrderPre highOrderPre) throws Exception {
        // 支付方式： 1：支付宝  2：微信  3：金币  4：汇联通工会卡  5：银联
        if (highOrder.getPayType() == 2) {
            // 微信退款
            OrderRefundModel orderRefundModel = WxOrderConfig.orderToRefund(highOrder.getPaySerialNo(),highOrder.getPayRealPrice(), highOrder.getPayRealPrice());
            if(orderRefundModel.getResult_code().equals("SUCCESS")) {
                highChildOrder.setChildOrdeStatus(5);
                highOrder.setOrderStatus(5);
                highOrder.setRefundTime(new Date());
                highOrder.setRefundPrice(highOrder.getPayRealPrice());
                highOrderService.updateOrderDetail(highOrder);
                highOrderService.updateChildOrder(highChildOrder);
                highOrderPre.setExt1("退款成功");
                highOrderPreService.updateOrderPre(highOrderPre);

            }
        }
    }
}
