package com.xiaoniu.system.usedcar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoniu.common.core.constant.Constants;
import com.xiaoniu.common.core.resdata.RowsData;
import com.xiaoniu.common.core.utils.StringUtils;
import com.xiaoniu.common.core.utils.bean.BeanUtils;
import com.xiaoniu.system.usedcar.domain.TbUsedCar;
import com.xiaoniu.system.usedcar.dto.EditUsedCarDTO;
import com.xiaoniu.system.usedcar.dto.UsedCarQueryDTO;
import com.xiaoniu.system.usedcar.mapper.TbUsedCarMapper;
import com.xiaoniu.system.usedcar.service.ITbUsedCarService;
import com.xiaoniu.system.usedcar.vo.UsedCarDetailsVO;
import com.xiaoniu.system.usedcar.vo.UsedCarListVO;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 二手车信息表 服务实现类
 * </p>
 *
 * @author haoxuyang
 * @since 2024-10-18 05:07:14
 */
@Service
public class TbUsedCarServiceImpl extends ServiceImpl<TbUsedCarMapper, TbUsedCar> implements ITbUsedCarService {

    @Override
    public RowsData<UsedCarListVO> usedCarList(UsedCarQueryDTO dto) {
        try (Page<Object> page = PageHelper.startPage(dto.getPageNum(), dto.getPageSize())) {
            LambdaQueryWrapper<TbUsedCar> wrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotEmpty(dto.getServiceOrderNumber())) {
                wrapper.like(TbUsedCar::getServiceOrderNumber, dto.getServiceOrderNumber());
            }
            if (StringUtils.isNotNull(dto.getServiceTypeId())) {
                wrapper.eq(TbUsedCar::getServiceTypeId, dto.getServiceTypeId());
            }
            if (StringUtils.isNotNull(dto.getServiceStatus())) {
                if (!Constants.ZERO.equals(dto.getServiceStatus())) {
                    wrapper.eq(TbUsedCar::getServiceStatus, dto.getServiceStatus());
                }
            }
            if (StringUtils.isNotEmpty(dto.getProvince())) {
                wrapper.eq(TbUsedCar::getProvince, dto.getProvince());
            }
            if (StringUtils.isNotEmpty(dto.getCity())) {
                wrapper.eq(TbUsedCar::getCity, dto.getCity());
            }
            if (StringUtils.isNotEmpty(dto.getArea())) {
                wrapper.eq(TbUsedCar::getArea, dto.getArea());
            }
            if (StringUtils.isNotEmpty(dto.getBeginTime())) {
                wrapper.gt(TbUsedCar::getCreateTime, dto.getBeginTime() + " 00:00:01");
            }
            if (StringUtils.isNotEmpty(dto.getEndTime())) {
                wrapper.lt(TbUsedCar::getCreateTime, dto.getEndTime() + " 23:59:59");
            }
            wrapper.orderByDesc(TbUsedCar::getCreateTime);
            wrapper.eq(TbUsedCar::getDelFlag, Constants.ZERO);
            List<UsedCarListVO> list = baseMapper.selectVoList(wrapper, UsedCarListVO.class);
            return new RowsData<>(list, page.getTotal(), dto.getPageNum());
        }
    }

    @Override
    public void editUsedCar(EditUsedCarDTO dto) {
        TbUsedCar tbUsedCar = baseMapper.selectById(dto.getId());
        if (StringUtils.isNotNull(tbUsedCar)) {
            tbUsedCar.setServiceStatus(dto.getServiceStatus());
            if (Constants.TWO.equals(dto.getServiceStatus())) {
                tbUsedCar.setProcessingTime(new Date());
            }
            baseMapper.updateById(tbUsedCar);
        }
    }

    @Override
    public UsedCarDetailsVO details(Long id) {
        UsedCarDetailsVO detailsVO = new UsedCarDetailsVO();
        TbUsedCar tbUsedCar = baseMapper.selectById(id);
        if (StringUtils.isNotNull(tbUsedCar)) {
            BeanUtils.copyProperties(tbUsedCar, detailsVO);
        }
        return detailsVO;
    }
}
