package com.ylx.massage.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.common.exception.ServiceException;
import com.ylx.common.utils.StringUtils;
import com.ylx.massage.domain.TJs;
import com.ylx.massage.domain.TOrder;
import com.ylx.massage.domain.YlxAlarm;
import com.ylx.massage.enums.OrderStatusEnum;
import com.ylx.massage.mapper.YlxAlarmMapper;
import com.ylx.massage.service.TJsService;
import com.ylx.massage.service.TOrderService;
import com.ylx.massage.service.YlxAlarmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 报警记录表(YlxAlarm)表服务实现类
 *
 * @author makejava
 * @since 2024-04-28 09:19:05
 */
@Slf4j
@Service("ylxAlarmService")
public class YlxAlarmServiceImpl extends ServiceImpl<YlxAlarmMapper, YlxAlarm> implements YlxAlarmService {
    @Resource
    private YlxAlarmMapper ylxAlarmDao;

    @Resource
    private TOrderService orderService;

    @Resource
    private TJsService jsService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public YlxAlarm queryById(String id) {
        return this.ylxAlarmDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param ylxAlarm    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<YlxAlarm> queryByPage(YlxAlarm ylxAlarm, PageRequest pageRequest) {
        long total = this.ylxAlarmDao.count(ylxAlarm);
        return new PageImpl<>(this.ylxAlarmDao.queryAllByLimit(ylxAlarm, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param ylxAlarm 实例对象
     * @return 实例对象
     */

    /**
     * 修改数据
     *
     * @param ylxAlarm 实例对象
     * @return 实例对象
     */
    @Override
    public YlxAlarm update(YlxAlarm ylxAlarm) {
        this.ylxAlarmDao.update(ylxAlarm);
        return this.queryById(ylxAlarm.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.ylxAlarmDao.deleteById(id) > 0;
    }

    @Override
    public YlxAlarm addAlarm(YlxAlarm ylxAlarm) {
        if(StringUtils.isBlank(ylxAlarm.getJsId())){
            throw new ServiceException("技师ID不能为空");
        }
        LambdaQueryWrapper<TOrder> tOrderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tOrderLambdaQueryWrapper.eq(TOrder::getcJsId,ylxAlarm.getJsId()).eq(TOrder::getnStatus, OrderStatusEnum.SERVICE.getCode());
        List<TOrder> orders = orderService.list(tOrderLambdaQueryWrapper);

        if(CollectionUtil.isEmpty(orders)) {
            throw new ServiceException("没有服务中的订单");
        }
        log.info("addAlarm-订单信息：{}", orders);
        TOrder order = orders.get(0);
        if(order!=null){
            LambdaQueryWrapper<YlxAlarm> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(YlxAlarm::getOrderNo,order.getOrderNo());
            YlxAlarm one = this.getOne(queryWrapper);
            if(one != null){
                throw new ServiceException("该订单已报警");
            }
            YlxAlarm ylxAlarm1 = new YlxAlarm();
            TJs js = jsService.getById(order.getcJsId());
            ylxAlarm1.setJsId(order.getcJsId());
            ylxAlarm1.setOrderNo(order.getOrderNo());
            ylxAlarm1.setJsName(js.getcName());
            ylxAlarm1.setJsPhone(js.getcPhone());
            ylxAlarm1.setUserName(order.getcName());
            ylxAlarm1.setUserPhone(order.getcPhone());
            ylxAlarm1.setAddress(order.getName()+" "+order.getAddress());
            ylxAlarm1.setOpenId(order.getcOpenId());
            this.save(ylxAlarm1);
            return ylxAlarm1;
        }
        return ylxAlarm;
    }
}
