package com.ruoyi.frequency.orderchangerecord.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.page.PageResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.controller.req.OrderChangeRecordPageReq;
import com.ruoyi.frequency.orderchangerecord.entity.OrderChangeRecord;
import com.ruoyi.frequency.orderchangerecord.mapper.OrderChangeRecordMapper;
import com.ruoyi.frequency.orderchangerecord.service.OrderChangeRecordService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单改价记录Service业务层处理
 *
 * @author zj
 * @date 2023-10-13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrderChangeRecordServiceImpl extends ServiceImpl<OrderChangeRecordMapper, OrderChangeRecord> implements OrderChangeRecordService {
    @Autowired
    private OrderChangeRecordMapper orderChangeRecordMapper;

    /**
     * 查询订单改价记录
     *
     * @param id 订单改价记录主键
     * @return 订单改价记录
     */
    @Override
    public OrderChangeRecord selectOrderChangeRecordById(Long id) {
        return orderChangeRecordMapper.selectOrderChangeRecordById(id);
    }

    /**
     * 查询订单改价记录列表
     *
     * @param orderChangeRecord 订单改价记录
     * @return 订单改价记录
     */
    @Override
    public List<OrderChangeRecord> selectOrderChangeRecordList(OrderChangeRecord orderChangeRecord) {

        List<OrderChangeRecord> orderChangeRecords = orderChangeRecordMapper.selectOrderChangeRecordList(orderChangeRecord);

        return orderChangeRecords;
    }

    /**
     * 新增订单改价记录
     *
     * @param orderChangeRecord 订单改价记录
     * @return 结果
     */
    @Override
    public int insertOrderChangeRecord(OrderChangeRecord orderChangeRecord) {
        return orderChangeRecordMapper.insertOrderChangeRecord(orderChangeRecord);
    }

    /**
     * 修改订单改价记录
     *
     * @param orderChangeRecord 订单改价记录
     * @return 结果
     */
    @Override
    public int updateOrderChangeRecord(OrderChangeRecord orderChangeRecord) {
        orderChangeRecord.setUpdateTime(DateUtils.getNowDate());
        return orderChangeRecordMapper.updateOrderChangeRecord(orderChangeRecord);
    }

    /**
     * 批量删除订单改价记录
     *
     * @param ids 需要删除的订单改价记录主键
     * @return 结果
     */
    @Override
    public int deleteOrderChangeRecordByIds(Long[] ids) {
        this.lambdaUpdate().set(OrderChangeRecord::getDelFlag, PublicCommon.删除).in(OrderChangeRecord::getId, ids).update();
        return ids.length;
    }

    /**
     * 删除订单改价记录信息
     *
     * @param id 订单改价记录主键
     * @return 结果
     */
    @Override
    public int deleteOrderChangeRecordById(Long id) {
        this.lambdaUpdate().set(OrderChangeRecord::getDelFlag, PublicCommon.删除).eq(OrderChangeRecord::getId, id).update();
        return 1;
    }

    @Override
    public ResponseData<PageResult<Page<OrderChangeRecord>>> orderChangeRecordPage(OrderChangeRecordPageReq req, UserVo userVo) {

        Page<OrderChangeRecord> page = this.lambdaQuery().eq(OrderChangeRecord::getDelFlag, PublicCommon.启用)
                .eq(OrderChangeRecord::getOrderId, req.getOrderId())
                .ne(OrderChangeRecord::getStatus, PublicCommon.OrderChange.待确认)
                .orderByDesc(OrderChangeRecord::getCreateTime).page(new Page<>(req.getPageNo(), req.getPageSize()));
        return ResponseData.success(new PageResult<>(page));
    }
}
