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

import java.util.List;
import java.util.Random;
import com.bonus.zlpt.common.core.domain.order.dto.ContractInfoDto;
import com.bonus.zlpt.common.core.domain.order.dto.FileInfoDto;
import com.bonus.zlpt.common.core.domain.order.vo.OrderDetailsVo;
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.OrderInfoMapper;
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.OrderDetailsMapper;
import com.bonus.zlpt.common.core.domain.order.OrderDetails;
import com.bonus.zlpt.order.service.IOrderDetailsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单详细Service业务层处理
 * 
 * @author xsheng
 * @date 2023-12-01
 */
@Service
public class OrderDetailsServiceImpl implements IOrderDetailsService 
{
    private final Integer ORDER_ATTACHMENT_DIC_ID = 21;

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    /**
     * 查询订单详细
     * 
     * @param id 订单详细主键
     * @return 订单详细
     */
    @Override
    public OrderDetailsVo selectOrderDetailsById(Long id)
    {
        return orderDetailsMapper.selectOrderDetailsById(id);
    }

    /**
     * 查询订单详细列表
     * 
     * @param orderDetails 订单详细
     * @return 订单详细
     */
    @Override
    public List<OrderDetails> selectOrderDetailsList(OrderDetails orderDetails)
    {
        return orderDetailsMapper.selectOrderDetailsList(orderDetails);
    }

    /**
     * 新增订单详细
     * 
     * @param orderDetails 订单详细
     * @return 结果
     */
    @Override
    public int insertOrderDetails(OrderDetailsVo orderDetails)
    {
        Random random = new Random();
        long orderId = random.nextInt(99999999) + 1;
        orderDetails.setOrderId(orderId);
        orderDetailsMapper.insertOrderDetails(orderDetails);
        //add orderInfo
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        orderInfoVo.setOrderId(orderId);
        orderInfoVo.setOrderStatus(orderDetails.getOrderStatus());
        orderInfoVo.setPayType(orderDetails.getPayType());
        return orderInfoMapper.insertOrderInfo(orderInfoVo);
    }

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

    /**
     * 批量删除订单详细
     * 
     * @param ids 需要删除的订单详细主键
     * @return 结果
     */
    @Override
    public int deleteOrderDetailsByIds(Long[] ids)
    {
        return orderDetailsMapper.deleteOrderDetailsByIds(ids);
    }

    @Override
    @Transactional
    public int addContract(ContractInfoDto dto) {
        dto.getFileInfoDto().setModelId(dto.getContractDto().getOrderId());
        dto.getFileInfoDto().setDicId(ORDER_ATTACHMENT_DIC_ID);
        LoginUser user = SecurityUtils.getLoginUser();
        dto.getFileInfoDto().setCreator(String.valueOf(user.getUserid()));
        return orderDetailsMapper.insertAttachment(dto.getFileInfoDto());
    }

    /**
     * 删除订单详细信息
     * 
     * @param id 订单详细主键
     * @return 结果
     */
    @Override
    public int deleteOrderDetailsById(Long id)
    {
        return orderDetailsMapper.deleteOrderDetailsById(id);
    }
}
