package com.tmerclub.cloud.cuerecycle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.cuerecycle.constant.CueOrderStatus;
import com.tmerclub.cloud.cuerecycle.model.dto.CueOrderDTO;
import com.tmerclub.cloud.cuerecycle.model.vo.CueOrderVO;
import com.tmerclub.cloud.cuerecycle.model.CueOrder;
import com.tmerclub.cloud.cuerecycle.mapper.CueOrderMapper;
import com.tmerclub.cloud.cuerecycle.service.CueOrderService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 回收订单Service实现类
 *
 * @author : frank
 * @create : 2025-04-15
 */
@Service
public class CueOrderServiceImpl implements CueOrderService {

    @Resource
    CueOrderMapper cueOrderMapper;

    @Override
    public PageVO<CueOrderVO> page(PageDTO pageDTO, CueOrderDTO cueOrderDTO) {
        return PageUtil.doPage(pageDTO, () -> cueOrderMapper.list(cueOrderDTO));
    }

    @Override
    public CueOrderVO getById(Long orderId) {
        return cueOrderMapper.getById(orderId);
    }

    @Override
    public int save(CueOrderDTO cueOrderDTO) {
        CueOrder cueOrder = BeanUtil.toBean(cueOrderDTO, CueOrder.class);
        cueOrder.setCreateTime(DateUtil.date());
        return cueOrderMapper.save(cueOrder);
    }

    @Override
    public int updateAuditStatus(CueOrderDTO cueOrderDTO) {
        CueOrder cueOrder = new CueOrder();
        cueOrder.setOrderId(cueOrderDTO.getOrderId());
        cueOrder.setOrderStatus(cueOrderDTO.getOrderStatus());
        cueOrder.setUpdateTime(DateUtil.date());
        return cueOrderMapper.update(cueOrder);
    }

    @Override
    public int updateEvaluateStatus(CueOrderDTO cueOrderDTO) {
        CueOrder cueOrder = new CueOrder();
        cueOrder.setOrderId(cueOrderDTO.getOrderId());
        cueOrder.setOrderStatus(cueOrderDTO.getOrderStatus());
        if(Objects.equals(cueOrderDTO.getOrderStatus(), CueOrderStatus.EVALUATION_SUCCESS.value())){
            cueOrder.setActualAmount(cueOrderDTO.getActualAmount());
            cueOrder.setVoucherAmount(cueOrderDTO.getVoucherAmount());
        }
        cueOrder.setUpdateTime(DateUtil.date());
        return cueOrderMapper.update(cueOrder);
    }

    @Override
    public int delete(Long id) {
        CueOrderVO cueOrderVO = cueOrderMapper.getById(id);
        CueOrder cueOrder = BeanUtil.toBean(cueOrderVO, CueOrder.class);
        cueOrder.setUpdateTime(DateUtil.date());
        cueOrder.setDeleted(1);
        return cueOrderMapper.update(cueOrder);
    }
}