package cn.pluss.platform.device.impl;

import cn.pluss.platform.device.DeviceOrderInfoService;
import cn.pluss.platform.deviceDetail.DeviceDetailService;
import cn.pluss.platform.dto.DeviceMerchantBuyDTO;
import cn.pluss.platform.entity.DeviceDetail;
import cn.pluss.platform.entity.DeviceOrderInfo;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.DeviceOrderInfoMapper;
import cn.pluss.platform.userApp.UserAppService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 设备商城，订单记录表 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2020-11-13
 */
@Slf4j
@Service
public class DeviceOrderInfoServiceImpl extends ServiceImpl<DeviceOrderInfoMapper, DeviceOrderInfo> implements DeviceOrderInfoService {

    @Setter(onMethod_ = {@Autowired})
    private DeviceDetailService deviceDetailService;
    @Setter(onMethod_ = {@Autowired})
    private UserAppService userAppService;

    @Override
    public void createOrder(String orderNo, DeviceMerchantBuyDTO deviceMerchantBuyDTO) {
        DeviceDetail deviceDetail = new DeviceDetail();
        deviceDetail.setVenderModel(deviceMerchantBuyDTO.getVenderModel());
        deviceDetail.setCode(deviceMerchantBuyDTO.getCode());
        deviceDetail.setType(deviceMerchantBuyDTO.getType());

        List<DeviceDetail> list = deviceDetailService.list(new QueryWrapper<>(deviceDetail));

        if (list.size() == 0) {
            throw new MsgException("无法找到对应的设备信息");
        }

        if (list.size() > 1) {
            throw new MsgException("无法确认唯一设备");
        }

        deviceDetail = list.get(0);

        UserApp userApp = userAppService.queryUserAppByToken();

        DeviceOrderInfo deviceOrderInfo = new DeviceOrderInfo();
        BeanUtils.copyProperties(deviceDetail, deviceOrderInfo);
        deviceOrderInfo.setSumPrice(BigDecimal.valueOf(deviceMerchantBuyDTO.getOrderAmount()));
        deviceOrderInfo.setNum(deviceMerchantBuyDTO.getQuantity());
        deviceOrderInfo.setDeviceType(deviceDetail.getType());
        deviceOrderInfo.setStatus("0");
        deviceOrderInfo.setUserId(userApp.getUserId().toString());
        deviceOrderInfo.setOrderNo(orderNo);
        deviceOrderInfo.setOrderType(deviceMerchantBuyDTO.getOrderType());
        deviceOrderInfo.setCreateTime(new Date());
        deviceOrderInfo.setUpdateTime(new Date());

        save(deviceOrderInfo);
    }

    /**
     * 根据订单号查询设备订单信息
     * @param orderNo
     * @return
     */
    @Override
    public DeviceOrderInfo getDeviceInfoByOrderNo(String orderNo) {
        return baseMapper.getDeviceInfoByOrderNo(orderNo);
    }
}
