package com.hai.service.impl;

import com.hai.common.utils.DateUtil;
import com.hai.dao.HighRefundAuditMapper;
import com.hai.entity.*;
import com.hai.model.UserInfoModel;
import com.hai.service.HighOrderPreService;
import com.hai.service.HighOrderService;
import com.hai.service.HighRefundAuditService;
import com.hai.service.OutRechargeOrderService;
import io.swagger.models.auth.In;
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("highRefundAuditService")
public class HighRefundAuditServiceImpl implements HighRefundAuditService {


    @Resource
    private HighRefundAuditMapper highRefundAuditMapper;

    @Resource
    private OutRechargeOrderService outRechargeOrderService;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private HighOrderPreService highOrderPreService;

    @Override
    public List<HighRefundAudit> getListRefundAudit(Map<String, String> map) {

        HighRefundAuditExample example  = new HighRefundAuditExample();
        HighRefundAuditExample.Criteria criteria = example.createCriteria();

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

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

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

        if (StringUtils.isNotBlank(map.get("payTimeS")) && StringUtils.isNotBlank(map.get("payTimeE"))) {
            criteria.andPayTimeBetween(
                    DateUtil.format(map.get("payTimeS") , "yyyy-MM-dd HH:mm:ss") ,
                    DateUtil.format(map.get("payTimeE") , "yyyy-MM-dd HH:mm:ss"));
        }

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

        example.setOrderByClause("created_time desc");

        return highRefundAuditMapper.selectByExample(example);
    }

    @Override
    public HighRefundAudit getRefundAuditById(Integer id) {
        return highRefundAuditMapper.selectByPrimaryKey(id);
    }

    @Override
    public HighRefundAudit findByOrderNo(String orderNo) {
        HighRefundAuditExample example  = new HighRefundAuditExample();
        example.createCriteria().andSourceOrderNoEqualTo(orderNo);
        return highRefundAuditMapper.selectByExample(example).get(0);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void insertRefundAudit(HighRefundAudit highRefundAudit , UserInfoModel userInfoModel) {

        if (highRefundAudit.getRefundSource() == 1) {
            OutRechargeOrder outRechargeOrder = outRechargeOrderService.findByOrderNo(highRefundAudit.getSourceOrderNo());
//            highRefundAudit.setRefundPrice(outRechargeOrder.getPayPrice());
            highRefundAudit.setPayType(outRechargeOrder.getPayType());
            highRefundAudit.setPayTime(outRechargeOrder.getPayTime());
            highRefundAudit.setExt1(String.valueOf(outRechargeOrder.getId()));
//            outRechargeOrder.setStatus(6);
            outRechargeOrderService.updateOrder(outRechargeOrder);
        } else {
            HighOrder highOrder = highOrderService.getOrderByOrderNo(highRefundAudit.getSourceOrderNo());
            highRefundAudit.setRefundPrice(highOrder.getPayPrice());
            highRefundAudit.setPayType(highOrder.getPayType());
            highRefundAudit.setPayTime(highOrder.getPayTime());
            highRefundAudit.setExt1(String.valueOf(highOrder.getId()));
            highOrder.setOrderStatus(6);

            HighOrderPre highOrderPre = highOrderPreService.findByOrderNo(highRefundAudit.getSourceOrderNo());
            highOrderPre.setExt1("退款中");

            highOrderService.updateOrder(highOrder);
            highOrderPreService.updateOrderPre(highOrderPre);
        }

        highRefundAudit.setCompanId(userInfoModel.getBsCompany().getId().intValue());
        highRefundAudit.setStatus(0);
        highRefundAudit.setCreatedTime(new Date());
        highRefundAudit.setCreatedUserId(userInfoModel.getSecUser().getId().intValue());
        highRefundAudit.setCreatedUserName(userInfoModel.getSecUser().getUserName());


        highRefundAuditMapper.insert(highRefundAudit);
    }

    @Override
    public void updateRefundAudit(HighRefundAudit highRefundAudit) {
         highRefundAuditMapper.updateByPrimaryKey(highRefundAudit);
    }


}
