package com.bonus.zlpt.order.service.impl;

import java.util.List;

import com.bonus.zlpt.common.core.domain.order.dto.FileInfoDto;
import com.bonus.zlpt.common.core.domain.order.vo.OrderInfoVo;
import com.bonus.zlpt.common.core.utils.StringUtils;
import com.bonus.zlpt.common.security.utils.SecurityUtils;
import com.bonus.zlpt.order.mapper.OrderDetailsMapper;
import com.bonus.zlpt.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.zlpt.order.mapper.OrderInfoMapper;
import com.bonus.zlpt.common.core.domain.order.OrderInfo;
import com.bonus.zlpt.order.service.IOrderInfoService;

/**
 * 订单信息Service业务层处理
 * 
 * @author xsheng
 * @date 2023-12-01
 */
@Service
public class OrderInfoServiceImpl implements IOrderInfoService 
{
    private final Integer ORDER_ATTACHMENT_DIC_ID = 21;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    OrderDetailsMapper orderDetailsMapper;

    /**
     * 查询订单信息
     * 
     * @param orderId 订单信息主键
     * @return 订单信息
     */
    @Override
    public OrderInfoVo selectOrderInfoByOrderId(Long orderId)
    {
        return orderInfoMapper.selectOrderInfoByOrderId(orderId);
    }

    /**
     * 查询订单信息列表
     * 
     * @param orderInfo 订单信息
     * @return 订单信息
     */
    @Override
    public List<OrderInfoVo> selectOrderInfoList(OrderInfoVo orderInfo)
    {
        return orderInfoMapper.selectOrderInfoList(orderInfo);
    }

    /**
     * 新增订单信息
     * 
     * @param orderInfo 订单信息
     * @return 结果
     */
    @Override
    public int insertOrderInfo(OrderInfoVo orderInfo)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        orderInfo.setOrderUser(user.getUserid());
        orderInfoMapper.insertOrderInfo(orderInfo);
        Long orderId = orderInfoMapper.selectLastInsertId();
        orderInfo.setOrderId(orderId);
        return orderInfoMapper.insertOrderDetails(orderInfo);
    }

    /**
     * 修改订单信息
     * 
     * @param orderInfo 订单信息
     * @return 结果
     */
    @Override
    public int updateOrderInfo(OrderInfoVo orderInfo)
    {
        if (StringUtils.isNotEmpty(orderInfo.getContractUrl())) {
            FileInfoDto fileInfoDto = new FileInfoDto();
            fileInfoDto.setModelId(Math.toIntExact(orderInfo.getOrderId()));
            fileInfoDto.setFileUrl(orderInfo.getContractUrl());
            fileInfoDto.setDicId(ORDER_ATTACHMENT_DIC_ID);
            LoginUser user = SecurityUtils.getLoginUser();
            fileInfoDto.setCreator(String.valueOf(user.getUserid()));
            orderDetailsMapper.deleteAttachment(fileInfoDto);
            orderDetailsMapper.insertAttachment(fileInfoDto);
        }
        orderInfoMapper.updateOrderDetails(orderInfo);
//        LoginUser user = SecurityUtils.getLoginUser();
        return orderInfoMapper.updateOrderInfo(orderInfo);
    }

    @Override
    public int rejectOrderInfo(OrderInfoVo orderInfo)
    {
        return orderInfoMapper.updateOrderInfo(orderInfo);
    }

    /**
     * 批量删除订单信息
     * 
     * @param orderIds 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderInfoByOrderIds(Long[] orderIds)
    {
        return orderInfoMapper.deleteOrderInfoByOrderIds(orderIds);
    }

    /**
     * 删除订单信息信息
     * 
     * @param orderId 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderInfoByOrderId(Long orderId)
    {
        return orderInfoMapper.deleteOrderInfoByOrderId(orderId);
    }

    @Override
    public long count()
    {
        LoginUser user = SecurityUtils.getLoginUser();
        return orderInfoMapper.count(user.getUserid());
    }
}
