package com.ylx.massage.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.base.BaseServiceImpl;
import com.ylx.common.core.domain.model.WxLoginUser;
import com.ylx.common.exception.ServiceException;
import com.ylx.common.utils.DateUtils;
import com.ylx.massage.domain.Alarm;
import com.ylx.massage.domain.vo.AlarmListVO;
import com.ylx.massage.mapper.AlarmMapper;
import com.ylx.massage.service.AlarmService;
import com.ylx.order.domain.TOrder;
import com.ylx.order.domain.dto.AlarmSubmitDTO;
import com.ylx.order.service.TOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static com.ylx.order.constant.OrderConstant.NOT_DELETE;

/**
 * 报警记录表(YlxAlarm)表服务实现类
 *
 * @author makejava
 * @since 2024-04-28 09:19:05
 */
@Slf4j
@Service("ylxAlarmService")
public class AlarmServiceImpl extends BaseServiceImpl<AlarmMapper, Alarm> implements AlarmService {

    @Resource
    private TOrderService orderService;

    @Resource
    private AlarmMapper alarmMapper;

    @Override
    public Page<AlarmListVO> selectAlarmPage(Page<AlarmListVO> page, Alarm alarm) {
        Alarm query = buildAlarmPageQuery(alarm);
        return baseMapper.selectAlarmPage(page, query);
    }

    @Override
    public AlarmListVO queryById(String id) {
        return alarmMapper.queryById(id);
    }


    @Override
    public Alarm update(Alarm alarm) {
        return null;
    }

    @Override
    public boolean deleteById(String id) {
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean submitAlarm(AlarmSubmitDTO dto) {

        WxLoginUser wxLoginUser = this.getCurrentWxLoginUser();
        Long merchantId = Long.parseLong(wxLoginUser.getId());

        LambdaQueryWrapper<TOrder> queryWrapper = Wrappers.lambdaQuery(TOrder.class)
                .eq(TOrder::getId, dto.getOrderId())
                .eq(TOrder::getIsDelete, NOT_DELETE);

        TOrder order = this.orderService.getOne(queryWrapper);
        if (ObjectUtil.isNull(order)) {
            throw new RuntimeException("订单不存在");
        }

        if (ObjectUtil.notEqual(order.getMerchantId(), merchantId)) {
            throw new ServiceException("您无权操作此订单");
        }

        Alarm alarm = new Alarm();
        alarm.setMerchantId(merchantId.toString());
        alarm.setOrderId(order.getId());
        alarm.setAddress(order.getContactAddressInfo());
        alarm.setOrderNo(order.getOrderNo());
        alarm.setIsDelete(NOT_DELETE);
        alarm.setAlarmStatus(0);
        alarm.setCreateTime(DateUtils.getNowDate());
        alarm.setCreateBy(wxLoginUser.getCNickName());

        return super.save(alarm);
    }

    private Alarm buildAlarmPageQuery(Alarm alarm) {
        Alarm query = new Alarm();
        if (alarm == null) {
            return query;
        }
        query.setMerchantName(alarm.getMerchantName());
        query.setMerchantNickName(alarm.getMerchantNickName());
        query.setMerchantSex(alarm.getMerchantSex());
        query.setMerchantPhone(alarm.getMerchantPhone());
        query.setAlarmStatus(alarm.getAlarmStatus());
        query.setBeginTime(normalizeBeginTime(alarm.getBeginTime()));
        query.setEndTime(normalizeEndTime(alarm.getEndTime()));
        return query;
    }

    private String normalizeBeginTime(String beginTime) {
        if (StringUtils.isBlank(beginTime)) {
            return null;
        }
        String value = beginTime.trim();
        return value.length() == 10 ? value + " 00:00:00" : value;
    }

    private String normalizeEndTime(String endTime) {
        if (StringUtils.isBlank(endTime)) {
            return null;
        }
        String value = endTime.trim();
        return value.length() == 10 ? value + " 23:59:59" : value;
    }
}
