package com.ruoyi.ss.clean.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.ServiceConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.clean.domain.Clean;
import com.ruoyi.ss.clean.domain.CleanQuery;
import com.ruoyi.ss.clean.domain.CleanVO;
import com.ruoyi.ss.clean.service.ICleanService;
import com.ruoyi.ss.order.domain.OrderVO;
import com.ruoyi.ss.room.domain.Room;
import com.ruoyi.ss.room.service.IRoomService;
import com.ruoyi.ss.user.domain.UserVO;
import com.ruoyi.ss.user.service.IUserService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ss.clean.mapper.CleanMapper;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;

import static com.ruoyi.common.constant.ServiceConstants.ROOM_STATUS_FREE;
import static com.ruoyi.common.constant.ServiceConstants.STATUS_COMPLETED;

/**
 * 保洁记录Service业务层处理
 *
 * @author qzz
 * @date 2024-11-19
 */
@Service
public class CleanServiceImpl implements ICleanService
{
    @Autowired
    private CleanMapper cleanMapper;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Resource
    private IUserService userService;

    /**
     * 查询保洁记录
     *
     * @param cleanId 保洁记录主键
     * @return 保洁记录
     */
    @Override
    public CleanVO selectECleanByCleanId(Long cleanId)
    {
        return cleanMapper.selectECleanByCleanId(cleanId);
    }

    /**
     * 查询保洁记录列表
     *
     * @param eClean 保洁记录
     * @return 保洁记录
     */
    @Override
    public List<CleanVO> selectECleanList(CleanQuery eClean)
    {
        return cleanMapper.selectECleanList(eClean);
    }

    /**
     * 新增保洁记录
     *
     * @param eClean 保洁记录
     * @return 结果
     */
    @Override
    public int insertEClean(Clean eClean)
    {
        return cleanMapper.insertEClean(eClean);
    }

    /**
     * 修改保洁记录
     *
     * @param eClean 保洁记录
     * @return 结果
     */
    @Override
    public int updateEClean(Clean eClean)
    {
        return cleanMapper.updateEClean(eClean);
    }

    /**
     * 批量删除保洁记录
     *
     * @param cleanIds 需要删除的保洁记录主键
     * @return 结果
     */
    @Override
    public int deleteECleanByCleanIds(Long[] cleanIds)
    {
        return cleanMapper.deleteECleanByCleanIds(cleanIds);
    }

    /**
     * 删除保洁记录信息
     *
     * @param cleanId 保洁记录主键
     * @return 结果
     */
    @Override
    public int deleteECleanByCleanId(Long cleanId)
    {
        return cleanMapper.deleteECleanByCleanId(cleanId);
    }

    /**
     * 完成保洁
     */
    @Override
    public boolean completeCleaning(Long cleanId) {
        CleanVO cleanVO = cleanMapper.selectECleanByCleanId(cleanId);
        ServiceUtil.assertion(ObjectUtil.isNull(cleanVO),"保洁订单不存在");

        Boolean execute = transactionTemplate.execute(e -> {
            // 改变房间状态、实际完成时间
            // 改变房间状态为空闲中
            Room room = new Room();
            room.setStatus(ROOM_STATUS_FREE);
            room.setRoomId(cleanVO.getRoomId());
            int i = roomService.updateERoom(room);
            ServiceUtil.assertion(i == 0,"更新房间状态失败");

            // 更新保洁订单状态
            CleanVO clean = new CleanVO();
            clean.setCleanId(cleanId);
            clean.setStatus(STATUS_COMPLETED);
            clean.setActualEndTime(DateUtils.getNowDate());
            int i1 = cleanMapper.updateEClean(clean);
            ServiceUtil.assertion(i1 == 0,"更新保洁状态失败");
            return true;
        });
        if(Boolean.FALSE.equals(execute))throw new ServiceException("【完成保洁】失败");
        return true;
    }

    /**
     * 根据订单对象创建保洁订单
     */
    @Override
    public @NotNull Clean buildClean(OrderVO orderVO) {
        // 根据店铺获取保洁员
        // 生成保洁订单 cleanService
        Clean clean = new Clean();
        List<UserVO> clearUserByStoreId = userService.getClearUserByStoreId(orderVO.getStoreId());
        if(!clearUserByStoreId.isEmpty()){
            UserVO userVO = clearUserByStoreId.get(0);
            clean.setUserId(userVO.getUserId());
            clean.setUserName(userVO.getUserName());
            clean.setUserPhone(userVO.getPhonenumber());
        }
        clean.setStartTime(DateUtils.getNowDate());
        clean.setMerchantId(orderVO.getMerchantId());
        clean.setRoomId(orderVO.getRoomId());
        clean.setRoomName(orderVO.getRoomName());
        clean.setStatus(ServiceConstants.STATUS_PENDING_CLEANING);
        clean.setOrderNo(orderVO.getOrderNo());
        clean.setStoreId(orderVO.getStoreId());
        clean.setStoreName(orderVO.getStoreName());
        return clean;
    }
}
