package com.xiaoniu.system.insurancerescue.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.exception.ServiceException;
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.insurancerescue.domain.TbInsuranceRescue;
import com.xiaoniu.system.insurancerescue.dto.EditInsuranceRescueDTO;
import com.xiaoniu.system.insurancerescue.dto.InsuranceRescueQueryDTO;
import com.xiaoniu.system.insurancerescue.mapper.TbInsuranceRescueMapper;
import com.xiaoniu.system.insurancerescue.service.ITbInsuranceRescueService;
import com.xiaoniu.system.insurancerescue.vo.InsuranceRescueDetailsVO;
import com.xiaoniu.system.insurancerescue.vo.InsuranceRescueListVO;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 保险救援信息表 服务实现类
 * </p>
 *
 * @author haoxuyang
 * @since 2024-10-18 05:03:00
 */
@Service
public class TbInsuranceRescueServiceImpl extends ServiceImpl<TbInsuranceRescueMapper, TbInsuranceRescue> implements ITbInsuranceRescueService {

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

    @Override
    public void editInsuranceRescue(EditInsuranceRescueDTO dto) {
        TbInsuranceRescue tbInsuranceRescue = baseMapper.selectById(dto.getId());
        if (StringUtils.isNotNull(tbInsuranceRescue)) {
            if (Constants.TWO.equals(tbInsuranceRescue.getServiceStatus())) {
                if (Constants.ONE.equals(dto.getServiceStatus())) {
                    throw new ServiceException("当前订单已经处理过，不能修改为待处理状态");
                }
            }
            tbInsuranceRescue.setServiceStatus(dto.getServiceStatus());
            if (Constants.TWO.equals(dto.getServiceStatus())) {
                tbInsuranceRescue.setProcessingTime(new Date());
            }
            tbInsuranceRescue.setRemark(dto.getRemark());
            baseMapper.updateById(tbInsuranceRescue);
        }
    }

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