package com.fdkankan.order.service.impl;

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.exception.BusinessException;
import com.fdkankan.common.util.NumberUtils;
import com.fdkankan.order.entity.ExpansionOrder;
import com.fdkankan.order.mapper.IExpansionOrderMapper;
import com.fdkankan.order.service.IExpansionOrderService;
import com.fdkankan.order.vo.ResponseExpansionOrder;
import com.fdkankan.user.request.RequestCamera;
import com.fdkankan.user.request.RequestExpansionOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
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());
        Integer start = (param.getPageNum() -1 ) * param.getPageSize();
        Integer count = this.expansionOrderMapper.getExpansionCountByParam(param.getUserId(), param.getChildName(), param.getCameraId());
        if(count<=0){
            return pageVo;
        }
        String orderBy = " o.trade_time desc";
        List<ResponseExpansionOrder> list = this.expansionOrderMapper.getExpansionListByParam(param.getUserId(), param.getChildName(), param.getCameraId(), start, param.getPageSize(),orderBy);
        pageVo.setTotal(count);
        pageVo.setRecords(list);
        return pageVo;
    }
}
