package com.sptg.pay.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.entity.User;
import com.sptg.estate.api.feigns.UserFeign;
import com.sptg.pay.api.dto.PayCertificateDto;
import com.sptg.pay.api.entity.PayCertificate;
import com.sptg.pay.api.entity.UserOrder;
import com.sptg.pay.api.feigns.UserOrderFeign;
import com.sptg.pay.biz.mapper.PayCertificateMapper;
import com.sptg.pay.biz.service.PayCertificateServiceInterface;
import lombok.AllArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

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

/**
 * @author zwx
 * @create 2020/5/25 10:21
 */
@Setter
@Service
@AllArgsConstructor
public class PayCertificateServiceImpl extends ServiceImpl<PayCertificateMapper, PayCertificate> implements PayCertificateServiceInterface {

    private UserOrderServiceImpl userOrderService;
    private final UserFeign userFeign;

    @Override
    public ResultBean<Page<PayCertificate>> showPayCertificatePage(PayCertificateDto payCertificateDto) {
        // 构建查询条件
        Wrapper<PayCertificate> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotEmpty(payCertificateDto.getOrderNo())){
            wrapper.eq("s_order_no",payCertificateDto.getOrderNo());
        }
        if (null != payCertificateDto.getPayStatus()){
            wrapper.eq("i_pay_status",payCertificateDto.getPayStatus());
        }
        if (StringUtils.isNotEmpty(payCertificateDto.getChannelCode())){
            wrapper.eq("s_channel_code",payCertificateDto.getChannelCode());
        }
        if (null != payCertificateDto.getRefund()){
            wrapper.eq("i_refund",payCertificateDto.getRefund());
        }
        if(null != payCertificateDto.getStartDateCondition()) {
            wrapper.ge("t_create_date",payCertificateDto.getStartDateCondition());
        }
        if(null != payCertificateDto.getEndDateCondition()) {
            wrapper.le("t_create_date",payCertificateDto.getEndDateCondition());
        }
        //过滤删除
        wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
        //  排序字段
        if (payCertificateDto.getSortField() != null && payCertificateDto.getSortOrder() != null){
            wrapper.orderBy(payCertificateDto.getSortField(),payCertificateDto.getSortOrder().equals("asc"));
        }else {
            //  排序字段 时间降序
            wrapper.orderBy("t_create_date",false);
        }
        return new ResultBean<>(ResultEnum.SUCCESS, this.selectPage(payCertificateDto.getPage(), wrapper));
    }

    @Override
    public ResultBean<Boolean> addPayCertificate(PayCertificateDto payCertificateDto) {
        Assert.isNull(payCertificateDto.getId(),"支付凭证已存在，请勿重复添加");
        Date today = new Date();
        payCertificateDto.setCreateDate(today);
        payCertificateDto.setUpdateDate(today);
        payCertificateDto.setPayStatus(OperationConstant.ZERO);
        boolean result = this.insert(payCertificateDto);
        if(result){
            return new ResultBean<>(ResultEnum.SUCCESS, true);
        }
        return new ResultBean<>(ResultEnum.ERROR, false);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public PayCertificate addPayCertificateByImport(PayCertificateDto payCertificateDto) {
        Assert.isNull(payCertificateDto.getId(),"支付凭证已存在，请勿重复添加");
        PayCertificate payCertificate = new PayCertificate();
        BeanUtils.copyProperties(payCertificateDto,payCertificate);
        Date today = new Date();
        payCertificate.setCreateDate(today);
        payCertificate.setUpdateDate(today);
        //oldOrderId => orderId and orderNo
        if (StringUtils.isNotEmpty(payCertificateDto.getOldOrderId())){
            UserOrder userOrder = this.userOrderService.getByOldId(payCertificateDto.getOldOrderId());
            payCertificate.setOrderId(userOrder.getId());
            payCertificate.setOrderNo(userOrder.getBizOrderNo());
        }
        //oldUserId => userId
        if (StringUtils.isNotEmpty(payCertificateDto.getOldUserId())){
            User user = this.userFeign.getByOldId(payCertificateDto.getOldUserId());
            payCertificate.setUserId(user.getId());
        }
        boolean result = this.insert(payCertificate);
        if(result){
            return payCertificate;
        }else {
            return null;
        }
    }

    @Override
    public ResultBean<Boolean> updatePayCertificate(PayCertificate payCertificate) {
        boolean result = this.updateById(payCertificate);
        if(result){
            return new ResultBean<>(ResultEnum.SUCCESS,result);
        }
        return new ResultBean<>(ResultEnum.ERROR,result);
    }

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

    @Override
    public PayCertificate findPayCertificateByCondition(PayCertificateDto payCertificateDto) {
        Wrapper<PayCertificate> wrapper = new EntityWrapper<>();
        //过滤删除
        wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
        return this.baseMapper.findPayCertificateByCondition(payCertificateDto, wrapper);
    }

    @Override
    public List<PayCertificate> findPayCertificateListByCondition(PayCertificateDto payCertificateDto) {
        Wrapper<PayCertificate> wrapper = new EntityWrapper<>();
        if(payCertificateDto.getUserId()!=null){
            wrapper.eq("l_user_id", payCertificateDto.getUserId());
        }
        if(payCertificateDto.getPayStatus() != null){
            wrapper.eq("i_pay_status",payCertificateDto.getPayStatus());
        }
        //过滤删除
        wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
        wrapper.orderDesc(Arrays.asList("t_create_date"));
        return this.selectList(wrapper);
    }
}
