package com.sptg.estate.biz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.estate.api.dto.ApplyPayRefundDto;
import com.sptg.estate.api.entity.ApplyPayRefund;
import com.sptg.estate.api.entity.User;
import com.sptg.estate.api.feigns.ApplyPayRefundFeign;
import com.sptg.estate.api.feigns.UserFeign;
import com.sptg.estate.biz.mapper.ApplyPayRefundMapper;
import com.sptg.estate.biz.service.ApplyPayRefundInterface;
import com.sptg.pay.api.entity.PayCertificate;
import com.sptg.pay.api.feigns.PayCertificateFeign;
import com.sptg.pay.api.feigns.WxPayFeign;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.io.FileNotFoundException;
import java.util.Date;
import java.util.Map;

/**
 * @author zwx
 * @create 2020/6/20 17:59
 */
@Service
@AllArgsConstructor
public class ApplyPayRefundServiceImpl extends ServiceImpl<ApplyPayRefundMapper, ApplyPayRefund> implements ApplyPayRefundInterface {

     private final WxPayFeign wxPayFeign;
     public final static String WXPAY = "wxPay";
     private final PayCertificateFeign payCertificateFeign;
     private final UserFeign userFeign;

    @Override
    public ResultBean<Page<ApplyPayRefund>> showApplyPayRefundList(ApplyPayRefundDto applyPayRefundDto) {
        //构建查询条件
        Wrapper<ApplyPayRefund> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotEmpty(applyPayRefundDto.getRefundNo())){
            wrapper.eq("s_refund_no",applyPayRefundDto.getRefundNo());
        }
        if (StringUtils.isNotEmpty(applyPayRefundDto.getPayType())){
            wrapper.eq("s_pay_type",applyPayRefundDto.getPayType());
        }
        if (StringUtils.isNotEmpty(applyPayRefundDto.getRefundStatus())){
            wrapper.eq("i_refund_status",applyPayRefundDto.getRefundStatus());
        }
        //审核状态
        if(applyPayRefundDto.getCheckStatus()!=null){
            wrapper.eq("i_check_status", applyPayRefundDto.getCheckStatus());
        }
        //名字
        if(StringUtils.isNotBlank(applyPayRefundDto.getName())){
            wrapper.eq("s_name",applyPayRefundDto.getName());
        }
        //联系电话
        if(StringUtils.isNotBlank(applyPayRefundDto.getPhone())){
            wrapper.eq("s_phone",applyPayRefundDto.getPhone());
        }
        //身份证
        if(StringUtils.isNotBlank(applyPayRefundDto.getRefundStatus())){
            wrapper.eq("i_refund_status", applyPayRefundDto.getRefundStatus());
        }
        //小区名字
        if(StringUtils.isNotBlank(applyPayRefundDto.getResidentialName())){
            wrapper.eq("s_residential_name", applyPayRefundDto.getResidentialName());
        }
        //根据单元ID查询
        if(StringUtils.isNotEmpty(applyPayRefundDto.getUnitName())){
            wrapper.eq("s_unit_name",applyPayRefundDto.getUnitName());
        }
        //根据单元ID查询
        if(StringUtils.isNotEmpty(applyPayRefundDto.getRoomName())){
            wrapper.eq("s_room_name",applyPayRefundDto.getRoomName());
        }
        if(null != applyPayRefundDto.getStartDateCondition()) {
            wrapper.ge("d_apply_date",applyPayRefundDto.getStartDateCondition());
        }
        if(null != applyPayRefundDto.getEndDateCondition()) {
            wrapper.le("d_apply_date",applyPayRefundDto.getEndDateCondition());
        }
        //  排序字段
        if (applyPayRefundDto.getSortField() != null && applyPayRefundDto.getSortOrder() != null){
            wrapper.orderBy(applyPayRefundDto.getSortField(),applyPayRefundDto.getSortOrder().equals("asc"));
        }else {
            //  排序字段 时间降序
            wrapper.orderBy("i_check_status", false);
//            wrapper.orderBy(true, false, "t_create_date",);
        }
        //  排序字段 审核状态,时间排序
//        wrapper.orderBy("d_apply_date",false);
//        wrapper.orderBy("d_update_date",false);
        return new ResultBean<>(ResultEnum.SUCCESS, this.selectPage(applyPayRefundDto.getPage(), wrapper));
    }

    @Override
    public ResultBean<Boolean> addApplyPayRefund(ApplyPayRefundDto applyPayRefundDto) {
        Long payCertificateId = applyPayRefundDto.getPayCertificateId();
        Long userId = applyPayRefundDto.getUserId();
        PayCertificate payCertificate = payCertificateFeign.findPayCertificateById(payCertificateId);
        User user = userFeign.findUserById(userId);
        Date today = new Date();
        applyPayRefundDto.setPayType(payCertificate.getChannelCode());
        applyPayRefundDto.setActualPayFee(payCertificate.getPayment());
        applyPayRefundDto.setRefundFee(payCertificate.getPayment());
        applyPayRefundDto.setCheckStatus(OperationConstant.ZERO);
        applyPayRefundDto.setApplyDate(today);
        applyPayRefundDto.setUpdateDate(today);
        applyPayRefundDto.setName(user.getName());
        applyPayRefundDto.setPhone(user.getTel());
        applyPayRefundDto.setResidentialName(user.getResidentialName());
        applyPayRefundDto.setUnitName(user.getUnitName());
        applyPayRefundDto.setRoomName(user.getRoomName());
        if(this.insert(applyPayRefundDto)){
            return new ResultBean<>(ResultEnum.SUCCESS);
        }else{
            return new ResultBean<>(ResultEnum.ERROR);
        }
    }

    @Override
    public ApplyPayRefund getApplyPayRefundById(Long id) {
        Wrapper<ApplyPayRefund> wrapper = new EntityWrapper<>();
        wrapper.eq("l_id",id);
        //过滤删除
        wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
        return this.selectOne(wrapper);
    }

    @Override
    public ApplyPayRefund getApplyPayRefundByCondition(ApplyPayRefundDto applyPayRefundDto) {
        Wrapper<ApplyPayRefund> wrapper = new EntityWrapper<>();
        //审核状态
        if(null != applyPayRefundDto.getPayCertificateId()){
            wrapper.eq("l_pay_certificate_id", applyPayRefundDto.getPayCertificateId());
        }
        //名字
        if(null != applyPayRefundDto.getUserId()){
            wrapper.eq("l_user_id", applyPayRefundDto.getUserId());
        }
        //名字
        if(null != applyPayRefundDto.getRefundNo()){
            wrapper.eq("s_refund_no", applyPayRefundDto.getRefundNo());
        }
        if(null != applyPayRefundDto.getOrderId()){
            wrapper.eq("l_order_id", applyPayRefundDto.getOrderId());
        }
        return this.selectOne(wrapper);
    }

    @Override
//    @Transactional(rollbackFor = Exception.class)
    public ResultBean<Boolean> auditApplyPayRefund(ApplyPayRefund applyPayRefund){
        boolean result = this.updateById(applyPayRefund);
        if(result){
            return new ResultBean<>(ResultEnum.SUCCESS);
        }
        /*if(result){
            ApplyPayRefund applyPayRe = this.getApplyPayRefundById(applyPayRefund.getId());
            if(WXPAY.equ
            als(applyPayRe.getPayType())){
                BeanMap beanMap = BeanMap.create(applyPayRe);
                wxPayFeign.refund(beanMap);
                return new ResultBean<>(ResultEnum.SUCCESS);
            }
        }*/
        return new ResultBean<>(ResultEnum.ERROR);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateApplyPayRefundByRefundNo(ApplyPayRefund applyPayRefund){
        Wrapper<ApplyPayRefund> wrapper = new EntityWrapper<>();
        wrapper.eq("s_refund_no", applyPayRefund.getRefundNo());
        Boolean result = this.update(applyPayRefund, wrapper);
        return result;
    }
}
