package com.fdkankan.platform.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.util.NumberUtils;
import com.fdkankan.platform.order.entity.ExpansionOrder;
import com.fdkankan.platform.order.mapper.IExpansionOrderMapper;
import com.fdkankan.platform.order.service.IExpansionOrderService;
import com.fdkankan.platform.order.vo.ResponseExpansionOrder;
import com.fdkankan.platform.user.request.RequestCamera;
import com.fdkankan.platform.user.request.RequestExpansionOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 扩容订单表（八目相机） 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-12-24
 */
@Service
public class ExpansionOrderServiceImpl extends ServiceImpl<IExpansionOrderMapper, ExpansionOrder> implements IExpansionOrderService {

    @Resource
    private IExpansionOrderMapper expansionOrderMapper;

    @Override
    public ResponseExpansionOrder saveByOrderParam(Long userId, RequestExpansionOrder order, BigDecimal price, String unit, int unitSize, int month, int delay) {
        BigDecimal total = price.multiply(new BigDecimal(order.getUnitSize())).multiply(new BigDecimal(order.getYears()));
        ExpansionOrder expansionOrderEntity = new ExpansionOrder();
        expansionOrderEntity.setOrderSn(NumberUtils.getOrderSn());
        expansionOrderEntity.setAmount(total);
        expansionOrderEntity.setPayType(order.getPayType());
        expansionOrderEntity.setPayStatus(-1);
        expansionOrderEntity.setStatus(-1);
        expansionOrderEntity.setUserId(userId);
        expansionOrderEntity.setCameraId(order.getCameraId());
        expansionOrderEntity.setYears(0);
        expansionOrderEntity.setMonth(month);
        expansionOrderEntity.setDelay(delay);
        expansionOrderEntity.setUnitSize(unitSize);
        expansionOrderEntity.setUnit(unit);
        expansionOrderEntity.setAbroad(order.getAbroad());
        if(!this.save(expansionOrderEntity)){
            throw new BusinessException(ErrorCode.ERROR_MSG);
        }
        ResponseExpansionOrder expansionOrder = new ResponseExpansionOrder();
        BeanUtils.copyProperties(expansionOrderEntity, expansionOrder);
        return expansionOrder;
    }

    @Override
    public Page<ResponseExpansionOrder> getExpansionListByParam(RequestCamera param) {
        Page<ResponseExpansionOrder> pageVo = new Page<>(param.getPageNum(),param.getPageSize());
        param.setOrderBy(" o.trade_time desc");
        return this.expansionOrderMapper.getExpansionListByParam(pageVo,param);
    }

    @Override
    public ExpansionOrder getByOrderSn(String orderSn) {
        QueryWrapper<ExpansionOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ExpansionOrder::getTbStatus, TbStatus.VALID.code());
        queryWrapper.lambda().eq(ExpansionOrder::getOrderSn,orderSn);
        List<ExpansionOrder> list = this.list(queryWrapper);
        if(list == null || list.size()<=0){
            return null;
        }
        return list.get(0);
    }

    @Override
    public boolean paySuccessExpansionOrder(String orderSn, String tradeNo, int paymentTypeName, int status) {
        LambdaUpdateWrapper<ExpansionOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ExpansionOrder::getPayType,paymentTypeName)
                .set(ExpansionOrder::getNumber,tradeNo)
                .set(ExpansionOrder::getStatus, status)
                .set(ExpansionOrder::getPayStatus, 1)
                .set(ExpansionOrder::getTradeTime, new Date())
                .eq(ExpansionOrder::getTbStatus,TbStatus.VALID.code())
                .eq(ExpansionOrder::getOrderSn,orderSn);
        return this.update(updateWrapper);
    }
}
